Class: Card::Query::SqlStatement

Inherits:
Object
  • Object
show all
Includes:
Joins, Order, Where
Defined in:
lib/card/query/sql_statement.rb,
lib/card/query/sql_statement/joins.rb,
lib/card/query/sql_statement/order.rb,
lib/card/query/sql_statement/where.rb

Overview

convert @query sort rep into order by statement order information is stored in @mods[:sort], @mods[:sort_as], and @mods[:dir]

Defined Under Namespace

Modules: Joins, Order, Where

Constant Summary collapse

ORDER_MAP =
{
  "id"        => "id",
  "update"    => "updated_at",
  "create"    => "created_at",
  "name"      => "key",
  "content"   => "db_content",
  "alpha"     => "key",       # DEPRECATED
  "relevance" => "updated_at" # DEPRECATED
}.freeze

Instance Method Summary collapse

Methods included from Order

#order, #order_as, #order_config, #order_dir, #order_directive, #order_directives, #order_field

Methods included from Where

#basic_conditions, #condition_joint, #conditions_from, #exist_condition, #explicit_conditions, #format_conditions, #implicit_conditions, #in_condition, #maybe_not, #permission_conditions, #spaced_subquery_sql, #standard_condition, #trash_condition, #where

Methods included from Joins

#join_clause, #join_clause_parts, #join_table, #joins, #joins_on_query, #on_card_conditions, #on_clause, #subjoins

Constructor Details

#initialize(query = nil) ⇒ SqlStatement

Returns a new instance of SqlStatement.



18
19
20
21
# File 'lib/card/query/sql_statement.rb', line 18

def initialize query=nil
  @query = query
  @mods = query && query.mods
end

Instance Method Details

#buildObject



23
24
25
26
27
28
29
30
31
32
# File 'lib/card/query/sql_statement.rb', line 23

def build
  @fields = fields
  @tables = tables
  @joins  = joins
  @where  = where
  @group  = group
  @order  = order
  @limit_and_offset = limit_and_offset
  self
end

#cast_type(type) ⇒ Object



115
116
117
118
# File 'lib/card/query/sql_statement.rb', line 115

def cast_type type
  cxn ||= ActiveRecord::Base.connection
  (val = cxn.cast_types[type.to_sym]) ? val[:name] : safe_sql(type)
end

#commentObject



52
53
54
55
# File 'lib/card/query/sql_statement.rb', line 52

def comment
  return nil unless Card.config.sql_comments && @query.comment
  "/* #{@query.comment} */\n"
end

#fieldsObject



61
62
63
64
65
66
67
# File 'lib/card/query/sql_statement.rb', line 61

def fields
  table = @query.table_alias
  field = @mods[:return] unless @mods[:return] =~ /^_\w+/
  field = field.blank? ? :card : field.to_sym
  field = full_field(table, field)
  [field, @mods[:sort_join_field]].compact * ", "
end

#fromObject



44
45
46
# File 'lib/card/query/sql_statement.rb', line 44

def from
  "FROM #{@tables}"
end

#full_field(table, field) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/card/query/sql_statement.rb', line 69

def full_field table, field
  case field
  when :raw      then "#{table}.*"
  when :card     then "#{table}.*"
  when :content  then "#{table}.db_content"
  when :count
    "coalesce(count( distinct #{table}.id),0) as count"
  else
    if ATTRIBUTES[field.to_sym] == :basic
      "#{table}.#{field}"
    else
      safe_sql field
    end
  end
end

#full_syntaxObject



102
103
104
# File 'lib/card/query/sql_statement.rb', line 102

def full_syntax
  @query.full? ? yield : return
end

#groupObject



85
86
87
88
# File 'lib/card/query/sql_statement.rb', line 85

def group
  group = @mods[:group]
  "GROUP BY #{safe_sql group}" if group.present?
end

#leading_spaceObject



48
49
50
# File 'lib/card/query/sql_statement.rb', line 48

def leading_space
  " " * (@query.depth * 2)
end

#limit_and_offsetObject



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/card/query/sql_statement.rb', line 90

def limit_and_offset
  full_syntax do
    limit = @mods[:limit]
    offset = @mods[:offset]
    if limit.to_i > 0
      string =  "LIMIT  #{limit.to_i} "
      string += "OFFSET #{offset.to_i} " if offset.present?
      string
    end
  end
end

#safe_sql(txt) ⇒ Object



106
107
108
109
110
111
112
113
# File 'lib/card/query/sql_statement.rb', line 106

def safe_sql txt
  txt = txt.to_s
  if txt =~ /[^\w\*\(\)\s\.\,]/
    raise "WQL contains disallowed characters: #{txt}"
  else
    txt
  end
end

#selectObject



40
41
42
# File 'lib/card/query/sql_statement.rb', line 40

def select
  "#{leading_space}SELECT DISTINCT #{@fields}"
end

#tablesObject



57
58
59
# File 'lib/card/query/sql_statement.rb', line 57

def tables
  "#{@query.table} #{@query.table_alias}"
end

#to_sObject



34
35
36
37
38
# File 'lib/card/query/sql_statement.rb', line 34

def to_s
  [
    comment, select, from, @joins, @where, @group, @order, @limit_and_offset
  ].compact.join " "
end