yesql 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +7 -0
  3. data/.rubocop.yml +20 -0
  4. data/Gemfile +9 -0
  5. data/Gemfile.lock +63 -0
  6. data/Rakefile +3 -0
  7. data/bin/console +14 -0
  8. data/bin/setup +8 -0
  9. data/lib/yesql.rb +15 -17
  10. data/lib/yesql/bindings/extract.rb +17 -11
  11. data/lib/yesql/bindings/extractor.rb +18 -20
  12. data/lib/yesql/bindings/transformed.rb +7 -5
  13. data/lib/yesql/bindings/utils.rb +6 -7
  14. data/lib/yesql/common/adapter.rb +11 -4
  15. data/lib/yesql/config/configuration.rb +3 -1
  16. data/lib/yesql/errors/file_path_does_not_exist_error.rb +17 -22
  17. data/lib/yesql/errors/no_bindings_provided_error.rb +18 -17
  18. data/lib/yesql/errors/output_argument_error.rb +4 -13
  19. data/lib/yesql/params/output.rb +12 -4
  20. data/lib/yesql/query/performer.rb +8 -4
  21. data/lib/yesql/query/result.rb +11 -5
  22. data/lib/yesql/query/transform_result.rb +19 -8
  23. data/lib/yesql/statement.rb +16 -10
  24. data/lib/yesql/utils/read.rb +8 -11
  25. data/lib/yesql/version.rb +1 -1
  26. data/yesql.gemspec +28 -0
  27. metadata +9 -54
  28. data/lib/.rbnext/2.3/yesql/errors/file_path_does_not_exist_error.rb +0 -40
  29. data/lib/.rbnext/2.3/yesql/errors/no_bindings_provided_error.rb +0 -38
  30. data/lib/.rbnext/2.3/yesql/errors/output_argument_error.rb +0 -31
  31. data/lib/.rbnext/2.3/yesql/query/transform_result.rb +0 -48
  32. data/lib/.rbnext/2.7/yesql/bindings/extract.rb +0 -71
  33. data/lib/.rbnext/2.7/yesql/bindings/extractor.rb +0 -38
  34. data/lib/.rbnext/2.7/yesql/bindings/transformed.rb +0 -58
  35. data/lib/.rbnext/2.7/yesql/bindings/utils.rb +0 -16
  36. data/lib/.rbnext/2.7/yesql/errors/file_path_does_not_exist_error.rb +0 -43
  37. data/lib/.rbnext/2.7/yesql/errors/no_bindings_provided_error.rb +0 -41
  38. data/lib/.rbnext/2.7/yesql/query/transform_result.rb +0 -48
  39. data/lib/.rbnext/2.7/yesql/statement.rb +0 -44
  40. data/lib/.rbnext/2.7/yesql/utils/read.rb +0 -20
  41. data/lib/.rbnext/3.0/yesql/bindings/extract.rb +0 -71
  42. data/lib/.rbnext/3.0/yesql/bindings/transformed.rb +0 -58
  43. data/lib/.rbnext/3.0/yesql/common/adapter.rb +0 -18
  44. data/lib/.rbnext/3.0/yesql/config/configuration.rb +0 -32
  45. data/lib/.rbnext/3.0/yesql/errors/file_path_does_not_exist_error.rb +0 -43
  46. data/lib/.rbnext/3.0/yesql/errors/no_bindings_provided_error.rb +0 -41
  47. data/lib/.rbnext/3.0/yesql/params/output.rb +0 -26
  48. data/lib/.rbnext/3.0/yesql/query/performer.rb +0 -44
  49. data/lib/.rbnext/3.0/yesql/query/result.rb +0 -41
  50. data/lib/.rbnext/3.0/yesql/query/transform_result.rb +0 -48
  51. data/lib/.rbnext/3.0/yesql/statement.rb +0 -44
  52. data/lib/.rbnext/3.0/yesql/utils/read.rb +0 -20
@@ -4,25 +4,17 @@ require "yesql/utils/read"
4
4
 
5
5
  module ::YeSQL
6
6
  module Errors
7
- class NoBindingsProvidedError
8
- include ::YeSQL::Utils::Read
7
+ module NoBindingsProvidedError
8
+ def validate_statement_bindings(binds, file_path)
9
+ return unless statement_binds(file_path).size.positive?
9
10
 
10
- def initialize(binds, file_path)
11
- @binds = binds
12
- @file_path = file_path
13
- end
14
-
15
- def validate_statement_bindings
16
- return unless statement_binds.size.positive?
17
- return if expected_binds?
18
-
19
- raise ::ArgumentError, format(MESSAGE, renderable_statement_binds)
11
+ format(MESSAGE, renderable_statement_binds(file_path)).tap do |message|
12
+ raise ::ArgumentError, message unless binds.is_a?(::Hash) && !binds.empty?
13
+ end
20
14
  end
