yesql 0.1.5 → 0.1.6

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d85f1a89f06d3fa6d3a35471d895bce7860ed93988fa91d1f08149d349782e79
4
- data.tar.gz: 4f6a7d1486495fe6291bddcfaa7d2fadbb051a79da98ba81c2e2a9958f3046ee
3
+ metadata.gz: 8f432f23ac16148c7439ec096a55c142c9985c537d06ef5ded891fb3178d3bbf
4
+ data.tar.gz: b5d82c03933a0401d4b24a3fd48f00ea3864adfb8566db7f73c518714784cc54
5
5
  SHA512:
6
- metadata.gz: 0faf664927b4ed1c605f33a79f6e65b93b2bddd2bfea6c4f71cdeec8b9ae425c17111363ce95a74c715b468d89fb7e68b3bdf3041c3feaf76fe366556bb6592f
7
- data.tar.gz: a1e04806de2b8b367c95f0167032a664315a8c1de57636855b430f702c84b1884432895f6393742e273c65b3dd9d54b9e35ac2e4e4c5ae59ec637caeac64509e
6
+ metadata.gz: '084397cf83aa10faf2d724a0be1c11548e36dd515932c4552498c130a96efb044ca5f498aa30d4ec9c002ea1f36c4ec3699bcd4e6e2db2313b0b56fab9da2ae5'
7
+ data.tar.gz: 70a91ebc63c23cd5c653351e728fc9c93207fd2583b1b72629d63717ba8e105c86318b016110f3040fd681b176da4d80db2807c40b4859ac3192c1a160901572
data/.gitignore CHANGED
@@ -1 +1,4 @@
1
- spec/minimalpg/log/test.log
1
+ spec/minimalpg/log/*
2
+ spec/minimalmysql/log/*
3
+ spec/minimalpg/db/*
4
+ spec/minimalmysql/db/*
@@ -0,0 +1,8 @@
1
+ require: rubocop-rspec
2
+
3
+ RSpec/FilePath:
4
+ Exclude:
5
+ - spec/**/*_spec.rb
6
+
7
+ RSpec/NestedGroups:
8
+ Enabled: False
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- yesql (0.1.4)
4
+ yesql (0.1.5)
5
5
  rails (>= 5.0)
6
6
 
7
7
  GEM
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'yesql'
4
+ require 'yesql/common/adapter'
5
+
6
+ module ::YeSQL
7
+ module Bindings
8
+ class Transformed
9
+ include ::YeSQL::Common::Adapter
10
+
11
+ def initialize(statement_binds:)
12
+ @statement_binds = statement_binds
13
+ end
14
+
15
+ def call
16
+ return mysql_rails5_binds if rails5? && mysql?
17
+ return mysql_binds if !rails5? && mysql?
18
+
19
+ pg_binds
20
+ end
21
+
22
+ private
23
+
24
+ attr_reader :statement_binds
25
+
26
+ def rails5?
27
+ ::Rails::VERSION::MAJOR == 5
28
+ end
29
+
30
+ def mysql_rails5_binds
31
+ statement_binds
32
+ .map(&:first)
33
+ .flatten(1)
34
+ .each_slice(2)
35
+ .flat_map do |first, last|
36
+ next [first, last].map(&:last) if first.is_a?(Array)
37
+
38
+ last
39
+ end
40
+ end
41
+
42
+ def mysql_binds
43
+ statement_binds
44
+ .map(&:first)
45
+ .flatten
46
+ .each_slice(2)
47
+ .to_a
48
+ end
49
+
50
+ def pg_binds
51
+ statement_binds
52
+ .sort_by { |_, position| position.to_s.tr('$', '').to_i }
53
+ .uniq
54
+ .map(&:first)
55
+ .flatten
56
+ .each_slice(2)
57
+ .to_a
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'yesql'
4
+ require 'forwardable'
5
+
6
+ module ::YeSQL
7
+ module Params
8
+ class Output
9
+ extend Forwardable
10
+
11
+ def initialize(output)
12
+ @output = output.to_s
13
+ end
14
+
15
+ def columns?
16
+ output == 'columns'
17
+ end
18
+
19
+ def rows?
20
+ output == 'rows'
21
+ end
22
+
23
+ def hash?
24
+ output == 'hash'
25
+ end
26
+
27
+ def_delegator(:output, :to_sym)
28
+
29
+ private
30
+
31
+ attr_reader :output
32
+ end
33
+ end
34
+ end
@@ -1,17 +1,17 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'yesql'
4
- require 'forwardable'
5
4
  require 'yesql/bindings/utils'
