Class: SqlWrangler::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/sql_wrangler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conn, sql_string, params = {}) ⇒ Query

Returns a new instance of Query.



40
41
42
43
44
45
# File 'lib/sql_wrangler.rb', line 40

def initialize(conn, sql_string, params={})
  @conn = conn
  @sql_string = sql_string
  @groupings = []
  @params = params
end

Instance Attribute Details

#connObject (readonly)

Returns the value of attribute conn.



37
38
39
# File 'lib/sql_wrangler.rb', line 37

def conn
  @conn
end

#groupingsObject (readonly)

Returns the value of attribute groupings.



36
37
38
# File 'lib/sql_wrangler.rb', line 36

def groupings
  @groupings
end

#paramsObject

Returns the value of attribute params.



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

def params
  @params
end

#sql_stringObject (readonly)

Returns the value of attribute sql_string.



35
36
37
# File 'lib/sql_wrangler.rb', line 35

def sql_string
  @sql_string
end

Instance Method Details

#executeObject



47
48
49
50
51
# File 'lib/sql_wrangler.rb', line 47

def execute
  raw_result = @conn.execute_sql(@sql_string, @params)
  init_groups_for_execution raw_result[0]
  return format_query_result(raw_result)
end

#format_query_result(raw_result) ⇒ Object



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

def format_query_result(raw_result)
  formatted_result = []
  columns_by_index = get_columns_by_index raw_result[0]

  raw_result[1,raw_result.length-1].each do |raw_row|
    merge_row raw_row, @groupings, formatted_result, columns_by_index
  end

  return formatted_result
end

#get_columns_by_index(columns) ⇒ Object



64
65
66
67
68
# File 'lib/sql_wrangler.rb', line 64

def get_columns_by_index columns
  columns_by_index = {}
  (0..columns.length-1).each { |i| columns_by_index[i] = columns[i] }
  return columns_by_index
end

#get_existing_grouped_row(grouped_vals, grouped_data) ⇒ Object



118
119
120
121
122
123
124
125
# File 'lib/sql_wrangler.rb', line 118

def get_existing_grouped_row grouped_vals, grouped_data
  grouped_data.each do |grouped_row|
    if not grouped_vals.keys.any? { |key| grouped_vals[key] != grouped_row[key] }
      return grouped_row
    end
  end
  return nil
end

#group_by(columns, options = {}) ⇒ Object



127
128
129
130
131
132
# File 'lib/sql_wrangler.rb', line 127

def group_by columns, options = {}
  new_grouping = QueryGrouping.new(options[:into], columns)
  new_grouping.level = @groupings.length
  @groupings << new_grouping
  return self
end

#init_groups_for_execution(columns) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/sql_wrangler.rb', line 70

def init_groups_for_execution columns
  used_indexes = []
  @groupings.each do |group|
    group.group_indexes = []
    group.content_indexes = []
    (0..(columns.length-1)).each do |i|
      if group.columns.any? { |c| c == columns[i] }
        used_indexes << i
        group.group_indexes << i
      elsif not used_indexes.any? { |used_index| used_index == i }
        group.content_indexes << i
      end
    end
  end
end

#merge_row(row, groups, grouped_data, columns_by_index) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/sql_wrangler.rb', line 86

def merge_row(row, groups, grouped_data, columns_by_index)

  if not @groupings.any?
    flat_row = {}
    columns_by_index.each { |index, column| flat_row[column] = row[index] }
    grouped_data << flat_row
  elsif groups.any?
    this_group = groups[0]
    remaining_groups = groups[1, groups.length-1]
    grouped_vals = {}
    this_group.group_indexes.each { |index| grouped_vals[columns_by_index[index]] = row[index] }

    if grouped_vals.values.any? { |value| value != nil }
      grouped_row = get_existing_grouped_row grouped_vals, grouped_data

      if grouped_row == nil
        grouped_row = grouped_vals
        grouped_row[this_group.name] = [] if this_group != nil
        grouped_data << grouped_row
      end

      merge_row(row, remaining_groups, grouped_row[this_group.name], columns_by_index)
    end

  else
    leaf_data = {}
    @groupings.last.content_indexes.each { |i| leaf_data[columns_by_index[i]] = row[i] }
    grouped_data << leaf_data if leaf_data.values.any? { |value| value != nil }
  end

end