21
15
 
22
16
  private
23
17
 
24
- attr_reader :binds, :file_path
25
-
26
18
  MESSAGE = <<~MSG
27
19
 
28
20
  YeSQL invoked without bindings.
@@ -33,9 +25,18 @@ module ::YeSQL
33
25
  MSG
34
26
  private_constant :MESSAGE
35
27
 
36
- def statement_binds = statement.scan(::YeSQL::BIND_REGEX).tap { break (_1 || []).sort }
37
- def renderable_statement_binds = statement_binds.flatten.map { "- `#{_1}`\n" }.join
38
- def expected_binds? = binds.is_a?(::Hash) && !binds.empty?
28
+ def statement_binds(file_path)
29
+ ::YeSQL::Utils::Read.statement(file_path)
30
+ .scan(::YeSQL::BIND_REGEX).tap do |scanned_binds|
31
+ break [] if scanned_binds.size.zero?
32
+
33
+ break scanned_binds.sort
34
+ end
35
+ end
36
+
37
+ def renderable_statement_binds(file_path)
38
+ statement_binds(file_path).flatten.map { |bind| "- `#{bind}`\n" }.join
39
+ end
39
40
  end
40
41
  end
41
42
  end
@@ -1,23 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module ::YeSQL
3
+ module YeSQL
4
4
  module Errors
5
- class OutputArgumentError
6
- def initialize(output)
7
- @output = output
8
- end
9
-
10
- def validate_output_options
5
+ module OutputArgumentError
6
+ def validate_output_options(output)
11
7
  return if output.nil?
12
- return if OPTIONS.include?(output.to_sym)
13
8
 
14
- raise ArgumentError, format(MESSAGE, output)
9
+ raise ArgumentError, format(MESSAGE, output) unless OPTIONS.include?(output.to_sym)
15
10
  end
16
11
 
17
- private
18
-
19
- attr_reader :output
20
-
21
12
  MESSAGE = <<~MSG
22
13
  Unsupported `output` option given `%s`. Possible values are:
23
14
  - `columns`: returns an array with the columns from the result.
@@ -6,15 +6,23 @@ require "forwardable"
6
6
  module ::YeSQL
7
7
  module Params
8
8
  class Output
9
- extend ::Forwardable
9
+ extend Forwardable
10
10
 
11
11
  def initialize(output)
12
12
  @output = output.to_s
13
13
  end
14
14
 
15
- def columns? = output == "columns"
16
- def rows? = output == "rows"
17
- def hash? = output == "hash"
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
18
26
 
19
27
  def_delegator(:output, :to_sym)
20
28
 
@@ -8,7 +8,7 @@ require "yesql/query/result"
8
8
  require "yesql/query/transform_result"
9
9
  require "yesql/params/output"
10
10
 
11
- module ::YeSQL
11
+ module YeSQL
12
12
  module Query
13
13
  class Performer
14
14
  include ::YeSQL::Bindings::Utils
@@ -21,11 +21,13 @@ module ::YeSQL
21
21
  @prepare = prepare
22
22
  end
23
23
 
24
- def call = ::YeSQL::Query::TransformResult.new(output: output, result: query_result).call
24
+ def call
25
+ ::YeSQL::Query::TransformResult.new(output: output, result: query_result).call
26
+ end
25
27
 
26
28
  private
27
29
 
28
- attr_reader :bind_statement, :file_path, :named_bindings, :output, :prepare
30
+ attr_reader :bind_statement, :file_path, :named_bindings, :output, :prepare, :rows
29
31
 
30
32
  def query_result
