class Card::Query::AbstractQuery

superclass for CardQuery, ReferenceQuery, ActQuery, and ActionQuery

Each of the Query classes handle interpretation of hash “statements” into a number of objects known to the SqlStatement class, including @conditions, @joins, @comment, and the catch-all @mods

Sql queries involving multiple tables are made possible by the query hierarchy as tracked by subqueries (children) and superqueries (parents). For example, if one card links to another, then this can be represented as a CardQuery with a ReferenceQuery child that in turn has another CardQuery as its child.

See AbstractQuery::Tie for more on how tables can be connected.

Constants

ROOT_VAR_DEFAULTS

Attributes

comment[R]
conditions[R]
conditions_on_join[RW]
joins[RW]
mods[R]
negate[R]
statement[R]
subqueries[R]
superquery[R]
table_suffix[R]
vars[R]

Public Class Methods

new(statement, _comment=nil) click to toggle source
# File lib/card/query/abstract_query.rb, line 26
def initialize statement, _comment=nil
  @subqueries = []
  @conditions = []
  @joins = []
  @mods = {}

  @statement = statement.clone
  init_instance_vars :context, :superquery, :fasten, :negate
  init_root_vars
  table_alias
end

Public Instance Methods

context() click to toggle source
# File lib/card/query/abstract_query.rb, line 73
def context
  if !@context.nil?
    @context
  else
    @context = superquery ? superquery.context : ""
  end
end
depth() click to toggle source
# File lib/card/query/abstract_query.rb, line 81
def depth
  @depth ||= case
             when !superquery       then 0
             when fasten == :direct then superquery.depth
             else                        superquery.depth + 1
             end
end
full?() click to toggle source
# File lib/card/query/abstract_query.rb, line 44
def full?
  false
end
interpret(hash) click to toggle source
# File lib/card/query/abstract_query.rb, line 38
def interpret hash
  hash.each do |action, card|
    send action, card
  end
end
root() click to toggle source
# File lib/card/query/abstract_query.rb, line 56
def root
  return @root unless @root.nil?

  @root = @superquery ? @superquery.root : self
end
root?() click to toggle source
# File lib/card/query/abstract_query.rb, line 62
def root?
  root == self
end
sql() click to toggle source
# File lib/card/query/abstract_query.rb, line 48
def sql
  @sql ||= sql_statement.build.to_s
end
sql_statement() click to toggle source
# File lib/card/query/abstract_query.rb, line 52
def sql_statement
  SqlStatement.new self
end
subquery(opts={}) click to toggle source
# File lib/card/query/abstract_query.rb, line 66
def subquery opts={}
  klass = opts.delete(:class) || Query
  subquery = klass.new opts.merge(superquery: self)
  @subqueries << subquery
  subquery
end

Private Instance Methods

init_instance_vars(*varnames) click to toggle source
# File lib/card/query/abstract_query.rb, line 91
def init_instance_vars *varnames
  varnames.each do |varname|
    instance_variable_set "@#{varname}", (@statement.delete(varname) || nil)
  end
end
init_root_vars() click to toggle source
# File lib/card/query/abstract_query.rb, line 97
def init_root_vars
  ROOT_VAR_DEFAULTS.each do |varname, default|
    val = root_var_value varname, default
    val = val.symbolize_keys if varname == :vars
    instance_variable_set "@#{varname}", val
  end
end
root_var_value(varname, default) click to toggle source
# File lib/card/query/abstract_query.rb, line 105
def root_var_value varname, default
  if root?
    @statement.delete(varname) || default
  else
    root.send varname
  end
end