execute_sql 0.1.0 → 0.1.1

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: 9d044ff6ed461e770b7cbdab32ae3631f63b67770d8bfac0db8c9b262d2921bb
4
- data.tar.gz: daceaad6d0633c7567783e213a73c23911798010609ebb4e71ba539530f7daff
3
+ metadata.gz: 31b3c1b47cb6e87cce5d7386c7a40fea2ef2e2ac61089162105cc679629d9885
4
+ data.tar.gz: 0ea679069626e874e931c7c9b9ca09d360926f9bff18529b935c7f679aed0593
5
5
  SHA512:
6
- metadata.gz: acb0057884f0562a44c564e4bdf68b2db53510c0daab1368fe83fd8fbf6908d3f62424c7273e06c531d821cf60927b4808d3e0255675e22f77b53e9fbcdb7094
7
- data.tar.gz: 5bec6886e112aa42fcbd6c5727e563717b93ccda3651903cac3fa339b2b2f7adb1f066f9beb681db069ddb85067d72ec2553e0a8d09a8b09750b68316755f53b
6
+ metadata.gz: b7ea65b2a1c8f57824aa3b16dd89435bf23a82a0d08d1531d989d848e3e688db57d802015e9ebcf66b4fe0b22118095e188b1a69108ef944c66272302a2a5d86
7
+ data.tar.gz: fac08ad05b1106ab61355f374c8759c9768c5c499fbba26d0b3410ff852f673e376ac9728f70da4710e6484d27594ca01062ccb1bad7b1d1360411a66848ef52
data/README.md CHANGED
@@ -24,6 +24,13 @@ ExecuteSQL.run "select count(*) from users where age > 50"
24
24
  # or with different mode
25
25
  # default mode: :print
26
26
 
27
+ # return array of HashWithIndifferentAccess objects
28
+ # please remember that arrays can be manipulated with Enumerable methods, but this is *not* a chainable ARel relation
29
+ ExecuteSQL.run "select * from users where age > 50", mode: :array
30
+
31
+ # return array of User objects
32
+ ExecuteSQL.run "select * from users where age > 50", mode: :array, klass: User
33
+
27
34
  # return single value
28
35
  ExecuteSQL.run "select count(*) from users where age > 50", mode: :single
29
36
 
@@ -64,6 +71,13 @@ In rails console use helper: `execute_sql "some SQL"`.
64
71
 
65
72
  Or `ExecuteSql.run "some SQL"` or `ExecuteSQL.run "some SQL"`.
66
73
 
74
+ ## TODO
75
+
76
+ - import SQL files
77
+ - travis CI
78
+ - verify with older rails
79
+ - more specs
80
+
67
81
  ## Contributing
68
82
 
69
83
  You are welcome to contribute.
@@ -71,3 +85,11 @@ You are welcome to contribute.
71
85
  ## License
72
86
 
73
87
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
88
+
89
+ Some pieces of code I took from another my gem: https://github.com/igorkasyanchuk/rails_db.
90
+
91
+ ## Contributors
92
+
93
+ - pjforde1978
94
+
95
+ Big thank you.
@@ -22,13 +22,22 @@ require "execute_sql/railtie"
22
22
  module ExecuteSql
23
23
  module ConsoleMethods
24
24
 
25
- def execute_sql(sql, mode: :print)
25
+ def execute_sql(sql, mode: :print, klass: HashWithIndifferentAccess)
26
26
  sql_query = ExecuteSql::SqlQuery.new("#{sql}".strip).execute
27
27
  rows = sql_query.data.rows
28
28
  cols = sql_query.data.columns
29
29
  case mode.to_s
30
30
  when 'print'
31
31
  puts Terminal::Table.new(rows: rows, headings: cols)
32
+ when 'array'
33
+ result = rows.map do |row|
34
+ record = klass.new
35
+ cols.each_with_index.map do |col, index|
36
+ record[col] = row[index]
37
+ end
38
+ record
39
+ end
40
+ result
32
41
  when 'raw'
33
42
  rows
34
43
  when 'single'
@@ -48,4 +57,4 @@ module ExecuteSql
48
57
  extend ConsoleMethods
49
58
  end
50
59
 
51
- ExecuteSQL = ExecuteSql
60
+ ExecuteSQL = ExecuteSql
@@ -1,3 +1,3 @@
1
1
  module ExecuteSql
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: execute_sql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Igor Kasyanchuk
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-03-13 00:00:00.000000000 Z
11
+ date: 2019-12-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: 1.3.13
47
+ version: '1.4'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: 1.3.13
54
+ version: '1.4'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: pry-rails
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -113,7 +113,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
113
113
  - !ruby/object:Gem::Version
114
114
  version: '0'
115
115
  requirements: []
116
- rubygems_version: 3.0.2
116
+ rubygems_version: 3.0.6
117
117
  signing_key:
118
118
  specification_version: 4
119
119
  summary: Execute SQL directly in rails console or in the code.