arql 0.1.15 → 0.1.20

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a5f18ea311aeb3c4f976d97c1b457c8eebefa9226637f80d7bfd0d7ef288099e
4
- data.tar.gz: 78aaa74f7d64934fb047c012cda6fc8c67904de55bb69db0499569d3973b8388
3
+ metadata.gz: b383963c2e71446f35b3eba8c9ff365698d93f377407f17e81d4775b5a841bab
4
+ data.tar.gz: 1efd73946f5f238a90f5d74370525460c25f3a930de2689ca47201cd43f76549
5
5
  SHA512:
6
- metadata.gz: dded93fc5d32b2d4f28c8270b7c106f9da5a6d187ad53ca3a35d315f24d17dd44dc573cf5125d15d6f9bbad8dceb248944cf68e876398d0eeb1c62fd3d099ce5
7
- data.tar.gz: 24c97fdb85051959783e89dd156b3361940bc6b8c7bee6752c322b9f63d3c177f5667387f588215f911d3d3a86284127cd40158a1e80e70b2e7e0f4fb87f86d2
6
+ metadata.gz: bc2581c870cd2102fddc37208145c7b84ab3806460de23c2289fafb494b74625aa0ec8fa4a44fdaf06cc18caf6048426a475149df55138c4bc17bbd1d06ca64c
7
+ data.tar.gz: a42fc963d40ffb65fb21db348321208202c2bd84c89d299e4863143bffd7436fa62591e7d52fe4f3bd9efcc02332872b85fdb76999c132329b9fdd37ddb5cb08
@@ -1,27 +1,35 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- arql (0.1.15)
4
+ arql (0.1.19)
5
5
  activerecord (~> 6.0.3)
6
+ activerecord-oracle_enhanced-adapter
7
+ activerecord-sqlserver-adapter
6
8
  activesupport (~> 6.0.3)
7
9
  mysql2 (~> 0.5.3)
8
10
  net-ssh-gateway (~> 2.0.0)
11
+ pg (>= 0.18, < 2.0)
9
12
  pry (~> 0.13.1)
10
13
  pry-byebug (~> 3.9.0)
11
14
  pry-doc (~> 1.1.0)
12
15
  rainbow (~> 3.0.0)
16
+ sqlite3 (~> 1.4)
13
17
  table_print (~> 1.5.6)
14
18
  terminal-table (~> 1.8.0)
15
19
 
16
20
  GEM
17
21
  remote: https://rubygems.org/
18
22
  specs:
19
- activemodel (6.0.3)
20
- activesupport (= 6.0.3)
21
- activerecord (6.0.3)
22
- activemodel (= 6.0.3)
23
- activesupport (= 6.0.3)
24
- activesupport (6.0.3)
23
+ activemodel (6.0.3.1)
24
+ activesupport (= 6.0.3.1)
25
+ activerecord (6.0.3.1)
26
+ activemodel (= 6.0.3.1)
27
+ activesupport (= 6.0.3.1)
28
+ activerecord-oracle_enhanced-adapter (6.0.2)
29
+ activerecord (~> 6.0.0)
30
+ ruby-plsql (>= 0.6.0)
31
+ activerecord-sqlserver-adapter (2.3.8)
32
+ activesupport (6.0.3.1)
25
33
  concurrent-ruby (~> 1.0, >= 1.0.2)
26
34
  i18n (>= 0.7, < 2)
27
35
  minitest (~> 5.1)
@@ -33,11 +41,12 @@ GEM
33
41
  i18n (1.8.2)
34
42
  concurrent-ruby (~> 1.0)
35
43
  method_source (1.0.0)
36
- minitest (5.14.0)
44
+ minitest (5.14.1)
37
45
  mysql2 (0.5.3)
38
46
  net-ssh (6.0.2)
39
47
  net-ssh-gateway (2.0.0)
40
48
  net-ssh (>= 4.0.0)
49
+ pg (1.2.3)
41
50
  pry (0.13.1)
42
51
  coderay (~> 1.1)
43
52
  method_source (~> 1.0)
@@ -49,6 +58,8 @@ GEM
49
58
  yard (~> 0.9.11)
50
59
  rainbow (3.0.0)
51
60
  rake (12.3.3)
61
+ ruby-plsql (0.7.1)
62
+ sqlite3 (1.4.2)
52
63
  table_print (1.5.6)
53
64
  terminal-table (1.8.0)
