Class: DirectiveRecord::Query::SQL
- Inherits:
-
Object
- Object
- DirectiveRecord::Query::SQL
- Defined in:
- lib/directive_record/query/sql.rb
Instance Method Summary collapse
-
#initialize(base) ⇒ SQL
constructor
A new instance of SQL.
- #to_sql(*args) ⇒ Object
- #to_trend_sql(q1, q2, join_column_count, options) ⇒ Object
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) = (args) = .deep_dup .reject!{|k, v| v.nil?} check_path_delimiter! optimize_query! , parse_joins! prepend_base_alias! compose_sql 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, ) 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 #{[:order]}" if [:order] limit = "\nLIMIT #{[:limit]}" if [:limit] offset = "\nOFFSET #{[:offset]}" if [:offset] <<-SQL SELECT #{select} FROM (\n#{q1}\n) q1 INNER JOIN (\n#{q2}\n) q2 ON #{on}#{order}#{limit}#{offset} SQL end |