arql 0.3.29 → 0.3.31

Sign up to get free protection for your applications and to get access to all the features.
data/README.org ADDED
@@ -0,0 +1,1173 @@
1
+ * Arql
2
+
3
+ [[./README-zh_CN.org][中文]]
4
+
5
+ Arql is a simple utility gem that combines Rails ActiveRecord with Pry and adds useful Pry commands. It can
6
+ automatically define model classes based on database table schema. If you are a Ruby guy, you can use this gem
7
+ as your database query tool.
8
+
9
+ ** Pre-requisite
10
+
11
+ + Ruby >= 2.6.0
12
+ + For different types of databases, you need to install the corresponding database adapter or client binary library:
13
+ - MySQL: Depending on your operating system, you may need to install: =libmariadb-dev=, =libmysqlclient-dev=, =mysql-devel=
14
+ =default-libmysqlclient-dev= ; see the software package guide for your distribution to find the specific package; or refer to [[https://github.com/brianmario/mysql2][mysql2 doc]]
15
+ - SQLite3: No additional libraries need to be installed
16
+ - PostgreSQL: ~gem install pg~
17
+ - Oracle: ~gem install activerecord-oracle_enhanced-adapter~
18
+ - SQL Server: ~gem install activerecord-sqlserver-adapter~
19
+
20
+ ** Installation
21
+
22
+ Run the following command to install the gem:
23
+
24
+ #+begin_example
25
+ $ gem install arql
26
+ #+end_example
27
+
28
+ Try using sudo if you encounter system permission issues:
29
+
30
+ #+begin_example
31
+ $ sudo gem install arql
32
+ #+end_example
33
+
34
+ ** Usage
35
+
36
+ *** CLI options
37
+
38
+ #+begin_example
39
+ Usage: arql [options] [ruby file]
40
+
41
+ If neither [ruby file] nor -E option is specified, and STDIN is a tty, a Pry REPL will be started,
42
+ otherwise the specified ruby file or -E option value or ruby code read from STDIN will be executed,
43
+ and the REPL will not be started
44
+
45
+ -c, --conf=CONFIG_FILE Specify the configuration file, default is $HOME/.arql.yml or $HOME/.arql.d/init.yml
46
+ -i, --initializer=INITIALIZER Specify the initializer Ruby file, default is $HOME/.arql.rb or $HOME/.arql.d/init.rb
47
+ -e, --env=ENVIRON Specify the configuration environment
48
+ -a, --db-adapter=DB_ADAPTER Specify the database adapter, default is sqlite3
49
+ -h, --db-host=DB_HOST Specify the database host
50
+ -p, --db-port=DB_PORT Specify the database port
51
+ -d, --db-name=DB_NAME Specify the database name
52
+ -u, --db-user=DB_USER Specify the database user
53
+ -P, --db-password=DB_PASSWORD Specify the database password
54
+ -n, --db-encoding=DB_ENCODING Specify the database encoding, default is utf8
55
+ -o, --db-pool=DB_POOL Specify the database connection pool size, default is 5
56
+ -H, --ssh-host=SSH_HOST Specify the SSH host
57
+ -O, --ssh-port=SSH_PORT Specify the SSH port
58
+ -U, --ssh-user=SSH_USER Specify the SSH user
59
+ -W, --ssh-password=SSH_PASSWORD Specify the SSH password
60
+ -L, --ssh-local-port=SSH_LOCAL_PORT Specify the local SSH proxy port
61
+ -E, --eval=CODE Evaluate code
62
+ -S, --show-sql Print SQL on STDOUT
63
+ -w, --write-sql=OUTPUT Write SQL to OUTPUT file
64
+ -A, --append-sql=OUTPUT Append SQL to OUTPUT file
65
+ --help Print this message
66
+ #+end_example
67
+
68
+ **** =-c, --config=CONFIG_FILE=
69
+
70
+ Specify the configuration file location, default is =$HOME/.arql.yml= or =$HOME/.arql.d/init.yml=. The configuration file
71
+ is usually the same as the Rails database configuration file, but with some additional configuration options, such as the
72
+ =ssh= option, etc. Refer to the =Configuration file= section.
73
+
74
+ **** =-i, --initializer=INITIALIZER=
75
+
76
+ Specify a Ruby source file, Arql executes the code in this file after defining the ActiveRecord model class, default is
77
+ =$HOME/.arql.rb= or =$HOME/.arql.d/init.rb=. You can add methods and association definitions for ActiveRecord model
78
+ classes in this file.
79
+
80
+ **** =-e, --env=ENVIRON=
81
+
82
+ Specify an environment name in the configuration file.
83
+
84
+ **** =-E, --eval=CODE=
85
+
86
+ Specify a Ruby code snippet, if this option is specified, the Pry REPL will not be started.
87
+
88
+ **** =-S, --show-sql=
89
+
90
+ arql does not display SQL logs by default, use this option to turn it on.
91
+
92
+ **** =-w, --write-sql=OUTPUT=
93
+
94
+ You can also use this option to let arql write SQL logs to a file.
95
+
96
+ **** =-A, --append-sql-OUTOUT=
97
+
98
+ Similar to =-w=, but uses append writing, does not truncate the existing file.
99
+
100
+ **** Database options
101
+
102
+ The options described in this section are usually configured in the configuration file. These options are just shortcuts
103
+ for the corresponding configuration items in the configuration file, so that certain configuration items can be directly
104
+ modified in the CLI.
105
+
106
+ ***** -a, --db-adapter=DB_ADAPTER
107
+
108
+ Specify the database adapter, available values:
109
+
110
+ - =mysql2=
111
+ - =postgresql=
112
+ - =sqlite3=
113
+ - =sqlserver=
114
+ - =oracle_enhanced=
115
+
116
+ ***** -h, --db-host=DB_HOST
117
+
118
+ Specify the database host
119
+
120
+ ***** -p, --db-port=DB_PORT
121
+
122
+ Specify the database port
123
+
124
+ ***** -d, --db-name=DB_NAME
125
+
126
+ Specify the database name
127
+
128
+ ***** -u, --db-user=DB_USER
129
+
130
+ Specify the database username
131
+
132
+ ***** -P, --db-password=DB_PASSWORD
133
+
134
+ Specify the database password
135
+
136
+ ***** -n, --db-encoding=DB_ENCODING
137
+
138
+ Specify the database character encoding, default is =utf8=
139
+
140
+ ***** -o, --db-pool=DB_POOL
141
+
142
+ Specify the database connection pool size, default is =5=
143
+
144
+ ***** -H, --ssh-host=SSH_HOST
145
+
146
+ Specify the SSH host, when the SSH related options are specified, arql will establish an SSH tunnel and connect to the
147
+ database using the SSH tunnel.
148
+
149
+ ***** -O, --ssh-port=SSH_PORT
150
+
151
+ Specify the SSH port
152
+
153
+ ***** -U, --ssh-user=SSH_USER
154
+
155
+ Specify the SSH username
156
+
157
+ ***** -W, --ssh-password=SSH_PASSWORD
158
+
159
+ Specify the SSH password
160
+
161
+ ***** -L, --ssh-local-port=SSH_LOCAL_PORT
162
+
163
+ Specify the SSH local port, default is a /random/ port
164
+
165
+ *** Configuration file
166
+
167
+ The path of the configuration file defaults to =$HOME/.arql.yml= or =$HOME/.arql.d/init.yml=. The configuration file is
168
+ usually the same as the Rails database configuration file, but there are some additional configuration options:
169
+
170
+ 1. =created_at= : An array of custom column names containing the ActiveRecord =created_at= field, default is =created_at=, if specified, the current timestamp will be used to fill the value of the column when created
171
+ 2. =updated_at= : An array of custom column names containing the ActiveRecord =updated_at= field, default is =updated_at=, if specified, the current timestamp will be used to fill the value of the column when updated
172
+ 3. =ssh.host= : ssh host, you can use the host name in the =ssh_config= file, or directly use the IP address or host name
173
+ 4. =ssh.port= : ssh port, default is =22=
174
+ 5. =ssh.user= : ssh username
175
+ 6. =ssh.password= : ssh password
176
+ 7. =ssh.local_port= : ssh local port
177
+ 8. =singularized_table_names=: Whether to use singular table names, default is =false=, if =false=, the =students= table will be defined as the =Student= model, if =true=, the =students= table will be defined as the =Students= model
178
+ 9. =table_name_prefixes=: An array of table name prefixes, defaults to an empty array. If specified, these prefixes will be ignored when generating models. For example, if =["t_"]= is specified, then the =t_students= table will be defined as the =Student= model
179
+
180
+ **** Example
181
+
182
+ #+begin_example
183
+ default: &default
184
+ adapter: mysql2
185
+ encoding: utf8
186
+ created_at: ["gmt_created"]
187
+ updated_at: ["gmt_modified"]
188
+ singularized_table_names: true
189
+
190
+ local:
191
+ <<: *default
192
+ username: root
193
+ database: blog
194
+ table_name_prefixes: ["t_"]
195
+ password:
196
+ socket: /tmp/mysql.sock
197
+
198
+ dev:
199
+ <<: *default
200
+ host: devdb.mycompany.com
201
+ port: 3306
202
+ username: root
203
+ password: 123456
204
+ database: blog
205
+ table_name_prefixes: ["t_"]
206
+ ssh:
207
+ host: dev.mycompany.com
208
+ port: 22
209
+ user: deploy
210
+ password: 12345678
211
+ local_port: 3307
212
+ #+end_example
213
+
214
+
215
+ The example defines a common configuration item =default=, and two specific database environments =local= and =dev=. =local=
216
+ and =dev= inherit the configuration items of =default= in the way of =<<: *default=.
217
+
218
+
219
+ When the command =arql -e dev= is executed, arql will use the =dev= configuration in the configuration file; when the command
220
+ =arql -e local= is executed, arql will use the =local= configuration in the configuration file.
221
+
222
+
223
+ The =dev= environment uses an SSH tunnel. When connecting to the =devdb.mycompany.com= database, an SSH tunnel will be established
224
+ to =dev.mycompany.com= first, and then connected to the database through the SSH tunnel.
225
+
226
+ *** Use Arql as REPL
227
+
228
+ If neither =[ruby file]= nor the =-E= option is specified, and STDIN is a =tty=, arql will start a Pry REPL. For example, execute:
229
+
230
+ #+BEGIN_EXAMPLE
231
+ arql -e dev
232
+ #+END_EXAMPLE
233
+
234
+
235
+ Arql provides some Pry commands:
236
+
237
+ **** =info=
238
+
239
+ The =info= command prints the current database connection information and SSH proxy information, for example:
240
+
241
+ #+begin_example
242
+ Database Connection Information:
243
+ Host:
244
+ Port:
245
+ Username: root
246
+ Password:
247
+ Database: test
248
+ Adapter: mysql2
249
+ Encoding: utf8
250
+ Pool Size: 5
251
+ #+end_example
252
+
253
+ **** =m= or =l=
254
+
255
+ The =m= (or =l=) command prints all table names and their corresponding model class names and abbreviated class names, for example:
256
+
257
+ #+begin_example
258
+ +--------------------+------------------+------+----------------+
259
+ | Table Name | Model Class | Abbr | Comment |
260
+ +--------------------+------------------+------+----------------+
261
+ | post | Post | P | Posts |
262
+ | org | Org | O | Orginations |
263
+ | user_org | UserOrg | UO | |
264
+ | student | Student | S | Students |
265
+ | course | Course | C | |
266
+ | score | Score | S2 | |
267
+ | users | Users | U | |
268
+ | posts | Posts | P2 | |
269
+ | authors | Authors | A | |
270
+ +--------------------+------------------+------+----------------+
271
+ #+end_example
272
+
273
+ Where:
274
+
275
+ - =Table Name= : Table name
276
+ - =Model Class= : Model class name
277
+ - =Abbr= : Abbreviated class name
278
+ - =Comment= : Comment
279
+
280
+ The =m= / =l= command can also accept a parameter to filter the list by table name or table comment, for example:
281
+
282
+ =m perm= will only list tables that contain =perm= in the table name or table comment; if you want to use regular expressions to match,
283
+ you can use =m /perm/i= to match.
284
+
285
+ **** =t=
286
+
287
+ The =t= command accepts a table name or model class name as a parameter and prints the table definition information, for example:
288
+
289
+ Executing the =t Person= command will print the definition information of the =person= table:
290
+
291
+ #+begin_example
292
+ Table: person
293
+ +----|------------|------------------|-----------|-------|-----------|-------|---------|----------|---------+
294
+ | PK | Name | SQL Type | Ruby Type | Limit | Precision | Scale | Default | Nullable | Comment |
295
+ +----|------------|------------------|-----------|-------|-----------|-------|---------|----------|---------+
296
+ | Y | id | int(11) unsigned | integer | 4 | | | | false | |
297
+ | | name | varchar(64) | string | 64 | | | | true | |
298
+ | | age | int(11) | integer | 4 | | | | true | |
299
+ | | gender | int(4) | integer | 4 | | | | true | |
300
+ | | grade | int(4) | integer | 4 | | | | true | |
301
+ | | blood_type | varchar(4) | string | 4 | | | | true | |
302
+ +----|------------|------------------|-----------|-------|-----------|-------|---------|----------|---------+
303
+ #+end_example
304
+
305
+ In addition, =t= is also a class method of the model class. Executing =Person.t= will also print the above information.
306
+
307
+
308
+ Where:
309
+
310
+ - =PK= : Whether it is a primary key
311
+ - =Name= : Column name
312
+ - =SQL Type= : Database type
313
+ - =Ruby Type= : Ruby type
314
+ - =Limit= : Length limit
315
+ - =Precision= : Precision
316
+ - =Scale= : Number of decimal places
317
+ - =Default= : Default value
318
+ - =Nullable= : Whether it can be empty
319
+ - =Comment= : Comment
320
+
321
+ **** =vd=
322
+
323
+
324
+ The =t= command prints the table definition information in the terminal in the form of a table. The disadvantage
325
+ is that if the number of columns in the table is too large, it will cause the table to wrap, which is not
326
+ convenient to view. =vd= (visidata) is a terminal data analysis tool written in Python, which can print the table
327
+ definition information in the terminal in the form of a table, but supports horizontal scrolling, which is
328
+ convenient for viewing.
329
+
330
+ If you want to use the =vd= command of Arql, you need to install visidata first:
331
+
332
+ #+begin_src sh
333
+ pipx install visidata
334
+ #+end_src
335
+
336
+
337
+ The =vd= command and usage are basically the same as the =t= command. In addition, objects such as =Array= /
338
+ =ActiveRecord::Base= can also use the =vd= method.
339
+
340
+ **** =show-sql= / =hide-sql=
341
+
342
+ This pair of commands can switch the display of SQL logs in the Pry REPL.
343
+
344
+ By default, SQL logs are not displayed:
345
+
346
+ #+begin_example
347
+ ARQL@demo247(main) [2] ❯ Student.count
348
+ => 0
349
+ #+end_example
350
+
351
+ After opening the SQL log, the SQL statement executed each time will be displayed:
352
+
353
+ #+begin_example
354
+ ARQL@demo247(main) [3] ❯ show-sql
355
+ ARQL@demo247(main) [4] ❯ Student.count
356
+ D, [2024-04-07T13:31:32.053903 #20440] DEBUG -- : Student Count (29.8ms) SELECT COUNT(*) FROM `student`
357
+ => 0
358
+ #+end_example
359
+
360
+ **** =reconnect=
361
+
362
+ The =reconnect= command is used to reconnect the current database connection. When the connection is disconnected
363
+ due to network reasons, you can use this command to reconnect. When reconnecting, the objects in the current Pry
364
+ session will not be lost. =reconnect= first checks whether the current connection is still valid. If it is valid,
365
+ it will not reconnect. If the validity judgment of =reconnect= for the connection is wrong, you can use the
366
+ =reconnect!= command to force a reconnection.
367
+
368
+ **** =redefine=
369
+ The =redefine= command is used to redefine the ActiveRecord model class and regenerate the model class based on
370
+ the information of the database table. If you have added new relationship definitions in =init.rb= and want the
371
+ new definitions to take effect in the current Pry session, you can use the =redefine= command.
372
+
373
+ **** Sandbox
374
+
375
+ The =sandbox-enter= command is used to enable sandbox mode. In sandbox mode, all database operations are executed
376
+ in a transaction, and the transaction is not automatically committed. You need to manually commit or roll back.
377
+
378
+ 1. Enter sandbox mode:
379
+ #+begin_example
380
+ ARQL@demo247(main) [6] ❯ sandbox-enter
381
+ ARQL@demo247 [sandbox] (main) [7] ❯
382
+ #+end_example
383
+ 2. Quit sandbox mode:
384
+ #+begin_example
385
+ ARQL@demo247 [sandbox] (main) [7] ❯ sandbox-quit
386
+ begin_transaction callbacks removed.
387
+ You still have open 1 transactions open, don't forget commit or rollback them.
388
+ #+end_example
389
+ 3. Commit the transaction:
390
+ #+begin_example
391
+ ARQL@demo247(main) [7] ❯ $C.commit_transaction
392
+ #+end_example
393
+ 4. Roll back the transaction:
394
+ #+begin_example
395
+ ARQL@demo247(main) [7] ❯ $C.rollback_transaction
396
+ #+end_example
397
+
398
+ *** Use Arql as Code Interpreter
399
+
400
+
401
+ If a Ruby file is specified as a command-line parameter, or the =-E= option is used, or STDIN is not a tty, Arql
402
+ will not start Pry, but will directly execute the specified file or code snippet (or read code from standard
403
+ input). Before executing the code snippet, the model class definition will be loaded first. You can think of this
404
+ usage as similar to the =runner= subcommand of =rails=.
405
+
406
+ **** Use =-E= option
407
+
408
+ The =-E= option can be used to execute code snippets directly without starting Pry:
409
+
410
+ #+begin_example
411
+ $ arql -e dev -E 'puts Person.count'
412
+ #+end_example
413
+
414
+ **** Pass a Ruby file as a command-line parameter
415
+
416
+ By specifying a Ruby file as a command-line parameter, you can directly execute the code in the Ruby file:
417
+
418
+ =test.rb=:
419
+
420
+ #+BEGIN_SRC ruby
421
+ puts Person.count
422
+ #+END_SRC
423
+
424
+ #+begin_example
425
+ $ arql -e dev test.rb
426
+ #+end_example
427
+
428
+ **** Read code from standard input
429
+
430
+ Reading code from standard input, you can directly execute code snippets:
431
+
432
+ #+begin_example
433
+ $ echo 'puts Person.count' | arql -e dev
434
+ #+end_example
435
+
436
+ *** Additional extensions
437
+ **** =to_insert_sql= / =to_upsert_sql=
438
+
439
+
440
+ You can call the =to_insert_sql= / =to_upsert_sql= method on any ActiveRecord model instance to get the insert or
441
+ update SQL statement for the object. These two methods can also be called on an array object containing ActiveRecord
442
+ model instance objects.
443
+
444
+ #+begin_example
445
+ ARQL ❯ Person.all.to_a.to_insert_sql
446
+ => "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`;"
447
+ #+end_example
448
+
449
+ **** =to_create_sql=
450
+
451
+ You can call the =to_create_sql= method on any ActiveRecord model class to get the create SQL statement for the table
452
+ corresponding to the model class.
453
+
454
+ #+begin_example
455
+ ARQL@demo247(main) [16] ❯ puts Post.to_create_sql
456
+ D, [2024-04-07T14:15:11.106693 #20440] DEBUG -- : SQL (24.9ms) show create table post
457
+ CREATE TABLE `post` (
458
+ `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',
459
+ `name` varchar(256) DEFAULT NULL,
460
+ `gender` varchar(256) DEFAULT NULL,
461
+ `phone` varchar(256) DEFAULT NULL,
462
+ `id_no` varchar(256) DEFAULT NULL,
463
+ `note` varchar(256) DEFAULT NULL,
464
+ `gmt_created` datetime NOT NULL COMMENT '创建时间',
465
+ `gmt_modified` datetime NOT NULL COMMENT '最后修改时间',
466
+ PRIMARY KEY (`id`),
467
+ KEY `index_post_on_name` (`name`)
468
+ ) ENGINE=InnoDB AUTO_INCREMENT=83 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci
469
+ #+end_example
470
+
471
+ **** =t=
472
+
473
+ =t= can be called as a class method on an ActiveRecord model class, or as an instance method on an ActiveRecord model
474
+ instance object.
475
+
476
+ #+begin_example
477
+ ARQL ❯ Person.last.t
478
+ +----------------|-----------------|------------------|---------+
479
+ | Attribute Name | Attribute Value | SQL Type | Comment |
480
+ +----------------|-----------------|------------------|---------+
481
+ | id | 11 | int(11) unsigned | |
482
+ | name | Jackson | varchar(64) | |
483
+ | age | 30 | int(11) | |
484
+ | gender | 2 | int(4) | |
485
+ | grade | 2 | int(4) | |
486
+ | blood_type | AB | varchar(4) | |
487
+ +----------------|-----------------|------------------|---------+
488
+ #+end_example
489
+
490
+ The =t= method can accept the following two options:
491
+
492
+ + =:except= option, used to specify the attribute names that are not displayed. The value can be a string or a regular
493
+ #+BEGIN_EXAMPLE
494
+ Person.last.t(except: 'id')
495
+ Student.where(condition).t(except: /id|name/)
496
+ #+END_EXAMPLE
497
+ + =:compact= option, used to specify whether to display in compact mode. The value can be =true= or =false=. If compact
498
+ mode is enabled, columns with all NULL values will not be displayed. This is useful for viewing tables with sparse
499
+
500
+ #+BEGIN_EXAMPLE
501
+ Person.last.t(compact: true)
502
+ Student.where(condition).t(compact: false)
503
+ #+END_EXAMPLE
504
+
505
+ **** =v=
506
+
507
+ The =v= method is used to integrate with Emacs org babel.
508
+
509
+ ***** =v= as instance method
510
+
511
+
512
+ Call the =v= method on any ActiveRecord model instance object to print an array. The first element of the array is
513
+ =['Attribute Name', 'Attribute Value', 'SQL Type', 'Comment']=, the second element is =nil=, and the remaining elements
514
+ are the attribute names and values of the object. In Emacs org-mode, if the =:result= type is =value= (the default), this
515
+ return value will be rendered as a beautiful table.
516
+
517
+ #+begin_example
518
+ ARQL ❯ Person.last.v
519
+ => [["Attribute Name", "Attribute Value", "SQL Type", "Comment"],
520
+ nil,
521
+ ["id", 11, "int(11) unsigned", ""],
522
+ ["name", "Jackson", "varchar(64)", ""],
523
+ ["age", 30, "int(11)", ""],
524
+ ["gender", 2, "int(4)", ""],
525
+ ["grade", 2, "int(4)", ""],
526
+ ["blood_type", "AB", "varchar(4)", ""]]
527
+ #+end_example
528
+
529
+ ***** Array containing only model instances
530
+
531
+ #+begin_example
532
+ ARQL ❯ Person.all.to_a.v
533
+ => [["id", "name", "age", "gender", "grade", "blood_type"],
534
+ nil,
535
+ [1, "Jack", 30, nil, nil, nil],
536
+ [2, "Jack", 11, 1, nil, nil],
537
+ [3, "Jack", 12, 1, nil, nil],
538
+ [4, "Jack", 30, 1, nil, nil],
539
+ [5, "Jack", 12, 2, nil, nil],
540
+ [6, "Jack", 2, 2, 2, nil],
541
+ [7, "Jack", 3, 2, 2, nil],
542
+ [8, "Jack", 30, 2, 2, "AB"],
543
+ [9, "Jack", 30, 2, 2, "AB"],
544
+ [10, "Jack", 30, 2, 2, "AB"],
545
+ [11, "Jackson", 30, 2, 2, "AB"]]
546
+ #+end_example
547
+
548
+ ***** Array containing only homogeneous Hash objects
549
+
550
+ #+begin_example
551
+ ARQL ❯ arr = [{name: 'Jack', age: 10}, {name: 'Lucy', age: 20}]
552
+ => [{:name=>"Jack", :age=>10}, {:name=>"Lucy", :age=>20}]
553
+ ARQL ❯ arr.v
554
+ => [[:name, :age], nil, ["Jack", 10], ["Lucy", 20]]
555
+ #+end_example
556
+
557
+ **** =q=
558
+
559
+ #+begin_example
560
+ ARQL ❯ rs = q 'select count(0) from person;'
561
+ => #<ActiveRecord::Result:0x00007fd1f8026ad0 @column_types={}, @columns=["count(0)"], @hash_rows=nil, @rows=[[11]]>
562
+ ARQL ❯ rs.rows
563
+ => [[11]]
564
+ #+end_example
565
+
566
+ **** JSON conversion and formatting
567
+
568
+ Call the =j= method on any object to get a JSON-formatted string, and call the =jj= method to get a formatted JSON string.
569
+
570
+ Use the =jp= method to print JSON and the =jjp= method to print formatted JSON.
571
+
572
+ **** The =$C= Global Variables
573
+
574
+
575
+ Arql assigns the =ActiveRecord::Base.connection= object to the globally available =$C= global variable, which represents the
576
+
577
+ The =q= method mentioned above is actually the =$C.exec_query= method. Other methods of the =$C= object are also useful:
578
+
579
+ ***** Create Table
580
+ #+begin_example
581
+ ARQL ❯ $C.create_table :post, id: false, primary_key: :id do |t|
582
+ ARQL ❯ t.column :id, :bigint, precison: 19, comment: 'ID'
583
+ ARQL ❯ t.column :name, :string, comment: '名称'
584
+ ARQL ❯ t.column :gmt_created, :datetime, comment: '创建时间'
585
+ ARQL ❯ t.column :gmt_modified, :datetime, comment: '最后修改时间'
586
+ ARQL ❯ end
587
+ #+end_example
588
+
589
+ The =create_table= method is also added to the =Kernel= module, so you can also call the =create_table= method directly:
590
+
591
+ #+begin_example
592
+ ARQL ❯ create_table :post, id: false, primary_key: :id do |t|
593
+ ARQL ❯ t.column :id, :bigint, precison: 19, comment: 'ID'
594
+ ARQL ❯ t.column :name, :string, comment: '名称'
595
+ ARQL ❯ t.column :gmt_created, :datetime, comment: '创建时间'
596
+ ARQL ❯ t.column :gmt_modified, :datetime, comment: '最后修改时间'
597
+ ARQL ❯ end
598
+ #+end_example
599
+
600
+ ***** Add Column
601
+
602
+ #+begin_example
603
+ $C.add_column :post, :note, :string, comment: 'the note'
604
+ #+end_example
605
+
606
+ The =add_column= method is also added to the class methods of the model class, so you can also call the
607
+ =add_column= method directly on the model class:
608
+
609
+ #+begin_example
610
+ Post.add_column :note, :string, comment: 'the note'
611
+ #+end_example
612
+
613
+ ***** Change Column
614
+
615
+ #+begin_example
616
+ $C.change_column :post, :note, :text, comment: '备注'
617
+ #+end_example
618
+
619
+ The =change_column= method is also added to the class methods of the model class, so you can also call the
620
+ =change_column= method directly on the model class:
621
+
622
+ #+begin_example
623
+ Post.change_column :note, :text, comment: '备注'
624
+ #+end_example
625
+
626
+ ***** Remove Column
627
+
628
+ #+begin_example
629
+ $C.remove_column :post, :note
630
+ #+end_example
631
+
632
+ The =remove_column= method is also added to the class methods of the model class, so you can also call the
633
+ =remove_column= method directly on the model class:
634
+
635
+ #+begin_example
636
+ Post.remove_column :note
637
+ #+end_example
638
+
639
+ ***** Drop Table
640
+
641
+ #+begin_example
642
+ $C.drop_table :post
643
+ #+end_example
644
+
645
+ The =drop_table= method is also added to the class methods of the model class, so you can also call the
646
+ =drop_table= method directly on the model class:
647
+
648
+ #+begin_example
649
+ Post.drop_table
650
+ #+end_example
651
+
652
+ ***** Add Index
653
+
654
+ #+begin_example
655
+ ARQL ❯ $C.add_index :post, :name
656
+ ARQL ❯ $C.add_index(:accounts, [:branch_id, :party_id], unique: true, name: 'by_branch_party')
657
+ #+end_example
658
+
659
+ The =add_index= method is also added to the class methods of the model class, so you can also call the
660
+ =add_index= method directly on the model class:
661
+
662
+ #+begin_example
663
+ Post.add_index :name
664
+ Post.add_index [:branch_id, :party_id], unique: true, name: 'by_branch_party'
665
+ #+end_example
666
+
667
+ **** Kernel Extensions
668
+
669
+ The functions under the =Kernel= module can be called directly like built-in functions without specifying the module name.
670
+ The following are the =Kernel= methods extended by Arql:
671
+
672
+ Pry has built-in =show-source= (alias =$= ) and =show-doc= (alias =?= ) commands to view the source code and documentation of methods. You can view the documentation of a method using =show-doc=. For example:
673
+
674
+
675
+ #+BEGIN_EXAMPLE
676
+ ARQL ❯ ? create_table
677
+ #+END_EXAMPLE
678
+
679
+
680
+ ***** =create_table=
681
+ #+BEGIN_EXAMPLE
682
+ create_table :post, id: false, primary_key: :id do |t|
683
+ t.column :id, :bigint, precison: 19, comment: 'ID'
684
+ t.column :name, :string, comment: '名称'
685
+ t.column :gmt_created, :datetime, comment: '创建时间'
686
+ t.column :gmt_modified, :datetime, comment: '最后修改时间'
687
+ end
688
+ #+END_EXAMPLE
689
+ ***** =create_join_table=
690
+ #+BEGIN_EXAMPLE
691
+ create_join_table :products, :categories do |t|
692
+ t.index :product_id
693
+ t.index :category_id
694
+ end
695
+ #+END_EXAMPLE
696
+ ***** =drop_table=
697
+ #+BEGIN_EXAMPLE
698
+ drop_table :post
699
+ #+END_EXAMPLE
700
+ ***** =drop_join_table=
701
+ #+BEGIN_EXAMPLE
702
+ drop_join_table :products, :categories
703
+ #+END_EXAMPLE
704
+ ***** =rename_table=
705
+ #+BEGIN_EXAMPLE
706
+ rename_table :post, :posts
707
+ #+END_EXAMPLE
708
+
709
+ ***** =print_tables=
710
+
711
+ Arql provides a =print_tables= method that exports information about all tables in the current database as:
712
+
713
+ + markdown table format: ~print_tables(:md)~
714
+ + org-mode table format: ~print_tables(:org)~
715
+ + create table SQL: ~print_tables(:sql)~
716
+
717
+ **** Model Class Methods Extensions
718
+
719
+ Arql adds some class methods to the model class for creating, modifying, and deleting columns, indexes, and other
720
+ operations on tables. These methods can be called directly on the model class without the need to call them
721
+ through an instance object of the =ActiveRecord::Migration= class.
722
+
723
+ Pry has built-in =show-source= (alias =$= ) and =show-doc= (alias =?= ) commands to view the source code and
724
+ documentation of methods. You can view the documentation of a method using =show-doc=. For example:
725
+
726
+ #+BEGIN_EXAMPLE
727
+ ARQL ❯ ? Student.add_column
728
+ #+END_EXAMPLE
729
+
730
+ ***** =add_column=
731
+ #+BEGIN_EXAMPLE
732
+ Student.add_column :note, :text, comment: '备注'
733
+ #+END_EXAMPLE
734
+
735
+ ***** =change_column=
736
+ #+BEGIN_EXAMPLE
737
+ Student.change_column :note, :string, comment: '备注'
738
+ #+END_EXAMPLE
739
+
740
+ ***** =remove_column=
741
+ #+BEGIN_EXAMPLE
742
+ Student.remove_column :note
743
+ #+END_EXAMPLE
744
+
745
+ ***** =add_index=
746
+ #+BEGIN_EXAMPLE
747
+ Student.add_index :name
748
+ Student.add_index [:branch_id, :party_id], unique: true, name: 'by_branch_party'
749
+ #+END_EXAMPLE
750
+
751
+ ***** =change_column_comment=
752
+ #+BEGIN_EXAMPLE
753
+ Student.change_column_comment :note, '备注'
754
+ #+END_EXAMPLE
755
+
756
+ ***** =change_column_default=
757
+ #+BEGIN_EXAMPLE
758
+ Student.change_column_default :note, '默认值'
759
+ #+END_EXAMPLE
760
+
761
+ ***** =rename_column=
762
+ #+BEGIN_EXAMPLE
763
+ Student.rename_column :note, :remark
764
+ #+END_EXAMPLE
765
+
766
+ ***** =rename_table=
767
+ #+BEGIN_EXAMPLE
768
+ Student.rename_table :seitou
769
+ #+END_EXAMPLE
770
+
771
+ ***** =change_table_comment=
772
+ #+BEGIN_EXAMPLE
773
+ Student.change_table_comment from: '', to: '学生表'
774
+ #+END_EXAMPLE
775
+
776
+ ***** =drop_table=
777
+ #+BEGIN_EXAMPLE
778
+ Student.drop_table
779
+ #+END_EXAMPLE
780
+
781
+ ***** =remove_index=
782
+ #+BEGIN_EXAMPLE
783
+ Student.remove_index :age
784
+ Student.remove_index name: 'by_branch_party'
785
+ #+END_EXAMPLE
786
+
787
+ ***** =table_comment=
788
+ #+BEGIN_EXAMPLE
789
+ Student.table_comment
790
+ #+END_EXAMPLE
791
+
792
+ ***** =indexes=
793
+ #+BEGIN_EXAMPLE
794
+ Student.indexes
795
+ #+END_EXAMPLE
796
+
797
+ **** Read and Write Excel and CSV Files
798
+
799
+ Arql integrates two Excel libraries, =roo= and =caxlsx= to provide methods for parsing and generating Excel files.
800
+ Arql also provides methods for reading and writing CSV files.
801
+
802
+ ***** Parse Excel
803
+
804
+ Arql adds a =parse_excel= method to the =Kernel= module, which can be used to parse Excel files. For example:
805
+
806
+ #+BEGIN_EXAMPLE
807
+ ARQL ❯ parse_excel 'path/to/excel.xlsx'
808
+ #+END_EXAMPLE
809
+
810
+ You can use =~/= in the file path to represent the user's home directory, and Arql will automatically expand it.
811
+
812
+ You can also call the =parse_excel= method on a String object representing a file path:
813
+
814
+ #+BEGIN_EXAMPLE
815
+ ARQL ❯ 'path/to/excel.xlsx'.parse_excel
816
+ #+END_EXAMPLE
817
+
818
+ The =parse_excel= method returns a Hash object, where the key is the sheet name and the value is the data of the
819
+ sheet, which is a two-dimensional array. For example:
820
+
821
+ #+BEGIN_EXAMPLE
822
+ {
823
+ 'Sheet1' => [
824
+ ['A1', 'B1', 'C1'],
825
+ ['A2', 'B2', 'C2'],
826
+ ['A3', 'B3', 'C3']
827
+ ],
828
+ 'Sheet2' => [
829
+ ['A1', 'B1', 'C1'],
830
+ ['A2', 'B2', 'C2'],
831
+ ['A3', 'B3', 'C3']
832
+ ]
833
+ }
834
+ #+END_EXAMPLE
835
+
836
+ ***** Generate Excel
837
+
838
+ Arql adds a =write_excel= method to Hash / Array / ActiveRecord::Relation / ActiveRecord::Base objects, which can
839
+ be used to generate Excel files:
840
+
841
+
842
+ ****** Genrate Excel from =Hash= Object
843
+
844
+ #+BEGIN_EXAMPLE
845
+ ARQL ❯ obj.write_excel 'path/to/excel.xlsx'
846
+ #+END_EXAMPLE
847
+
848
+ =Hash#write_excel= requires that the key of the Hash object is the sheet name and the value is the data of the
849
+ sheet. The type of the value can be:
850
+
851
+ + An array, where the elements of the array can be:
852
+ + An array, representing a row of data
853
+ + A Hash object, representing a row of data, where the key is the column name and the value is the column value
854
+ + An =ActiveRecord::Base= object, representing a row of data
855
+ + A Hash object, which contains two key-value pairs:
856
+ + =:fields=, an array representing the column names
857
+ + =:data=, a two-dimensional array representing the data
858
+
859
+ ****** Generate Excel from =Array= Object
860
+
861
+ #+BEGIN_EXAMPLE
862
+ ARQL ❯ obj.write_excel 'path/to/excel.xlsx', :name, :age, :gender, sheet_name: '订单数据'
863
+ #+END_EXAMPLE
864
+
865
+ Where:
866
+
867
+ + =:name, :age, :gender= These parameters are column names. If not specified, the column names will be determined
868
+ based on the first element of the array:
869
+ - If the element is an ActiveRecord::Base object, all attribute names of the object (i.e., the database field
870
+ list) will be used as column names
871
+ - If the element is a Hash object, all keys of the Hash will be used as column names
872
+ + =sheet_name= Specifies the sheet name. If not specified, the default sheet name =Sheet1= will be used
873
+
874
+ Each element of the Array object represents a row of data. =Array#write_excel= requires that each element of the
875
+ Array object:
876
+
877
+ + An =ActiveRecord::Base= object
878
+ + A =Hash= object, representing a row of data, where the key is the column name and the value is the column value
879
+ + An array, representing a row of data
880
+
881
+ ****** Generate Excel from =ActiveRecord::Base= Object
882
+
883
+ #+BEGIN_EXAMPLE
884
+ ARQL ❯ Student.find(123).write_excel 'path/to/excel.xlsx', sheet_name: '学生数据'
885
+ #+END_EXAMPLE
886
+
887
+ The =write_excel= method of =ActiveRecord::Base= actually wraps the =ActiveRecord::Base= object into an =Array= object with
888
+ only one element, and then calls the =write_excel= method of Array.
889
+
890
+ ****** Generate Excel from =ActiveRecord::Relation= Object
891
+
892
+ #+BEGIN_EXAMPLE
893
+ ARQL ❯ Student.where(gender: 'M').write_excel 'path/to/excel.xlsx', sheet_name: '男学生'
894
+ #+END_EXAMPLE
895
+
896
+ The =write_excel= method of =ActiveRecord::Relation= actually converts the =ActiveRecord::Relation= object into an Array
897
+ object, and then calls the =write_excel= method of =Array=.
898
+
899
+ ***** Parse CSV
900
+
901
+ Arql provides the =parse_csv= method, which can be used to parse CSV files:
902
+
903
+ #+BEGIN_EXAMPLE
904
+ ARQL ❯ parse_csv 'path/to/csv.csv'
905
+ #+END_EXAMPLE
906
+
907
+
908
+ The =parse_csv= method returns a CSV object in the standard library.
909
+
910
+ =parse_csv= can have the following optional parameters:
911
+
912
+ - =encoding=, specifies the encoding of the CSV file, which is =UTF-16= (with BOM) by default
913
+ - =headers=, specifies whether to include the header, which is =false= by default
914
+ - =col_sep=, specifies the column separator, which is =\t= by default
915
+ - =row_sep=, specifies the row separator, which is =\r\n= by default
916
+
917
+
918
+ (The default values above are actually the configurations used by Microsoft Office Excel when saving CSV files)
919
+
920
+
921
+ You can also call the =parse_csv= method on a String object representing the file path:
922
+
923
+ #+BEGIN_EXAMPLE
924
+ ARQL ❯ 'path/to/csv.csv'.parse_csv
925
+ #+END_EXAMPLE
926
+
927
+ ***** Generate CSV
928
+
929
+ Arql adds the =write_csv= method to =Array= / =ActiveRecord::Relation= / =ActiveRecord::Base= objects, which can be used to
930
+ generate CSV files:
931
+
932
+ ****** Generate CSV from =Array= Object
933
+
934
+ #+BEGIN_EXAMPLE
935
+ ARQL ❯ obj.write_csv 'path/to/csv.csv', :name, :age, :gender, sheet_name: '订单数据'
936
+ #+END_EXAMPLE
937
+
938
+ The usage is similar to the write_excel method of =Array= objects.
939
+
940
+
941
+ ****** Generate CSV from =ActiveRecord::Base= Object
942
+
943
+ #+BEGIN_EXAMPLE
944
+ ARQL ❯ Student.find(123).write_csv 'path/to/csv.csv', sheet_name: '学生数据'
945
+ #+END_EXAMPLE
946
+
947
+ The usage is similar to the =write_excel= method of =ActiveRecord::Base= objects.
948
+
949
+ ****** Generate CSV from =ActiveRecord::Relation= Object
950
+
951
+
952
+ #+BEGIN_EXAMPLE
953
+ ARQL ❯ Student.where(gender: 'M').write_csv 'path/to/csv.csv', sheet_name: '男学生'
954
+ #+END_EXAMPLE
955
+
956
+
957
+ The usage is similar to the =write_excel= method of =ActiveRecord::Relation= objects.
958
+
959
+ **** dump data from database
960
+
961
+ Note: Only MySQL databases are supported
962
+
963
+ Arql adds the =dump= method to =Array= / =ActiveRecord::Base= / =ActiveRecord::Relation= objects, which can be used to
964
+ export data to SQL files:
965
+
966
+
967
+ ***** Dump data from =Array= Object
968
+
969
+ #+BEGIN_EXAMPLE
970
+ ARQL ❯ obj.dump 'path/to/dump.sql', batch_size: 5000
971
+ #+END_EXAMPLE
972
+
973
+
974
+ Each element of the =Array= object must be an =ActiveRecord::Base= object
975
+
976
+ The =batch_size= parameter specifies the data queried in each batch, with a default value of 500
977
+
978
+ ***** Dump data from =ActiveRecord::Base= Object
979
+
980
+ #+BEGIN_EXAMPLE
981
+ ARQL ❯ Student.find(123).dump 'path/to/dump.sql', batch_size: 5000
982
+ #+END_EXAMPLE
983
+
984
+
985
+ The =dump= method of =ActiveRecord::Base= objects actually wraps the =ActiveRecord::Base= object into an =Array= object
986
+ with only one element, and then calls the =dump= method of =Array=.
987
+
988
+ ***** Dump data from =ActiveRecord::Relation= Object
989
+
990
+ #+BEGIN_EXAMPLE
991
+ ARQL ❯ Student.where(gender: 'M').dump 'path/to/dump.sql', batch_size: 5000
992
+ #+END_EXAMPLE
993
+
994
+
995
+ The =dump= method of =ActiveRecord::Relation= objects actually converts the =ActiveRecord::Relation= object into an =Array= object,
996
+ and then calls the =dump= method of Array.
997
+
998
+
999
+ ***** Use the =dump= class method of =ActiveRecord::Base=
1000
+
1001
+ #+BEGIN_EXAMPLE
1002
+ ARQL ❯ Student.dump 'path/to/dump.sql', no_create_table: false
1003
+ #+END_EXAMPLE
1004
+
1005
+ This method will export all data in the Student table to the SQL file using the =mysqldump= command.
1006
+
1007
+ The =no_create_table= parameter specifies whether to include the create table statement in the SQL file, with a default value of =false=.
1008
+
1009
+
1010
+
1011
+ ***** Use the dump method on the global connection object =$C=
1012
+
1013
+ #+BEGIN_EXAMPLE
1014
+ ARQL ❯ $C.dump 'path/to/dump.sql', no_create_db: false
1015
+ #+END_EXAMPLE
1016
+
1017
+
1018
+ This method will export the data of all tables in the current database to the SQL file using the =mysqldump= command.
1019
+
1020
+ The =no_create_db= parameter specifies whether to include the create database statement in the SQL file, with a default value of =false=.
1021
+
1022
+
1023
+ **** Plot
1024
+
1025
+ Arql integrates the youplot library of Ruby and adds some methods to Array that can be used to draw charts:
1026
+
1027
+ + =barplot=
1028
+ + =countplot=
1029
+ + =histo=
1030
+ + =lineplot=
1031
+ + =lineplots=
1032
+ + =scatter=
1033
+ + =density=
1034
+ + =boxplot=
1035
+
1036
+ Example:
1037
+
1038
+ Count plot:
1039
+
1040
+ #+BEGIN_EXAMPLE
1041
+ ARQL@demo247(main) [44] ❯ Student.pluck(:gender)
1042
+ => ["M", "M", "M", "M", "M", "M", "M", "F", "M", "F", "M", "M", "M", "M", "M"]
1043
+ ARQL@demo247(main) [45] ❯ Student.pluck(:gender).countplot
1044
+ ┌ ┐
1045
+ M ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 13.0
1046
+ F ┤■■■■■ 2.0
1047
+ └ ┘
1048
+ #+END_EXAMPLE
1049
+
1050
+ Histo plot:
1051
+
1052
+ #+BEGIN_EXAMPLE
1053
+ ARQL@jicai.dev(main) [18] ❯ Order.last(20).pluck(:order_sum)
1054
+ => [0.21876e5, 0.336571e5, 0.1934e5, 0.966239e4, 0.38748e3, 0.31092e4, 0.483e5, 0.445121e5, 0.1305e4, 0.2296e6, 0.943e5, 0.352e4, 0.3756e5, 0.323781e5, 0.7937622e5, 0.982e4, 0.338393e5, 0.316597e5, 0.213678e5, 0.336845e5]
1055
+ ARQL@jicai.dev(main) [19] ❯ Order.last(20).pluck(:order_sum).histo
1056
+ ┌ ┐
1057
+ [ 0.0, 50000.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 17
1058
+ [ 50000.0, 100000.0) ┤▇▇▇▇ 2
1059
+ [100000.0, 150000.0) ┤ 0
1060
+ [150000.0, 200000.0) ┤ 0
1061
+ [200000.0, 250000.0) ┤▇▇ 1
1062
+ └ ┘
1063
+ Frequency
1064
+
1065
+ #+END_EXAMPLE
1066
+
1067
+ **** =String=
1068
+
1069
+ ***** =Srting#p=
1070
+
1071
+ The definition of the =p= method is as follows:
1072
+
1073
+ #+begin_example
1074
+ class String
1075
+ def p
1076
+ puts self
1077
+ end
1078
+ end
1079
+ #+end_example
1080
+
1081
+
1082
+ =​"hello".p= is equivalent to =puts "hello"​=.
1083
+
1084
+ ***** =String#parse=
1085
+
1086
+ For a string representing a file path, you can call the =parse= method to parse Excel, CSV, and JSON files based
1087
+ on the file extension in the file path.
1088
+
1089
+ #+BEGIN_EXAMPLE
1090
+ excel = 'path/to/excel.xlsx'.parse
1091
+ csv = 'path/to/csv.csv'.parse
1092
+ json = 'path/to/json.json'.parse
1093
+ #+END_EXAMPLE
1094
+
1095
+ **** =ID=
1096
+
1097
+ Arql provides an ID class for generating snowflake algorithm IDs and UUIDs.
1098
+
1099
+ #+BEGIN_EXAMPLE
1100
+ id = ID.long # 生成一个雪花算法 ID
1101
+ id = ID.uuid # 生成一个 UUID
1102
+ #+END_EXAMPLE
1103
+
1104
+ **** Ransack
1105
+
1106
+ Arql integrates Ransack:
1107
+
1108
+ #+BEGIN_EXAMPLE
1109
+ Student.ransack(name_cont: 'Tom').result # 模糊查询名字中包含 'Tom' 的学生
1110
+ Student.ransack(name_start: 'Tom').result # 模糊查询名字以 'Tom' 开头的学生
1111
+ #+END_EXAMPLE
1112
+
1113
+ *** Emacs Org Babel Integration
1114
+
1115
+ Here is an [[https://github.com/lululau/spacemacs-layers/blob/master/ob-arql/local/ob-arql/ob-arql.el][ob-arql]], which is used to integrate Emacs org babel.
1116
+
1117
+ ** Guides and Tips
1118
+ *** [[./define-associations.org][Define Associations in Initializer File]]
1119
+ *** [[./initializer-structure.org][Put Initialization Code for Different Environments in Different Files]]
1120
+ *** [[./helper-for-datetime-range-query.org][Define Convenient Methods for Quickly Querying by Time]]
1121
+ *** [[./auto-set-id-before-save.org][Automatically Set ID for New Objects Before Saving]]
1122
+ *** [[./custom-configurations.org][Custom Configuration Items in the Configuration File]]
1123
+ *** [[./sql-log.org][Automatically Record SQL Logs and REPL Input History]]
1124
+ *** [[./fuzzy-field-query.org][Fuzzy Field Name Query]]
1125
+ *** [[./oss-files.org][OSS Data Download and View]]
1126
+ *** Use Arql to query SQLite3 database file
1127
+
1128
+ You can use Arql to view SQLite3 database files, for example:
1129
+
1130
+ #+BEGIN_EXAMPLE
1131
+ arql -d db/development.sqlite3
1132
+ #+END_EXAMPLE
1133
+
1134
+ *** Find fields by name or comment
1135
+
1136
+ When we are familiar with a project, we often encounter the following situation: we know the name or comment of a
1137
+ field, but we do not know the corresponding table name and field name. At this time, we can use the following method
1138
+ to find:
1139
+
1140
+ #+BEGIN_SRC ruby
1141
+ puts model_classes.flat_map { |m| m.columns.select {|c| c.name =~ /amount/ || c.comment =~ /金额/ }.map {|c| "Table: #{m.table_name}, Column: #{c.name} (#{c.comment})"} }
1142
+
1143
+ # 输出:
1144
+ # Table: order, Column: entry_amount (订单金额)
1145
+ # Table: sub_order, Column: entry_price (金额)
1146
+ #+END_SRC
1147
+
1148
+ ** Development
1149
+
1150
+ After checking out the code, run =bin/setup= to install dependencies. You can also run =bin/console= for an
1151
+ interactive prompt that will allow you to experiment.
1152
+
1153
+ To install this gem onto your local machine, run =bundle exec rake install=. To release a new version, update the
1154
+ version number in =version.rb=, and then run =bundle exec rake release=, which will create a git tag for the
1155
+ version, push git commits and tags, and push the =.gem= file to [[https://rubygems.org][rubygems.org]].
1156
+
1157
+
1158
+ ** Contributing
1159
+
1160
+ Bug reports and pull requests are welcome on GitHub at https://github.com/lululau/arql. This project is intended to
1161
+ be a safe, welcoming space for collaboration, and contributors are expected to adhere to the
1162
+ [[https://github.com/lululau/arql/blob/master/CODE_OF_CONDUCT.md][Code of Conduct]].
1163
+
1164
+
1165
+ ** License
1166
+
1167
+ The gem is available as open source under the terms of the [[https://opensource.org/licenses/MIT][MIT License]].
1168
+
1169
+ ** Code of Conduct
1170
+
1171
+
1172
+ Everyone interacting in the Arql project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow
1173
+ the [[https://github.com/lululau/arql/blob/master/CODE_OF_CONDUCT.md][Code of Conduct]].