54
65
  unicode-display_width (~> 1.1, >= 1.1.1)
data/README.md CHANGED
@@ -1,28 +1,440 @@
1
1
  # Arql
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/arql`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
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.
6
5
 
7
6
  ## Installation
8
7
 
9
- Add this line to your application's Gemfile:
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
10
19
 
11
- ```ruby
12
- gem 'arql'
13
20
  ```
21
+ Usage: arql [options] [ruby file]
14
22
 
15
- And then execute:
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
16
25
 
17
- $ bundle install
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
+ ```
18
50
 
19
- Or install it yourself as:
51
+ #### -c, --config=CONFIG_FILE
20
52
 
21
- $ gem install arql
53
+ Specify location of config file, default is `$HOME/.arql.yml`, or `$HOME/.arql.d/init.yml`.
22
54
 
23
- ## Usage
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
24
436
 
25
- TODO: Write usage instructions here
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.
26
438
 
27
439
  ## Development
28
440
 
@@ -24,6 +24,10 @@ Gem::Specification.new do |spec|
24
24
  spec.require_paths = ["lib"]
25
25
 
26
26
  spec.add_dependency 'mysql2', '~> 0.5.3'
27
+ spec.add_dependency 'pg', '>= 0.18', '< 2.0'
28
+ spec.add_dependency 'sqlite3', '~> 1.4'
29
+ spec.add_dependency 'activerecord-sqlserver-adapter'
30
+ spec.add_dependency 'activerecord-oracle_enhanced-adapter'
27
31
  spec.add_dependency 'activerecord', '~> 6.0.3'
28
32
  spec.add_dependency 'activesupport', '~> 6.0.3'
29
33
  spec.add_dependency 'net-ssh-gateway', '~> 2.0.0'
@@ -4,6 +4,8 @@ module Arql
4
4
  class App
5
5
 
6
6
  class << self
7
+ attr_accessor :log_io
8
+
7
9
  def config
8
10
  @@effective_config
9
11
  end
@@ -21,12 +23,13 @@ module Arql
21
23
  @options = options
22
24
  Connection.open(connect_options)
23
25
  @definition = Definition.new(effective_config)
26
+ load_initializer!
24
27
  end
25
28
 
26
29
  def connect_options
27
30
  connect_conf = effective_config.slice(:adapter, :host, :username,
28
31
  :password, :database, :encoding,
29
- :pool, :port)
32
+ :pool, :port, :socket)
30
33
  if effective_config[:ssh].present?
31
34
  connect_conf.merge!(start_ssh_proxy!)
32
35
  end
@@ -34,6 +37,15 @@ module Arql
34
37
  connect_conf
35
38
  end
36
39
 
40
+ def load_initializer!
41
+ return unless effective_config[:initializer]
42
+ unless File.exists?(effective_config[:initializer])
43
+ STDERR.puts "Specified initializer file not found, #{effective_config[:initializer]}"
44
+ exit(1)
45
+ end
46
+ load(effective_config[:initializer])
47
+ end
48
+
37
49
  def start_ssh_proxy!
38
50
  ssh_config = effective_config[:ssh]
39
51
  @ssh_gateway = Net::SSH::Gateway.new(ssh_config[:host], ssh_config[:user], ssh_config.slice(:port, :password).symbolize_keys)
@@ -49,6 +61,9 @@ module Arql
49
61
  end
50
62
 
51
63
  def selected_config
64
+ if @options.env.present? && !config[@options.env].present?
65
+ STDERR.puts "Specified ENV `#{@options.env}' not exists"
66
+ end
52
67
  config[@options.env]
53
68
  end
54
69
 
@@ -88,23 +103,23 @@ module Arql
88
103
  end
89
104
 
90
105
  def show_sql
91
- @log_io ||= MultiIO.new
92
- ActiveRecord::Base.logger = Logger.new(@log_io)
93
- @log_io << STDOUT
106
+ App.log_io ||= MultiIO.new
107
+ ActiveRecord::Base.logger = Logger.new(App.log_io)
108
+ App.log_io << STDOUT
94
109
  end
95
110
 
96
111
  def write_sql
97
112
  write_sql_file = effective_config[:write_sql]
98
- @log_io ||= MultiIO.new
99
- ActiveRecord::Base.logger = Logger.new(@log_io)
100
- @log_io << File.new(write_sql_file, 'w')
113
+ App.log_io ||= MultiIO.new
114
+ ActiveRecord::Base.logger = Logger.new(App.log_io)
115
+ App.log_io << File.new(write_sql_file, 'w')
101
116
  end
