Class: Querier

Inherits:
Object
  • Object
show all
Defined in:
lib/querier.rb,
lib/querier_bkp.rb,
lib/querier_bkp2.rb

Constant Summary collapse

PARAM_NAME_INDEX =
0
PARAM_VALUE_INDEX =
1

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template_query_params = {}) ⇒ Querier

Returns a new instance of Querier.



13
14
15
16
17
# File 'lib/querier.rb', line 13

def initialize(template_query_params = {})
  @active_record_class = self.class.active_record_class || self.class.superclass.active_record_class
  @query_params = template_query_params
  @query = fill_query_params
end

Class Attribute Details

.active_record_classObject

Returns the value of attribute active_record_class.



8
9
10
# File 'lib/querier.rb', line 8

def active_record_class
  @active_record_class
end

Instance Attribute Details

#queryObject (readonly)

Returns the value of attribute query.



11
12
13
# File 'lib/querier.rb', line 11

def query
  @query
end

#query_execution_countObject (readonly)

Returns the value of attribute query_execution_count.



14
15
16
# File 'lib/querier_bkp.rb', line 14

def query_execution_count
  @query_execution_count
end

#query_paramsObject (readonly)

Returns the value of attribute query_params.



11
12
13
# File 'lib/querier.rb', line 11

def query_params
  @query_params
end

#query_templateObject (readonly)

Returns the value of attribute query_template.



11
12
13
# File 'lib/querier.rb', line 11

def query_template
  @query_template
end

Instance Method Details

#cached_result(format: :hash) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/querier_bkp.rb', line 28

def cached_result(format: :hash)
  raise 'query not executed yet' if @query_execution_count.eql?(0)

  case format
  when :hash
    @execution_cached_result
  when :open_struct
    hash_to_open_struct(dataset: @execution_cached_result)
  else
    raise 'invalid value type'
  end
end

#exec_queryObject



20
# File 'lib/querier.rb', line 20

def exec_query = decorate_dataset_format(@active_record_class.connection.exec_query(@query))

#executeObject



19
# File 'lib/querier.rb', line 19

def execute = @active_record_class.connection.execute(@query)

#field_group_and_count(field_name:, sort_element_index: nil, reverse_sort: true) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/querier_bkp.rb', line 54

def field_group_and_count(field_name:, sort_element_index: nil, reverse_sort: true)
  count_result = cached_result(format: :open_struct).group_by(&field_name).map { |k, v| [k, v.count] }

  unless sort_element_index.nil?
    count_result = count_result.sort_by { |el| el[sort_element_index] }
    count_result.reverse! if reverse_sort.eql? true
  end

  count_result
end

#select_allObject



21
# File 'lib/querier.rb', line 21

def select_all = decorate_dataset_format(@active_record_class.connection.select_all(@query))

#select_oneObject



22
# File 'lib/querier.rb', line 22

def select_one = @active_record_class.connection.select_one(@query)

#select_rowsObject



24
# File 'lib/querier.rb', line 24

def select_rows = @active_record_class.connection.select_rows(@query)

#select_soleObject



38
39
40
# File 'lib/querier_bkp2.rb', line 38

def select_sole
  @active_record_class.connection.select_sole(@query)
end

#select_valueObject



25
# File 'lib/querier.rb', line 25

def select_value = @active_record_class.connection.select_value(@query)

#select_valuesObject



23
# File 'lib/querier.rb', line 23

def select_values = @active_record_class.connection.select_values(@query)

#structured_resultsObject



41
42
43
# File 'lib/querier_bkp.rb', line 41

def structured_results
  hash_to_open_struct(dataset: execute)
end

#to_fileObject



49
50
51
52
# File 'lib/querier_bkp.rb', line 49

def to_file
  file_name = "querier #{Time.now.strftime '[%d-%m-%Y]-[%Hh %Mm %Ss]'}.sql"
  File.write "tmp/#{file_name}", to_sql
end

#to_sqlObject



45
46
47
# File 'lib/querier_bkp.rb', line 45

def to_sql
  fill_query_params(query_template: @query_template, query_params: @query_params)
end