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
Constant Summary collapse
- ROOT_VAR_DEFAULTS =
{ vars: {}, table_suffix: "" }.freeze
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_suffix ⇒ Object
readonly
Returns the value of attribute table_suffix.
-
#vars ⇒ Object
readonly
Returns the value of attribute vars.
Instance Method Summary collapse
- #context ⇒ Object
- #depth ⇒ Object
- #full? ⇒ Boolean
-
#initialize(statement, _comment = nil) ⇒ AbstractQuery
constructor
A new instance of AbstractQuery.
- #interpret(hash) ⇒ Object
- #root ⇒ Object
- #root? ⇒ Boolean
- #sql ⇒ Object
- #sql_statement ⇒ 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, #subqueries_with_fasten, #table_alias, #table_seq
Constructor Details
#initialize(statement, _comment = nil) ⇒ AbstractQuery
Returns a new instance of AbstractQuery.
26 27 28 29 30 31 32 33 34 35 36 |
# 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 |
Instance Attribute Details
#comment ⇒ Object (readonly)
Returns the value of attribute comment.
22 23 24 |
# File 'lib/card/query/abstract_query.rb', line 22 def comment @comment end |
#conditions ⇒ Object (readonly)
Returns the value of attribute conditions.
22 23 24 |
# File 'lib/card/query/abstract_query.rb', line 22 def conditions @conditions end |
#conditions_on_join ⇒ Object
Returns the value of attribute conditions_on_join.
24 25 26 |
# File 'lib/card/query/abstract_query.rb', line 24 def conditions_on_join @conditions_on_join end |
#joins ⇒ Object
Returns the value of attribute joins.
24 25 26 |
# File 'lib/card/query/abstract_query.rb', line 24 def joins @joins end |
#mods ⇒ Object (readonly)
Returns the value of attribute mods.
22 23 24 |
# File 'lib/card/query/abstract_query.rb', line 22 def mods @mods end |
#negate ⇒ Object (readonly)
Returns the value of attribute negate.
22 23 24 |
# File 'lib/card/query/abstract_query.rb', line 22 def negate @negate end |
#statement ⇒ Object (readonly)
Returns the value of attribute statement.
22 23 24 |
# File 'lib/card/query/abstract_query.rb', line 22 def statement @statement end |
#subqueries ⇒ Object (readonly)
Returns the value of attribute subqueries.
22 23 24 |
# File 'lib/card/query/abstract_query.rb', line 22 def subqueries @subqueries end |
#superquery ⇒ Object (readonly)
Returns the value of attribute superquery.
22 23 24 |
# File 'lib/card/query/abstract_query.rb', line 22 def superquery @superquery end |
#table_suffix ⇒ Object (readonly)
Returns the value of attribute table_suffix.
22 23 24 |
# File 'lib/card/query/abstract_query.rb', line 22 def table_suffix @table_suffix end |
#vars ⇒ Object (readonly)
Returns the value of attribute vars.
22 23 24 |
# File 'lib/card/query/abstract_query.rb', line 22 def vars @vars end |
Instance Method Details
#context ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/card/query/abstract_query.rb', line 73 def context if !@context.nil? @context else @context = superquery ? superquery.context : "" end end |
#depth ⇒ Object
81 82 83 84 85 86 87 |
# 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? ⇒ Boolean
44 45 46 |
# File 'lib/card/query/abstract_query.rb', line 44 def full? false end |
#interpret(hash) ⇒ Object
38 39 40 41 42 |
# File 'lib/card/query/abstract_query.rb', line 38 def interpret hash hash.each do |action, card| send action, card end end |
#root ⇒ Object
56 57 58 59 60 |
# File 'lib/card/query/abstract_query.rb', line 56 def root return @root unless @root.nil? @root = @superquery ? @superquery.root : self end |
#root? ⇒ Boolean
62 63 64 |
# File 'lib/card/query/abstract_query.rb', line 62 def root? root == self end |
#sql ⇒ Object
48 49 50 |
# File 'lib/card/query/abstract_query.rb', line 48 def sql @sql ||= sql_statement.build.to_s end |
#sql_statement ⇒ Object
52 53 54 |
# File 'lib/card/query/abstract_query.rb', line 52 def sql_statement SqlStatement.new self end |
#subquery(opts = {}) ⇒ Object
66 67 68 69 70 71 |
# 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 |