102
117
 
103
118
  def append_sql
104
119
  write_sql_file = effective_config[:append_sql]
105
- @log_io ||= MultiIO.new
106
- ActiveRecord::Base.logger = Logger.new(@log_io)
107
- @log_io << File.new(write_sql_file, 'a')
120
+ App.log_io ||= MultiIO.new
121
+ ActiveRecord::Base.logger = Logger.new(App.log_io)
122
+ App.log_io << File.new(write_sql_file, 'a')
108
123
  end
109
124
  end
110
125
  end
@@ -22,8 +22,8 @@ module Arql
22
22
  opts.banner = <<~EOF
23
23
  Usage: arql [options] [ruby file]
24
24
 
25
- If neither [ruby file] nor -e option specified, a Pry REPL will be launched,
26
- otherwise the specified ruby file or -e option value will be run, and no REPL launched
25
+ If neither [ruby file] nor -e option specified, and STDIN is not a tty, a Pry REPL will be launched,
26
+ otherwise the specified ruby file or -e option value or ruby code read from STDIN will be run, and no REPL launched
27
27
 
28
28
  EOF
29
29
 
@@ -107,6 +107,11 @@ module Arql
107
107
  @options.append_sql = file
108
108
  end
109
109
 
110
+ opts.on('-V', '--version', 'Prints version') do
111
+ puts "ARQL #{Arql::VERSION}"
112
+ exit
113
+ end
114
+
110
115
  opts.on('', '--help', 'Prints this help') do
111
116
  puts opts
112
117
  exit
@@ -129,13 +134,9 @@ module Arql
129
134
  end
130
135
 
131
136
  def default_initializer
132
- conf = File.expand_path('~/.arql.yml')
137
+ conf = File.expand_path('~/.arql.rb')
133
138
  return conf if File.file?(conf)
134
- conf = File.expand_path('~/.arql.yaml')
135
- return conf if File.file?(conf)
136
- conf = File.expand_path('~/.arql.d/init.yml')
137
- return conf if File.file?(conf)
138
- conf = File.expand_path('~/.arql.d/init.yaml')
139
+ conf = File.expand_path('~/.arql.d/init.rb')
139
140
  return conf if File.file?(conf)
140
141
  end
141
142
  end
@@ -3,6 +3,7 @@ require 'arql/commands/models'
3
3
  require 'arql/commands/table'
4
4
  require 'arql/commands/reconnect'
5
5
  require 'arql/commands/redefine'
6
+ require 'arql/commands/show_sql'
6
7
 
7
8
  module Arql::Commands
8
9
  end
@@ -0,0 +1,25 @@
1
+ module Arql::Commands
2
+ module ShowSql
3
+ class << self
4
+ def show
5
+ return if Arql::App.log_io.is_a?(Arql::MultiIO) && Arql::App.log_io.include?(STDOUT)
6
+ Arql::App.log_io ||= Arql::MultiIO.new
7
+ ActiveRecord::Base.logger = Logger.new(Arql::App.log_io)
8
+ Arql::App.log_io << STDOUT
9
+ end
10
+
11
+ def hide
12
+ return if !Arql::App.log_io.is_a?(Arql::MultiIO) || !Arql::App.log_io.include?(STDOUT)
13
+ Arql::App.log_io.delete(STDOUT)
14
+ end
15
+ end
16
+
17
+ Pry.commands.block_command 'show-sql' do
18
+ ShowSql.show
19
+ end
20
+
21
+ Pry.commands.block_command 'hide-sql' do
22
+ ShowSql.hide
23
+ end
24
+ end
25
+ end
@@ -3,6 +3,7 @@ module Arql
3
3
  class << self
4
4
  def open(options)
5
5
  ActiveRecord::Base.establish_connection(options)
6
+ $C = ActiveRecord::Base.connection
6
7
  end
7
8
  end
8
9
  end
@@ -50,6 +50,10 @@ module Arql
50
50
  ActiveRecord::InsertAll.new(self, group.map(&:attributes), on_duplicate: on_duplicate).send(:to_sql) + ';'
51
51
  end.join("\n")
52
52
  end
53
+
54
+ def to_create_sql
55
+ ActiveRecord::Base.connection.exec_query("show create table #{table_name}").rows.last.last
56
+ end
53
57
  end