31
33
  @query_result ||= ::YeSQL::Query::Result.new(binds: binds,
@@ -38,7 +40,9 @@ module ::YeSQL
38
40
  ::YeSQL::Bindings::Transformed.new(statement_binds: statement_binds(extractor)).call
39
41
  end
40
42
 
41
- def extractor = ::YeSQL::Bindings::Extractor.new(bindings: named_bindings).call
43
+ def extractor
44
+ ::YeSQL::Bindings::Extractor.new(bindings: named_bindings).call
45
+ end
42
46
  end
43
47
  end
44
48
  end
@@ -7,13 +7,14 @@ require "yesql/common/adapter"
7
7
  module ::YeSQL
8
8
  module Query
9
9
  class Result
10
- extend ::Forwardable
10
+ extend Forwardable
11
11
 
12
12
  include ::YeSQL::Common::Adapter
13
13
 
14
14
  def initialize(bind_statement:, file_path:, prepare:, binds: [])
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
@@ -27,15 +28,20 @@ module ::YeSQL
27
28
 
28
29
  private
29
30
 
30
- attr_reader :binds, :bind_statement, :file_path, :prepare_option
31
+ attr_reader :binds, :bind_statement, :connection, :file_path, :prepare_option
31
32
 
32
33
  def_delegators(:bind_statement, :bound, :to_s, :view?)
33
34
  def_delegators(:connection, :exec_query, :raw_connection)
34
35
  def_delegators(:raw_connection, :prepare)
35
36
 
36
- def view_result = exec_query(bound)
37
- def rails5_result = prepare(bound).execute(*binds)
38
- def connection = @connection ||= ActiveRecord::Base.connection
37
+ def view_result
38
+ exec_query(bound)
39
+ end
40
+
41
+ # TODO: recheck this case
42
+ def rails5_result
43
+ prepare(bound).execute(*binds)
44
+ end
39
45
  end
40
46
  end
41
47
  end
@@ -7,7 +7,7 @@ require "forwardable"
7
7
  module ::YeSQL
8
8
  module Query
9
9
  class TransformResult
10
- extend ::Forwardable
10
+ extend Forwardable
11
11
 
12
12
  include ::YeSQL::Common::Adapter
13
13
 
@@ -17,7 +17,7 @@ module ::YeSQL
17
17
  end
18
18
 
19
19
  def call
20
- if rails_5? && mysql?
20
+ if ::ActiveRecord::VERSION::MAJOR == 5 && mysql?
21
21
  return columns if columns?
22
22
  return rows_values if rows?
23
23
  return array_of_symbol_hashes if hash?
@@ -32,17 +32,28 @@ module ::YeSQL
32
32
 
33
33
  attr_reader :output, :result
34
34
 
35
- def_delegators(:result, :fields, :rows, :to_a)
35
+ def_delegators(:result, :rows, :to_a)
36
36
  def_delegators(:output, :columns?, :hash?, :rows?)
37
37
 
38
+ def rows_values
39
+ to_a.map { |e| e.respond_to?(:values) ? e.values : e }
40
+ end
41
+
38
42
  def array_of_symbol_hashes
39
- to_a.tap { break hashed_rows(_1) if rails_5? }.map { _1&.symbolize_keys || _1 }
43
+ to_a
44
+ .tap { |rows| break hashed_rows(rows) if ::ActiveRecord::VERSION::MAJOR == 5 }
45
+ .map { |e| e.respond_to?(:symbolize_keys) ? e.symbolize_keys : e }
46
+ end
47
+
48
+ def hashed_rows(rows)
49
+ rows.map { |row| columns.zip(row).to_h }
40
50
  end
41
51
 
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
52
+ def columns
53
+ return result.fields if result.respond_to?(:fields)
54
+
55
+ result.columns
56
+ end
46
57
  end
47
58
  end
48
59
  end
@@ -13,7 +13,6 @@ module ::YeSQL
13
13
  extend ::Forwardable
14
14
 
15
15
  include ::YeSQL::Common::Adapter
16
- include ::YeSQL::Utils::Read
17
16
  # Give access to the quote method.
18
17
  include ::ActiveRecord::ConnectionAdapters::Quoting
19
18
 
@@ -23,22 +22,29 @@ module ::YeSQL
23
22
  end
24
23
 
25
24
  def bound
26
- to_s.gsub(::YeSQL::BIND_REGEX) { extract_bind_values(extractor[_1[/(\w+)/].to_sym]) }
25
+ to_s.gsub(::YeSQL::BIND_REGEX) do |match|
26
+ extractor[match[/(\w+)/].to_sym].tap do |extract|
27
+ break quote(extract[:value]) if view?
28
+
29
+ break extract[:bind][:vars]
30
+ end
31
+ end
32
+ end
33
+
34
+ def to_s
35
+ @to_s ||= ::YeSQL::Utils::Read.statement(file_path, readable: true)
27
36
  end
28
37
 
29
- def to_s = @to_s ||= statement(readable: true)
30
- def view? = to_s =~ /^create\s.*view\s/i
38
+ def view?
39
+ to_s =~ /^create\s.*view\s/i
40
+ end
31
41
 
32
42
  private
33
43
 
34
44
  attr_reader :bindings, :file_path
35
45
 
36
- def extract_bind_values(match)
37
- return quote(match[:value]) if view?
38
-
39
- match[:bind][:vars]
46
+ def extractor
47
+ ::YeSQL::Bindings::Extractor.new(bindings: bindings).call
40
48
  end
41
-
42
- def extractor = @extractor ||= ::YeSQL::Bindings::Extractor.new(bindings: bindings).call
43
49
  end
44
50
  end
@@ -1,20 +1,17 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module ::YeSQL
3
+ module YeSQL
4
4
  module Utils
5
5
  module Read
6
- def statement(readable: false) = read_file(found_file, readable)
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
7
11
 
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)
12
+ break File.read(sql_file_path)
13
+ end
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"]
18
15
  end
