querier 0.4.1 → 0.4.4

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: 40c24ee7c142f7bf047c967cdbee1e703a7c41ee6aafda911c36e02008642b6f
4
- data.tar.gz: d4203cdcc429107b363c917ec493dc30c25e730fcef9cbf7a76f9653e533eb36
3
+ metadata.gz: 17df356b959c87427cc72225d58d40a38a0e7ad8ba7c0f4f15ceab9c603176eb
4
+ data.tar.gz: 0a6286066f90301cd9cf433ad118afe10ab49015fb839a5a7a38233f3821f48d
5
5
  SHA512:
6
- metadata.gz: 5400b60fbc5e6c9bcaec3cc844ed919f855557e8e76b78c6eede62da5700a040beeac4b2fae880fd92a5bdf068cf5052e255d5182306ba411d8fd8b3e6a925ca
7
- data.tar.gz: f7304d4dfaf39b60845e1931e2040f4bafa84a745158d4ea03f08459bf48e0f4eec72a28b255c2d6537c4e31da1279fd91c2240f875eaba03c11172ba1c6b1c2
6
+ metadata.gz: 92d410ee75923192069927f59609ba491424f672475eda7cc96d333e6ef0ef7f2f1fcab485c4c7463d2e216ce95b660f6a598abdc4591c5a84980395e02a5cef
7
+ data.tar.gz: 5e1a04e6e19141b75cae7c9c1ac547b0e0b0bc07d61c0f19800619480743be123d60f218621bf9d6b833f54cb0d1b9d5ee50bc1168370ddc27532a9748eb6588
data/README.md CHANGED
@@ -1,13 +1,15 @@
1
- # Active Record Querier
1
+ # Querier
2
2
 
3
- > class UserQuerier < Querier
4
- > > @active_record_class = ApplicationRecord
5
- def initialize user_name:, active:
6
- > > > @query_template = "SELECT * FROM users WHERE name = ${user_name} AND active = ${active/no_quote}"
7
- super
8
- > >
9
- > > end
10
- > >
11
- > end
3
+ Active Record query executer utility.
12
4
 
13
- > UserQuerier.new(user_name: 'foo', active: true).select_all.to_struct
5
+ ```ruby
6
+ class UserQuerier < Querier
7
+ @active_record_class = ApplicationRecord
8
+ def initialize user_name:, active:
9
+ @query_template = "SELECT * FROM users WHERE name = ${user_name} AND active = ${active/no_quote}"
10
+ super
11
+ end
12
+ end
13
+
14
+ UserQuerier.new(user_name: 'foo', active: true)select_all.to_struct
15
+ ```
data/lib/querier.rb CHANGED
@@ -1,9 +1,6 @@
1
1
  require 'active_record'
2
2
 
3
3
  class Querier
4
- PARAM_NAME_INDEX = 0
5
- PARAM_VALUE_INDEX = 1
6
-
7
4
  @active_record_class = ActiveRecord::Base
8
5
  # based on rubocop's tips at: https://www.rubydoc.info/gems/rubocop/RuboCop/Cop/Style/ClassVars
9
6
  # solution here: https://www.ruby-lang.org/en/documentation/faq/8/
@@ -19,71 +16,33 @@ class Querier
19
16
  @query = fill_query_params
20
17
  end
21
18
 
22
- def execute
23
- @active_record_class.connection.execute(@query)
24
- end
25
-
26
- def exec_query
27
- decorate_dataset_format(@active_record_class.connection.exec_query(@query))
28
- end
29
-
30
- def select_all
31
- decorate_dataset_format(@active_record_class.connection.select_all(@query))
32
- end
33
-
34
- def select_one
35
- @active_record_class.connection.select_one(@query)
36
- end
37
-
38
- def select_values
39
- @active_record_class.connection.select_values(@query)
40
- end
41
-
42
- def select_rows
43
- @active_record_class.connection.select_rows(@query)
44
- end
45
-
46
- def select_value
47
- @active_record_class.connection.select_value(@query)
48
- end
19
+ def execute = @active_record_class.connection.execute(@query)
20
+ def exec_query = decorate_dataset_format(@active_record_class.connection.exec_query(@query))
21
+ def select_all = decorate_dataset_format(@active_record_class.connection.select_all(@query))
22
+ def select_one = @active_record_class.connection.select_one(@query)
23
+ def select_values = @active_record_class.connection.select_values(@query)
24
+ def select_rows = @active_record_class.connection.select_rows(@query)
25
+ def select_value = @active_record_class.connection.select_value(@query)
49
26
 
50
27
  private
51
28
 
52
29
  def decorate_dataset_format(dataset)
53
- def dataset.as_hash
54
- map(&:symbolize_keys)
55
- end
56
-
57
- def dataset.as_struct
58
- map { |record| OpenStruct.new(record) }
59
- end
60
-
30
+ def dataset.as_hash = map(&:symbolize_keys)
31
+ def dataset.as_struct = map { |record| OpenStruct.new(record) }
61
32
  dataset
62
33
  end
63
34
 
64
- def get_param_value(raw_query_param:, quotefy_param: true)
65
- if raw_query_param.instance_of?(String) && quotefy_param
66
- @active_record_class.connection.quote(raw_query_param.to_s)
67
- else
68
- raw_query_param.to_s
69
- end
70
- end
71
-
72
35
  def fill_query_params
73
36
  query = @query_template.dup
74
37
 
