querier 0.0.10 → 0.1.1
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 +4 -4
 - data/README.md +2 -14
 - data/lib/querier.rb +83 -83
 - metadata +1 -1
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            SHA256:
         
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: b9b2cf6f8f3a3f4aa087c68f85753ddcca380e076a165d0abef3fe750815630a
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: b8825c18fae966fbcdc54dd33a799c108efa7c5cebb4c46336c53093cb03bd1d
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: 84affb4d1704196d94de6dfbefb66795dc68636ddbfdf0fd6131a7b925313954642032d052d756124bff9e6a04a38b3a0a0917fc50d923c7d85d0cd9cd571a37
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: e4ff00606711107491de8ea7f9e7066786136859da62f2fdc3ff1b1f27e7a8ef4e85d7918c1fc9fa8962c840ecf71961f9c8464748a5ad1f39ba862f063d3cf7
         
     | 
    
        data/README.md
    CHANGED
    
    | 
         @@ -1,22 +1,10 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            # querier
         
     | 
| 
       2 
2 
     | 
    
         | 
| 
       3 
3 
     | 
    
         
             
            # class UserQuerier < Querier
         
     | 
| 
       4 
     | 
    
         
            -
                QUERY_TEMPLATE = <<-END_OF_QUERY_TEMPLATE
         
     | 
| 
       5 
     | 
    
         
            -
             
     | 
| 
       6 
     | 
    
         
            -
            SELECT
         
     | 
| 
       7 
     | 
    
         
            -
              *
         
     | 
| 
       8 
     | 
    
         
            -
            FROM
         
     | 
| 
       9 
     | 
    
         
            -
              users
         
     | 
| 
       10 
     | 
    
         
            -
            WHERE
         
     | 
| 
       11 
     | 
    
         
            -
              name = {?user_name}
         
     | 
| 
       12 
     | 
    
         
            -
              AND active = {?active}
         
     | 
| 
       13 
     | 
    
         
            -
             
     | 
| 
       14 
     | 
    
         
            -
              END_OF_QUERY_TEMPLATE
         
     | 
| 
       15 
     | 
    
         
            -
             
     | 
| 
       16 
4 
     | 
    
         
             
                def initialize user_name:, active:
         
     | 
| 
       17 
     | 
    
         
            -
                    @query_template =  
     | 
| 
      
 5 
     | 
    
         
            +
                    @query_template = "SELECT * FROM users WHERE name = ${user_name} AND active = ${active}"
         
     | 
| 
       18 
6 
     | 
    
         
             
                    super
         
     | 
| 
       19 
7 
     | 
    
         
             
                end
         
     | 
| 
       20 
8 
     | 
    
         
             
            end
         
     | 
| 
       21 
9 
     | 
    
         | 
| 
       22 
     | 
    
         
            -
            # UserQuerier.new(user_name: 'foo', active: true).execute
         
     | 
| 
      
 10 
     | 
    
         
            +
            # UserQuerier.new(user_name: 'foo', active: true).execute
         
     | 
    
        data/lib/querier.rb
    CHANGED
    
    | 
         @@ -1,83 +1,83 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            class Querier
         
     | 
| 
       2 
     | 
    
         
            -
              PARAM_NAME_INDEX = 0
         
     | 
| 
       3 
     | 
    
         
            -
              PARAM_VALUE_INDEX = 1
         
     | 
| 
       4 
     | 
    
         
            -
             
     | 
| 
       5 
     | 
    
         
            -
              attr_reader :query_execution_count, :query_template, :query_params
         
     | 
| 
       6 
     | 
    
         
            -
             
     | 
| 
       7 
     | 
    
         
            -
              def initialize 
     | 
| 
       8 
     | 
    
         
            -
                @query_execution_count = 0
         
     | 
| 
       9 
     | 
    
         
            -
                @query_params = template_query_params.dup
         
     | 
| 
       10 
     | 
    
         
            -
              end
         
     | 
| 
       11 
     | 
    
         
            -
             
     | 
| 
       12 
     | 
    
         
            -
              def execute
         
     | 
| 
       13 
     | 
    
         
            -
                @query_execution_count += 1
         
     | 
