querier 0.2.1 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +11 -9
  3. data/lib/querier.rb +89 -91
  4. data/lib/querier_bkp.rb +93 -0
  5. metadata +5 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: de1729ce8cc885a52a8a5892e79d2a4e4008b48d95d5a821c2f985bc5925abf9
4
- data.tar.gz: 0db5814a6b46ecd4d73f766763d1f82347c2fecf3075c0f1c3218d6870b4cfa9
3
+ metadata.gz: 40c24ee7c142f7bf047c967cdbee1e703a7c41ee6aafda911c36e02008642b6f
4
+ data.tar.gz: d4203cdcc429107b363c917ec493dc30c25e730fcef9cbf7a76f9653e533eb36
5
5
  SHA512:
6
- metadata.gz: b3384fe46ceb0cbfbd19e15caa51d4e8bb14e77e2565d4db001f773d6fb2d8ac1a88f53ede1527f150ae3d916a50d41e7ac7aed789ca59f72b58a9f0f1871c8f
7
- data.tar.gz: 343e400b47d07e0e8e55be4b025fbca6c3e672e01ece56ab3bd6c13061eb9e1597d27dfe70c36ebeb17904efcc2676a4ee7f90ffc921f1f63b9799755f5af88d
6
+ metadata.gz: 5400b60fbc5e6c9bcaec3cc844ed919f855557e8e76b78c6eede62da5700a040beeac4b2fae880fd92a5bdf068cf5052e255d5182306ba411d8fd8b3e6a925ca
7
+ data.tar.gz: f7304d4dfaf39b60845e1931e2040f4bafa84a745158d4ea03f08459bf48e0f4eec72a28b255c2d6537c4e31da1279fd91c2240f875eaba03c11172ba1c6b1c2
data/README.md CHANGED
@@ -1,11 +1,13 @@
1
- # querier
1
+ # Active Record 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}"
7
- super
8
- end
9
- end
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
10
12
 