75
- @query_params.each_pair do |query_param|
76
- query_param_name = query_param[PARAM_NAME_INDEX].to_s
77
-
78
- query.gsub!(/\${#{query_param_name}}/,
79
- get_param_value(raw_query_param: query_param[PARAM_VALUE_INDEX],
80
- quotefy_param: true))
81
-
82
- query.gsub!(/\${#{query_param_name}\/no_quote}/,
83
- get_param_value(raw_query_param: query_param[PARAM_VALUE_INDEX],
84
- quotefy_param: false))
38
+ @query_params.each do |param_name, param_value|
39
+ placeholder = "${#{param_name}}"
40
+ placeholder_no_quote = "${#{param_name}/no_quote}"
41
+
42
+ query.gsub!(placeholder, @active_record_class.connection.quote(param_value.to_s))
43
+ query.gsub!(placeholder_no_quote, param_value.to_s)
85
44
  end
86
-
45
+
87
46
  query
88
47
  end
89
48
  end
data/lib/querier_bkp.rb CHANGED
File without changes
@@ -0,0 +1,105 @@
1
+ require 'active_record'
2
+
3
+ class Querier
4
+ PARAM_NAME_INDEX = 0
5
+ PARAM_VALUE_INDEX = 1
6
+
7
+ @active_record_class = ActiveRecord::Base
8
+ # based on rubocop's tips at: https://www.rubydoc.info/gems/rubocop/RuboCop/Cop/Style/ClassVars
9
+ # solution here: https://www.ruby-lang.org/en/documentation/faq/8/
10
+ class << self
11
+ attr_accessor :active_record_class
12
+ end
13
+
14
+ attr_reader :query, :query_template, :query_params
15
+
16
+ def initialize(template_query_params = {})
17
+ @active_record_class = self.class.active_record_class || self.class.superclass.active_record_class
18
+ @query_params = template_query_params
19
+ @query = fill_query_params
20
+ end
21
+
22
+ def execute
23
+ @active_record_class.connection.execute(@query)
24
+ end
25
+
26
+ def exec_query
27
+ decorate_dataset_format(@active_record_class.connection.exec_query(@query))
28
+ end
29
+
30
+ def select_all
31
+ decorate_dataset_format(@active_record_class.connection.select_all(@query))
32
+ end
33
+
34
+ def select_one
35
+ @active_record_class.connection.select_one(@query)
36
+ end
37
+
38
+ def select_sole
39
+ @active_record_class.connection.select_sole(@query)
40
+ end
41
+
42
+ def select_values
43
+ @active_record_class.connection.select_values(@query)
44
+ end
45
+
46
+ def select_rows
47
+ @active_record_class.connection.select_rows(@query)
48
+ end
49
+
50
+ def select_value
51
+ @active_record_class.connection.select_value(@query)
52
+ end
53
+
54
+ private
55
+
56
+ def decorate_dataset_format(dataset)
57
+ def dataset.as_hash
58
+ map(&:symbolize_keys)
59
+ end
60
+
61
+ def dataset.as_struct
62
+ map { |record| OpenStruct.new(record) }
63
+ end
64
+
65
+ dataset
66
+ end
67
+
68
+ def get_param_value(raw_query_param:, quotefy_param: true)
69
+ if raw_query_param.instance_of?(String) && quotefy_param
70
+ @active_record_class.connection.quote(raw_query_param.to_s)
71
+ else
72
+ raw_query_param.to_s
73
+ end
74
+ end
75
+
76
+ def fill_query_params
77
+ query = @query_template.dup
78
+
79
+ @query_params.each_pair do |query_param|
80
+ query_param_name = query_param[PARAM_NAME_INDEX].to_s
81
+
82
+ query.gsub!(/\${#{query_param_name}}/,
83
+ get_param_value(raw_query_param: query_param[PARAM_VALUE_INDEX],
84
+ quotefy_param: true))
85
+
86
+ query.gsub!(/\${#{query_param_name}\/no_quote}/,
87
+ get_param_value(raw_query_param: query_param[PARAM_VALUE_INDEX],
88
+ quotefy_param: false))
89
+ end
90
+
91
+ query
92
+ end
93
+ end
94
+
95
+
96
+ # def fill_query_params
97
+ # query = @query_template.dup
98
+
99
+ # @query_params.each do |param_name, param_value|
100
+ # placeholder = "${#{param_name}}"
101
+ # query.gsub!(placeholder, param_value.to_s)
102
+ # end
103
+
104
+ # query
105
+ # end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: querier
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.4.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gedean Dias
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-09-13 00:00:00.000000000 Z
11
+ date: 2024-10-03 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Active Record queries with variable number of params
14
14
  email: gedean.dias@gmail.com
@@ -19,11 +19,12 @@ files:
19
19
  - README.md
20
20
  - lib/querier.rb
21
21
  - lib/querier_bkp.rb
22
+ - lib/querier_bkp2.rb
22
23
  homepage: https://github.com/gedean/querier
23
24
  licenses:
24
25
  - MIT
25
26
  metadata: {}
26
- post_install_message:
27
+ post_install_message:
27
28
  rdoc_options: []
28
29
  require_paths:
29
30
  - lib
@@ -31,15 +32,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
31
32
  requirements:
32
33
  - - ">="
33
34
  - !ruby/object:Gem::Version
34
- version: 3.0.2
35
+ version: '3.1'
35
36
  required_rubygems_version: !ruby/object:Gem::Requirement
36
37
  requirements:
37
38
  - - ">="
38
39
  - !ruby/object:Gem::Version
39
40
  version: '0'
40
41
  requirements: []
41
- rubygems_version: 3.2.27
42
- signing_key:
42
+ rubygems_version: 3.5.21
43
+ signing_key:
43
44
  specification_version: 4
44
45
  summary: Active Record Querier
45
46
  test_files: []