Class: Card::Query::AbstractQuery
- Inherits:
-
Object
- Object
- Card::Query::AbstractQuery
- 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
Defined Under Namespace
Modules: QueryHelper, Tie
Instance Attribute Summary collapse
-
#comment ⇒ Object
readonly
Returns the value of attribute comment.
-
#conditions ⇒ Object
readonly
Returns the value of attribute conditions.
-
#conditions_on_join ⇒ Object
Returns the value of attribute conditions_on_join.
-
#joins ⇒ Object
Returns the value of attribute joins.
-
#mods ⇒ Object
readonly
Returns the value of attribute mods.
-
#negate ⇒ Object
readonly
Returns the value of attribute negate.
-
#statement ⇒ Object
readonly
Returns the value of attribute statement.
-
#subqueries ⇒ Object
readonly
Returns the value of attribute subqueries.
-
#superquery ⇒ Object
readonly
Returns the value of attribute superquery.
-
#table_seq ⇒ Object
Returns the value of attribute table_seq.
-
#vars ⇒ Object
readonly
Returns the value of attribute vars.
Instance Method Summary collapse
- #context ⇒ Object
- #depth ⇒ Object
- #full? ⇒ Boolean
- #init_instance_vars(*varnames) ⇒ Object
- #init_query_vars ⇒ Object
-
#initialize(statement, _comment = nil) ⇒ AbstractQuery
constructor
A new instance of AbstractQuery.
- #interpret(hash) ⇒ Object
- #root ⇒ Object
- #root? ⇒ Boolean
- #sql ⇒ Object
- #subquery(opts = {}) ⇒ Object
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
#comment ⇒ Object (readonly)
Returns the value of attribute comment.
20 21 22 |
# File 'lib/card/query/abstract_query.rb', line 20 def comment @comment end |
#conditions ⇒ Object (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_join ⇒ Object
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 |
#joins ⇒ Object
Returns the value of attribute joins.
22 23 24 |
# File 'lib/card/query/abstract_query.rb', line 22 def joins @joins end |
#mods ⇒ Object (readonly)
Returns the value of attribute mods.
20 21 22 |
# File 'lib/card/query/abstract_query.rb', line 20 def mods @mods end |
#negate ⇒ Object (readonly)
Returns the value of attribute negate.
20 21 22 |
# File 'lib/card/query/abstract_query.rb', line 20 def negate @negate end |
#statement ⇒ Object (readonly)
Returns the value of attribute statement.
20 21 22 |
# File 'lib/card/query/abstract_query.rb', line 20 def statement @statement end |
#subqueries ⇒ Object (readonly)
Returns the value of attribute subqueries.
20 21 22 |
# File 'lib/card/query/abstract_query.rb', line 20 def subqueries @subqueries end |
#superquery ⇒ Object (readonly)
Returns the value of attribute superquery.
20 21 22 |
# File 'lib/card/query/abstract_query.rb', line 20 def superquery @superquery end |
#table_seq ⇒ Object
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 |
#vars ⇒ Object (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
#context ⇒ Object
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 |
#depth ⇒ Object
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
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_vars ⇒ Object
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 |
#root ⇒ Object
63 64 65 |
# File 'lib/card/query/abstract_query.rb', line 63 def root @root ||= @superquery ? @superquery.root : self end |
#root? ⇒ Boolean
67 68 69 |
# File 'lib/card/query/abstract_query.rb', line 67 def root? root == self end |
#sql ⇒ Object
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 |