19
16
  end
20
17
  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.2.1"
4
+ VERSION = "0.2.2"
5
5
  end
data/yesql.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/yesql/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "yesql"
7
+ spec.version = YeSQL::VERSION
8
+ spec.authors = ["Sebastián Palma"]
9
+ spec.email = ["vnhnhm.github@gmail.com"]
10
+ spec.summary = "Ruby library to use SQL"
11
+ spec.description = 'SQL "raw" for Rails projects'
12
+ spec.homepage = "https://github.com/sebastian-palma/yesql"
13
+ spec.license = "MIT"
14
+ spec.metadata["homepage_uri"] = spec.homepage
15
+ spec.metadata["source_code_uri"] = "https://github.com/sebastian-palma/yesql"
16
+ spec.metadata["changelog_uri"] = "https://github.com/sebastian-palma/yesql"
17
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
18
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+ spec.add_dependency "activerecord", ">= 4.0.0.beta1"
24
+ spec.add_development_dependency "mysql2", "~> 0.5.3"
25
+ spec.add_development_dependency "pg", ">= 0.18"
26
+ spec.add_development_dependency "pry", "~> 0.13.1"
27
+ spec.add_development_dependency "rspec", "~> 3.9.0"
28
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yesql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sebastián Palma
@@ -24,34 +24,6 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
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: backports
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - "~>"
46
- - !ruby/object:Gem::Version
47
- version: 3.21.0
48
- type: :runtime
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - "~>"
53
- - !ruby/object:Gem::Version
54
- version: 3.21.0
55
27
  - !ruby/object:Gem::Dependency
56
28
  name: mysql2
57
29
  requirement: !ruby/object:Gem::Requirement
@@ -115,33 +87,15 @@ executables: []
115
87
  extensions: []
116
88
  extra_rdoc_files: []
117
89
  files:
90
+ - ".gitignore"
91
+ - ".rubocop.yml"
92
+ - Gemfile
93
+ - Gemfile.lock
118
94
  - LICENSE.txt
119
95
  - README.md
120
- - lib/.rbnext/2.3/yesql/errors/file_path_does_not_exist_error.rb
121
- - lib/.rbnext/2.3/yesql/errors/no_bindings_provided_error.rb
122
- - lib/.rbnext/2.3/yesql/errors/output_argument_error.rb
123
- - lib/.rbnext/2.3/yesql/query/transform_result.rb
124
- - lib/.rbnext/2.7/yesql/bindings/extract.rb
125
- - lib/.rbnext/2.7/yesql/bindings/extractor.rb
126
- - lib/.rbnext/2.7/yesql/bindings/transformed.rb
127
- - lib/.rbnext/2.7/yesql/bindings/utils.rb
128
- - lib/.rbnext/2.7/yesql/errors/file_path_does_not_exist_error.rb
129
- - lib/.rbnext/2.7/yesql/errors/no_bindings_provided_error.rb
130
- - lib/.rbnext/2.7/yesql/query/transform_result.rb
131
- - lib/.rbnext/2.7/yesql/statement.rb
132
- - lib/.rbnext/2.7/yesql/utils/read.rb
133
- - lib/.rbnext/3.0/yesql/bindings/extract.rb
134
- - lib/.rbnext/3.0/yesql/bindings/transformed.rb
135
- - lib/.rbnext/3.0/yesql/common/adapter.rb
136
- - lib/.rbnext/3.0/yesql/config/configuration.rb
137
- - lib/.rbnext/3.0/yesql/errors/file_path_does_not_exist_error.rb
138
- - lib/.rbnext/3.0/yesql/errors/no_bindings_provided_error.rb
139
- - lib/.rbnext/3.0/yesql/params/output.rb
140
- - lib/.rbnext/3.0/yesql/query/performer.rb
141
- - lib/.rbnext/3.0/yesql/query/result.rb
142
- - lib/.rbnext/3.0/yesql/query/transform_result.rb
143
- - lib/.rbnext/3.0/yesql/statement.rb
144
- - lib/.rbnext/3.0/yesql/utils/read.rb
96
+ - Rakefile
97
+ - bin/console
98
+ - bin/setup
145
99
  - lib/yesql.rb
146
100
  - lib/yesql/bindings/extract.rb
147
101
  - lib/yesql/bindings/extractor.rb
@@ -159,6 +113,7 @@ files:
159
113
  - lib/yesql/statement.rb
160
114
  - lib/yesql/utils/read.rb
161
115
  - lib/yesql/version.rb
116
+ - yesql.gemspec
162
117
  homepage: https://github.com/sebastian-palma/yesql
163
118
  licenses:
164
119
  - MIT