Module: Databender::SQLHelper

Included in:
Runner
Defined in:
lib/databender/sql_helper.rb

Instance Method Summary collapse

Instance Method Details

#count_all_query(source_db, table) ⇒ Object



31
32
33
# File 'lib/databender/sql_helper.rb', line 31

def count_all_query(source_db, table)
  'SELECT count(1) cnt from %s.%s' % [source_db, table]
end

#count_filtered_query(source_db, table, condition = nil) ⇒ Object



35
36
37
38
39
# File 'lib/databender/sql_helper.rb', line 35

def count_filtered_query(source_db, table, condition = nil)
  sql = 'SELECT 1 from %s.%s' % [source_db, table]
  sql = apply_condition(condition, sql)
  sql
end

#insert_into_select(source_db, table, condition = nil) ⇒ Object



6
7
8
9
10
# File 'lib/databender/sql_helper.rb', line 6

def insert_into_select(source_db, table, condition = nil)
  sql = 'INSERT INTO %s SELECT * FROM %s.%s' % [table, source_db, table]
  sql = apply_condition(condition, sql)
  sql
end

#merge_filters(filter, additional_filter) ⇒ Object



21
22
23
# File 'lib/databender/sql_helper.rb', line 21

def merge_filters(filter, additional_filter)
  additional_filter ? [filter, 'and', additional_filter].join(' ') : filter
end

#resolve_column_filter(target_db, column, filter) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/databender/sql_helper.rb', line 12

def resolve_column_filter(target_db, column, filter)
  if filter.starts_with? 'refers'
    match = filter.match(/refers\((\w+),\s*(\w+)/)
    '%s in (select %s from %s.%s)' % [column, match[2], target_db, match[1]]
  else
    filter
  end
end

#where_clause_by_reference(target_db, parents) ⇒ Object



25
26
27
28
29
# File 'lib/databender/sql_helper.rb', line 25

def where_clause_by_reference(target_db, parents)
  parents.collect do |parent|
    '%s in (select %s from %s.%s)' % [parent.column_name, parent.ref_column_name, target_db, parent.ref_table_name]
  end.join(' and ')
end