6
5
  require 'yesql/common/adapter'
6
+ require 'yesql/bindings/transformed'
7
+ require 'yesql/query/result'
8
+ require 'yesql/query/transform_result'
9
+ require 'yesql/params/output'
7
10
 
8
11
  module YeSQL
9
12
  module Query
10
13
  class Performer
11
- extend Forwardable
12
-
13
14
  include ::YeSQL::Bindings::Utils
14
- include ::YeSQL::Common::Adapter
15
15
 
16
16
  # rubocop:disable Metrics/ParameterLists
17
17
  def initialize(bind_statement:,
@@ -23,11 +23,10 @@ module YeSQL
23
23
  @bind_statement = bind_statement
24
24
  @cache = cache
25
25
  @cache_key = cache[:key] || file_path
26
- @connection = ActiveRecord::Base.connection
27
26
  @expires_in = cache[:expires_in]
28
27
  @file_path = file_path
29
28
  @named_bindings = bindings.transform_keys(&:to_sym)
30
- @output = output
29
+ @output = ::YeSQL::Params::Output.new(output)
31
30
  @prepare = prepare
32
31
  end
33
32
  # rubocop:enable Metrics/ParameterLists
@@ -43,7 +42,6 @@ module YeSQL
43
42
  attr_reader :bind_statement,
44
43
  :cache,
45
44
  :cache_key,
46
- :connection,
47
45
  :expires_in,
48
46
  :file_path,
49
47
  :named_bindings,
@@ -51,37 +49,24 @@ module YeSQL
51
49
  :prepare,
52
50
  :rows
53
51
 
54
- def_delegator(:query_result, :columns)
55
- private :columns
56
- def_delegator(:query_result, :rows)
57
- private :rows
58
-
59
52
  def modified_output
60
53
  @modified_output ||=
61
- begin
62
- return query_result.public_send(output) if %w[columns rows].include?(output.to_s)
63
-
64
- columns.map(&:to_sym).tap { |cols| break rows.map { |row| cols.zip(row).to_h } }
65
- end
54
+ ::YeSQL::Query::TransformResult.new(output: output, result: query_result).call
66
55
  end
67
56
 
68
57
  def query_result
69
- @query_result ||= connection.exec_query(bind_statement, file_path, binds, prepare: prepare)
58
+ @query_result ||= ::YeSQL::Query::Result.new(binds: binds,
59
+ bind_statement: bind_statement,
60
+ file_path: file_path,
61
+ prepare: prepare).call
70
62
  end
71
63
 
72
64
  def extractor
73
65
  ::YeSQL::Bindings::Extractor.new(bindings: named_bindings).call
74
66
  end
75
67
 
76
- # TODO: move this somewhere else.
77
68
  def binds
78
- statement_binds(extractor)
79
- .sort_by { |_, position| position.to_s.tr('$', '').to_i }
80
- .tap { |x| break(mysql? ? x : x.uniq) }
81
- .map(&:first)
82
- .flatten
83
- .each_slice(2)
84
- .to_a
69
+ ::YeSQL::Bindings::Transformed.new(statement_binds: statement_binds(extractor)).call
85
70
  end
86
71
  end
87
72
  end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'yesql'
4
+ require 'forwardable'
5
+ require 'yesql/common/adapter'
6
+
7
+ module ::YeSQL
8
+ module Query
9
+ class Result
10
+ extend Forwardable
11
+
12
+ include ::YeSQL::Common::Adapter
13
+
14
+ def initialize(binds:, bind_statement:, file_path:, prepare:)
15
+ @binds = binds
16
+ @bind_statement = bind_statement
17
+ @file_path = file_path
18
+ @prepare_option = prepare
19
+ end
20
+
21
+ def call
22
+ return rails5_result if ::Rails::VERSION::MAJOR == 5 && mysql?
23
+
24
+ exec_query(bind_statement, file_path, binds, prepare: prepare_option)
25
+ end
26
+
27
+ private
28
+
29
+ attr_reader :binds, :bind_statement, :file_path, :prepare_option
30
+
31
+ def_delegators(:connection, :exec_query, :raw_connection)
32
+ def_delegators(:raw_connection, :prepare)
33
+
34
+ def connection
35
+ ActiveRecord::Base.connection
36
+ end
37
+
38
+ def rails5_result
39
+ prepare(bind_statement).execute(*binds)
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'yesql'
4
+ require 'forwardable'
5
+
6
+ module ::YeSQL
7
+ module Query
8
+ class TransformResult
9
+ extend Forwardable
10
+
11
+ def initialize(output:, result:)
12
+ @output = output
13
+ @result = result
14
+ end
15
+
16
+ def call
17
+ if ::Rails::VERSION::MAJOR == 5
18
+ return columns if columns?
19
+ return rows_values if rows?
20
+ end
21
+
22
+ return result.public_send(output.to_sym) if columns? || rows?
23
+
24
+ array_of_symbol_hashes
25
+ end
26
+
27
+ private
28
+
29
+ attr_reader :output, :result
30
+
31
+ def_delegators(:result, :rows, :to_a)
32
+ def_delegators(:output, :columns?, :hash?, :rows?)
33
+
34
+ def rows_values
35
+ to_a.map { |e| e.respond_to?(:values) ? e.values : e }
36
+ end
37
+
38
+ def array_of_symbol_hashes
39
+ to_a.tap { |rows| break hashed_rows(rows) if ::Rails::VERSION::MAJOR == 5 }
40
+ .map { |e| e.respond_to?(:symbolize_keys) ? e.symbolize_keys : e }
41
+ end
42
+
43
+ def hashed_rows(rows)
44
+ rows.map { |row| columns.zip(row).to_h }
45
+ end
46
+
47
+ def columns
48
+ return result.fields if result.respond_to?(:fields)
49
+
50
+ result.columns
51
+ end
52
+ end
53
+ end
54
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module YeSQL
4
- VERSION = '0.1.5'
4
+ VERSION = '0.1.6'
5
5
  end
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.5
4
+ version: 0.1.6
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-10-24 00:00:00.000000000 Z
11
+ date: 2020-10-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -88,6 +88,7 @@ extensions: []
88
88
  extra_rdoc_files: []
89
89
  files:
90
90
  - ".gitignore"
91
+ - ".rubocop.yml"
91
92
  - Gemfile
92
93
  - Gemfile.lock
93
94
  - LICENSE.txt
@@ -99,6 +100,7 @@ files:
99
100
  - lib/yesql/bindings/binder.rb
100
101
  - lib/yesql/bindings/extract.rb
101
102
  - lib/yesql/bindings/extractor.rb
103
+ - lib/yesql/bindings/transformed.rb
102
104
  - lib/yesql/bindings/utils.rb
103
105
  - lib/yesql/common/adapter.rb
104
106
  - lib/yesql/config/configuration.rb
@@ -106,7 +108,10 @@ files:
106
108
  - lib/yesql/errors/file_path_does_not_exist_error.rb
107
109
  - lib/yesql/errors/no_bindings_provided_error.rb
108
110
  - lib/yesql/errors/output_argument_error.rb
111
+ - lib/yesql/params/output.rb
109
112
  - lib/yesql/query/performer.rb
113
+ - lib/yesql/query/result.rb
114
+ - lib/yesql/query/transform_result.rb
110
115
  - lib/yesql/utils/read.rb
111
116
  - lib/yesql/version.rb
112
117
  - yesql.gemspec