yesql 0.1.4 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +14 -31
  3. data/lib/.rbnext/2.3/yesql/errors/file_path_does_not_exist_error.rb +40 -0
  4. data/lib/.rbnext/2.3/yesql/errors/no_bindings_provided_error.rb +38 -0
  5. data/lib/.rbnext/2.3/yesql/errors/output_argument_error.rb +31 -0
  6. data/lib/.rbnext/2.3/yesql/query/transform_result.rb +48 -0
  7. data/lib/.rbnext/2.7/yesql/bindings/extract.rb +71 -0
  8. data/lib/.rbnext/2.7/yesql/bindings/extractor.rb +38 -0
  9. data/lib/.rbnext/2.7/yesql/bindings/transformed.rb +58 -0
  10. data/lib/.rbnext/2.7/yesql/bindings/utils.rb +16 -0
  11. data/lib/.rbnext/2.7/yesql/errors/file_path_does_not_exist_error.rb +43 -0
  12. data/lib/.rbnext/2.7/yesql/errors/no_bindings_provided_error.rb +41 -0
  13. data/lib/.rbnext/2.7/yesql/query/transform_result.rb +48 -0
  14. data/lib/.rbnext/2.7/yesql/statement.rb +44 -0
  15. data/lib/.rbnext/2.7/yesql/utils/read.rb +20 -0
  16. data/lib/.rbnext/3.0/yesql/bindings/extract.rb +71 -0
  17. data/lib/.rbnext/3.0/yesql/bindings/transformed.rb +58 -0
  18. data/lib/.rbnext/3.0/yesql/common/adapter.rb +18 -0
  19. data/lib/.rbnext/3.0/yesql/config/configuration.rb +32 -0
  20. data/lib/.rbnext/3.0/yesql/errors/file_path_does_not_exist_error.rb +43 -0
  21. data/lib/.rbnext/3.0/yesql/errors/no_bindings_provided_error.rb +41 -0
  22. data/lib/.rbnext/3.0/yesql/params/output.rb +26 -0
  23. data/lib/.rbnext/3.0/yesql/query/performer.rb +44 -0
  24. data/lib/.rbnext/3.0/yesql/query/result.rb +41 -0
  25. data/lib/.rbnext/3.0/yesql/query/transform_result.rb +48 -0
  26. data/lib/.rbnext/3.0/yesql/statement.rb +44 -0
  27. data/lib/.rbnext/3.0/yesql/utils/read.rb +20 -0
  28. data/lib/yesql.rb +25 -32
  29. data/lib/yesql/bindings/extract.rb +22 -19
  30. data/lib/yesql/bindings/extractor.rb +21 -21
  31. data/lib/yesql/bindings/transformed.rb +58 -0
  32. data/lib/yesql/bindings/utils.rb +7 -5
  33. data/lib/yesql/common/adapter.rb +18 -0
  34. data/lib/yesql/config/configuration.rb +4 -5
  35. data/lib/yesql/errors/file_path_does_not_exist_error.rb +22 -17
  36. data/lib/yesql/errors/no_bindings_provided_error.rb +20 -19
  37. data/lib/yesql/errors/output_argument_error.rb +13 -4
  38. data/lib/yesql/params/output.rb +26 -0
  39. data/lib/yesql/query/performer.rb +20 -62
  40. data/lib/yesql/query/result.rb +41 -0
  41. data/lib/yesql/query/transform_result.rb +48 -0
  42. data/lib/yesql/statement.rb +44 -0
  43. data/lib/yesql/utils/read.rb +11 -8
  44. data/lib/yesql/version.rb +1 -1
  45. metadata +68 -18
  46. data/.gitignore +0 -1
  47. data/Gemfile +0 -9
  48. data/Gemfile.lock +0 -165
  49. data/Rakefile +0 -1
  50. data/bin/console +0 -14
  51. data/bin/setup +0 -8
  52. data/lib/yesql/bindings/binder.rb +0 -19
  53. data/lib/yesql/errors/cache_expiration_error.rb +0 -18
  54. data/yesql.gemspec +0 -27
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "yesql"
4
+ require "yesql/common/adapter"
5
+ require "forwardable"
6
+
7
+ module ::YeSQL
8
+ module Query
9
+ class TransformResult
10
+ extend ::Forwardable
11
+
12
+ include ::YeSQL::Common::Adapter
13
+
14
+ def initialize(output:, result:)
15
+ @output = output
16
+ @result = result
17
+ end
18
+
19
+ def call
20
+ if rails_5? && mysql?
21
+ return columns if columns?
22
+ return rows_values if rows?
23
+ return array_of_symbol_hashes if hash?
24
+ end
25
+
26
+ return result.public_send(output.to_sym) if columns? || rows?
27
+
28
+ to_a.map(&:symbolize_keys)
29
+ end
30
+
31
+ private
32
+
33
+ attr_reader :output, :result
34
+
35
+ def_delegators(:result, :fields, :rows, :to_a)
36
+ def_delegators(:output, :columns?, :hash?, :rows?)
37
+
38
+ def array_of_symbol_hashes
39
+ to_a.tap { break hashed_rows(_1) if rails_5? }.map { _1&.symbolize_keys || _1 }
40
+ end
41
+
42
+ def columns = fields || result.columns
43
+ def rows_values = to_a.map { _1.respond_to?(:values) ? _1.values : _1 }
44
+ def hashed_rows(rows) = rows.map { columns.zip(_1).to_h }
45
+ def rails_5? = ::ActiveRecord::VERSION::MAJOR == 5
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_record"
4
+
5
+ require "forwardable"
6
+
7
+ require "yesql/utils/read"
8
+ require "yesql/bindings/extractor"
9
+ require "yesql/common/adapter"
10
+
11
+ module ::YeSQL
12
+ class Statement
13
+ extend ::Forwardable
14
+
15
+ include ::YeSQL::Common::Adapter
16
+ include ::YeSQL::Utils::Read
17
+ # Give access to the quote method.
18
+ include ::ActiveRecord::ConnectionAdapters::Quoting
19
+
20
+ def initialize(bindings = {}, file_path)
21
+ @bindings = bindings
22
+ @file_path = file_path
23
+ end
24
+
25
+ def bound
26
+ to_s.gsub(::YeSQL::BIND_REGEX) { extract_bind_values(extractor[_1[/(\w+)/].to_sym]) }
27
+ end
28
+
29
+ def to_s = @to_s ||= statement(readable: true)
30
+ def view? = to_s =~ /^create\s.*view\s/i
31
+
32
+ private
33
+
34
+ attr_reader :bindings, :file_path
35
+
36
+ def extract_bind_values(match)
37
+ return quote(match[:value]) if view?
38
+
39
+ match[:bind][:vars]
40
+ end
41
+
42
+ def extractor = @extractor ||= ::YeSQL::Bindings::Extractor.new(bindings: bindings).call
43
+ end
44
+ end
@@ -1,17 +1,20 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module YeSQL
3
+ module ::YeSQL
4
4
  module Utils
