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

Constant Summary collapse

ROOT_VAR_DEFAULTS =
{ vars: {}, table_suffix: "" }.freeze

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, #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

#commentObject (readonly)

Returns the value of attribute comment.



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

def comment
  @comment
end

#conditionsObject (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_joinObject

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

#joinsObject

Returns the value of attribute joins.



24
25
26
# File 'lib/card/query/abstract_query.rb', line 24

def joins
  @joins
end

#modsObject (readonly)

Returns the value of attribute mods.



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

def mods
  @mods
end

#negateObject (readonly)

Returns the value of attribute negate.



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

def negate
  @negate
end

#statementObject (readonly)

Returns the value of attribute statement.



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

def statement
  @statement
end

#subqueriesObject (readonly)

Returns the value of attribute subqueries.



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

def subqueries
  @subqueries
end

#superqueryObject (readonly)

Returns the value of attribute superquery.



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

def superquery
  @superquery
end

#table_suffixObject (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

#varsObject (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

#contextObject



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

#depthObject



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

Returns:

  • (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

#rootObject



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

Returns:

  • (Boolean)


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

def root?
  root == self
end

#sqlObject



48
49
50
# File 'lib/card/query/abstract_query.rb', line 48

def sql
  @sql ||= sql_statement.build.to_s
end

#sql_statementObject



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