54
58
  end
55
59
 
@@ -2,3 +2,4 @@ require 'arql/ext/object'
2
2
  require 'arql/ext/array'
3
3
  require 'arql/ext/time'
4
4
  require 'arql/ext/string'
5
+ require 'arql/ext/kernel'
@@ -13,6 +13,31 @@ class Array
13
13
  end.join("\n")
14
14
  end
15
15
 
16
+ def t(*attrs)
17
+ if attrs.present? && present? && first.is_a?(ActiveRecord::Base)
18
+ puts Terminal::Table.new { |t|
19
+ t << attrs
20
+ t << :separator
21
+ each do |e|
22
+ t << e.attributes.values_at(*attrs.map(&:to_s))
23
+ end
24
+ }
25
+ else
26
+ table = Terminal::Table.new { |t|
27
+ v.each { |row| t << (row || :separator)}
28
+ }.to_s
29
+
30
+ terminal_width = `tput cols`.to_i
31
+ if table.first.size > terminal_width
32
+ table = table.lines.map(&:chomp)
33
+ puts table[0..2].join("\n")
34
+ puts table[3..-1].join("\n#{'-' * terminal_width}\n")
35
+ else
36
+ puts table
37
+ end
38
+ end
39
+ end
40
+
16
41
  def v
17
42
  return self unless present?
18
43
  t = []
@@ -0,0 +1,5 @@
1
+ module Kernel
2
+ def sql(sql)
3
+ ActiveRecord::Base.connection.exec_query(sql)
4
+ end
5
+ end
@@ -16,5 +16,13 @@ module Arql
16
16
  @targets ||= []
17
17
  @targets << target
18
18
  end
19
+
20
+ def delete(io)
21
+ @targets.delete(io)
22
+ end
23
+
24
+ def include?(io)
25
+ @targets.include?(io)
26
+ end
19
27
  end
20
28
  end
@@ -1,3 +1,3 @@
1
1
  module Arql
2
- VERSION = "0.1.15"
2
+ VERSION = "0.1.20"
3
3
  end
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.1.15
4
+ version: 0.1.20
5
5
  platform: ruby
6
6
  authors:
7
7
  - Liu Xiang
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-05-14 00:00:00.000000000 Z
11
+ date: 2020-06-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mysql2
@@ -24,6 +24,68 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: 0.5.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: pg
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0.18'
34
+ - - "<"
35
+ - !ruby/object:Gem::Version
36
+ version: '2.0'
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '0.18'
44
+ - - "<"
45
+ - !ruby/object:Gem::Version
46
+ version: '2.0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: sqlite3
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '1.4'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '1.4'
61
+ - !ruby/object:Gem::Dependency
62
+ name: activerecord-sqlserver-adapter
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ type: :runtime
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: activerecord-oracle_enhanced-adapter
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ type: :runtime
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
27
89
  - !ruby/object:Gem::Dependency
28
90
  name: activerecord
29
91
  requirement: !ruby/object:Gem::Requirement
@@ -177,11 +239,13 @@ files:
177
239
  - lib/arql/commands/models.rb
178
240
  - lib/arql/commands/reconnect.rb
179
241
  - lib/arql/commands/redefine.rb
242
+ - lib/arql/commands/show_sql.rb
180
243
  - lib/arql/commands/table.rb
181
244
  - lib/arql/connection.rb
182
245
  - lib/arql/definition.rb
183
246
  - lib/arql/ext.rb
184
247
  - lib/arql/ext/array.rb
248
+ - lib/arql/ext/kernel.rb
185
249
  - lib/arql/ext/object.rb
186
250
  - lib/arql/ext/string.rb
187
251
  - lib/arql/ext/time.rb
@@ -193,7 +257,7 @@ homepage: https://github.com/lululau/arql
193
257
  licenses:
194
258
  - MIT
195
259
  metadata: {}
196
- post_install_message:
260
+ post_install_message:
197
261
  rdoc_options: []
198
262
  require_paths:
199
263
  - lib
@@ -209,7 +273,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
209
273
  version: '0'
210
274
  requirements: []
211
275
  rubygems_version: 3.1.2
212
- signing_key:
276
+ signing_key:
213
277
  specification_version: 4
214
278
  summary: Rails ActiveRecord + Pry is the best SQL query editor
215
279
  test_files: []