5
5
  module Read
6
- def self.statement(file_path, readable: false)
7
- Dir["./#{::YeSQL.config.path}/**/*.sql"]
8
- .find { |dir_file_path| dir_file_path.include?("#{file_path}.sql") }
9
- .tap do |sql_file_path|
10
- break File.readlines(sql_file_path, chomp: true).join(' ') if readable == true
6
+ def statement(readable: false) = read_file(found_file, readable)
11
7
 
12
- break File.read(sql_file_path)
13
- end
8
+ private
9
+
10
+ def read_file(file, readable)
11
+ return File.readlines(file, chomp: true).join(" ") if readable == true
12
+
13
+ File.read(file)
14
14
  end
15
+
16
+ def found_file = dir_sql_files.find { _1.include?("#{file_path}.sql") }
17
+ def dir_sql_files = Dir["./#{::YeSQL.config.path}/**/*.sql"]
15
18
  end
16
19
  end
17
20
  end
data/lib/yesql/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module YeSQL
4
- VERSION = '0.1.4'
4
+ VERSION = "0.2.0"
5
5
  end
metadata CHANGED
@@ -1,29 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yesql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sebastián Palma
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-10-18 00:00:00.000000000 Z
11
+ date: 2021-04-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: rails
14
+ name: activerecord
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '5.0'
19
+ version: 4.0.0.beta1
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '5.0'
26
+ version: 4.0.0.beta1
27
+ - !ruby/object:Gem::Dependency
28
+ name: ruby-next-core
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.12.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.12.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: mysql2
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.5.3
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.5.3
27
55
  - !ruby/object:Gem::Dependency
