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, Railtie

Constant Summary collapse

OPERATOR =
['=', '>', '>=', '<', '<=', '!=',
'LIKE', 'NOT LIKE', 'IN', 'NOT IN',
'BETWEEN', 'NOT BETWEEN', 'IS NULL', 'IS NOT NULL']
MAJOR =
0
MINOR =
7
PATCH =
1
VERSION =
[MAJOR, MINOR, PATCH].compact.join('.')

Instance Method Summary collapse

Instance Method Details

#all_columns_in(*models) ⇒ Object



51
52
53
# File 'lib/dynamic_query.rb', line 51

def all_columns_in(*models)
  models.map { |m| m.columns.map { |col| "#{m.table_name}.#{col.name}" } }.flatten.join ', '
end

#dynamic_query(*models, opt) ⇒ Object



12
13
14
# File 'lib/dynamic_query.rb', line 12

def dynamic_query(*models, opt)
  DynamicQueryInstance.new(*models, opt)
end

#tables_joined_by(from, *to_tables) ⇒ Object



16
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
# File 'lib/dynamic_query.rb', line 16

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