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-zh_CN.org ADDED
@@ -0,0 +1,1102 @@
1
+ * Arql
2
+
3
+ Arql是一个简单的工具性 Gem,它将 Rails ActiveRecord 和 Pry 结合在一起,并添加了有用的 Pry 命令。它可以根据数据库表的信
4
+ 息自动定义模型类。如果你是 Ruby 用户,你可以将这个 Arql 用作你的数据库查询工具。
5
+
6
+ ** 依赖
7
+
8
+ + Ruby 2.6.0 或更高版本
9
+ + 对于不同类型的数据库,需要安装相应的数据库适配器或客户端二进制库:
10
+ - MySQL: 根据你的操作系统,你可能需要安装: =libmariadb-dev= 、 =libmysqlclient-dev= 、 =mysql-devel=
11
+ =default-libmysqlclient-dev= ; 请参阅发行版的软件包指南以查找特定的软件包;或者参考 [[https://github.com/brianmario/mysql2][mysql2 的文档]]
12
+ - SQLite3: 不需要安装任何额外的库
13
+ - PostgreSQL: ~gem install pg~
14
+ - Oracle: ~gem install activerecord-oracle_enhanced-adapter~
15
+ - SQL Server: ~gem install activerecord-sqlserver-adapter~
16
+
17
+ ** 安装
18
+
19
+ 执行:
20
+
21
+ #+begin_example
22
+ $ gem install arql
23
+ #+end_example
24
+
25
+ 如果遇到系统权限问题,请尝试使用 sudo:
26
+
27
+ #+begin_example
28
+ $ sudo gem install arql
29
+ #+end_example
30
+
31
+ ** 使用方法
32
+
33
+ *** 命令行选项
34
+
35
+ #+begin_example
36
+ Usage: arql [options] [ruby file]
37
+
38
+ 如果既没有指定 [ruby file] 也没有指定 -E 选项,并且 STDIN 是一个 tty,将启动 Pry REPL,
39
+ 否则将运行指定的 ruby file 或 -E 选项值或从 STDIN 读取的 ruby 代码,并且不会启动 REPL
40
+
41
+ -c, --conf=CONFIG_FILE 指定配置文件,默认为 $HOME/.arql.yml 或 $HOME/.arql.d/init.yml
42
+ -i, --initializer=INITIALIZER 指定初始化 Ruby 文件,默认为 $HOME/.arql.rb 或 $HOME/.arql.d/init.rb
43
+ -e, --env=ENVIRON 指定配置环境
44
+ -a, --db-adapter=DB_ADAPTER 指定数据库适配器,默认为 sqlite3
45
+ -h, --db-host=DB_HOST 指定数据库主机
46
+ -p, --db-port=DB_PORT 指定数据库端口
47
+ -d, --db-name=DB_NAME 指定数据库名称
48
+ -u, --db-user=DB_USER 指定数据库用户
49
+ -P, --db-password=DB_PASSWORD 指定数据库密码
50
+ -n, --db-encoding=DB_ENCODING 指定数据库编码,默认为 utf8
51
+ -o, --db-pool=DB_POOL 指定数据库连接池大小,默认为 5
52
+ -H, --ssh-host=SSH_HOST 指定 SSH 主机
53
+ -O, --ssh-port=SSH_PORT 指定 SSH 端口
54
+ -U, --ssh-user=SSH_USER 指定 SSH 用户
55
+ -W, --ssh-password=SSH_PASSWORD 指定 SSH 密码
56
+ -L, --ssh-local-port=SSH_LOCAL_PORT 指定本地 SSH 代理端口
57
+ -E, --eval=CODE 执行代码
58
+ -S, --show-sql 在 STDOUT 上显示 SQL
59
+ -w, --write-sql=OUTPUT 将 SQL 写入 OUTPUT 文件
60
+ -A, --append-sql=OUTPUT 将 SQL 追加到 OUTPUT 文件
61
+ --help 打印帮助信息
62
+ #+end_example
63
+
64
+ **** =-c, --config=CONFIG_FILE=
65
+
66
+ 指定配置文件位置,默认为 =$HOME/.arql.yml= 或 =$HOME/.arql.d/init.yml= 。 配置文件通常与 Rails数据库配置文件相同,
67
+ 但有一些额外的配置选项,例如 =ssh= 选项等。 参考 =配置文件= 部分。
68
+
69
+ **** =-i, --initializer=INITIALIZER=
70
+
71
+ 指定一个 Ruby 源文件,Arql 定义 ActiveRecord 模型类之后执行此文件的代码,默认为 =$HOME/.arql.rb= 或者
72
+ =$HOME/.arql.d/init.rb= 。你可以在这个文件中为 ActiveRecord 模型类添加方法和关联关系定义。
73
+
74
+ **** =-e, --env=ENVIRON=
75
+
76
+ 指定一个在配置文件中的环境名称。
77
+
78
+ **** =-E, --eval=CODE=
79
+
80
+ 指定一个 Ruby 代码片段,如果指定了此选项,将不会启动 Pry REPL。
81
+
82
+ **** =-S, --show-sql=
83
+
84
+ arql 默认不显示 SQL 日志,使用此选项打开。
85
+
86
+ **** =-w, --write-sql=OUTPUT=
87
+
88
+ 你也可以使用此选项让 arql 将 SQL 日志写入文件。
89
+
90
+ **** =-A, --append-sql-OUTOUT=
91
+
92
+ 与 =-w= 类似,但是采用追加写入的方式,不会截断已有文件。
93
+
94
+ **** 数据库选项
95
+
96
+ 本节中描述的选项通常会在配置文件中配置,这些选项只是对应配置文件中的配置项的快捷方式,以便在 CLI 中直接修改某些配置项。
97
+
98
+ ***** -a, --db-adapter=DB_ADAPTER
99
+
100
+ 指定数据库适配器,可用值:
101
+
102
+ - =mysql2=
103
+ - =postgresql=
104
+ - =sqlite3=
105
+ - =sqlserver=
106
+ - =oracle_enhanced=
107
+
108
+ ***** -h, --db-host=DB_HOST
109
+
110
+ 指定数据库主机
111
+
112
+ ***** -p, --db-port=DB_PORT
113
+
114
+ 指定数据库端口
115
+
116
+ ***** -d, --db-name=DB_NAME
117
+
118
+ 指定数据库名称
119
+
120
+ ***** -u, --db-user=DB_USER
121
+
122
+ 指定数据库用户名
123
+
124
+ ***** -P, --db-password=DB_PASSWORD
125
+
126
+ 指定数据库密码
127
+
128
+ ***** -n, --db-encoding=DB_ENCODING
129
+
130
+ 指定数据库字符编码,默认为 =utf8=
131
+
132
+ ***** -o, --db-pool=DB_POOL
133
+
134
+ 指定数据库连接池大小,默认为 =5=
135
+
136
+ ***** -H, --ssh-host=SSH_HOST
137
+
138
+ 指定 SSH 主机, 当指定了 SSH 相关的选项时, arql 会建立 SSH 隧道,使用 SSH 隧道连接数据库。
139
+
140
+ ***** -O, --ssh-port=SSH_PORT
141
+
142
+ 指定 SSH 端口
143
+
144
+ ***** -U, --ssh-user=SSH_USER
145
+
146
+ 指定 SSH 用户名
147
+
148
+ ***** -W, --ssh-password=SSH_PASSWORD
149
+
150
+ 指定 SSH 密码
151
+
152
+ ***** -L, --ssh-local-port=SSH_LOCAL_PORT
153
+
154
+ 指定 SSH 本地端口,默认为一个 /随机/ 端口
155
+
156
+ *** 配置文件
157
+
158
+ 配置文件的路径默认为 =$HOME/.arql.yml= 或 =$HOME/.arql.d/init.yml= 。 配置文件通常与 Rails数据库配置文件相同,但有一
159
+ 些额外的配置选项:
160
+
161
+ 1. =created_at= : 一个包含 ActiveRecord =created_at= 字段的自定义列名的数组,默认值为 =created_at= ,如果指定了此项,创建时将使用当前时间戳填充列的值
162
+ 2. =updated_at= : 一个包含 ActiveRecord =updated_at= 字段的自定义列名的数组,默认值为 =updated_at= ,如果指定了此项,更新时将使用当前时间戳填充列的值
163
+ 3. =ssh.host= : ssh 主机, 可以使用 =ssh_config= 文件中的主机名,也可以是直接的 IP 地址或主机名
164
+ 4. =ssh.port= : ssh 端口,默认值为 =22=
165
+ 5. =ssh.user= : ssh 用户名
166
+ 6. =ssh.password= : ssh 密码
167
+ 7. =ssh.local_port= : ssh 本地端口
168
+ 8. =singularized_table_names=: 是否使用单数表名,默认为 =false=, 如果为 =false=, 则 =students= 表将定义为 =Student= 模型,如果为 =true=, 则 =students= 表将定义为 =Students= 模型
169
+ 9. =table_name_prefixes=: 表名前缀数组,默认为空数组,如果指定了此项,在生成模型时将忽略这些前缀,例如,如果指定了 =["t_"]=, 则 =t_students= 表将定义为 =Student= 模型
170
+
171
+ **** 配置文件示例
172
+
173
+ #+begin_example
174
+ default: &default
175
+ adapter: mysql2
176
+ encoding: utf8
177
+ created_at: ["gmt_created"]
178
+ updated_at: ["gmt_modified"]
179
+ singularized_table_names: true
180
+
181
+ local:
182
+ <<: *default
183
+ username: root
184
+ database: blog
185
+ password:
186
+ table_name_prefixes: ["t_"]
187
+ socket: /tmp/mysql.sock
188
+
189
+ dev:
190
+ <<: *default
191
+ host: devdb.mycompany.com
192
+ port: 3306
193
+ username: root
194
+ password: 123456
195
+ database: blog
196
+ table_name_prefixes: ["t_"]
197
+ ssh:
198
+ host: dev.mycompany.com
199
+ port: 22
200
+ user: deploy
201
+ password: 12345678
202
+ local_port: 3307
203
+ #+end_example
204
+
205
+ 示例中定义了一个通用的配置项 =default= ,以及两个具体的数据库环境 =local= 和 =dev= 。 =local= 和 =dev= 同 =<<:
206
+ *default= 的方式继承了 =default= 的配置项。
207
+
208
+ 执行命令 =arql -e dev= 时,arql 会使用配置文件中的 =dev= 配置; 执行命令 =arql -e local= 时,arql 会使用配置文件中的
209
+ =local= 配置。
210
+
211
+ =dev= 环境使用了 SSH 隧道,连接到 =devdb.mycompany.com= 数据库时,会先建立一个 SSH 隧道到 =dev.mycompany.com= ,然
212
+ 后通过 SSH 隧道连接到数据库。
213
+
214
+ *** 作为 REPL 使用
215
+
216
+ 如果既没有指定 =[ruby file]= 也没有指定 =-E= 选项,并且 STDIN 是一个 =tty= ,arql 会启动一个 Pry REPL。例如执行:
217
+
218
+ #+BEGIN_EXAMPLE
219
+ arql -e dev
220
+ #+END_EXAMPLE
221
+
222
+
223
+ Arql 提供了一些 Pry 命令:
224
+
225
+ **** =info=
226
+
227
+ =info= 命令打印当前的数据库连接信息和 SSH 代理信息,例如:
228
+
229
+ #+begin_example
230
+ Database Connection Information:
231
+ Host:
232
+ Port:
233
+ Username: root
234
+ Password:
235
+ Database: test
236
+ Adapter: mysql2
237
+ Encoding: utf8
238
+ Pool Size: 5
239
+ #+end_example
240
+
241
+ **** =m= 或者 =l=
242
+
243
+ =m= (或者 =l= ) 命令打印所有表名及对应的模型类名和缩写类名,例如:
244
+
245
+ #+begin_example
246
+ +--------------------+------------------+------+---------+
247
+ | Table Name | Model Class | Abbr | Comment |
248
+ +--------------------+------------------+------+---------+
249
+ | post | Post | P | 帖子 |
250
+ | org | Org | O | 组织 |
251
+ | user_org | UserOrg | UO | |
252
+ | student | Student | S | 学生 |
253
+ | course | Course | C | |
254
+ | score | Score | S2 | |
255
+ | users | Users | U | |
256
+ | posts | Posts | P2 | |
257
+ | authors | Authors | A | |
258
+ +--------------------+------------------+------+---------+
259
+ #+end_example
260
+
261
+ 其中:
262
+
263
+ - =Table Name= : 表名
264
+ - =Model Class= : 模型类名
265
+ - =Abbr= : 缩写类名
266
+ - =Comment= : 注释
267
+
268
+ =m= / =l= 命令还可以接受一个参数,用于通过表名或表注释来对列表进行过滤, 例如:
269
+
270
+ =m perm= 只会列出表名或表注释中包含 =perm= 的表;如果要使用正则表达式匹配,可以使用 =m /perm/i= 来进行匹配。
271
+
272
+ **** =t=
273
+
274
+ =t= 命令接受一个表名或模型类名作为参数,打印表的定义信息,例如:
275
+
276
+ 执行 =t Person= 命令会打印 =person= 表的定义信息:
277
+
278
+ #+begin_example
279
+ Table: person
280
+ +----|------------|------------------|-----------|-------|-----------|-------|---------|----------|---------+
281
+ | PK | Name | SQL Type | Ruby Type | Limit | Precision | Scale | Default | Nullable | Comment |
282
+ +----|------------|------------------|-----------|-------|-----------|-------|---------|----------|---------+
283
+ | Y | id | int(11) unsigned | integer | 4 | | | | false | |
284
+ | | name | varchar(64) | string | 64 | | | | true | |
285
+ | | age | int(11) | integer | 4 | | | | true | |
286
+ | | gender | int(4) | integer | 4 | | | | true | |
287
+ | | grade | int(4) | integer | 4 | | | | true | |
288
+ | | blood_type | varchar(4) | string | 4 | | | | true | |
289
+ +----|------------|------------------|-----------|-------|-----------|-------|---------|----------|---------+
290
+ #+end_example
291
+
292
+ 另外, =t= 同时也是模型类的一个类方法,执行 =Person.t= 会同样会打印出上述信息。
293
+
294
+ 其中:
295
+
296
+ - =PK= : 是否为主键
297
+ - =Name= : 列名
298
+ - =SQL Type= : 数据库类型
299
+ - =Ruby Type= : Ruby 类型
300
+ - =Limit= : 长度限制
301
+ - =Precision= : 精度
302
+ - =Scale= : 小数位数
303
+ - =Default= : 默认值
304
+ - =Nullable= : 是否可为空
305
+ - =Comment= : 注释
306
+
307
+ **** =vd=
308
+
309
+ =t= 命令在终端中以表格的形式打印表的定义信息,缺点是如果表的列数过多,会导致表格这行,不方便查看。而 =vd=
310
+ (visidata) 是一个使用 Python 编写的终端数据分析工具,可以在终端中以表格的形式打印表的定义信息,但是支持水平滚动,方
311
+ 便查看。
312
+
313
+ 如果要使用 Arql 的 =vd= 命令,需要先安装 =visidata=:
314
+
315
+ #+begin_src sh
316
+ pipx install visidata
317
+ #+end_src
318
+
319
+ =vd= 命令和用法和 =t= 命令基本相同,另外, =Array= / =ActiveRecord::Base= 等对象也可以使用 =vd= 方法。
320
+
321
+ **** =show-sql= / =hide-sql=
322
+
323
+ 这对命令可以切换 Pry REPL 中 SQL 日志的显示。
324
+
325
+ 默认情况下,SQL 日志是不显示的:
326
+
327
+ #+begin_example
328
+ ARQL@demo247(main) [2] ❯ Student.count
329
+ => 0
330
+ #+end_example
331
+
332
+ 而打开 SQL 日志后,会显示每次执行的 SQL 语句:
333
+
334
+ #+begin_example
335
+ ARQL@demo247(main) [3] ❯ show-sql
336
+ ARQL@demo247(main) [4] ❯ Student.count
337
+ D, [2024-04-07T13:31:32.053903 #20440] DEBUG -- : Student Count (29.8ms) SELECT COUNT(*) FROM `student`
338
+ => 0
339
+ #+end_example
340
+
341
+ **** =reconnect=
342
+
343
+ =reconnect= 命令用于重新连接当前的数据库连接。当因网络原因导致连接断开时,可以使用该命令重新连接。重新连接,当前的
344
+ Pry 会话中的对象不会丢失。 =reconnect= 首先会判断当前连接是否还是有效的,如果是有效的,则不会重新连接;如果
345
+ =reconnect= 对连接的有效性判断错误,可以使用 =reconnect!= 命令强制重新连接。
346
+
347
+ **** =redefine=
348
+
349
+ =redefine= 命令用于重新定义 ActiveRecord 模型类,根据数据库表的信息重新生成模型类。对于在 =init.rb= 中添加了新的关
350
+ 系定义,想使新定义的关系在当前 Pry 会话中生效,可以使用 =redefine= 命令。
351
+
352
+ **** 沙盒模式
353
+
354
+ =sandbox-enter= 命令用于开启沙盒模式。在沙盒模式下,所有的数据库操作都会在事务中执行,该事务不会自动提交,需要手动提交或回滚。
355
+
356
+ 1. 开启沙盒模式:
357
+ #+begin_example
358
+ ARQL@demo247(main) [6] ❯ sandbox-enter
359
+ ARQL@demo247 [sandbox] (main) [7] ❯
360
+ #+end_example
361
+ 2. 退出沙盒模式:
362
+ #+begin_example
363
+ ARQL@demo247 [sandbox] (main) [7] ❯ sandbox-quit
364
+ begin_transaction callbacks removed.
365
+ You still have open 1 transactions open, don't forget commit or rollback them.
366
+ #+end_example
367
+ 3. 提交事务:
368
+ #+begin_example
369
+ ARQL@demo247(main) [7] ❯ $C.commit_transaction
370
+ #+end_example
371
+ 4. 回滚事务:
372
+ #+begin_example
373
+ ARQL@demo247(main) [7] ❯ $C.rollback_transaction
374
+ #+end_example
375
+
376
+
377
+
378
+ *** 作为代码解释器使用
379
+
380
+ 如果指定了一个 Ruby 文件作为命令行参数,或者使用了 =-E= 选项,或者 STDIN 不是一个 =tty= ,那么 Arql 不会启动 Pry,而是直
381
+ 接执行指定的文件或代码片段(或从标准输入读取代码)。在执行代码片段之前,会先加载模型类定义。你可以把这种用法看作类似
382
+ 是 =rails= 的 =runner= 子命令。
383
+
384
+ **** 使用 =-E= 选项
385
+
386
+ 通过 =-E= 选项可以直接执行代码片段,而不启动 Pry:
387
+
388
+ #+begin_example
389
+ $ arql -e dev -E 'puts Person.count'
390
+ #+end_example
391
+
392
+ **** 指定 Ruby 文件作为命令行参数
393
+
394
+ 通过指定 Ruby 文件作为命令行参数,可以直接执行 Ruby 文件中的代码:
395
+
396
+ =test.rb=:
397
+
398
+ #+BEGIN_SRC ruby
399
+ puts Person.count
400
+ #+END_SRC
401
+
402
+ #+begin_example
403
+ $ arql -e dev test.rb
404
+ #+end_example
405
+
406
+ **** 从标准输入读取代码
407
+
408
+ 从标准输入读取代码,可以直接执行代码片段:
409
+
410
+ #+begin_example
411
+ $ echo 'puts Person.count' | arql -e dev
412
+ #+end_example
413
+
414
+ *** 额外的扩展方法
415
+ **** =to_insert_sql= / =to_upsert_sql=
416
+
417
+ 可以在任何 ActiveRecord 模型实例上调用 =to_insert_sql= / =to_upsert_sql= 方法,获取该对象的插入或更新 SQL 语句。
418
+ 这两个方法也可以在包含 ActiveRecord 模型实例对象的数组对象上调用。
419
+
420
+ #+begin_example
421
+ ARQL ❯ Person.all.to_a.to_insert_sql
422
+ => "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`;"
423
+ #+end_example
424
+
425
+ **** =to_create_sql=
426
+
427
+ 可以在任何 ActiveRecord 模型类上调用 =to_create_sql= 方法,获取该模型类对应的表的创建 SQL 语句。
428
+
429
+ #+begin_example
430
+ ARQL@demo247(main) [16] ❯ puts Post.to_create_sql
431
+ D, [2024-04-07T14:15:11.106693 #20440] DEBUG -- : SQL (24.9ms) show create table post
432
+ CREATE TABLE `post` (
433
+ `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',
434
+ `name` varchar(256) DEFAULT NULL,
435
+ `gender` varchar(256) DEFAULT NULL,
436
+ `phone` varchar(256) DEFAULT NULL,
437
+ `id_no` varchar(256) DEFAULT NULL,
438
+ `note` varchar(256) DEFAULT NULL,
439
+ `gmt_created` datetime NOT NULL COMMENT '创建时间',
440
+ `gmt_modified` datetime NOT NULL COMMENT '最后修改时间',
441
+ PRIMARY KEY (`id`),
442
+ KEY `index_post_on_name` (`name`)
443
+ ) ENGINE=InnoDB AUTO_INCREMENT=83 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci
444
+ #+end_example
445
+
446
+ **** =t=
447
+
448
+ =t= 除了可以作为类方法在 ActiveRecord 模型类上调用,也可以作为实例方法在 ActiveRecord 模型实例对象上调用。
449
+
450
+ #+begin_example
451
+ ARQL ❯ Person.last.t
452
+ +----------------|-----------------|------------------|---------+
453
+ | Attribute Name | Attribute Value | SQL Type | Comment |
454
+ +----------------|-----------------|------------------|---------+
455
+ | id | 11 | int(11) unsigned | |
456
+ | name | Jackson | varchar(64) | |
457
+ | age | 30 | int(11) | |
458
+ | gender | 2 | int(4) | |
459
+ | grade | 2 | int(4) | |
460
+ | blood_type | AB | varchar(4) | |
461
+ +----------------|-----------------|------------------|---------+
462
+ #+end_example
463
+
464
+ =t= 方法可以接受以下两个选项:
465
+
466
+ + =:except= 选项,用于指定不显示的属性名,值可以是字符串或正则表达式,例如:
467
+ #+BEGIN_EXAMPLE
468
+ Person.last.t(except: 'id')
469
+ Student.where(condition).t(except: /id|name/)
470
+ #+END_EXAMPLE
471
+ + =:compact= 选项,用于指定是否紧凑显示,值可以是 =true= 或 =false= ,如果启用紧凑显示,那些值全部为 =NULL= 的列将不
472
+ 会显示,这对于查看那些数据稀疏的表很有帮助,例如:
473
+ #+BEGIN_EXAMPLE
474
+ Person.last.t(compact: true)
475
+ Student.where(condition).t(compact: false)
476
+ #+END_EXAMPLE
477
+
478
+ **** =v=
479
+
480
+ =v= 方法用于与 Emacs org babel 集成。
481
+
482
+ ***** =v= 作为模型类的实例方法
483
+
484
+ 在任何 ActiveRecord 模型实例对象上调用 =v= 方法,可以打印一个数组,数组的第一个元素是 =['Attribute Name',
485
+ 'Attribute Value', 'SQL Type', 'Comment']= ,第二个元素是 =nil= ,剩下的元素是对象的属性名和值。在Emacs org-mode
486
+ 中,如果 =:result= 类型是 =value= (默认值),这个返回值会被渲染成一个漂亮的表格。
487
+
488
+ #+begin_example
489
+ ARQL ❯ Person.last.v
490
+ => [["Attribute Name", "Attribute Value", "SQL Type", "Comment"],
491
+ nil,
492
+ ["id", 11, "int(11) unsigned", ""],
493
+ ["name", "Jackson", "varchar(64)", ""],
494
+ ["age", 30, "int(11)", ""],
495
+ ["gender", 2, "int(4)", ""],
496
+ ["grade", 2, "int(4)", ""],
497
+ ["blood_type", "AB", "varchar(4)", ""]]
498
+ #+end_example
499
+
500
+ ***** 只包含模型实例的数组
501
+ #+begin_example
502
+ ARQL ❯ Person.all.to_a.v
503
+ => [["id", "name", "age", "gender", "grade", "blood_type"],
504
+ nil,
505
+ [1, "Jack", 30, nil, nil, nil],
506
+ [2, "Jack", 11, 1, nil, nil],
507
+ [3, "Jack", 12, 1, nil, nil],
508
+ [4, "Jack", 30, 1, nil, nil],
509
+ [5, "Jack", 12, 2, nil, nil],
510
+ [6, "Jack", 2, 2, 2, nil],
511
+ [7, "Jack", 3, 2, 2, nil],
512
+ [8, "Jack", 30, 2, 2, "AB"],
513
+ [9, "Jack", 30, 2, 2, "AB"],
514
+ [10, "Jack", 30, 2, 2, "AB"],
515
+ [11, "Jackson", 30, 2, 2, "AB"]]
516
+ #+end_example
517
+
518
+ ***** 只包含同构 Hash 对象的数组
519
+
520
+ #+begin_example
521
+ ARQL ❯ arr = [{name: 'Jack', age: 10}, {name: 'Lucy', age: 20}]
522
+ => [{:name=>"Jack", :age=>10}, {:name=>"Lucy", :age=>20}]
523
+ ARQL ❯ arr.v
524
+ => [[:name, :age], nil, ["Jack", 10], ["Lucy", 20]]
525
+ #+end_example
526
+
527
+ **** =q=
528
+
529
+ #+begin_example
530
+ ARQL ❯ rs = q 'select count(0) from person;'
531
+ => #<ActiveRecord::Result:0x00007fd1f8026ad0 @column_types={}, @columns=["count(0)"], @hash_rows=nil, @rows=[[11]]>
532
+ ARQL ❯ rs.rows
533
+ => [[11]]
534
+ #+end_example
535
+
536
+ **** JSON 转换和格式化
537
+
538
+ 在任何对象上调用 =j= 方法,可以得到 JSON 格式的字符串,调用 =jj= 方法可以得到格式化后的 JSON 字符串。
539
+
540
+ 使用 =jp= 方法打印 JSON,使用 =jjp= 方法打印格式化后的 JSON。
541
+
542
+ **** $C 全局变量
543
+
544
+ Arql 将 =ActiveRecord::Base.connection= 对象赋值给全局可用的 =$C= 全局变量,它代表当前的数据库连接。
545
+
546
+ 上文中的 =q= 方法实际上是 =$C.exec_query= 方法, =$C= 对象的其他方法也很有用:
547
+
548
+ ***** 创建表
549
+ #+begin_example
550
+ ARQL ❯ $C.create_table :post, id: false, primary_key: :id do |t|
551
+ ARQL ❯ t.column :id, :bigint, precison: 19, comment: 'ID'
552
+ ARQL ❯ t.column :name, :string, comment: '名称'
553
+ ARQL ❯ t.column :gmt_created, :datetime, comment: '创建时间'
554
+ ARQL ❯ t.column :gmt_modified, :datetime, comment: '最后修改时间'
555
+ ARQL ❯ end
556
+ #+end_example
557
+
558
+ =create_table= 同样也被加入到 =Kernel= 下面,所以也可以直接调用 =create_table= 方法:
559
+
560
+ #+begin_example
561
+ ARQL ❯ create_table :post, id: false, primary_key: :id do |t|
562
+ ARQL ❯ t.column :id, :bigint, precison: 19, comment: 'ID'
563
+ ARQL ❯ t.column :name, :string, comment: '名称'
564
+ ARQL ❯ t.column :gmt_created, :datetime, comment: '创建时间'
565
+ ARQL ❯ t.column :gmt_modified, :datetime, comment: '最后修改时间'
566
+ ARQL ❯ end
567
+ #+end_example
568
+
569
+ ***** 添加字段
570
+
571
+ #+begin_example
572
+ $C.add_column :post, :note, :string, comment: '备注'
573
+ #+end_example
574
+
575
+ =add_column= 也被加入到模型类的类方法中,所以也可以直接在模型类上调用 =add_column= 方法:
576
+
577
+ #+begin_example
578
+ Post.add_column :note, :string, comment: '备注'
579
+ #+end_example
580
+
581
+ ***** 修改字段
582
+
583
+ #+begin_example
584
+ $C.change_column :post, :note, :text, comment: '备注'
585
+ #+end_example
586
+
587
+ =change_column= 也被加入到模型类的类方法中,所以也可以直接在模型类上调用 =change_column= 方法:
588
+
589
+ #+begin_example
590
+ Post.change_column :note, :text, comment: '备注'
591
+ #+end_example
592
+
593
+ ***** 删除字段
594
+
595
+ #+begin_example
596
+ $C.remove_column :post, :note
597
+ #+end_example
598
+
599
+ =remove_column= 也被加入到模型类的类方法中,所以也可以直接在模型类上调用 =remove_column= 方法:
600
+
601
+ #+begin_example
602
+ Post.remove_column :note
603
+ #+end_example
604
+
605
+ ***** 删除表
606
+
607
+ #+begin_example
608
+ $C.drop_table :post
609
+ #+end_example
610
+
611
+ =drop_table= 也被加入到模型类的类方法中,所以也可以直接在模型类上调用 =drop_table= 方法:
612
+
613
+ #+begin_example
614
+ Post.drop_table
615
+ #+end_example
616
+
617
+ ***** 添加索引
618
+
619
+ #+begin_example
620
+ ARQL ❯ $C.add_index :post, :name
621
+ ARQL ❯ $C.add_index(:accounts, [:branch_id, :party_id], unique: true, name: 'by_branch_party')
622
+ #+end_example
623
+
624
+ =add_index= 也被加入到模型类的类方法中,所以也可以直接在模型类上调用 =add_index= 方法:
625
+
626
+ #+begin_example
627
+ Post.add_index :name
628
+ Post.add_index [:branch_id, :party_id], unique: true, name: 'by_branch_party'
629
+ #+end_example
630
+
631
+ **** =Kernel= 扩展方法
632
+
633
+ =Kernel= 模块下的函数可以想语言内置函数一样直接调用,不需要指定模块名。 以下是 Arql 扩展的 =Kernel= 方法:
634
+
635
+ Pry 内建了 =show-source= (别名 =$= ) 和 =show-doc= (别名 =?= )命令,可以查看方法的源码和文档。可以通过 =show-doc= 查看方法的文档。例如:
636
+
637
+ #+BEGIN_EXAMPLE
638
+ ARQL ❯ ? create_table
639
+ #+END_EXAMPLE
640
+
641
+
642
+ ***** 创建表 =create_table=
643
+ #+BEGIN_EXAMPLE
644
+ create_table :post, id: false, primary_key: :id do |t|
645
+ t.column :id, :bigint, precison: 19, comment: 'ID'
646
+ t.column :name, :string, comment: '名称'
647
+ t.column :gmt_created, :datetime, comment: '创建时间'
648
+ t.column :gmt_modified, :datetime, comment: '最后修改时间'
649
+ end
650
+ #+END_EXAMPLE
651
+ ***** 创建多对多关系的中间表 =create_join_table=
652
+ #+BEGIN_EXAMPLE
653
+ create_join_table :products, :categories do |t|
654
+ t.index :product_id
655
+ t.index :category_id
656
+ end
657
+ #+END_EXAMPLE
658
+ ***** 删除表 =drop_table=
659
+ #+BEGIN_EXAMPLE
660
+ drop_table :post
661
+ #+END_EXAMPLE
662
+ ***** 删除多对多关系的中间表 =drop_join_table=
663
+ #+BEGIN_EXAMPLE
664
+ drop_join_table :products, :categories
665
+ #+END_EXAMPLE
666
+ ***** 修改表名 =rename_table=
667
+ #+BEGIN_EXAMPLE
668
+ rename_table :post, :posts
669
+ #+END_EXAMPLE
670
+
671
+ ***** =print_tables=
672
+
673
+ Arql 提供了一个 =print_tables= 方法,可以将当前数据库中的所有表的信息,导出为:
674
+
675
+ + markdown 表格格式: ~print_tables(:md)~
676
+ + org-mode 表格格式: ~print_tables(:org)~
677
+ + create table SQL: ~print_tables(:sql)~
678
+
679
+ **** 模型类类方法扩展
680
+
681
+ Pry 内建了 =show-source= (别名 =$= ) 和 =show-doc= (别名 =?= )命令,可以查看方法的源码和文档。可以通过 =show-doc= 查看方法的文档。例如:
682
+
683
+ #+BEGIN_EXAMPLE
684
+ ARQL ❯ ? Student.add_column
685
+ #+END_EXAMPLE
686
+
687
+ ***** 添加字段 =add_column=
688
+ #+BEGIN_EXAMPLE
689
+ Student.add_column :note, :text, comment: '备注'
690
+ #+END_EXAMPLE
691
+
692
+ ***** 修改字段 =change_column=
693
+ #+BEGIN_EXAMPLE
694
+ Student.change_column :note, :string, comment: '备注'
695
+ #+END_EXAMPLE
696
+
697
+ ***** 删除字段 =remove_column=
698
+ #+BEGIN_EXAMPLE
699
+ Student.remove_column :note
700
+ #+END_EXAMPLE
701
+
702
+ ***** 添加索引 =add_index=
703
+ #+BEGIN_EXAMPLE
704
+ Student.add_index :name
705
+ Student.add_index [:branch_id, :party_id], unique: true, name: 'by_branch_party'
706
+ #+END_EXAMPLE
707
+
708
+ ***** 修改字段注释 =change_column_comment=
709
+ #+BEGIN_EXAMPLE
710
+ Student.change_column_comment :note, '备注'
711
+ #+END_EXAMPLE
712
+
713
+ ***** 修改字段默认值 =change_column_default=
714
+ #+BEGIN_EXAMPLE
715
+ Student.change_column_default :note, '默认值'
716
+ #+END_EXAMPLE
717
+
718
+ ***** 修改字段名称 =rename_column=
719
+ #+BEGIN_EXAMPLE
720
+ Student.rename_column :note, :remark
721
+ #+END_EXAMPLE
722
+
723
+ ***** 修改表名 =rename_table=
724
+ #+BEGIN_EXAMPLE
725
+ Student.rename_table :seitou
726
+ #+END_EXAMPLE
727
+
728
+ ***** 修改表注释 =change_table_comment=
729
+ #+BEGIN_EXAMPLE
730
+ Student.change_table_comment from: '', to: '学生表'
731
+ #+END_EXAMPLE
732
+
733
+ ***** 删除表 =drop_table=
734
+ #+BEGIN_EXAMPLE
735
+ Student.drop_table
736
+ #+END_EXAMPLE
737
+
738
+ ***** 删除索引 =remove_index=
739
+ #+BEGIN_EXAMPLE
740
+ Student.remove_index :age
741
+ Student.remove_index name: 'by_branch_party'
742
+ #+END_EXAMPLE
743
+
744
+ ***** 查询表注释 =table_comment=
745
+ #+BEGIN_EXAMPLE
746
+ Student.table_comment
747
+ #+END_EXAMPLE
748
+
749
+ ***** 列出表的索引 =indexes=
750
+ #+BEGIN_EXAMPLE
751
+ Student.indexes
752
+ #+END_EXAMPLE
753
+
754
+ **** 读写 Excel 和 CSV 文件
755
+
756
+ Arql 集成了 =roo= 和 =caxlsx= 两个 Excel 库,提供了用于解析和生成 Excel 文件的方法。同时,Arql 也提供了用于读写 CSV 文件的方法。
757
+
758
+ ***** 解析 Excel
759
+
760
+ Arql 为 =Kernel= 模块添加了 =parse_excel= 方法,可以用来解析 Excel 文件。例如:
761
+
762
+ #+BEGIN_EXAMPLE
763
+ ARQL ❯ parse_excel 'path/to/excel.xlsx'
764
+ #+END_EXAMPLE
765
+
766
+ 文件路径中可以使用 =~/= 表示用户的主目录,Arql 会自动展开。
767
+
768
+
769
+ 也可以在一个表示文件路径的 =String= 对象上调用 =parse_excel= 方法:
770
+
771
+ #+BEGIN_EXAMPLE
772
+ ARQL ❯ 'path/to/excel.xlsx'.parse_excel
773
+ #+END_EXAMPLE
774
+
775
+ =parse_excel= 方法会返回一个 =Hash= 对象,Key 为 Sheet 名称,Value 为 Sheet 的数据,Value 是一个二维数组。例如:
776
+
777
+ #+BEGIN_EXAMPLE
778
+ {
779
+ 'Sheet1' => [
780
+ ['A1', 'B1', 'C1'],
781
+ ['A2', 'B2', 'C2'],
782
+ ['A3', 'B3', 'C3']
783
+ ],
784
+ 'Sheet2' => [
785
+ ['A1', 'B1', 'C1'],
786
+ ['A2', 'B2', 'C2'],
787
+ ['A3', 'B3', 'C3']
788
+ ]
789
+ }
790
+ #+END_EXAMPLE
791
+
792
+ ***** 生成 Excel
793
+
794
+ Arql 为 =Hash= / =Array= / =ActiveRecord::Relation= / =ActiveRecord::Base= 对象添加了 =write_excel= 方法,可以用来
795
+ 生成 Excel 文件:
796
+
797
+ ****** 从 =Hash= 对象生成 Excel
798
+
799
+ #+BEGIN_EXAMPLE
800
+ ARQL ❯ obj.write_excel 'path/to/excel.xlsx'
801
+ #+END_EXAMPLE
802
+
803
+ =Hash#write_excel= 要求 Hash 对象 Key 是 Sheet 名称,Value 是 Sheet 的数据,Value 的类型可以是:
804
+
805
+ + 一个数组,数组的元素可以是:
806
+ + 一个数组,表示一行数据
807
+ + 一个 Hash 对象,表示一行数据,Key 是列名,Value 是列值
808
+ + 一个 ActiveRecord::Base 对象,表示一行数据
809
+ + 一个 Hash 对象,一共包含两个键值对:
810
+ + =:fields=, 一个数组,表示列名
811
+ + =:data=, 一个二维数组,表示数据
812
+
813
+ ****** 从 =Array= 对象生成 Excel
814
+
815
+ #+BEGIN_EXAMPLE
816
+ ARQL ❯ obj.write_excel 'path/to/excel.xlsx', :name, :age, :gender, sheet_name: '订单数据'
817
+ #+END_EXAMPLE
818
+
819
+ 其中:
820
+
821
+ + =:name, :age, :gender= 这几个参数是列名,如果不指定,会根据数组的第一个元素来确定列名:
822
+ - 如果元素是 =ActiveRecord::Base= 对象,会使用对象的全部属性名(即数据库字段列表)作为列名
823
+ - 如果元素是 =Hash= 对象,会使用 =Hash= 的 全部 Key 作为列名
824
+ + =sheet_name= 指定 Sheet 名称,如果不指定,会使用默认的 Sheet 名称 =Sheet1=
825
+
826
+ =Array= 对象的每一个元素表示一行数据, =Array#write_excel= 要求 Array 对象每个元素:
827
+
828
+ + 一个 =ActiveRecord::Base= 对象
829
+ + 一个 =Hash= 对象,表示一行数据,Key 是列名,Value 是列值
830
+ + 一个数组,表示一行数据
831
+
832
+ ****** 从 =ActiveRecord::Base= 对象生成 Excel
833
+
834
+ #+BEGIN_EXAMPLE
835
+ ARQL ❯ Student.find(123).write_excel 'path/to/excel.xlsx', sheet_name: '学生数据'
836
+ #+END_EXAMPLE
837
+
838
+ =ActiveRecord::Base= 的 =write_excel= 对象实际上就是把这个 =ActiveRecord::Base= 对象包装成一个只有一个元素的 =Array= 对
839
+ 象,然后调用 =Array= 的 =write_excel= 方法。
840
+
841
+ ****** 从 =ActiveRecord::Relation= 对象生成 Excel
842
+
843
+ #+BEGIN_EXAMPLE
844
+ ARQL ❯ Student.where(gender: 'M').write_excel 'path/to/excel.xlsx', sheet_name: '男学生'
845
+ #+END_EXAMPLE
846
+
847
+ =ActiveRecord::Relation= 的 =write_excel= 对象实际上就是把这个 =ActiveRecord::Relation= 对象转换成一个 =Array= 对象,然
848
+ 后调用 =Array= 的 =write_excel= 方法。
849
+
850
+ ***** 解析 CSV
851
+
852
+ Arql 提供了 =parse_csv= 方法,可以用来解析 CSV 文件:
853
+
854
+ #+BEGIN_EXAMPLE
855
+ ARQL ❯ parse_csv 'path/to/csv.csv'
856
+ #+END_EXAMPLE
857
+
858
+ =parse_csv= 方法返回一个标准库中的 CSV 对象。
859
+
860
+ =parse_csv= 可以有以下选项参数:
861
+
862
+ - =encoding=, 指定 CSV 文件的编码,默认是 =UTF-16= (with BOM)
863
+ - =headers=, 指定是否包含表头,默认是 =false=
864
+ - =col_sep=, 指定列分隔符,默认是 =\t=
865
+ - =row_sep=, 指定行分隔符,默认是 =\r\n=
866
+
867
+ (以上默认值实际就是 Microsoft Office Excel 保存 CSV 文件时默认使用的配置)
868
+
869
+ 也可以在一个表示文件路径的 =String= 对象上调用 =parse_csv= 方法:
870
+
871
+ #+BEGIN_EXAMPLE
872
+ ARQL ❯ 'path/to/csv.csv'.parse_csv
873
+ #+END_EXAMPLE
874
+
875
+ ***** 生成 CSV
876
+ Arql 为 =Array= / =ActiveRecord::Relation= / =ActiveRecord::Base= 对象添加了 =write_csv= 方法,可以用来生成 CSV 文件:
877
+
878
+ ****** 从 =Array= 对象生成 CSV
879
+
880
+ #+BEGIN_EXAMPLE
881
+ ARQL ❯ obj.write_csv 'path/to/csv.csv', :name, :age, :gender, sheet_name: '订单数据'
882
+ #+END_EXAMPLE
883
+
884
+ 用法和 =Array= 对象的 =write_excel= 方法类似。
885
+
886
+
887
+ ****** 从 =ActiveRecord::Base= 对象生成 CSV
888
+
889
+ #+BEGIN_EXAMPLE
890
+ ARQL ❯ Student.find(123).write_csv 'path/to/csv.csv', sheet_name: '学生数据'
891
+ #+END_EXAMPLE
892
+
893
+ 用法和 =ActiveRecord::Base= 对象的 =write_excel= 方法类似。
894
+
895
+ ****** 从 =ActiveRecord::Relation= 对象生成 CSV
896
+
897
+ #+BEGIN_EXAMPLE
898
+ ARQL ❯ Student.where(gender: 'M').write_csv 'path/to/csv.csv', sheet_name: '男学生'
899
+ #+END_EXAMPLE
900
+
901
+ 用法和 =ActiveRecord::Relation= 对象的 =write_excel= 方法类似。
902
+
903
+ **** dump 数据
904
+
905
+ 注意: 仅支持 MySQL 数据库
906
+
907
+ Arql 为 =Array= / =ActiveRecord::Base= / =ActiveRecord::Relation= 等对象添加了 =dump= 方法,可以用来导出数据到 SQL 文件:
908
+
909
+
910
+ ***** 从 Array 对象导出数据
911
+
912
+ #+BEGIN_EXAMPLE
913
+ ARQL ❯ obj.dump 'path/to/dump.sql', batch_size: 5000
914
+ #+END_EXAMPLE
915
+
916
+ =Array= 对象的每一个元素必须是一个 =ActiveRecord::Base= 对象
917
+
918
+ =batch_size= 参数指定每个批次查询出的数据,默认值为 500
919
+
920
+ ***** 从 ActiveRecord::Base 对象导出数据
921
+
922
+ #+BEGIN_EXAMPLE
923
+ ARQL ❯ Student.find(123).dump 'path/to/dump.sql', batch_size: 5000
924
+ #+END_EXAMPLE
925
+
926
+ =ActiveRecord::Base= 对象的 =dump= 方法实际上就是把这个 =ActiveRecord::Base= 对象包装成一个只有一个元素的 =Array= 对象,然后调用 =Array= 的 =dump= 方法。
927
+
928
+ ***** 从 ActiveRecord::Relation 对象导出数据
929
+
930
+ #+BEGIN_EXAMPLE
931
+ ARQL ❯ Student.where(gender: 'M').dump 'path/to/dump.sql', batch_size: 5000
932
+ #+END_EXAMPLE
933
+
934
+ =ActiveRecord::Relation= 的 =dump= 对象实际上就是把这个 =ActiveRecord::Relation= 对象转换成一个 =Array= 对象,然后调用 =Array= 的 =dump= 方法。
935
+
936
+
937
+ ***** 调用 ActiveRecord::Base 的 dump 类方法
938
+
939
+ #+BEGIN_EXAMPLE
940
+ ARQL ❯ Student.dump 'path/to/dump.sql', no_create_table: false
941
+ #+END_EXAMPLE
942
+
943
+ 这个方法会通过 =mysqldump= 命令 把 =Student= 表中的所有数据导出到 SQL 文件中。
944
+
945
+ =no_create_table= 参数指定是否在 SQL 文件中包含创建表的语句,默认值为 =false= 。
946
+
947
+
948
+
949
+ ***** 在全局连接对象 =$C= 上调用 dump 方法
950
+
951
+ #+BEGIN_EXAMPLE
952
+ ARQL ❯ $C.dump 'path/to/dump.sql', no_create_db: false
953
+ #+END_EXAMPLE
954
+
955
+ 这个方法会通过 mysqldump 命令把当前数据库中的所有表的数据导出到 SQL 文件中。
956
+
957
+ =no_create_db= 参数指定是否在 SQL 文件中包含创建数据库的语句,默认值为 =false= 。
958
+
959
+
960
+ **** Plot
961
+
962
+ Arql 集成了 Ruby 的 =youplot= 库,为 =Array= 添加了一些可以用来绘制图表的方法:
963
+
964
+ + =barplot=
965
+ + =countplot=
966
+ + =histo=
967
+ + =lineplot=
968
+ + =lineplots=
969
+ + =scatter=
970
+ + =density=
971
+ + =boxplot=
972
+
973
+ 示例:
974
+
975
+ 数量统计图:
976
+
977
+ #+BEGIN_EXAMPLE
978
+ ARQL@demo247(main) [44] ❯ Student.pluck(:gender)
979
+ => ["M", "M", "M", "M", "M", "M", "M", "F", "M", "F", "M", "M", "M", "M", "M"]
980
+ ARQL@demo247(main) [45] ❯ Student.pluck(:gender).countplot
981
+ ┌ ┐
982
+ M ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 13.0
983
+ F ┤■■■■■ 2.0
984
+ └ ┘
985
+ #+END_EXAMPLE
986
+
987
+ 分布图:
988
+
989
+ #+BEGIN_EXAMPLE
990
+ ARQL@jicai.dev(main) [18] ❯ Order.last(20).pluck(:order_sum)
991
+ => [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]
992
+ ARQL@jicai.dev(main) [19] ❯ Order.last(20).pluck(:order_sum).histo
993
+ ┌ ┐
994
+ [ 0.0, 50000.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 17
995
+ [ 50000.0, 100000.0) ┤▇▇▇▇ 2
996
+ [100000.0, 150000.0) ┤ 0
997
+ [150000.0, 200000.0) ┤ 0
998
+ [200000.0, 250000.0) ┤▇▇ 1
999
+ └ ┘
1000
+ Frequency
1001
+
1002
+ #+END_EXAMPLE
1003
+
1004
+ **** =String=
1005
+
1006
+ ***** =Srting#p=
1007
+
1008
+ =p= 方法的定义如下:
1009
+
1010
+ #+begin_example
1011
+ class String
1012
+ def p
1013
+ puts self
1014
+ end
1015
+ end
1016
+ #+end_example
1017
+
1018
+ =​"hello".p= 等价于 =puts "hello"​= 。
1019
+
1020
+ ***** =String#parse=
1021
+
1022
+ 对于一个表示文件路径的字符串,可以调用 =parse= 方法通过文件路径中的后缀名来分别对 Excel、CSV、JSON 文件进行解析。
1023
+
1024
+ #+BEGIN_EXAMPLE
1025
+ excel = 'path/to/excel.xlsx'.parse
1026
+ csv = 'path/to/csv.csv'.parse
1027
+ json = 'path/to/json.json'.parse
1028
+ #+END_EXAMPLE
1029
+
1030
+ **** =ID=
1031
+
1032
+ Arql 提供了一个 =ID= 类,用来生成雪花算法 ID 和 UUID。
1033
+
1034
+ #+BEGIN_EXAMPLE
1035
+ id = ID.long # 生成一个雪花算法 ID
1036
+ id = ID.uuid # 生成一个 UUID
1037
+ #+END_EXAMPLE
1038
+
1039
+ **** Ransack
1040
+
1041
+ Arql 集成了 =Ransack=:
1042
+
1043
+ #+BEGIN_EXAMPLE
1044
+ Student.ransack(name_cont: 'Tom').result # 模糊查询名字中包含 'Tom' 的学生
1045
+ Student.ransack(name_start: 'Tom').result # 模糊查询名字以 'Tom' 开头的学生
1046
+ #+END_EXAMPLE
1047
+
1048
+ *** Emacs Org Babel 集成
1049
+
1050
+ 这里有一个 [[https://github.com/lululau/spacemacs-layers/blob/master/ob-arql/local/ob-arql/ob-arql.el][ob-arql]] 用于集成 Emacs org babel。
1051
+
1052
+ ** Guides and Tips
1053
+ *** [[./define-associations-zh_CN.org][在 Initializer 文件中定义关联关系]]
1054
+ *** [[./initializer-structure-zh_CN.org][将不同环境的初始化代码放在不同的文件中]]
1055
+ *** [[./helper-for-datetime-range-query-zh_CN.org][定义快速按时间查询的便利方法]]
1056
+ *** [[./auto-set-id-before-save-zh_CN.org][新建对象在保存之前自动设置 ID]]
1057
+ *** [[./custom-configurations-zh_CN.org][配置文件中的自定义配置项]]
1058
+ *** [[./sql-log-zh_CN.org][自动记录 SQL 日志和 REPL 输入历史]]
1059
+ *** [[./fuzzy-field-query-zh_CN.org][字段名 Fuzzy 化查询]]
1060
+ *** [[./oss-files-zh_CN.org][OSS 数据下载和查看]]
1061
+ *** 使用 Arql 查看 SQLite3 数据库文件
1062
+
1063
+ 可以使用 Arql 查看 SQLite3 数据库文件,例如:
1064
+
1065
+ #+BEGIN_EXAMPLE
1066
+ arql -d db/development.sqlite3
1067
+ #+END_EXAMPLE
1068
+
1069
+ *** 根据名称或注释查找字段
1070
+
1071
+ 我们在熟悉一个项目的时候,经常会遇到这样的情况:我们知道某个字段的名称或注释,但是不知道它对应的表名和字段名。这时候我们可以使用如下方法来查找:
1072
+
1073
+ #+BEGIN_SRC ruby
1074
+ 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})"} }
1075
+
1076
+ # 输出:
1077
+ # Table: order, Column: entry_amount (订单金额)
1078
+ # Table: sub_order, Column: entry_price (金额)
1079
+ #+END_SRC
1080
+
1081
+ *** [[./ruby-guides-for-java-developer-zh_CN.org][给 Java 开发者的 Ruby 入门简明教程]]
1082
+ *** [[./simple-pry-guides-zh_CN.org][简明 Pry 使用指南]]
1083
+ *** [[./simple-active-record-guide-zh_CN.org][简明 ActiveRecord 使用指南]]
1084
+ ** 开发
1085
+
1086
+ 检出代码后,运行 =bin/setup= 安装依赖。你也可以运行 =bin/console= 进入交互式控制台。
1087
+
1088
+ 运行 =bundle exec rake install= 将这个 gem 安装到本地。发布新版本时,更新 =version.rb= 中的版本号,然后运行 =bundle
1089
+ exec rake release= ,这将为该版本创建一个 git 标签,推送 git 提交和标签,并将 =.gem= 文件推送到 [[https://rubygems.org][rubygems.org]]。
1090
+
1091
+ ** 贡献代码
1092
+
1093
+ 欢迎在 GitHub 上提交 bug 报告和 pull request: https://github.com/lululau/arql 。这个项目旨在成为一个安全、友好的协作
1094
+ 空间,期望贡献者遵守 [[https://github.com/lululau/arql/blob/master/CODE_OF_CONDUCT.md][行为准则]]。
1095
+
1096
+ ** 许可证
1097
+
1098
+ 这个 gem 是根据 [[https://opensource.org/licenses/MIT][MIT License]] 条款开源的。
1099
+
1100
+ ** Code of Conduct
1101
+
1102
+ 与 Arql 项目的代码库、问题跟踪器、聊天室和邮件列表中的每个人都应遵守 [[https://github.com/lululau/arql/blob/master/CODE_OF_CONDUCT.md][行为准则]]。