Class: DirectiveRecord::Query::SQL

Inherits:
Object
  • Object
show all
Defined in:
lib/directive_record/query/sql.rb

Direct Known Subclasses

BigQuery, MySQL

Instance Method Summary collapse

Constructor Details

#initialize(base) ⇒ SQL

Returns a new instance of SQL.



5
6
7
# File 'lib/directive_record/query/sql.rb', line 5

def initialize(base)
  @base = base
end

Instance Method Details

#to_sql(*args) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/directive_record/query/sql.rb', line 9

def to_sql(*args)
  options = to_options(args)
  validate_options! options

  original_options = options.deep_dup
  original_options.reject!{|k, v| v.nil?}

  check_path_delimiter! options
  optimize_query! options

  prepare_options! options
  normalize_options! options, original_options

  parse_joins! options

  prepend_base_alias! options
  finalize_options! options

  flatten_options! options
  compose_sql options
end

#to_trend_sql(q1, q2, join_column_count, options) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/directive_record/query/sql.rb', line 31

def to_trend_sql(q1, q2, join_column_count, options)
  i      = join_column_count + 1
  select = "q1.*, q2.c#{i}, (((q1.c#{i} - q2.c#{i}) / ABS(q2.c#{i})) * 100) AS trend"
  on     = (1..join_column_count).to_a.collect{|x| "q1.c#{x} = q2.c#{x}"}.join(" AND ")
  order  = "\nORDER BY #{options[:order]}" if options[:order]
  limit  = "\nLIMIT #{options[:limit]}" if options[:limit]
  offset = "\nOFFSET #{options[:offset]}" if options[:offset]
<<-SQL
SELECT #{select}
FROM
(\n#{q1}\n) q1
INNER JOIN
(\n#{q2}\n) q2
ON #{on}#{order}#{limit}#{offset}
SQL
end