querier 0.3.2 → 0.4.0

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.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +11 -9
  3. data/lib/querier.rb +64 -93
  4. data/lib/querier_bkp.rb +93 -0
  5. metadata +3 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 87c48d900c8bc55a2f243a7b6f5f0a6a09934111a97b2a1473a8085fe3812419
4
- data.tar.gz: 22b6a63a4ab9026812b9b64e1e82acbd4da724598d3064598c17713cd4fca1bd
3
+ metadata.gz: 03d97150ee77685d381ebb876ae14ff305c3d69af0d64acc7b37340c9aa43581
4
+ data.tar.gz: 22d2b0f0605663a12105c8e4e98447fa8fbe3106f606d05b6fc7dea740e7a725
5
5
  SHA512:
6
- metadata.gz: f7bae86825831e8cc9356cb77aea20b212646e4018d036a24f3ca09b4a35ad35aefab4f6ae40d52ca332815ef04996f6814a0e87da4e6485e0794cbf53d3c8fd
7
- data.tar.gz: feb4ed977acae65a211e801abf6a19dd6adcd8206ccb41098580af74f98f0b1c70ea16bbc40b3647a4a6b9193a6ed872726250196bda9ad43e1c393b32cf0cd4
6
+ metadata.gz: eda402af270f074d728d12d9ed2c0df757f44bcdfba4c8053ff06ca8480776de20088128a3a7dd6b428b9f3902ceaa6cc4790e35a81a95d8ffd0570e0c8833cd
7
+ data.tar.gz: 85045af2ce676784cf34a338d8dfbfc6cf7d9f10b4392053c7b908e7c80f1ae66986ac64b47ca71a695d7f7bcc4993095a6e91d718a68c0a5cc5c57436f4717b
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,93 +1,64 @@
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
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 select_all
27
+ result = @active_record_class.connection.select_all(@query)
28
+
29
+ def result.as_hash
30
+ map(&:symbolize_keys!)
31
+ end
32
+
33
+ def result.as_struct
34
+ map { |record| OpenStruct.new(record) }
35
+ end
36
+
37
+ result
38
+ end
39
+
40
+ private
41
+
42
+ def get_param_value(raw_query_param:, quotefy_param: true)
43
+ # where's String#quote when we need it?
44
+ raw_query_param.instance_of?(String) && quotefy_param ? "'#{raw_query_param.to_s}'" : raw_query_param.to_s
45
+ end
46
+
47
+ def fill_query_params
48
+ query = @query_template.dup
49
+
50
+ @query_params.each_pair do |query_param|
51
+ query_param_name = query_param[PARAM_NAME_INDEX].to_s
52
+
53
+ query.gsub!(/\${#{query_param_name}}/,
54
+ get_param_value(raw_query_param: query_param[PARAM_VALUE_INDEX],
55
+ quotefy_param: true))
56
+
57
+ query.gsub!(/\${#{query_param_name}\/no_quote}/,
58
+ get_param_value(raw_query_param: query_param[PARAM_VALUE_INDEX],
59
+ quotefy_param: false))
60
+ end
61
+
62
+ query
63
+ end
64
+ 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.3.2
4
+ version: 0.4.0
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-30 00:00:00.000000000 Z
11
+ date: 2021-12-09 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