Module: Card::Query::SqlStatement::Where

Included in:
Card::Query::SqlStatement
Defined in:
lib/card/query/sql_statement/where.rb

Overview

handle where clause in SqlStatement

Instance Method Summary collapse

Instance Method Details

#basic_conditions(conditions) ⇒ Object

the conditions stored in the query’s @conditions variable



52
53
54
55
56
57
58
59
# File 'lib/card/query/sql_statement/where.rb', line 52

def basic_conditions conditions
  conditions.map do |condition|
    case condition
    when String    then condition
    when Array     then standard_condition(condition)
    end
  end
end

#condition_joint(query) ⇒ Object



96
97
98
# File 'lib/card/query/sql_statement/where.rb', line 96

def condition_joint query
  " #{query.current_conjunction.upcase} "
end

#conditions_from(subqueries) ⇒ Object

depending on how a query is “fastened”, its conditions may be rendered along with the superquery’s



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

def conditions_from subqueries
  subqueries.map do |query|
    next if query.conditions_on_join

    case query.fasten
    when :exist     then exist_condition query
    when :in        then in_condition query
    else                 explicit_conditions query
    end
  end
end

#exist_condition(query) ⇒ Object



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

def exist_condition query
  "#{maybe_not query}EXISTS (#{spaced_subquery_sql query})"
end

#explicit_conditions(query) ⇒ Object

conditions explicitly specified in the query object



13
14
15
16
17
18
# File 'lib/card/query/sql_statement/where.rb', line 13

def explicit_conditions query
  cond_list = basic_conditions query.conditions
  cond_list += conditions_from query.subqueries
  cond_list.reject!(&:blank?)
  format_conditions cond_list, query
end

#format_conditions(cond_list, query) ⇒ Object

convert list of conditions to string



88
89
90
91
92
93
94
# File 'lib/card/query/sql_statement/where.rb', line 88

def format_conditions cond_list, query
  if cond_list.size > 1
    "(#{cond_list.join condition_joint(query)})"
  else
    cond_list.join
  end
end

#implicit_conditions(query) ⇒ Object

handle trash and permissions only applies to card queries



68
69
70
71
72
73
# File 'lib/card/query/sql_statement/where.rb', line 68

def implicit_conditions query
  return unless query.is_a?(CardQuery)

  table = query.table_alias
  [trash_condition(table), permission_conditions(table)].compact * " AND "
end

#in_condition(query) ⇒ Object



42
43
44
45
# File 'lib/card/query/sql_statement/where.rb', line 42

def in_condition query
  field = query.mods[:in_field]
  "#{field} #{maybe_not query}IN (#{spaced_subquery_sql query})"
end

#maybe_not(query) ⇒ Object



38
39
40
# File 'lib/card/query/sql_statement/where.rb', line 38

def maybe_not query
  query.negate ? "NOT " : ""
end

#permission_conditions(table) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/card/query/sql_statement/where.rb', line 79

def permission_conditions table
  return if Auth.always_ok?

  read_rules = Auth.as_card.read_rules
  read_rule_list = read_rules.present? ? read_rules.join(",") : 1
  "#{table}.read_rule_id IN (#{read_rule_list})"
end

#spaced_subquery_sql(subquery) ⇒ Object



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

def spaced_subquery_sql subquery
  "\n#{subquery.sql}\n#{leading_space}"
end

#standard_condition(condition) ⇒ Object



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

def standard_condition condition
  field, val = condition
  val.to_sql field
end

#trash_condition(table) ⇒ Object



75
76
77
# File 'lib/card/query/sql_statement/where.rb', line 75

def trash_condition table
  "#{table}.trash is false"
end

#where(explicit = true) ⇒ Object



6
7
8
9
10
# File 'lib/card/query/sql_statement/where.rb', line 6

def where explicit=true
  conditions = [explicit_conditions(@query), implicit_conditions(@query)]
  conditions = conditions.reject(&:blank?).join " AND "
  "#{'WHERE ' if explicit}#{conditions}" unless conditions.blank?
end