28
56
  name: pg
29
57
  requirement: !ruby/object:Gem::Requirement
@@ -73,28 +101,50 @@ executables: []
73
101
  extensions: []
74
102
  extra_rdoc_files: []
75
103
  files:
76
- - ".gitignore"
77
- - Gemfile
78
- - Gemfile.lock
79
104
  - LICENSE.txt
80
105
  - README.md
81
- - Rakefile
82
- - bin/console
83
- - bin/setup
106
+ - lib/.rbnext/2.3/yesql/errors/file_path_does_not_exist_error.rb
107
+ - lib/.rbnext/2.3/yesql/errors/no_bindings_provided_error.rb
108
+ - lib/.rbnext/2.3/yesql/errors/output_argument_error.rb
109
+ - lib/.rbnext/2.3/yesql/query/transform_result.rb
110
+ - lib/.rbnext/2.7/yesql/bindings/extract.rb
111
+ - lib/.rbnext/2.7/yesql/bindings/extractor.rb
112
+ - lib/.rbnext/2.7/yesql/bindings/transformed.rb
113
+ - lib/.rbnext/2.7/yesql/bindings/utils.rb
114
+ - lib/.rbnext/2.7/yesql/errors/file_path_does_not_exist_error.rb
115
+ - lib/.rbnext/2.7/yesql/errors/no_bindings_provided_error.rb
116
+ - lib/.rbnext/2.7/yesql/query/transform_result.rb
117
+ - lib/.rbnext/2.7/yesql/statement.rb
118
+ - lib/.rbnext/2.7/yesql/utils/read.rb
119
+ - lib/.rbnext/3.0/yesql/bindings/extract.rb
120
+ - lib/.rbnext/3.0/yesql/bindings/transformed.rb
121
+ - lib/.rbnext/3.0/yesql/common/adapter.rb
122
+ - lib/.rbnext/3.0/yesql/config/configuration.rb
123
+ - lib/.rbnext/3.0/yesql/errors/file_path_does_not_exist_error.rb
124
+ - lib/.rbnext/3.0/yesql/errors/no_bindings_provided_error.rb
125
+ - lib/.rbnext/3.0/yesql/params/output.rb
126
+ - lib/.rbnext/3.0/yesql/query/performer.rb
127
+ - lib/.rbnext/3.0/yesql/query/result.rb
128
+ - lib/.rbnext/3.0/yesql/query/transform_result.rb
129
+ - lib/.rbnext/3.0/yesql/statement.rb
130
+ - lib/.rbnext/3.0/yesql/utils/read.rb
84
131
  - lib/yesql.rb
85
- - lib/yesql/bindings/binder.rb
86
132
  - lib/yesql/bindings/extract.rb
87
133
  - lib/yesql/bindings/extractor.rb
134
+ - lib/yesql/bindings/transformed.rb
88
135
  - lib/yesql/bindings/utils.rb
136
+ - lib/yesql/common/adapter.rb
89
137
  - lib/yesql/config/configuration.rb
90
- - lib/yesql/errors/cache_expiration_error.rb
91
138
  - lib/yesql/errors/file_path_does_not_exist_error.rb
92
139
  - lib/yesql/errors/no_bindings_provided_error.rb
93
140
  - lib/yesql/errors/output_argument_error.rb
141
+ - lib/yesql/params/output.rb
94
142
  - lib/yesql/query/performer.rb
143
+ - lib/yesql/query/result.rb
144
+ - lib/yesql/query/transform_result.rb
145
+ - lib/yesql/statement.rb
95
146
  - lib/yesql/utils/read.rb
96
147
  - lib/yesql/version.rb