| 
       14 
     | 
    
         
            -
                @execution_cached_result = ActiveRecord::Base.connection.select_all(fill_query_params(query_template: @query_template, 
     | 
| 
       15 
     | 
    
         
            -
             
     | 
| 
       16 
     | 
    
         
            -
              end 
     | 
| 
       17 
     | 
    
         
            -
             
     | 
| 
       18 
     | 
    
         
            -
              def cached_result 
     | 
| 
       19 
     | 
    
         
            -
                raise 'query not executed yet' if @query_execution_count.eql?(0)
         
     | 
| 
       20 
     | 
    
         
            -
             
     | 
| 
       21 
     | 
    
         
            -
                case format 
     | 
| 
       22 
     | 
    
         
            -
             
     | 
| 
       23 
     | 
    
         
            -
             
     | 
| 
       24 
     | 
    
         
            -
             
     | 
| 
       25 
     | 
    
         
            -
             
     | 
| 
       26 
     | 
    
         
            -
             
     | 
| 
       27 
     | 
    
         
            -
             
     | 
| 
       28 
     | 
    
         
            -
                end
         
     | 
| 
       29 
     | 
    
         
            -
              end
         
     | 
| 
       30 
     | 
    
         
            -
             
     | 
| 
       31 
     | 
    
         
            -
              def structured_results
         
     | 
| 
       32 
     | 
    
         
            -
                hash_to_open_struct 
     | 
| 
       33 
     | 
    
         
            -
              end 
     | 
| 
       34 
     | 
    
         
            -
             
     | 
| 
       35 
     | 
    
         
            -
              def to_sql
         
     | 
| 
       36 
     | 
    
         
            -
                fill_query_params(query_template: @query_template, query_params: @query_params)
         
     | 
| 
       37 
     | 
    
         
            -
              end
         
     | 
| 
       38 
     | 
    
         
            -
             
     | 
| 
       39 
     | 
    
         
            -
              def to_file
         
     | 
| 
       40 
     | 
    
         
            -
                file_name = "querier #{Time.now.strftime  
     | 
| 
       41 
     | 
    
         
            -
                File.open("tmp/#{file_name}", 'w') {|f| f <<  
     | 
| 
       42 
     | 
    
         
            -
              end
         
     | 
| 
       43 
     | 
    
         
            -
             
     | 
| 
       44 
     | 
    
         
            -
              def field_group_and_count 
     | 
| 
       45 
     | 
    
         
            -
                count_result =  
     | 
| 
       46 
     | 
    
         
            -
             
     | 
| 
       47 
     | 
    
         
            -
                unless sort_element_index.nil?
         
     | 
| 
       48 
     | 
    
         
            -
                  count_result = count_result.sort_by {|el| el[sort_element_index]}
         
     | 
| 
       49 
     | 
    
         
            -
                  count_result.reverse! if reverse_sort.eql? true
         
     | 
| 
       50 
     | 
    
         
            -
                end
         
     | 
| 
       51 
     | 
    
         
            -
             
     | 
| 
       52 
     | 
    
         
            -
                count_result
         
     | 
| 
       53 
     | 
    
         
            -
              end 
     | 
| 
       54 
     | 
    
         
            -
             
     | 
| 
       55 
     | 
    
         
            -
              private
         
     | 
| 
       56 
     | 
    
         
            -
             
     | 
| 
       57 
     | 
    
         
            -
              def hash_to_open_struct 
     | 
| 
       58 
     | 
    
         
            -
                dataset.map {|record| OpenStruct.new(record.symbolize_keys!)}
         
     | 
| 
       59 
     | 
    
         
            -
              end 
     | 
| 
       60 
     | 
    
         
            -
             
     | 
| 
       61 
     | 
    
         
            -
              def get_param_value 
     | 
| 
       62 
     | 
    
         
            -
                # where's String#quote when we need it?
         
     | 
| 
       63 
     | 
    
         
            -
                raw_query_param. 
     | 
| 
       64 
     | 
    
         
            -
              end
         
     | 
| 
       65 
     | 
    
         
            -
             
     | 
| 
       66 
     | 
    
         
            -
              def fill_query_params 
     | 
| 
       67 
     | 
    
         
            -
                query = query_template.dup
         
     | 
| 
       68 
     | 
    
         
            -
             
     | 
| 
       69 
     | 
    
         
            -
                query_params.each do |query_param|
         
     | 
| 
       70 
     | 
    
         
            -
                  query_param_name = query_param[PARAM_NAME_INDEX].to_s
         
     | 
| 
       71 
     | 
    
         
            -
             
     | 
| 
       72 
     | 
    
         
            -
                  query.gsub!(/\${#{query_param_name}}/,
         
     | 
| 
       73 
     | 
    
         
            -
                              get_param_value(raw_query_param: query_param[PARAM_VALUE_INDEX], 
         
     | 
| 
       74 
     | 
    
         
            -
                                              quotefy_param: true))
         
     | 
| 
       75 
     | 
    
         
            -
             
     | 
| 
       76 
     | 
    
         
            -
                  query.gsub!(/\${#{query_param_name}\/no_quote}/,
         
     | 
| 
       77 
     | 
    
         
            -
                              get_param_value(raw_query_param: query_param[PARAM_VALUE_INDEX],
         
     | 
| 
       78 
     | 
    
         
            -
                                              quotefy_param: false))
         
     | 
| 
       79 
     | 
    
         
            -
                end
         
     | 
| 
       80 
     | 
    
         
            -
             
     | 
| 
       81 
     | 
    
         
            -
                query
         
     | 
| 
       82 
     | 
    
         
            -
              end
         
     | 
| 
       83 
     | 
    
         
            -
            end
         
     | 
| 
      
 1 
     | 
    
         
            +
            class Querier
         
     | 
| 
      
 2 
     | 
    
         
            +
              PARAM_NAME_INDEX = 0
         
     | 
| 
      
 3 
     | 
    
         
            +
              PARAM_VALUE_INDEX = 1
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
              attr_reader :query_execution_count, :query_template, :query_params
         
     | 
| 
      
 6 
     | 
    
         
            +
             
     | 
| 
      
 7 
     | 
    
         
            +
              def initialize(**template_query_params)
         
     | 
| 
      
 8 
     | 
    
         
            +
                @query_execution_count = 0
         
     | 
| 
      
 9 
     | 
    
         
            +
                @query_params = template_query_params.dup
         
     | 
| 
      
 10 
     | 
    
         
            +
              end
         
     | 
| 
      
 11 
     | 
    
         
            +
             
     | 
| 
      
 12 
     | 
    
         
            +
              def execute
         
     | 
| 
      
 13 
     | 
    
         
            +
                @query_execution_count += 1
         
     | 
| 
      
 14 
     | 
    
         
            +
                @execution_cached_result = ActiveRecord::Base.connection.select_all(fill_query_params(query_template: @query_template,
         
     | 
| 
      
 15 
     | 
    
         
            +
                                                                                                      query_params: @query_params)).map(&:symbolize_keys!)
         
     | 
| 
      
 16 
     | 
    
         
            +
              end
         
     | 
| 
      
 17 
     | 
    
         
            +
             
     | 
| 
      
 18 
     | 
    
         
            +
              def cached_result(format: :hash)
         
     | 
| 
      
 19 
     | 
    
         
            +
                raise 'query not executed yet' if @query_execution_count.eql?(0)
         
     | 
| 
      
 20 
     | 
    
         
            +
             
     | 
| 
      
 21 
     | 
    
         
            +
                case format
         
     | 
| 
      
 22 
     | 
    
         
            +
                when :hash
         
     | 
| 
      
 23 
     | 
    
         
            +
                  @execution_cached_result
         
     | 
| 
      
 24 
     | 
    
         
            +
                when :open_struct
         
     | 
| 
      
 25 
     | 
    
         
            +
                  hash_to_open_struct(dataset: @execution_cached_result)
         
     | 
| 
      
 26 
     | 
    
         
            +
                else
         
     | 
| 
      
 27 
     | 
    
         
            +
                  raise 'invalid value type'
         
     | 
| 
      
 28 
     | 
    
         
            +
                end
         
     | 
| 
      
 29 
     | 
    
         
            +
              end
         
     | 
| 
      
 30 
     | 
    
         
            +
             
     | 
| 
      
 31 
     | 
    
         
            +
              def structured_results
         
     | 
| 
      
 32 
     | 
    
         
            +
                hash_to_open_struct(dataset: execute)
         
     | 
| 
      
 33 
     | 
    
         
            +
              end
         
     | 
| 
      
 34 
     | 
    
         
            +
             
     | 
| 
      
 35 
     | 
    
         
            +
              def to_sql
         
     | 
| 
      
 36 
     | 
    
         
            +
                fill_query_params(query_template: @query_template, query_params: @query_params)
         
     | 
| 
      
 37 
     | 
    
         
            +
              end
         
     | 
| 
      
 38 
     | 
    
         
            +
             
     | 
| 
      
 39 
     | 
    
         
            +
              def to_file
         
     | 
| 
      
 40 
     | 
    
         
            +
                file_name = "querier #{Time.now.strftime '[%d-%m-%Y]-[%Hh %Mm %Ss]'}.sql"
         
     | 
| 
      
 41 
     | 
    
         
            +
                File.open("tmp/#{file_name}", 'w') { |f| f << to_sql }
         
     | 
| 
      
 42 
     | 
    
         
            +
              end
         
     | 
| 
      
 43 
     | 
    
         
            +
             
     | 
| 
      
 44 
     | 
    
         
            +
              def field_group_and_count(field_name:, sort_element_index: nil, reverse_sort: true)
         
     | 
| 
      
 45 
     | 
    
         
            +
                count_result = cached_results(format: :open_struct).group_by(&field_name).map { |k, v| [k, v.count] }
         
     | 
| 
      
 46 
     | 
    
         
            +
             
     | 
| 
      
 47 
     | 
    
         
            +
                unless sort_element_index.nil?
         
     | 
| 
      
 48 
     | 
    
         
            +
                  count_result = count_result.sort_by { |el| el[sort_element_index] }
         
     | 
| 
      
 49 
     | 
    
         
            +
                  count_result.reverse! if reverse_sort.eql? true
         
     | 
| 
      
 50 
     | 
    
         
            +
                end
         
     | 
| 
      
 51 
     | 
    
         
            +
             
     | 
| 
      
 52 
     | 
    
         
            +
                count_result
         
     | 
| 
      
 53 
     | 
    
         
            +
              end
         
     | 
| 
      
 54 
     | 
    
         
            +
             
     | 
| 
      
 55 
     | 
    
         
            +
              private
         
     | 
| 
      
 56 
     | 
    
         
            +
             
     | 
| 
      
 57 
     | 
    
         
            +
              def hash_to_open_struct(dataset:)
         
     | 
| 
      
 58 
     | 
    
         
            +
                dataset.map { |record| OpenStruct.new(record.symbolize_keys!) }
         
     | 
| 
      
 59 
     | 
    
         
            +
              end
         
     | 
| 
      
 60 
     | 
    
         
            +
             
     | 
| 
      
 61 
     | 
    
         
            +
              def get_param_value(raw_query_param:, quotefy_param: true)
         
     | 
| 
      
 62 
     | 
    
         
            +
                # where's String#quote when we need it?
         
     | 
| 
      
 63 
     | 
    
         
            +
                raw_query_param.instance_of?(String) && quotefy_param ? "'#{raw_query_param.to_s}'" : raw_query_param.to_s
         
     | 
| 
      
 64 
     | 
    
         
            +
              end
         
     | 
| 
      
 65 
     | 
    
         
            +
             
     | 
| 
      
 66 
     | 
    
         
            +
              def fill_query_params(query_template:, query_params:)
         
     | 
| 
      
 67 
     | 
    
         
            +
                query = query_template.dup
         
     | 
| 
      
 68 
     | 
    
         
            +
             
     | 
| 
      
 69 
     | 
    
         
            +
                query_params.each do |query_param|
         
     | 
| 
      
 70 
     | 
    
         
            +
                  query_param_name = query_param[PARAM_NAME_INDEX].to_s
         
     | 
| 
      
 71 
     | 
    
         
            +
             
     | 
| 
      
 72 
     | 
    
         
            +
                  query.gsub!(/\${#{query_param_name}}/,
         
     | 
| 
      
 73 
     | 
    
         
            +
                              get_param_value(raw_query_param: query_param[PARAM_VALUE_INDEX], 
         
     | 
| 
      
 74 
     | 
    
         
            +
                                              quotefy_param: true))
         
     | 
| 
      
 75 
     | 
    
         
            +
             
     | 
| 
      
 76 
     | 
    
         
            +
                  query.gsub!(/\${#{query_param_name}\/no_quote}/,
         
     | 
| 
      
 77 
     | 
    
         
            +
                              get_param_value(raw_query_param: query_param[PARAM_VALUE_INDEX],
         
     | 
| 
      
 78 
     | 
    
         
            +
                                              quotefy_param: false))
         
     | 
| 
      
 79 
     | 
    
         
            +
                end
         
     | 
| 
      
 80 
     | 
    
         
            +
             
     | 
| 
      
 81 
     | 
    
         
            +
                query
         
     | 
| 
      
 82 
     | 
    
         
            +
              end
         
     | 
| 
      
 83 
     | 
    
         
            +
            end
         
     |