Class: Rgviz::Executor
- Inherits:
-
Object
- Object
- Rgviz::Executor
- Defined in:
- lib/rgviz_rails/executor.rb
Instance Attribute Summary collapse
-
#adapter ⇒ Object
readonly
Returns the value of attribute adapter.
-
#model_class ⇒ Object
readonly
Returns the value of attribute model_class.
Instance Method Summary collapse
- #add_joins(joins) ⇒ Object
- #column_id(col, i) ⇒ Object
- #column_label(string) ⇒ Object
- #column_select(col) ⇒ Object
- #column_type(col) ⇒ Object
- #column_value(col, value) ⇒ Object
- #execute(query, options = {}) ⇒ Object
- #format_value(col, format, value) ⇒ Object
- #generate_columns ⇒ Object
- #generate_conditions ⇒ Object
- #generate_group ⇒ Object
- #generate_order ⇒ Object
- #generate_rows ⇒ Object
-
#initialize(model_class) ⇒ Executor
constructor
A new instance of Executor.
- #process_formats ⇒ Object
- #process_labels ⇒ Object
- #process_pivot ⇒ Object
- #rails_column_type(col) ⇒ Object
- #to_string(node, visitor_class) ⇒ Object
Constructor Details
#initialize(model_class) ⇒ Executor
Returns a new instance of Executor.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/rgviz_rails/executor.rb', line 6 def initialize(model_class) @model_class = model_class @selects = [] @joins = {} @labels = {} @formats = {} @pivots = {} @group_bys = {} @original_columns = [] case ActiveRecord::Base.connection.adapter_name.downcase when 'sqlite' require File.dirname(__FILE__) + '/adapters/sqlite_adapter.rb' @adapter = SqliteAdapter.new when 'mysql', 'mysql2' require File.dirname(__FILE__) + '/adapters/mysql_adapter.rb' @adapter = MySqlAdapter.new when 'postgresql' require File.dirname(__FILE__) + '/adapters/postgresql_adapter.rb' @adapter = PostgreSqlAdapter.new end end |
Instance Attribute Details
#adapter ⇒ Object (readonly)
Returns the value of attribute adapter.
4 5 6 |
# File 'lib/rgviz_rails/executor.rb', line 4 def adapter @adapter end |
#model_class ⇒ Object (readonly)
Returns the value of attribute model_class.
3 4 5 |
# File 'lib/rgviz_rails/executor.rb', line 3 def model_class @model_class end |
Instance Method Details
#add_joins(joins) ⇒ Object
79 80 81 82 83 84 85 86 87 |
# File 'lib/rgviz_rails/executor.rb', line 79 def add_joins(joins) map = @joins joins.each do |join| key = join.name val = map[key] map[key] = {} unless val map = map[key] end end |
#column_id(col, i) ⇒ Object
304 305 306 307 308 309 310 311 |
# File 'lib/rgviz_rails/executor.rb', line 304 def column_id(col, i) case col when IdColumn col.name else "c#{i}" end end |
#column_label(string) ⇒ Object
391 392 393 |
# File 'lib/rgviz_rails/executor.rb', line 391 def column_label(string) @labels[string] || string end |
#column_select(col) ⇒ Object
347 348 349 |
# File 'lib/rgviz_rails/executor.rb', line 347 def column_select(col) to_string col, ColumnVisitor end |
#column_type(col) ⇒ Object
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
# File 'lib/rgviz_rails/executor.rb', line 313 def column_type(col) case col when IdColumn klass, rails_col, joins = Rgviz::find_rails_col @model_class, col.name raise "Unknown column #{col}" unless rails_col rails_column_type rails_col when NumberColumn :number when StringColumn :string when BooleanColumn :boolean when DateColumn :date when DateTimeColumn :datetime when TimeOfDayColumn :timeofday when ScalarFunctionColumn case col.function when ScalarFunctionColumn::Now :datetime when ScalarFunctionColumn::ToDate :date when ScalarFunctionColumn::Upper, ScalarFunctionColumn::Lower, ScalarFunctionColumn::Concat :string else :number end when AggregateColumn :number end end |
#column_value(col, value) ⇒ Object
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 |
# File 'lib/rgviz_rails/executor.rb', line 351 def column_value(col, value) case col.type when :number i = value.to_i f = value.to_f i == f ? i : f when :boolean value == 1 || value == '1' ? true : false when :date value = Time.parse(value).to_date if value.is_a? String def value.as_json( = {}) self end def value.encode_json(*) "new Date(#{strftime('%Y, %d, %m')})" end value when :datetime value = Time.parse(value) if value.is_a? String def value.as_json(*) self end def value.encode_json(*) "new Date(#{strftime('%Y, %d, %m, %H, %M, %S')})" end value when :timeofday value = Time.parse(value) if value.is_a? String def value.as_json(*) self end def value.encode_json(*) "new Date(#{strftime('0, 0, 0, %H, %M, %S')})" end value else value.to_s end end |
#execute(query, options = {}) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/rgviz_rails/executor.rb', line 28 def execute(query, = {}) @query = query @query = Parser.parse(@query, ) unless @query.kind_of?(Query) @table = Table.new @extra_conditions = [:conditions] process_pivot process_labels process_formats generate_columns generate_conditions generate_group generate_order generate_rows @table end |
#format_value(col, format, value) ⇒ Object
395 396 397 398 399 400 401 402 403 404 |
# File 'lib/rgviz_rails/executor.rb', line 395 def format_value(col, format, value) return nil if value.nil? case col.type when :boolean, :number, :string format % value when :date, :datetime, :timeofday value.strftime(format) end end |
#generate_columns ⇒ Object
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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/rgviz_rails/executor.rb', line 89 def generate_columns if @query.select && @query.select.columns.present? # Select the specified columns i = 0 @query.select.columns.each do |col| col_to_s = col.to_s @table.cols << (Column.new :id => column_id(col, i), :type => column_type(col), :label => column_label(col_to_s)) @selects << "(#{column_select(col)}) as c#{i}" @original_columns << col_to_s i += 1 end else # Select all columns i = 0 @model_class.send(:columns).each do |col| @table.cols << (Column.new :id => col.name, :type => (rails_column_type col), :label => column_label(col.name)) @selects << "(#{ActiveRecord::Base.connection.quote_column_name(col.name)}) as c#{i}" @original_columns << col.name i += 1 end end # Select pivot columns and group by columns if @query.pivot @max_before_pivot_columns = @original_columns.length @query.pivot.columns.each do |col| col_to_s = col.to_s @table.cols << (Column.new :id => column_id(col, i), :type => column_type(col), :label => column_label(col_to_s)) @selects << "(#{column_select(col)}) as c#{i}" @original_columns << col_to_s i += 1 end @max_original_columns = @original_columns.length if @query.group_by @query.group_by.columns.each do |col| col_to_s = col.to_s @table.cols << (Column.new :id => column_id(col, i), :type => column_type(col), :label => column_label(col_to_s)) @selects << "(#{column_select(col)}) as c#{i}" i += 1 end end end end |
#generate_conditions ⇒ Object
139 140 141 |
# File 'lib/rgviz_rails/executor.rb', line 139 def generate_conditions @conditions = to_string @query.where, WhereVisitor if @query.where end |
#generate_group ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/rgviz_rails/executor.rb', line 143 def generate_group @group = to_string @query.group_by, ColumnVisitor if @query.group_by pivot = to_string @query.pivot, ColumnVisitor if @query.pivot if pivot.present? if @group.present? @group += ',' + pivot else @group = pivot end end end |
#generate_order ⇒ Object
156 157 158 |
# File 'lib/rgviz_rails/executor.rb', line 156 def generate_order @order = to_string @query.order_by, OrderVisitor if @query.order_by end |
#generate_rows ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
# File 'lib/rgviz_rails/executor.rb', line 160 def generate_rows conditions = @conditions if @extra_conditions if conditions if @extra_conditions.kind_of? String conditions = "(#{conditions}) AND #{@extra_conditions}" elsif @extra_conditions.kind_of? Array conditions = ["(#{conditions}) AND #{@extra_conditions[0]}", *@extra_conditions[1 .. -1]] end else conditions = @extra_conditions end end results = @model_class.send :all, :select => @selects.join(','), :conditions => conditions, :group => @group, :order => @order, :limit => @query.limit, :offset => @query.offset, :joins => @joins if @pivots.empty? || results.empty? @table.cols = @table.cols[0 ... @max_before_pivot_columns] if @pivots.present? # Simple, just convert the results to a table results.each do |result| row = Row.new @table.rows << row i = 0 @table.cols.each do |col| hash = {} hash[:v] = column_value(col, result.send("c#{i}")) unless @query. && @query..no_values format = @formats[@original_columns[i]] hash[:f] = format_value(col, format, hash[:v]) if format row.c << Cell.new(hash) i += 1 end end else # A little more complicated... # This is grouping => pivot => [selections] fin = ActiveSupport::OrderedHash.new # The uniq pivot values uniq_pivots = [] # Fill fin and uniq_pivots results.each do |result| # The grouping key of this result grouped_by = [] # The pivots of this result pivots = [] # The selections of this result selections = [] # Fill grouped_by, pivots and selections, as well as uniq_pivots @table.cols.each_with_index do |col, i| val = column_value(col, result.send("c#{i}")) if i >= @max_original_columns || @group_bys.include?(@original_columns[i]) grouped_by << val elsif @pivots.include?(@original_columns[i]) pivots << val else selections << val end end uniq_pivots << pivots unless uniq_pivots.include? pivots # Now put all this info into fin fin[grouped_by] = {} unless fin[grouped_by] fin[grouped_by][pivots] = selections end # Sort the uniq pivots so the results will be sorted for a human uniq_pivots.sort! # Regenerate the columns info: the current info has the values # we needed to get the info we needed col_i = 0 new_cols = [] @original_columns.each_with_index do |original_column, i| break if i >= @max_original_columns old_col = @table.cols[i] if @group_bys.include?(original_column) old_col.id = "c#{col_i}" new_cols << @table.cols[i] col_i += 1 elsif !@pivots.include?(original_column) uniq_pivots.each do |uniq_pivot| new_cols << (Column.new :id => "c#{col_i}", :type => old_col.type, :label => "#{uniq_pivot.join(', ')} #{old_col.label}") col_i += 1 end end end @table.cols = new_cols # Create the rows fin.each do |key, value| row = Row.new @table.rows << row group_i = 0 value_i = 0 @original_columns.each_with_index do |original_column, i| if @group_bys.include?(original_column) hash = {} hash[:v] = key[group_i] unless @query. && @query..no_values format = @formats[original_column] hash[:f] = format_value(@table.cols[i], format, hash[:v]) if format row.c << (Cell.new hash) group_i += 1 elsif !@pivots.include?(original_column) uniq_pivots.each do |uniq_pivot| v = value[uniq_pivot] v = v[value_i] if v hash = {} hash[:v] = v unless @query. && @query..no_values format = @formats[original_column] hash[:f] = format_value(@table.cols[i], format, hash[:v]) if format row.c << (Cell.new hash) end value_i += 1 end end end end end |
#process_formats ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/rgviz_rails/executor.rb', line 57 def process_formats return unless @query.formats.present? @query.formats.each do |format| @formats[format.column.to_s] = format.pattern end end |
#process_labels ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/rgviz_rails/executor.rb', line 49 def process_labels return unless @query.labels.present? @query.labels.each do |label| @labels[label.column.to_s] = label.label end end |
#process_pivot ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/rgviz_rails/executor.rb', line 65 def process_pivot if @query.pivot @query.pivot.columns.each do |column| @pivots[column.to_s] = true end end if @query.group_by @query.group_by.columns.each do |column| @group_bys[column.to_s] = true end end end |
#rails_column_type(col) ⇒ Object
412 413 414 415 416 417 418 419 |
# File 'lib/rgviz_rails/executor.rb', line 412 def rails_column_type(col) case col.type when :integer, :float, :decimal :number else col.type end end |
#to_string(node, visitor_class) ⇒ Object
406 407 408 409 410 |
# File 'lib/rgviz_rails/executor.rb', line 406 def to_string(node, visitor_class) visitor = visitor_class.new self node.accept visitor visitor.string end |