11
- # UserQuerier.new(user_name: 'foo', active: true).execute
13
+ > UserQuerier.new(user_name: 'foo', active: true).select_all.to_struct
data/lib/querier.rb CHANGED
@@ -1,91 +1,89 @@
1
- class Querier
2
- PARAM_NAME_INDEX = 0
3
- PARAM_VALUE_INDEX = 1
4
-
5
- @active_record_class = ActiveRecord::Base
6
- # based on rubocop's tips at: https://www.rubydoc.info/gems/rubocop/RuboCop/Cop/Style/ClassVars
7
- # solution here: https://www.ruby-lang.org/en/documentation/faq/8/
8
- class << self
9
- attr_accessor :active_record_class
10
- end
11
-
12
- attr_reader :query_execution_count, :query_template, :query_params
13
-
14
- def initialize(**template_query_params)
15
- @active_record_class = self.class.active_record_class || self.class.superclass.active_record_class
16
- @query_execution_count = 0
17
- @query_params = template_query_params.dup
18
- end
19
-
20
- def execute
21
- @query_execution_count += 1
22
- @execution_cached_result = @active_record_class.connection.select_all(fill_query_params(query_template: @query_template,
23
- query_params: @query_params)).map(&:symbolize_keys!)
24
- end
25
-
26
- def cached_result(format: :hash)
27
- raise 'query not executed yet' if @query_execution_count.eql?(0)
28
-
29
- case format
30
- when :hash
31
- @execution_cached_result
32
- when :open_struct
33
- hash_to_open_struct(dataset: @execution_cached_result)
34
- else
35
- raise 'invalid value type'
36
- end
37
- end
38
-
39
- def structured_results
40
- hash_to_open_struct(dataset: execute)
41
- end
42
-
43
- def to_sql
44
- fill_query_params(query_template: @query_template, query_params: @query_params)
45
- end
46
-
47
- def to_file
48
- file_name = "querier #{Time.now.strftime '[%d-%m-%Y]-[%Hh %Mm %Ss]'}.sql"
49
- File.open("tmp/#{file_name}", 'w') { |f| f << to_sql }
50
- end
51
-
52
- def field_group_and_count(field_name:, sort_element_index: nil, reverse_sort: true)
53
- count_result = cached_result(format: :open_struct).group_by(&field_name).map { |k, v| [k, v.count] }
54
-
55
- unless sort_element_index.nil?
56
- count_result = count_result.sort_by { |el| el[sort_element_index] }
57
- count_result.reverse! if reverse_sort.eql? true
58
- end
59
-
60
- count_result
61
- end
62
-
63
- private
64
-
65
- def hash_to_open_struct(dataset:)
66
- dataset.map { |record| OpenStruct.new(record.symbolize_keys!) }
67
- end
68
-
69
- def get_param_value(raw_query_param:, quotefy_param: true)
70
- # where's String#quote when we need it?
71
- raw_query_param.instance_of?(String) && quotefy_param ? "'#{raw_query_param.to_s}'" : raw_query_param.to_s
72
- end
73
-
74
- def fill_query_params(query_template:, query_params:)
75
- query = query_template.dup
76
-
77
- query_params.each do |query_param|
78
- query_param_name = query_param[PARAM_NAME_INDEX].to_s
79
-
80
- query.gsub!(/\${#{query_param_name}}/,
81
- get_param_value(raw_query_param: query_param[PARAM_VALUE_INDEX],
82
- quotefy_param: true))
83
-
84
- query.gsub!(/\${#{query_param_name}\/no_quote}/,
85
- get_param_value(raw_query_param: query_param[PARAM_VALUE_INDEX],
86
- quotefy_param: false))
87
- end
88
-
89
- query
90
- end
91
- end
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_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
49
+
50
+ private
51
+
52
+ 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
+
61
+ dataset
62
+ end
63
+
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
+ def fill_query_params
73
+ query = @query_template.dup
74
+
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))
85
+ end
86
+
87
+ query
88
+ end
89
+ end
@@ -0,0 +1,93 @@
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_execution_count, :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_execution_count = 0
19
+ @query_params = template_query_params
20
+ end
21
+
22
+ def execute
23
+ @query_execution_count += 1
24
+ @execution_cached_result = @active_record_class.connection.select_all(fill_query_params(query_template: @query_template,
25
+ query_params: @query_params)).map(&:symbolize_keys!)
26
+ end
27
+
28
+ def cached_result(format: :hash)
29
+ raise 'query not executed yet' if @query_execution_count.eql?(0)
30
+
31
+ case format
32
+ when :hash
33
+ @execution_cached_result
34
+ when :open_struct
35
+ hash_to_open_struct(dataset: @execution_cached_result)
36
+ else
37
+ raise 'invalid value type'
38
+ end
39
+ end
40
+
41
+ def structured_results
42
+ hash_to_open_struct(dataset: execute)
43
+ end
44
+
45
+ def to_sql
46
+ fill_query_params(query_template: @query_template, query_params: @query_params)
47
+ end
48
+
49
+ def to_file
50
+ file_name = "querier #{Time.now.strftime '[%d-%m-%Y]-[%Hh %Mm %Ss]'}.sql"
51
+ File.write "tmp/#{file_name}", to_sql
52
+ end
53
+
54
+ def field_group_and_count(field_name:, sort_element_index: nil, reverse_sort: true)
55
+ count_result = cached_result(format: :open_struct).group_by(&field_name).map { |k, v| [k, v.count] }
56
+
57
+ unless sort_element_index.nil?
58
+ count_result = count_result.sort_by { |el| el[sort_element_index] }
59
+ count_result.reverse! if reverse_sort.eql? true
60
+ end
61
+
62
+ count_result
63
+ end
64
+
65
+ private
66
+
67
+ def hash_to_open_struct(dataset:)
68
+ dataset.map { |record| OpenStruct.new(record.symbolize_keys!) }
69
+ end
70
+
71
+ def get_param_value(raw_query_param:, quotefy_param: true)
72
+ # where's String#quote when we need it?
73
+ raw_query_param.instance_of?(String) && quotefy_param ? "'#{raw_query_param.to_s}'" : raw_query_param.to_s
74
+ end
75
+
76
+ def fill_query_params(query_template:, query_params:)
77
+ query = query_template
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
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.2.1
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gedean Dias
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-08-14 00:00:00.000000000 Z
11
+ date: 2021-09-13 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
@@ -18,6 +18,7 @@ extra_rdoc_files: []
18
18
  files:
19
19
  - README.md
20
20
  - lib/querier.rb
21
+ - lib/querier_bkp.rb
21
22
  homepage: https://github.com/gedean/querier
22
23
  licenses:
23
24
  - MIT
@@ -30,14 +31,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
30
31
  requirements:
31
32
  - - ">="
32
33
  - !ruby/object:Gem::Version
33
- version: 2.7.0
34
+ version: 3.0.2
34
35
  required_rubygems_version: !ruby/object:Gem::Requirement
35
36
  requirements:
36
37
  - - ">="
37
38
  - !ruby/object:Gem::Version
38
39
  version: '0'
39
40
  requirements: []
40
- rubygems_version: 3.2.25
41
+ rubygems_version: 3.2.27
41
42
  signing_key:
42
43
  specification_version: 4
43
44
  summary: Active Record Querier