97
- - yesql.gemspec
98
148
  homepage: https://github.com/sebastian-palma/yesql
99
149
  licenses:
100
150
  - MIT
@@ -102,7 +152,7 @@ metadata:
102
152
  homepage_uri: https://github.com/sebastian-palma/yesql
103
153
  source_code_uri: https://github.com/sebastian-palma/yesql
104
154
  changelog_uri: https://github.com/sebastian-palma/yesql
105
- post_install_message:
155
+ post_install_message:
106
156
  rdoc_options: []
107
157
  require_paths:
108
158
  - lib
@@ -117,8 +167,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
117
167
  - !ruby/object:Gem::Version
118
168
  version: '0'
119
169
  requirements: []
120
- rubygems_version: 3.1.2
121
- signing_key:
170
+ rubygems_version: 3.2.3
171
+ signing_key:
122
172
  specification_version: 4
123
173
  summary: Ruby library to use SQL
124
174
  test_files: []
data/.gitignore DELETED
@@ -1 +0,0 @@
1
- spec/minimalpg/log/test.log
data/Gemfile DELETED
@@ -1,9 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- source 'https://rubygems.org'
4
-
5
- gemspec
6
-
7
- group :docs do
8
- gem 'yard'
9
- end
data/Gemfile.lock DELETED
@@ -1,165 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- yesql (0.1.3)
5
- rails (>= 5.0)
6
-
7
- GEM
8
- remote: https://rubygems.org/
9
- specs:
10
- actioncable (6.0.3.3)
11
- actionpack (= 6.0.3.3)
12
- nio4r (~> 2.0)
13
- websocket-driver (>= 0.6.1)
14
- actionmailbox (6.0.3.3)
15
- actionpack (= 6.0.3.3)
16
- activejob (= 6.0.3.3)
17
- activerecord (= 6.0.3.3)
18
- activestorage (= 6.0.3.3)
19
- activesupport (= 6.0.3.3)
20
- mail (>= 2.7.1)
21
- actionmailer (6.0.3.3)
22
- actionpack (= 6.0.3.3)
23
- actionview (= 6.0.3.3)
24
- activejob (= 6.0.3.3)
25
- mail (~> 2.5, >= 2.5.4)
26
- rails-dom-testing (~> 2.0)
27
- actionpack (6.0.3.3)
28
- actionview (= 6.0.3.3)
29
- activesupport (= 6.0.3.3)
30
- rack (~> 2.0, >= 2.0.8)
31
- rack-test (>= 0.6.3)
32
- rails-dom-testing (~> 2.0)
33
- rails-html-sanitizer (~> 1.0, >= 1.2.0)
34
- actiontext (6.0.3.3)
35
- actionpack (= 6.0.3.3)
36
- activerecord (= 6.0.3.3)
37
- activestorage (= 6.0.3.3)
38
- activesupport (= 6.0.3.3)
39
- nokogiri (>= 1.8.5)
40
- actionview (6.0.3.3)
41
- activesupport (= 6.0.3.3)
42
- builder (~> 3.1)
43
- erubi (~> 1.4)
44
- rails-dom-testing (~> 2.0)
45
- rails-html-sanitizer (~> 1.1, >= 1.2.0)
46
- activejob (6.0.3.3)
47
- activesupport (= 6.0.3.3)
48
- globalid (>= 0.3.6)
49
- activemodel (6.0.3.3)
50
- activesupport (= 6.0.3.3)
51
- activerecord (6.0.3.3)
52
- activemodel (= 6.0.3.3)
53
- activesupport (= 6.0.3.3)
54
- activestorage (6.0.3.3)
55
- actionpack (= 6.0.3.3)
56
- activejob (= 6.0.3.3)
57
- activerecord (= 6.0.3.3)
58
- marcel (~> 0.3.1)
59
- activesupport (6.0.3.3)
60
- concurrent-ruby (~> 1.0, >= 1.0.2)
61
- i18n (>= 0.7, < 2)
62
- minitest (~> 5.1)
63
- tzinfo (~> 1.1)
64
- zeitwerk (~> 2.2, >= 2.2.2)
65
- builder (3.2.4)
66
- coderay (1.1.3)
67
- concurrent-ruby (1.1.7)
68
- crass (1.0.6)
69
- diff-lcs (1.4.4)
70
- erubi (1.9.0)
71
- globalid (0.4.2)
72
- activesupport (>= 4.2.0)
73
- i18n (1.8.5)
74
- concurrent-ruby (~> 1.0)
75
- loofah (2.7.0)
76
- crass (~> 1.0.2)
77
- nokogiri (>= 1.5.9)
78
- mail (2.7.1)
79
- mini_mime (>= 0.1.1)
80
- marcel (0.3.3)
81
- mimemagic (~> 0.3.2)
82
- method_source (1.0.0)
83
- mimemagic (0.3.5)
84
- mini_mime (1.0.2)
85
- mini_portile2 (2.4.0)
86
- minitest (5.14.2)
87
- nio4r (2.5.4)
88
- nokogiri (1.10.10)
89
- mini_portile2 (~> 2.4.0)
90
- pg (1.2.3)
91
- pry (0.13.1)
92
- coderay (~> 1.1)
93
- method_source (~> 1.0)
94
- rack (2.2.3)
95
- rack-test (1.1.0)
96
- rack (>= 1.0, < 3)
97
- rails (6.0.3.3)
98
- actioncable (= 6.0.3.3)
99
- actionmailbox (= 6.0.3.3)
100
- actionmailer (= 6.0.3.3)
101
- actionpack (= 6.0.3.3)
102
- actiontext (= 6.0.3.3)
103
- actionview (= 6.0.3.3)
104
- activejob (= 6.0.3.3)
105
- activemodel (= 6.0.3.3)
106
- activerecord (= 6.0.3.3)
107
- activestorage (= 6.0.3.3)
108
- activesupport (= 6.0.3.3)
109
- bundler (>= 1.3.0)
110
- railties (= 6.0.3.3)
111
- sprockets-rails (>= 2.0.0)
112
- rails-dom-testing (2.0.3)
113
- activesupport (>= 4.2.0)
114
- nokogiri (>= 1.6)
115
- rails-html-sanitizer (1.3.0)
116
- loofah (~> 2.3)
117
- railties (6.0.3.3)
118
- actionpack (= 6.0.3.3)
119
- activesupport (= 6.0.3.3)
120
- method_source
121
- rake (>= 0.8.7)
122
- thor (>= 0.20.3, < 2.0)
123
- rake (13.0.1)
124
- rspec (3.9.0)
125
- rspec-core (~> 3.9.0)
126
- rspec-expectations (~> 3.9.0)
127
- rspec-mocks (~> 3.9.0)
128
- rspec-core (3.9.2)
129
- rspec-support (~> 3.9.3)
130
- rspec-expectations (3.9.2)
131
- diff-lcs (>= 1.2.0, < 2.0)
132
- rspec-support (~> 3.9.0)
133
- rspec-mocks (3.9.1)
134
- diff-lcs (>= 1.2.0, < 2.0)
135
- rspec-support (~> 3.9.0)
136
- rspec-support (3.9.3)
137
- sprockets (4.0.2)
138
- concurrent-ruby (~> 1.0)
139
- rack (> 1, < 3)
140
- sprockets-rails (3.2.2)
141
- actionpack (>= 4.0)
142
- activesupport (>= 4.0)
143
- sprockets (>= 3.0.0)
144
- thor (1.0.1)
145
- thread_safe (0.3.6)
146
- tzinfo (1.2.7)
147
- thread_safe (~> 0.1)
148
- websocket-driver (0.7.3)
149
- websocket-extensions (>= 0.1.0)
150
- websocket-extensions (0.1.5)
151
- yard (0.9.25)
152
- zeitwerk (2.4.0)
153
-
154
- PLATFORMS
155
- ruby
156
-
157
- DEPENDENCIES
158
- pg (>= 0.18)
159
- pry (~> 0.13.1)
160
- rspec (~> 3.9.0)
161
- yard
162
- yesql!
163
-
164
- BUNDLED WITH
165
- 2.1.4