Class: Card::Query::AbstractQuery

Inherits:
Object
  • Object
show all
Includes:
QueryHelper, Tie
Defined in:
lib/card/query/abstract_query.rb,
lib/card/query/abstract_query/tie.rb,
lib/card/query/abstract_query/query_helper.rb

Overview

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.

Direct Known Subclasses

ActQuery, ActionQuery, CardQuery, ReferenceQuery

Defined Under Namespace

Modules: QueryHelper, Tie

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Tie

#fasten, #fasten_tie, #id_from_val, #inherit_fasten, #left_join?, #negate_join, #restrict, #super_conditions, #superfield, #tie, #tie_subquery, #tie_with_exist, #tie_with_in, #tie_with_join, #tie_with_join_args

Methods included from QueryHelper

#add_condition, #current_conjunction, #direct_subqueries, #fld, #next_table_suffix, #subqueries_with_fasten, #table_alias

Constructor Details

#initialize(statement, _comment = nil) ⇒ AbstractQuery

Returns a new instance of AbstractQuery.



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

def initialize statement, _comment=nil
  @subqueries = []
  @conditions = []
  @joins = []
  @mods = {}

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

Instance Attribute Details

#commentObject (readonly)

Returns the value of attribute comment.



20
21
22
# File 'lib/card/query/abstract_query.rb', line 20

def comment
  @comment
end

#conditionsObject (readonly)

Returns the value of attribute conditions.



20
21
22
# File 'lib/card/query/abstract_query.rb', line 20

def conditions
  @conditions
end

#conditions_on_joinObject

Returns the value of attribute conditions_on_join.



22
23
24
# File 'lib/card/query/abstract_query.rb', line 22

def conditions_on_join
  @conditions_on_join
end

#joinsObject

Returns the value of attribute joins.



22
23
24
# File 'lib/card/query/abstract_query.rb', line 22

def joins
  @joins
end

#modsObject (readonly)

Returns the value of attribute mods.



20
21
22
# File 'lib/card/query/abstract_query.rb', line 20

def mods
  @mods
end

#negateObject (readonly)

Returns the value of attribute negate.



20
21
22
# File 'lib/card/query/abstract_query.rb', line 20

def negate
  @negate
end

#statementObject (readonly)

Returns the value of attribute statement.



20
21
22
# File 'lib/card/query/abstract_query.rb', line 20

def statement
  @statement
end

#subqueriesObject (readonly)

Returns the value of attribute subqueries.



20
21
22
# File 'lib/card/query/abstract_query.rb', line 20

def subqueries
  @subqueries
end

#superqueryObject (readonly)

Returns the value of attribute superquery.



20
21
22
# File 'lib/card/query/abstract_query.rb', line 20

def superquery
  @superquery
end

#table_seqObject

Returns the value of attribute table_seq.



22
23
24
# File 'lib/card/query/abstract_query.rb', line 22

def table_seq
  @table_seq
end

#varsObject (readonly)

Returns the value of attribute vars.



20
21
22
# File 'lib/card/query/abstract_query.rb', line 20

def vars
  @vars
end

Instance Method Details

#contextObject



78
79
80
81
82
83
84
# File 'lib/card/query/abstract_query.rb', line 78

def context
  if !@context.nil?
    @context
  else
    @context = superquery ? superquery.context : ""
  end
end

#depthObject



86
87
88
89
90
91
92
# File 'lib/card/query/abstract_query.rb', line 86

def depth
  @depth ||= case
             when !superquery       then 0
             when fasten == :direct then superquery.depth
             else                        superquery.depth + 1
             end
end

#full?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/card/query/abstract_query.rb', line 55

def full?
  false
end

#init_instance_vars(*varnames) ⇒ Object



36
37
38
39
40
# File 'lib/card/query/abstract_query.rb', line 36

def init_instance_vars *varnames
  varnames.each do |varname|
    instance_variable_set "@#{varname}", (@statement.delete(varname) || nil)
  end
end

#init_query_varsObject



42
43
44
45
46
47
# File 'lib/card/query/abstract_query.rb', line 42

def init_query_vars
  if (v = @statement.delete :vars) then v.symbolize_keys
  elsif @superquery                then @superquery.vars
  else                                  {}
  end
end

#interpret(hash) ⇒ Object



49
50
51
52
53
# File 'lib/card/query/abstract_query.rb', line 49

def interpret hash
  hash.each do |action, card|
    send action, card
  end
end

#rootObject



63
64
65
# File 'lib/card/query/abstract_query.rb', line 63

def root
  @root ||= @superquery ? @superquery.root : self
end

#root?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/card/query/abstract_query.rb', line 67

def root?
  root == self
end

#sqlObject



59
60
61
# File 'lib/card/query/abstract_query.rb', line 59

def sql
  @sql ||= Query::SqlStatement.new(self).build.to_s
end

#subquery(opts = {}) ⇒ Object



71
72
73
74
75
76
# File 'lib/card/query/abstract_query.rb', line 71

def subquery opts={}
  klass = opts.delete(:class) || Query
  subquery = klass.new opts.merge(superquery: self)
  @subqueries << subquery
  subquery
end