module DynamicQuery
Constants
- MAJOR
- MINOR
- OPERATOR
- PATCH
- VERSION
Public Instance Methods
all_columns_in(*models)
click to toggle source
# File lib/dynamic_query.rb, line 52 def all_columns_in(*models) models.map { |m| m.columns.map { |col| "#{m.table_name}.#{col.name}" } }.flatten.join ', ' end
conditioned_count(dq, query, model)
click to toggle source
# File lib/dynamic_query.rb, line 65 def conditioned_count(dq, query, model) conditions = dq.conditions query, model.table_name statement = Querier.statement conditions model.where(statement).count end
dynamic_query(*models, opt)
click to toggle source
# File lib/dynamic_query.rb, line 12 def dynamic_query(*models, opt) models.flatten! DynamicQueryInstance.new(*models, opt) end
estimate_total(dq, query)
click to toggle source
# File lib/dynamic_query.rb, line 56 def estimate_total(dq, query) models = dq.models et = models.map { |m| { :table => m.table_name, :total => m.count, :conditioned => conditioned_count(dq, query, m) } } major = et.max_by { |i| i[:total] } et.delete major estimate = major[:conditioned] * et.map { |i| i[:conditioned].to_f / i[:total] }.reduce(:*) estimate = estimate.to_i end
tables_joined_by(from, *to_tables)
click to toggle source
# File lib/dynamic_query.rb, line 17 def tables_joined_by(from, *to_tables) sql = '' from_table = from.table_name to_tables_count = Hash.new(0) to_tables.each do |to| to_table = to.first.table_name if to_tables_count[to_table] == 0 sql << "INNER JOIN `#{to_table}` ON " mappings = [] to.drop(1).each do |mapping| if mapping.kind_of? Array mappings << "`#{from_table}`.`#{mapping.first}` = `#{to_table}`.`#{mapping.last}`" else mappings << "`#{from_table}`.`#{mapping}` = `#{to_table}`.`#{mapping}`" end end sql << "(#{mappings.join(' AND ')})" to_tables_count[to_table] += 1 else sql << "INNER JOIN `#{to_table}` `#{to_table}_#{to_tables_count[to_table]}` ON " mappings = [] to.drop(1).each do |mapping| if mapping.kind_of? Array mappings << "`#{from_table}`.`#{mapping.first}` = `#{to_table}_#{to_tables_count[to_table]}`.`#{mapping.last}`" else mappings << "`#{from_table}`.`#{mapping}` = `#{to_table}_#{to_tables_count[to_table]}`.`#{mapping}`" end end sql << "(#{mappings.join(' AND ')})" to_tables_count[to_table] += 1 end end sql end