Module: DynamicQuery
- Defined in:
- lib/dynamic_query.rb,
lib/dynamic_query/helper.rb,
lib/dynamic_query/joiner.rb,
lib/dynamic_query/railtie.rb,
lib/dynamic_query/version.rb,
lib/dynamic_query/validator.rb,
lib/generators/helper_generator.rb,
lib/dynamic_query/combined_query.rb
Defined Under Namespace
Modules: CombinedQuery, Helper, Joiner, Validator
Classes: DynamicQueryInstance, HelperGenerator, Querier, Railtie
Constant Summary
collapse
- OPERATOR =
['=', '>', '>=', '<', '<=', '!=',
'LIKE', 'NOT LIKE', 'IN', 'NOT IN',
'BETWEEN', 'NOT BETWEEN', 'IS NULL', 'IS NOT NULL']
- MAJOR =
0
- MINOR =
7
- PATCH =
2
- VERSION =
[MAJOR, MINOR, PATCH].compact.join('.')
Instance Method Summary
collapse
Instance Method Details
#all_columns_in(*models) ⇒ Object
52
53
54
|
# 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) ⇒ Object
65
66
67
68
69
|
# 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) ⇒ Object
12
13
14
15
|
# File 'lib/dynamic_query.rb', line 12
def dynamic_query(*models, opt)
models.flatten!
DynamicQueryInstance.new(*models, opt)
end
|
#estimate_total(dq, query) ⇒ Object
56
57
58
59
60
61
62
63
|
# 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) ⇒ Object
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# 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
|