arql 0.3.28 → 0.3.30
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README-zh_CN.org +1096 -0
- data/README.org +1170 -0
- data/auto-set-id-before-save-zh_CN.org +35 -0
- data/auto-set-id-before-save.org +33 -0
- data/custom-configurations-zh_CN.org +50 -0
- data/custom-configurations.org +54 -0
- data/define-associations-zh_CN.org +38 -0
- data/define-associations.org +37 -0
- data/fuzzy-field-query-zh_CN.org +165 -0
- data/fuzzy-field-query.org +165 -0
- data/helper-for-datetime-range-query-zh_CN.org +216 -0
- data/helper-for-datetime-range-query.org +218 -0
- data/initializer-structure-zh_CN.org +33 -0
- data/initializer-structure.org +31 -0
- data/lib/arql/cli.rb +1 -1
- data/lib/arql/definition.rb +15 -3
- data/lib/arql/version.rb +1 -1
- data/oss-files-zh_CN.org +161 -0
- data/oss-files.org +163 -0
- data/sql-log-zh_CN.org +55 -0
- data/sql-log.org +55 -0
- metadata +20 -3
- data/README.md +0 -456
data/README.md
DELETED
@@ -1,456 +0,0 @@
|
|
1
|
-
# Arql
|
2
|
-
|
3
|
-
Arql is a simple utility gem which combines Rails ActiveRecord and Pry together, with additional helpful Pry commands. It defines model clases automatically from DB table informations.
|
4
|
-
You can use this utility as your SQL editor and querier if you are a Ruby guy.
|
5
|
-
|
6
|
-
## Installation
|
7
|
-
|
8
|
-
Execute:
|
9
|
-
|
10
|
-
$ gem install arql
|
11
|
-
|
12
|
-
If system permission problems occurs, try with sudo:
|
13
|
-
|
14
|
-
$ sudo gem install arql
|
15
|
-
|
16
|
-
## Usage
|
17
|
-
|
18
|
-
### Command line options
|
19
|
-
|
20
|
-
```
|
21
|
-
Usage: arql [options] [ruby file]
|
22
|
-
|
23
|
-
If neither [ruby file] nor -e option specified, and STDIN is not a tty, a Pry REPL will be launched,
|
24
|
-
otherwise the specified ruby file or -e option value or ruby code read from STDIN will be run, and no REPL launched
|
25
|
-
|
26
|
-
-c, --conf=CONFIG_FILE Specify config file, default is $HOME/.arql.yml, or $HOME/.arql.d/init.yml.
|
27
|
-
-i, --initializer=INITIALIZER Specify initializer ruby file, default is $HOME/.arql.rb, or $HOME/.arql.d/init.rb.
|
28
|
-
-e, --env=ENVIRON Specify config environment.
|
29
|
-
-a, --db-adapter=DB_ADAPTER Specify database Adapter, default is mysql2
|
30
|
-
-h, --db-host=DB_HOST Specify database host
|
31
|
-
-p, --db-port=DB_PORT Specify database port
|
32
|
-
-d, --db-name=DB_NAME Specify database name
|
33
|
-
-u, --db-user=DB_USER Specify database user
|
34
|
-
-P, --db-password=DB_PASSWORD Specify database password
|
35
|
-
-n, --db-encoding=DB_ENCODING Specify database encoding, default is utf8
|
36
|
-
-o, --db-pool=DB_POOL Specify database pool size, default is 5
|
37
|
-
-H, --ssh-host=SSH_HOST Specify SSH host
|
38
|
-
-O, --ssh-port=SSH_PORT Specify SSH port
|
39
|
-
-U, --ssh-user=SSH_USER Specify SSH user
|
40
|
-
-W, --ssh-password=SSH_PASSWORD Specify SSH password
|
41
|
-
-LSSH_LOCAL_PORT, Specify local SSH proxy port
|
42
|
-
--ssh-local-port
|
43
|
-
-E, --eval=CODE evaluate CODE
|
44
|
-
-S, --show-sql Show SQL on STDOUT
|
45
|
-
-w, --write-sql=OUTPUT Write SQL to OUTPUT file
|
46
|
-
-A, --append-sql=OUTPUT Append SQL to OUTPUT file
|
47
|
-
--help
|
48
|
-
Prints this help
|
49
|
-
```
|
50
|
-
|
51
|
-
#### -c, --config=CONFIG_FILE
|
52
|
-
|
53
|
-
Specify location of config file, default is `$HOME/.arql.yml`, or `$HOME/.arql.d/init.yml`.
|
54
|
-
|
55
|
-
The config file typically is as same as a Rails database config file, but with several additional config options, e.g. `ssh` options, etc.
|
56
|
-
|
57
|
-
See `Config File` section.
|
58
|
-
|
59
|
-
#### -i, --initializer=INITIALIZER
|
60
|
-
|
61
|
-
Specify a Ruby source file to be evaluated after ActiveRecord models are defined, default is `$HOME/.arql.rb`, or `$HOME/.arql.d/init.rb` if exists.
|
62
|
-
|
63
|
-
#### -e, --env=ENVIRON
|
64
|
-
|
65
|
-
Specify an environment name which should be defined in config file.
|
66
|
-
|
67
|
-
#### -E, --eval=CODE
|
68
|
-
|
69
|
-
Specify a Ruby code fragment to be evaluated, if this option was specified, no Pry REPL will be launched.
|
70
|
-
|
71
|
-
#### -S, --show-sql
|
72
|
-
|
73
|
-
arql does not show SQL log by default, use this option to turn it on.
|
74
|
-
|
75
|
-
#### -w, --write-sql=OUTPUT
|
76
|
-
|
77
|
-
You can also let arql write SQL logs into a file use this option.
|
78
|
-
|
79
|
-
#### -A, --append-sql-OUTOUT
|
80
|
-
|
81
|
-
Like `-w`, but without file content truncation.
|
82
|
-
|
83
|
-
#### DB options
|
84
|
-
|
85
|
-
Options described in this section typically should be configured in the config file, these options are just shortcuts of
|
86
|
-
the corresponding configurations in case of that you want modify some configuration items on CLI directly.
|
87
|
-
|
88
|
-
##### -a, --db-adapter=DB_ADAPTER
|
89
|
-
|
90
|
-
Specify DB adapter, available values:
|
91
|
-
|
92
|
-
+ `mysql2`
|
93
|
-
+ `postgresql`
|
94
|
-
+ `sqlite3`
|
95
|
-
+ `sqlserver`
|
96
|
-
+ `oracle_enhanced`
|
97
|
-
|
98
|
-
##### -h, --db-host=DB_HOST
|
99
|
-
|
100
|
-
Specify DB host
|
101
|
-
|
102
|
-
##### -p, --db-port=DB_PORT
|
103
|
-
|
104
|
-
Specify DB port
|
105
|
-
|
106
|
-
##### -d, --db-name=DB_NAME
|
107
|
-
|
108
|
-
Specify DB name
|
109
|
-
|
110
|
-
##### -u, --db-user=DB_USER
|
111
|
-
|
112
|
-
Specify DB username
|
113
|
-
|
114
|
-
##### -P, --db-password=DB_PASSWORD
|
115
|
-
|
116
|
-
Specify DB password
|
117
|
-
|
118
|
-
##### -n, --db-encoding=DB_ENCODING
|
119
|
-
|
120
|
-
Specify DB character encoding, default is `utf8`
|
121
|
-
|
122
|
-
##### -o, --db-pool=DB_POOL
|
123
|
-
|
124
|
-
Specify size of DB connection pool, default is `5`
|
125
|
-
|
126
|
-
##### -H, --ssh-host=SSH_HOST
|
127
|
-
|
128
|
-
Specify ssh host for ssh proxy
|
129
|
-
|
130
|
-
##### -O, --ssh-port=SSH_PORT
|
131
|
-
|
132
|
-
Specify ssh port for ssh proxy
|
133
|
-
|
134
|
-
##### -U, --ssh-user=SSH_USER
|
135
|
-
|
136
|
-
Specify ssh username for ssh proxy
|
137
|
-
|
138
|
-
##### -W, --ssh-password=SSH_PASSWORD
|
139
|
-
|
140
|
-
Specify ssh password for ssh proxy
|
141
|
-
|
142
|
-
##### -LSSH_LOCAL_PORT
|
143
|
-
|
144
|
-
Specify local port for ssh proxy, default is a _random_ port
|
145
|
-
|
146
|
-
### Config file
|
147
|
-
|
148
|
-
The config file typically is as same as a Rails database config file, but with several additional config options.
|
149
|
-
|
150
|
-
#### Additional configurations
|
151
|
-
|
152
|
-
1. `created_at`: An array contains customized column names for the ActiveRecord `created_at` field, default value is `created_at`, value of the column will be filled with current time stamp when created if specified
|
153
|
-
2. `updated_at`: An array contains customized column names for the ActiveRecord `updated_at` field, default value is `updated_at`, value of the column will be filled with current time stamp when updated if specified
|
154
|
-
3. `ssh.host`: host of ssh proxy
|
155
|
-
4. `ssh.port`: port of ssh proxy
|
156
|
-
5. `ssh.user`: username of ssh proxy
|
157
|
-
6. `ssh.password`: password of ssh proxy
|
158
|
-
7. `ssh.local_port`: local port of ssh proxy
|
159
|
-
|
160
|
-
#### Config Example
|
161
|
-
|
162
|
-
```
|
163
|
-
default: &default
|
164
|
-
adapter: mysql2
|
165
|
-
encoding: utf8
|
166
|
-
created_at: ["gmt_created"]
|
167
|
-
updated_at: ["gmt_modified"]
|
168
|
-
|
169
|
-
local:
|
170
|
-
<<: *default
|
171
|
-
username: root
|
172
|
-
database: blog
|
173
|
-
password:
|
174
|
-
socket: /tmp/mysql.sock
|
175
|
-
|
176
|
-
dev:
|
177
|
-
<<: *default
|
178
|
-
host: devdb.mycompany.com
|
179
|
-
port: 3306
|
180
|
-
username: root
|
181
|
-
password: 123456
|
182
|
-
database: blog
|
183
|
-
ssh:
|
184
|
-
host: dev.mycompany.com
|
185
|
-
port: 22
|
186
|
-
user: deploy
|
187
|
-
password: 12345678
|
188
|
-
local_port: 3307
|
189
|
-
```
|
190
|
-
|
191
|
-
### Use as a REPL
|
192
|
-
|
193
|
-
If neither [ruby file] nor -e option specified, and STDIN is not a tty, a Pry REPL will be launched with pry-byebug loaded.
|
194
|
-
|
195
|
-
Arql provides some Pry commands:
|
196
|
-
|
197
|
-
#### info
|
198
|
-
|
199
|
-
The `info` command prints current DB connection information and SSH proxy information, e.g.:
|
200
|
-
|
201
|
-
```
|
202
|
-
Database Connection Information:
|
203
|
-
Host:
|
204
|
-
Port:
|
205
|
-
Username: root
|
206
|
-
Password:
|
207
|
-
Database: test
|
208
|
-
Adapter: mysql2
|
209
|
-
Encoding: utf8
|
210
|
-
Pool Size: 5
|
211
|
-
```
|
212
|
-
|
213
|
-
#### m
|
214
|
-
|
215
|
-
The `m` command print all table names with corresponding model class, and abbr class names, e.g.:
|
216
|
-
|
217
|
-
```
|
218
|
-
+-----------------|----------------|------+
|
219
|
-
| Table Name | Model Class | Abbr |
|
220
|
-
+-----------------|----------------|------+
|
221
|
-
| all_songs | AllSongs | AS |
|
222
|
-
| datetypes | Datetypes | D |
|
223
|
-
| hello | Hello | H |
|
224
|
-
| permission | Permission | P |
|
225
|
-
| permission_role | PermissionRole | PR |
|
226
|
-
| person | Person | |
|
227
|
-
| role | Role | R |
|
228
|
-
| role_user | RoleUser | RU |
|
229
|
-
| test | Test | T |
|
230
|
-
| user | User | U |
|
231
|
-
+-----------------|----------------|------+
|
232
|
-
```
|
233
|
-
|
234
|
-
The `m` command has an alias: `l`
|
235
|
-
|
236
|
-
#### t
|
237
|
-
|
238
|
-
Given a table name or model class, the `t` command prints the table's definition information.
|
239
|
-
|
240
|
-
```
|
241
|
-
Table: person
|
242
|
-
+----|------------|------------------|-----------|-------|-----------|-------|---------|----------|---------+
|
243
|
-
| PK | Name | SQL Type | Ruby Type | Limit | Precision | Scale | Default | Nullable | Comment |
|
244
|
-
+----|------------|------------------|-----------|-------|-----------|-------|---------|----------|---------+
|
245
|
-
| Y | id | int(11) unsigned | integer | 4 | | | | false | |
|
246
|
-
| | name | varchar(64) | string | 64 | | | | true | |
|
247
|
-
| | age | int(11) | integer | 4 | | | | true | |
|
248
|
-
| | gender | int(4) | integer | 4 | | | | true | |
|
249
|
-
| | grade | int(4) | integer | 4 | | | | true | |
|
250
|
-
| | blood_type | varchar(4) | string | 4 | | | | true | |
|
251
|
-
+----|------------|------------------|-----------|-------|-----------|-------|---------|----------|---------+
|
252
|
-
```
|
253
|
-
|
254
|
-
#### show-sql / hide-sql
|
255
|
-
|
256
|
-
This pair of commands toggle display of SQL logs in Pry REPL.
|
257
|
-
|
258
|
-
#### reconnect
|
259
|
-
|
260
|
-
The `reconnect` command just simply reconnects current DB connection.
|
261
|
-
|
262
|
-
#### redefine
|
263
|
-
|
264
|
-
The `redefine` command redefines ActiveRecord model classes from DB tables informations.
|
265
|
-
|
266
|
-
### Use as code interpreter
|
267
|
-
|
268
|
-
If a ruby file is specified as command line argument, or the `-e` option is specified, or STDIN is a tty, then no Pry
|
269
|
-
REPL will be launched, instead, it evaluates the file or code fragment specified, just after model class definition is
|
270
|
-
done. You can think this usage as the `runner` sub-command of `rails` command.
|
271
|
-
|
272
|
-
### Additional extension methods
|
273
|
-
|
274
|
-
#### to_insert_sql / to_upsert_sql
|
275
|
-
|
276
|
-
You can call `to_insert_sql` / `to_upsert_sql` on any ActiveRecord model instance to get a insert or upsert SQL of the object.
|
277
|
-
|
278
|
-
These tow methods are also available on any array object which contains only ActiveRecord model instance objects.
|
279
|
-
|
280
|
-
```
|
281
|
-
ARQL ❯ Person.all.to_a.to_insert_sql
|
282
|
-
=> "INSERT INTO `person` (`id`,`name`,`age`,`gender`,`grade`,`blood_type`) VALUES (1, 'Jack', 30, NULL, NULL, NULL), (2, 'Jack', 11, 1, NULL, NULL), (3, 'Jack', 12, 1, NULL, NULL), (4, 'Jack', 30, 1, NULL, NULL), (5, 'Jack', 12, 2, NULL, NULL), (6, 'Jack', 2, 2, 2, NULL), (7, 'Jack', 3, 2, 2, NULL), (8, 'Jack', 30, 2, 2, 'AB'), (9, 'Jack', 30, 2, 2, 'AB'), (10, 'Jack', 30, 2, 2, 'AB'), (11, 'Jackson', 30, 2, 2, 'AB') ON DUPLICATE KEY UPDATE `id`=`id`;"
|
283
|
-
```
|
284
|
-
|
285
|
-
#### to_create_sql
|
286
|
-
|
287
|
-
You can call `to_create_sql` on any ActiveRecord model class to get create table SQL of the model class / table.
|
288
|
-
|
289
|
-
#### t
|
290
|
-
|
291
|
-
You can call `t` method on any ActiveRecord model instance to print a pretty table of attributes names and values of the object.
|
292
|
-
|
293
|
-
```
|
294
|
-
ARQL ❯ Person.last.t
|
295
|
-
+----------------|-----------------|------------------|---------+
|
296
|
-
| Attribute Name | Attribute Value | SQL Type | Comment |
|
297
|
-
+----------------|-----------------|------------------|---------+
|
298
|
-
| id | 11 | int(11) unsigned | |
|
299
|
-
| name | Jackson | varchar(64) | |
|
300
|
-
| age | 30 | int(11) | |
|
301
|
-
| gender | 2 | int(4) | |
|
302
|
-
| grade | 2 | int(4) | |
|
303
|
-
| blood_type | AB | varchar(4) | |
|
304
|
-
+----------------|-----------------|------------------|---------+
|
305
|
-
```
|
306
|
-
|
307
|
-
#### v
|
308
|
-
|
309
|
-
The `v` method is for integration with Emacs org babel.
|
310
|
-
|
311
|
-
#### v for ActiveRecord instances
|
312
|
-
|
313
|
-
Call `v` method on any ActiveRecord model instance to print an Array which first element is `['Attribute Name', 'Attribute Value', 'SQL Type', 'Comment']`, and the second is `nil`, and the rest elements are attribute names and values of the object. In Emacs org-mode, is `:result` type is `value`(the default), this return value will be rendered as a pretty table.
|
314
|
-
|
315
|
-
```
|
316
|
-
ARQL ❯ Person.last.v
|
317
|
-
=> [["Attribute Name", "Attribute Value", "SQL Type", "Comment"],
|
318
|
-
nil,
|
319
|
-
["id", 11, "int(11) unsigned", ""],
|
320
|
-
["name", "Jackson", "varchar(64)", ""],
|
321
|
-
["age", 30, "int(11)", ""],
|
322
|
-
["gender", 2, "int(4)", ""],
|
323
|
-
["grade", 2, "int(4)", ""],
|
324
|
-
["blood_type", "AB", "varchar(4)", ""]]
|
325
|
-
```
|
326
|
-
|
327
|
-
#### v for array
|
328
|
-
|
329
|
-
The `v` method is also available for arrays:
|
330
|
-
|
331
|
-
#### Array which only contains ActiveRecord instances
|
332
|
-
|
333
|
-
```
|
334
|
-
ARQL ❯ Person.all.to_a.v
|
335
|
-
=> [["id", "name", "age", "gender", "grade", "blood_type"],
|
336
|
-
nil,
|
337
|
-
[1, "Jack", 30, nil, nil, nil],
|
338
|
-
[2, "Jack", 11, 1, nil, nil],
|
339
|
-
[3, "Jack", 12, 1, nil, nil],
|
340
|
-
[4, "Jack", 30, 1, nil, nil],
|
341
|
-
[5, "Jack", 12, 2, nil, nil],
|
342
|
-
[6, "Jack", 2, 2, 2, nil],
|
343
|
-
[7, "Jack", 3, 2, 2, nil],
|
344
|
-
[8, "Jack", 30, 2, 2, "AB"],
|
345
|
-
[9, "Jack", 30, 2, 2, "AB"],
|
346
|
-
[10, "Jack", 30, 2, 2, "AB"],
|
347
|
-
[11, "Jackson", 30, 2, 2, "AB"]]
|
348
|
-
```
|
349
|
-
|
350
|
-
#### Array which only contains same-structured Hash objects
|
351
|
-
|
352
|
-
```
|
353
|
-
ARQL ❯ arr = [{name: 'Jack', age: 10}, {name: 'Lucy', age: 20}]
|
354
|
-
=> [{:name=>"Jack", :age=>10}, {:name=>"Lucy", :age=>20}]
|
355
|
-
ARQL ❯ arr.v
|
356
|
-
=> [[:name, :age], nil, ["Jack", 10], ["Lucy", 20]]
|
357
|
-
```
|
358
|
-
|
359
|
-
#### sql
|
360
|
-
|
361
|
-
Use `sql` method to execute raw SQL statements:
|
362
|
-
|
363
|
-
```
|
364
|
-
ARQL ❯ rs = sql 'select count(0) from person;'
|
365
|
-
=> #<ActiveRecord::Result:0x00007fd1f8026ad0 @column_types={}, @columns=["count(0)"], @hash_rows=nil, @rows=[[11]]>
|
366
|
-
ARQL ❯ rs.rows
|
367
|
-
=> [[11]]
|
368
|
-
```
|
369
|
-
|
370
|
-
#### JSON convertion and prints
|
371
|
-
|
372
|
-
Call `j` on any object to get JSON presentation of it, and `jj` to get pretty-printed JSON presentation.
|
373
|
-
|
374
|
-
Use `jp` to print JSON, `jjp` to pretty print.
|
375
|
-
|
376
|
-
#### String#p
|
377
|
-
|
378
|
-
The `p` methods is defined as:
|
379
|
-
|
380
|
-
```
|
381
|
-
class String
|
382
|
-
def p
|
383
|
-
puts self
|
384
|
-
end
|
385
|
-
end
|
386
|
-
```
|
387
|
-
|
388
|
-
#### $C
|
389
|
-
|
390
|
-
Arql assigns `ActiveRecord::Base.connection` object to the global available `$C`
|
391
|
-
|
392
|
-
The `sql` method in above description is actually `$C.exec_query` in fact, and other methods of `$C` is also pretty helpful:
|
393
|
-
|
394
|
-
##### Create a table
|
395
|
-
|
396
|
-
```
|
397
|
-
ARQL ❯ $C.create_table :post, id: false, primary_key: :id do |t|
|
398
|
-
ARQL ❯ t.column :id, :bigint, precison: 19, comment: 'ID'
|
399
|
-
ARQL ❯ t.column :name, :string, comment: '名称'
|
400
|
-
ARQL ❯ t.column :gmt_created, :datetime, comment: '创建时间'
|
401
|
-
ARQL ❯ t.column :gmt_modified, :datetime, comment: '最后修改时间'
|
402
|
-
ARQL ❯ end
|
403
|
-
```
|
404
|
-
|
405
|
-
##### Add a column
|
406
|
-
|
407
|
-
```
|
408
|
-
$C.add_column :post, :note, :string, comment: '备注'
|
409
|
-
```
|
410
|
-
|
411
|
-
##### Modify a column
|
412
|
-
|
413
|
-
```
|
414
|
-
$C.change_column :post, :note, :text, comment: '备注'
|
415
|
-
```
|
416
|
-
|
417
|
-
##### Delete a column
|
418
|
-
|
419
|
-
```
|
420
|
-
$C.remove_column :post, :note
|
421
|
-
```
|
422
|
-
##### Delete a table
|
423
|
-
|
424
|
-
```
|
425
|
-
$C.drop_table :post
|
426
|
-
```
|
427
|
-
|
428
|
-
##### Add an index
|
429
|
-
|
430
|
-
```
|
431
|
-
ARQL ❯ $C.add_index :post, :name
|
432
|
-
ARQL ❯ $C.add_index(:accounts, [:branch_id, :party_id], unique: true, name: 'by_branch_party')
|
433
|
-
```
|
434
|
-
|
435
|
-
### Use with Emacs org babel
|
436
|
-
|
437
|
-
Here is a [ob-arql](https://github.com/lululau/spacemacs-layers/blob/master/ob-arql/local/ob-arql/ob-arql.el) for integration with Emacs org babel.
|
438
|
-
|
439
|
-
## Development
|
440
|
-
|
441
|
-
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
442
|
-
|
443
|
-
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
444
|
-
|
445
|
-
## Contributing
|
446
|
-
|
447
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/lululau/arql. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/lululau/arql/blob/master/CODE_OF_CONDUCT.md).
|
448
|
-
|
449
|
-
|
450
|
-
## License
|
451
|
-
|
452
|
-
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
453
|
-
|
454
|
-
## Code of Conduct
|
455
|
-
|
456
|
-
Everyone interacting in the Arql project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/lululau/arql/blob/master/CODE_OF_CONDUCT.md).
|