yesql 0.1.6 → 0.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop.yml +12 -0
- data/Gemfile.lock +1 -1
- data/lib/yesql.rb +2 -2
- data/lib/yesql/bindings/extract.rb +1 -0
- data/lib/yesql/errors/no_bindings_provided_error.rb +4 -2
- data/lib/yesql/query/performer.rb +8 -8
- data/lib/yesql/query/result.rb +10 -7
- data/lib/yesql/query/transform_result.rb +6 -2
- data/lib/yesql/statement.rb +49 -0
- data/lib/yesql/version.rb +1 -1
- metadata +3 -3
- data/lib/yesql/bindings/binder.rb +0 -19
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 180f75c325ecfc85f0d95e0d0a9422fcde92db273487bd19a7f03318c3206f16
|
4
|
+
data.tar.gz: '00429a2c7880d6546e82b5c530d097be14e524ed161a81d7c75b55c61f86f2b7'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6a13774afd3b568927652026d00ac25d1b521c4eebcdcf4d2f4bc9d3de86652613322d449e30f276eabfebcb2aaa044bdc66abc3757f0f759533b0fcf5c53868
|
7
|
+
data.tar.gz: 3017fb1e072485f1c3d469517c1e326d74c32f2d14873a8801c78da8d0bb1f49459a81f6abc64b9c0fa1680dd8799cd12328496ec0077fa2823e24e204f80c1a
|
data/.rubocop.yml
CHANGED
@@ -1,8 +1,20 @@
|
|
1
1
|
require: rubocop-rspec
|
2
2
|
|
3
|
+
Metrics/BlockLength:
|
4
|
+
Exclude:
|
5
|
+
- spec/**/*_spec.rb
|
6
|
+
- spec/spec_helper.rb
|
7
|
+
- spec/yesql/shared/*
|
8
|
+
|
3
9
|
RSpec/FilePath:
|
4
10
|
Exclude:
|
5
11
|
- spec/**/*_spec.rb
|
6
12
|
|
7
13
|
RSpec/NestedGroups:
|
8
14
|
Enabled: False
|
15
|
+
|
16
|
+
RSpec/ExampleLength:
|
17
|
+
Enabled: False
|
18
|
+
|
19
|
+
RSpec/BeforeAfterAll:
|
20
|
+
Enabled: False
|
data/Gemfile.lock
CHANGED
data/lib/yesql.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'yesql/statement'
|
3
4
|
require 'yesql/version'
|
4
5
|
require 'yesql/config/configuration'
|
5
6
|
require 'yesql/query/performer'
|
@@ -7,7 +8,6 @@ require 'yesql/errors/cache_expiration_error'
|
|
7
8
|
require 'yesql/errors/file_path_does_not_exist_error'
|
8
9
|
require 'yesql/errors/no_bindings_provided_error'
|
9
10
|
require 'yesql/errors/output_argument_error'
|
10
|
-
require 'yesql/bindings/binder'
|
11
11
|
|
12
12
|
module YeSQL
|
13
13
|
include ::YeSQL::Config
|
@@ -40,7 +40,7 @@ module YeSQL
|
|
40
40
|
def execute(bindings, cache, file_path, output, options)
|
41
41
|
::YeSQL::Query::Performer.new(
|
42
42
|
bindings: bindings,
|
43
|
-
bind_statement: ::YeSQL::
|
43
|
+
bind_statement: ::YeSQL::Statement.new(bindings, file_path),
|
44
44
|
cache: cache,
|
45
45
|
file_path: file_path,
|
46
46
|
output: output,
|
@@ -1,13 +1,15 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
require 'yesql/utils/read'
|
4
|
+
|
5
|
+
module ::YeSQL
|
4
6
|
module Errors
|
5
7
|
module NoBindingsProvidedError
|
6
8
|
def validate_statement_bindings(binds, file_path)
|
7
9
|
return unless statement_binds(file_path).size.positive?
|
8
10
|
|
9
11
|
format(MESSAGE, renderable_statement_binds(file_path)).tap do |message|
|
10
|
-
raise ArgumentError, message unless binds.is_a?(Hash) && !binds.empty?
|
12
|
+
raise ::ArgumentError, message unless binds.is_a?(::Hash) && !binds.empty?
|
11
13
|
end
|
12
14
|
end
|
13
15
|
|
@@ -32,9 +32,9 @@ module YeSQL
|
|
32
32
|
# rubocop:enable Metrics/ParameterLists
|
33
33
|
|
34
34
|
def call
|
35
|
-
return
|
35
|
+
return transformed_result if cache.empty?
|
36
36
|
|
37
|
-
Rails.cache.fetch(cache_key, expires_in: expires_in) {
|
37
|
+
Rails.cache.fetch(cache_key, expires_in: expires_in) { transformed_result }
|
38
38
|
end
|
39
39
|
|
40
40
|
private
|
@@ -49,8 +49,8 @@ module YeSQL
|
|
49
49
|
:prepare,
|
50
50
|
:rows
|
51
51
|
|
52
|
-
def
|
53
|
-
@
|
52
|
+
def transformed_result
|
53
|
+
@transformed_result ||=
|
54
54
|
::YeSQL::Query::TransformResult.new(output: output, result: query_result).call
|
55
55
|
end
|
56
56
|
|
@@ -61,13 +61,13 @@ module YeSQL
|
|
61
61
|
prepare: prepare).call
|
62
62
|
end
|
63
63
|
|
64
|
-
def extractor
|
65
|
-
::YeSQL::Bindings::Extractor.new(bindings: named_bindings).call
|
66
|
-
end
|
67
|
-
|
68
64
|
def binds
|
69
65
|
::YeSQL::Bindings::Transformed.new(statement_binds: statement_binds(extractor)).call
|
70
66
|
end
|
67
|
+
|
68
|
+
def extractor
|
69
|
+
::YeSQL::Bindings::Extractor.new(bindings: named_bindings).call
|
70
|
+
end
|
71
71
|
end
|
72
72
|
end
|
73
73
|
end
|
data/lib/yesql/query/result.rb
CHANGED
@@ -11,32 +11,35 @@ module ::YeSQL
|
|
11
11
|
|
12
12
|
include ::YeSQL::Common::Adapter
|
13
13
|
|
14
|
-
def initialize(binds
|
14
|
+
def initialize(binds: [], bind_statement:, file_path:, prepare:)
|
15
15
|
@binds = binds
|
16
16
|
@bind_statement = bind_statement
|
17
|
+
@connection = ActiveRecord::Base.connection
|
17
18
|
@file_path = file_path
|
18
19
|
@prepare_option = prepare
|
19
20
|
end
|
20
21
|
|
21
22
|
def call
|
23
|
+
return view_result if view?
|
22
24
|
return rails5_result if ::Rails::VERSION::MAJOR == 5 && mysql?
|
23
25
|
|
24
|
-
exec_query(
|
26
|
+
exec_query(bound, file_path, binds, prepare: prepare_option)
|
25
27
|
end
|
26
28
|
|
27
29
|
private
|
28
30
|
|
29
|
-
attr_reader :binds, :bind_statement, :file_path, :prepare_option
|
31
|
+
attr_reader :binds, :bind_statement, :connection, :file_path, :prepare_option
|
30
32
|
|
31
|
-
def_delegators(:
|
33
|
+
def_delegators(:bind_statement, :bound, :to_s, :view?)
|
34
|
+
def_delegators(:connection, :exec_query, :raw_connection)
|
32
35
|
def_delegators(:raw_connection, :prepare)
|
33
36
|
|
34
|
-
def
|
35
|
-
|
37
|
+
def view_result
|
38
|
+
exec_query(bound)
|
36
39
|
end
|
37
40
|
|
38
41
|
def rails5_result
|
39
|
-
prepare(
|
42
|
+
prepare(bound).execute(*binds)
|
40
43
|
end
|
41
44
|
end
|
42
45
|
end
|
@@ -1,6 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'yesql'
|
4
|
+
require 'yesql/common/adapter'
|
4
5
|
require 'forwardable'
|
5
6
|
|
6
7
|
module ::YeSQL
|
@@ -8,20 +9,23 @@ module ::YeSQL
|
|
8
9
|
class TransformResult
|
9
10
|
extend Forwardable
|
10
11
|
|
12
|
+
include ::YeSQL::Common::Adapter
|
13
|
+
|
11
14
|
def initialize(output:, result:)
|
12
15
|
@output = output
|
13
16
|
@result = result
|
14
17
|
end
|
15
18
|
|
16
19
|
def call
|
17
|
-
if ::Rails::VERSION::MAJOR == 5
|
20
|
+
if ::Rails::VERSION::MAJOR == 5 && mysql?
|
18
21
|
return columns if columns?
|
19
22
|
return rows_values if rows?
|
23
|
+
return array_of_symbol_hashes if hash?
|
20
24
|
end
|
21
25
|
|
22
26
|
return result.public_send(output.to_sym) if columns? || rows?
|
23
27
|
|
24
|
-
|
28
|
+
to_a.map(&:symbolize_keys)
|
25
29
|
end
|
26
30
|
|
27
31
|
private
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'forwardable'
|
4
|
+
|
5
|
+
require 'yesql/utils/read'
|
6
|
+
require 'yesql/bindings/extractor'
|
7
|
+
require 'yesql/common/adapter'
|
8
|
+
|
9
|
+
module ::YeSQL
|
10
|
+
class Statement
|
11
|
+
extend Forwardable
|
12
|
+
|
13
|
+
include ::YeSQL::Common::Adapter
|
14
|
+
|
15
|
+
def initialize(bindings = {}, file_path)
|
16
|
+
@bindings = bindings
|
17
|
+
@connection = ::ActiveRecord::Base.connection
|
18
|
+
@file_path = file_path
|
19
|
+
end
|
20
|
+
|
21
|
+
def bound
|
22
|
+
to_s.gsub(::YeSQL::BIND_REGEX) do |match|
|
23
|
+
extractor[match[/(\w+)/].to_sym].tap do |extract|
|
24
|
+
break quote(extract[:value]) if view?
|
25
|
+
|
26
|
+
break extract[:bind][:vars]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def to_s
|
32
|
+
@to_s ||= ::YeSQL::Utils::Read.statement(file_path, readable: true)
|
33
|
+
end
|
34
|
+
|
35
|
+
def view?
|
36
|
+
to_s =~ /^create\s.*view\s/i
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def_delegator(:connection, :quote)
|
42
|
+
|
43
|
+
attr_reader :bindings, :connection, :file_path
|
44
|
+
|
45
|
+
def extractor
|
46
|
+
::YeSQL::Bindings::Extractor.new(bindings: bindings).call
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
data/lib/yesql/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yesql
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sebastián Palma
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-11-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -97,7 +97,6 @@ files:
|
|
97
97
|
- bin/console
|
98
98
|
- bin/setup
|
99
99
|
- lib/yesql.rb
|
100
|
-
- lib/yesql/bindings/binder.rb
|
101
100
|
- lib/yesql/bindings/extract.rb
|
102
101
|
- lib/yesql/bindings/extractor.rb
|
103
102
|
- lib/yesql/bindings/transformed.rb
|
@@ -112,6 +111,7 @@ files:
|
|
112
111
|
- lib/yesql/query/performer.rb
|
113
112
|
- lib/yesql/query/result.rb
|
114
113
|
- lib/yesql/query/transform_result.rb
|
114
|
+
- lib/yesql/statement.rb
|
115
115
|
- lib/yesql/utils/read.rb
|
116
116
|
- lib/yesql/version.rb
|
117
117
|
- yesql.gemspec
|
@@ -1,19 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'yesql/utils/read'
|
4
|
-
require 'yesql/bindings/extractor'
|
5
|
-
|
6
|
-
module YeSQL
|
7
|
-
module Bindings
|
8
|
-
class Binder
|
9
|
-
def self.bind_statement(file_path, bindings)
|
10
|
-
::YeSQL::Bindings::Extractor.new(bindings: bindings).call.tap do |extractor|
|
11
|
-
break ::YeSQL::Utils::Read.statement(file_path, readable: true)
|
12
|
-
.gsub(::YeSQL::BIND_REGEX) do |match|
|
13
|
-
extractor[match[/(\w+)/].to_sym][:bind][:vars]
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|