arql 0.3.29 → 0.3.31

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,83 @@
1
+ * ActiveRecord 简明教程
2
+
3
+ ActiveRecord 是 Rails 中的 ORM 框架,它将数据库表映射到 Ruby 对象,让我们可以通过 Ruby 对象来操作数据库。
4
+
5
+
6
+ ** 定义模型类
7
+
8
+ 模型类是继承自 ActiveRecord::Base 的类,它们是数据库中的表在 Ruby 语言中的映射。
9
+
10
+ #+BEGIN_SRC ruby
11
+ class User < ActiveRecord::Base
12
+ end
13
+ #+END_SRC
14
+
15
+ 而 User 对象的属性和数据库表的字段的对应关系,是 ActiveRecord 根据数据库中的表信息,自动完成的。
16
+
17
+ 在 Arql 中,模型类的定义是 Arql 根据数据库表的表信息自动生成的,所以我们不需要像这里一样手动定义模型类。
18
+
19
+ ** 定义关联关系
20
+
21
+ 在 ActiveRecord 中,我们可以通过 has_many, belongs_to, has_one 等方法来定义模型类之间的关联关系。
22
+
23
+ - =has_many= 表明此表是一对多关系的“一”方
24
+ - =has_one= 表明此表是一对一关系的属主
25
+ - =belongs_to= 表明此表是一对多或一对一关系的从属方
26
+ - =has_and_belongs_to_many= 表明此表是多对多关系的其中一方
27
+
28
+
29
+ 使用 Arql 查询数据库时,我们希望也可以通过定义好的关联关系来查询数据。
30
+
31
+ 例如,我们有两个模型类 User 和 Post,User 有多个 Post,Post 属于一个 User, 如果我们希望查询张三的所有文章,
32
+
33
+ 在不使用关联关系的情况下,我们需要这样查询:
34
+
35
+ #+BEGIN_SRC ruby
36
+ user = User.find_by(name: '张三')
37
+ posts = Post.where(user_id: user.id)
38
+ #+END_SRC
39
+
40
+ 而如果我们定义了关联关系,我们可以这样查询:
41
+
42
+ #+BEGIN_SRC ruby
43
+ user = User.find_by(name: '张三')
44
+ posts = user.posts
45
+ #+END_SRC
46
+
47
+ 关联关系是在模型类中定义的,而 Arql 中模型类是 Arql 替我们自动生成的,那么我们要在哪里定义关联关系呢?
48
+
49
+ 别忘了 Ruby 的类是可以重新打开的,我们可以在 Arql 中重新打开模型类,定义关联关系:
50
+
51
+ #+BEGIN_SRC ruby
52
+ class User < ActiveRecord::Base
53
+ has_many :posts
54
+ end
55
+ #+END_SRC
56
+
57
+ 像 has_many, belongs_to, has_one 这样的方法,ActiveRecord 会根据默认的规则,来关联关系关联到的是哪个表的哪个字段。这也就是 Rails 中所谓的约定大于配置的体现。
58
+
59
+ 使用 Arql 查询既有系统的数据库时,数据库中的表、字段名称往往不符合 Rails 的规则约定,这时我们可以通过传递参数来指定关联关系的关联字段:
60
+
61
+ #+BEGIN_SRC ruby
62
+ class User < ActiveRecord::Base
63
+ has_many :posts, foreign_key: 'author_id', class_name: 'Article', primary_key: 'uid'
64
+ end
65
+ #+END_SRC
66
+
67
+ =has_many=, =belongs_to=, =has_one= 方法常用的参数如下:
68
+
69
+ - =class_name=: 表明此关联关系对应的对方的 Model 类名
70
+ - =foreign_key=: 表明此关联关系中,从属表一侧的关联字段名
71
+ - =primary_key=: 表明此关联关系中,属主表一侧的关联字段名
72
+ - =join_table=: 在多对多关系中,表明关联两个表的中间表名
73
+ - =association_foreign_key=: 在多对多关系中,表明对方 Model 在中间表中的关联字段名
74
+
75
+ ** 简单 CRUD
76
+
77
+ 参考:https://guides.rubyonrails.org/active_record_querying.html
78
+
79
+ ** 参考
80
+
81
+ - https://guides.rubyonrails.org/active_record_basics.html
82
+ - https://guides.rubyonrails.org/active_record_querying.html
83
+ - https://guides.rubyonrails.org/association_basics.html
@@ -0,0 +1,114 @@
1
+ * Pry 简明教程
2
+
3
+ ** Pry 是什么?
4
+
5
+ 和 Python 一样,Ruby 也有自己的 REPL 工具,叫做 irb(Interactive Ruby)。
6
+
7
+ Pry 是另外一个功能更加强大的 Ruby REPL 工具,它可以让你在 Ruby REPL 中做很多事情,比如查看源码、查看文档、调试等等。
8
+
9
+ Arql 的主要功能就是基于 Pry 实现的,所以你可以把 Pry 当做 Arql 的一个子集。Pry 的命令和功能在 Arql 中都是可以使用的。而且 Arql 还提供了一些额外的 Pry 命令。
10
+
11
+
12
+ 当然也可以单独安装和使用 Pry。
13
+
14
+
15
+ ** 单独安装 Pry
16
+
17
+ #+BEGIN_EXAMPLE
18
+ $ gem install pry
19
+ #+END_EXAMPLE
20
+
21
+ Pry 本身也支持扩展,你可以安装一些 Pry 的插件,比如 pry-doc、pry-byebug 等等。
22
+
23
+ #+BEGIN_EXAMPLE
24
+ $ gem install pry-doc
25
+ $ gem install pry-byebug
26
+ #+END_EXAMPLE
27
+
28
+
29
+ ** 单独使用 Pry
30
+
31
+ #+BEGIN_EXAMPLE
32
+ $ pry
33
+ #+END_EXAMPLE
34
+
35
+
36
+ ** 常用的 Pry 命令
37
+
38
+
39
+ *** 查看帮助
40
+
41
+ #+BEGIN_EXAMPLE
42
+ [1] pry(main)> help
43
+ #+END_EXAMPLE
44
+
45
+
46
+ *** 查看变量
47
+
48
+ #+BEGIN_EXAMPLE
49
+ [2] pry(main)> ls
50
+ #+END_EXAMPLE
51
+
52
+
53
+ *** 查看一个实例方法的源码
54
+
55
+ #+BEGIN_EXAMPLE
56
+ [3] pry(main)> show-source ActiveRecord::Base#save
57
+ #+END_EXAMPLE
58
+
59
+
60
+ *** 查看一个实例方法的文档
61
+
62
+ #+BEGIN_EXAMPLE
63
+ [4] pry(main)> show-doc ActiveRecord::Base#save
64
+ #+END_EXAMPLE
65
+
66
+
67
+ *** 查看一个类的的源码
68
+
69
+ #+BEGIN_EXAMPLE
70
+ [5] pry(main)> show-source ActiveRecord::Base
71
+ #+END_EXAMPLE
72
+
73
+
74
+ *** 查看一个类的文档
75
+
76
+ #+BEGIN_EXAMPLE
77
+ [6] pry(main)> show-doc ActiveRecord::Base
78
+ #+END_EXAMPLE
79
+
80
+
81
+ *** 在 Pry 中直接修改代码
82
+
83
+ 你甚至可以在 Pry 中用 edit 命令直接修改代码,然后 Pry 会自动保存修改后的代码到一个临时文件中,然后你可以在 Pry 中直接调用修改后的代码。
84
+
85
+ #+BEGIN_EXAMPLE
86
+ [7] pry(main)> edit ActiveRecord::Base#save
87
+ #+END_EXAMPLE
88
+
89
+ ** 分号
90
+
91
+ 在 Ruby 语言中, 行尾的分号是可以省略的。
92
+
93
+
94
+ Pry 中每次执行一个 Ruby 表达式,都会自动打印出这个表达式的值:
95
+
96
+ #+BEGIN_EXAMPLE
97
+ [7] pry(main)> user.posts = Post.all
98
+ => [#<Post id: 1, title: "Hello World", content: "Hello World", created_at: "2016 -12-12 12:12:12", updated_at: "2016-12-12 12:12:12">,
99
+ #<Post id: 2, title: "Hello Ruby", content: "Hello Ruby", created_at: "2016-12-12 12:12:12", updated_at: "2016-12-12 12:12:12">,
100
+ ... ...]
101
+ #+END_EXAMPLE
102
+
103
+ 这是通过调用 Ruby 的 表达式的值对象的 inspect 方法 (Object#inspect 方法)实现的。如果你不想打印出这个值,可以在表达式后面加上分号:
104
+
105
+ #+BEGIN_EXAMPLE
106
+ [8] pry(main)> user.posts = Post.all;
107
+ #+END_EXAMPLE
108
+
109
+ 我们知道在 ActiveRecord 中,像 =User.where(gender: 'Male')= 这样的表达式,返回结果是一个 ActiveRecord::Relation 对象,而不是一个数组。
110
+ 这样设计的目的是为了支持 Lazy Loading,只有在需要的时候才会去执行 SQL 查询。但是当我们在 Pry 中直接对 =User.where(gender: 'Male')= 的时候,却发现
111
+ 它立即执行了 SQL 查询,并且输出的是一个数组对象;这就是因为 Pry 在打印对象的时候,会调用对象的 inspect 方法,而 ActiveRecord::Relation 对象的 inspect 方法
112
+ 会立即执行 SQL 查询并返回一个数组对象。如果你不想立即执行 SQL 查询,就可以在表达式后面加上分号。
113
+
114
+
data/sql-log-zh_CN.org ADDED
@@ -0,0 +1,55 @@
1
+ * 自动记录 SQL 日志和 REPL 输入历史
2
+
3
+ 如果希望 Arql 可以自动记录 SQL 日志和 REPL 输入历史,可以:
4
+
5
+ 创建目录 =~/.arql.d/logs=
6
+
7
+ 创建一个文件 =~/.arql.d/sql_log.rb= ,内容如下:
8
+
9
+ #+BEGIN_SRC ruby
10
+ unless Arql::App.config[:append_sql]
11
+ log_root_dir = File.expand_path('~/.arql.d/logs')
12
+ log_dir = "#{log_root_dir}/%s" % Arql::App.env
13
+ FileUtils.mkdir_p(log_dir)
14
+ now = Time.now
15
+ log_file = "#{log_dir}/%s.%s.%s.log" % [Time.now.strftime('%Y_%m%d_%H%M%S'), `hostname -s`.chomp.downcase, Process.pid]
16
+ Arql::App.config[:append_sql] = log_file
17
+
18
+ lfile = File.new(log_file, 'a')
19
+ lfile.sync = true
20
+ InputLogger = Logger.new(lfile)
21
+
22
+ module Readline
23
+ class << self
24
+ alias_method :original_readline, :readline
25
+ def readline(*args)
26
+ Readline.original_readline(*args).tap do |user_input|
27
+ InputLogger.info(user_input)
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ #+END_SRC
34
+
35
+
36
+ 然后在 =~/.arql.d/init.rb= 中引入这个文件:
37
+
38
+ #+BEGIN_SRC ruby
39
+ load(File.absolute_path(File.dirname(__FILE__) + "/sql_log.rb"))
40
+ #+END_SRC
41
+
42
+ 这样就可以在 =~/.arql.d/logs= 目录下看到 SQL 日志和 REPL 输入历史了。
43
+
44
+ 示例:
45
+
46
+ #+BEGIN_EXAMPLE
47
+ I, [2024-04-07T17:12:00.530341 #20440] INFO -- : P.count
48
+ D, [2024-04-07T17:12:00.577305 #20440] DEBUG -- : Post Count (22.6ms) SELECT COUNT(*) FROM `post`
49
+ I, [2024-04-07T17:12:02.879312 #20440] INFO -- : P.all.t
50
+ D, [2024-04-07T17:12:02.960014 #20440] DEBUG -- : Post Load (64.1ms) SELECT `post`.* FROM `post`
51
+ I, [2024-04-07T17:12:54.721861 #20440] INFO -- : P.pluck(:gender)
52
+ D, [2024-04-07T17:12:54.756435 #20440] DEBUG -- : Post Pluck (28.0ms) SELECT `post`.`gender` FROM `post`
53
+ #+END_EXAMPLE
54
+
55
+
data/sql-log.org ADDED
@@ -0,0 +1,55 @@
1
+ * Save SQL logs and REPL history automatically
2
+
3
+ If you want Arql to automatically save SQL logs and REPL history, you can:
4
+
5
+ Create a directory =~/.arql.d/logs=
6
+
7
+ Create a file =~/.arql.d/sql_log.rb= with the following content:
8
+
9
+ #+BEGIN_SRC ruby
10
+ unless Arql::App.config[:append_sql]
11
+ log_root_dir = File.expand_path('~/.arql.d/logs')
12
+ log_dir = "#{log_root_dir}/%s" % Arql::App.env
13
+ FileUtils.mkdir_p(log_dir)
14
+ now = Time.now
15
+ log_file = "#{log_dir}/%s.%s.%s.log" % [Time.now.strftime('%Y_%m%d_%H%M%S'), `hostname -s`.chomp.downcase, Process.pid]
16
+ Arql::App.config[:append_sql] = log_file
17
+
18
+ lfile = File.new(log_file, 'a')
19
+ lfile.sync = true
20
+ InputLogger = Logger.new(lfile)
21
+
22
+ module Readline
23
+ class << self
24
+ alias_method :original_readline, :readline
25
+ def readline(*args)
26
+ Readline.original_readline(*args).tap do |user_input|
27
+ InputLogger.info(user_input)
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ #+END_SRC
34
+
35
+ Then include this file in =~/.arql.d/init.rb=:
36
+
37
+ #+BEGIN_SRC ruby
38
+ load(File.absolute_path(File.dirname(__FILE__) + "/sql_log.rb"))
39
+ #+END_SRC
40
+
41
+ This way you can see SQL logs and REPL history in the =~/.arql.d/logs= directory.
42
+
43
+ Example:
44
+
45
+ #+BEGIN_EXAMPLE
46
+ I, [2024-04-07T17:12:00.530341 #20440] INFO -- : P.count
47
+ D, [2024-04-07T17:12:00.577305 #20440] DEBUG -- : Post Count (22.6ms) SELECT COUNT(*) FROM `post`
48
+ I, [2024-04-07T17:12:02.879312 #20440] INFO -- : P.all.t
49
+ D, [2024-04-07T17:12:02.960014 #20440] DEBUG -- : Post Load (64.1ms) SELECT `post`.* FROM `post`
50
+ I, [2024-04-07T17:12:54.721861 #20440] INFO -- : P.pluck
51
+ D, [2024-04-07T17:12:54.756435 #20440] DEBUG -- : Post Pluck (28.0ms) SELECT `post`.`gender` FROM `post`
52
+ #+END_EXAMPLE
53
+
54
+
55
+
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: arql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.29
4
+ version: 0.3.31
5
5
  platform: ruby
6
6
  authors:
7
7
  - Liu Xiang
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-03-13 00:00:00.000000000 Z
11
+ date: 2024-04-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mysql2
@@ -304,13 +304,26 @@ files:
304
304
  - Gemfile
305
305
  - Gemfile.lock
306
306
  - LICENSE.txt
307
- - README.md
307
+ - README-zh_CN.org
308
+ - README.org
308
309
  - Rakefile
309
310
  - arql.gemspec
311
+ - auto-set-id-before-save-zh_CN.org
312
+ - auto-set-id-before-save.org
310
313
  - bin/console
311
314
  - bin/setup
315
+ - custom-configurations-zh_CN.org
316
+ - custom-configurations.org
317
+ - define-associations-zh_CN.org
318
+ - define-associations.org
312
319
  - exe/arql
313
320
  - exe/arql_setsid_wrapper
321
+ - fuzzy-field-query-zh_CN.org
322
+ - fuzzy-field-query.org
323
+ - helper-for-datetime-range-query-zh_CN.org
324
+ - helper-for-datetime-range-query.org
325
+ - initializer-structure-zh_CN.org
326
+ - initializer-structure.org
314
327
  - lib/arql.rb
315
328
  - lib/arql/app.rb
316
329
  - lib/arql/cli.rb
@@ -345,6 +358,13 @@ files:
345
358
  - lib/arql/ssh_proxy_patch.rb
346
359
  - lib/arql/vd.rb
347
360
  - lib/arql/version.rb
361
+ - oss-files-zh_CN.org
362
+ - oss-files.org
363
+ - ruby-guides-for-java-developer-zh_CN.org
364
+ - simple-active-record-guide-zh_CN.org
365
+ - simple-pry-guides-zh_CN.org
366
+ - sql-log-zh_CN.org
367
+ - sql-log.org
348
368
  homepage: https://github.com/lululau/arql
349
369
  licenses:
350
370
  - MIT