Module: Card::Query
- Defined in:
- card/lib/card/query.rb,
card/lib/card/query/join.rb,
card/lib/card/query/value.rb,
card/lib/card/query/clause.rb,
card/lib/card/query/act_query.rb,
card/lib/card/query/card_class.rb,
card/lib/card/query/card_query.rb,
card/lib/card/query/action_query.rb,
card/lib/card/query/sql_statement.rb,
card/lib/card/query/abstract_query.rb,
card/lib/card/query/card_query/run.rb,
card/lib/card/query/reference_query.rb,
card/lib/card/query/card_query/custom.rb,
card/lib/card/query/value/match_value.rb,
card/lib/card/query/abstract_query/tie.rb,
card/lib/card/query/card_query/sorting.rb,
card/lib/card/query/card_query/found_by.rb,
card/lib/card/query/sql_statement/joins.rb,
card/lib/card/query/sql_statement/order.rb,
card/lib/card/query/sql_statement/where.rb,
card/lib/card/query/card_query/conjunctions.rb,
card/lib/card/query/card_query/normalization.rb,
card/lib/card/query/card_query/interpretation.rb,
card/lib/card/query/abstract_query/query_helper.rb,
card/lib/card/query/card_query/match_attributes.rb,
card/lib/card/query/card_query/reference_attributes.rb,
card/lib/card/query/card_query/relational_attributes.rb
Overview
Card::Query is for finding implicit lists (or counts of lists) of cards.
Search and Set cards use Card::Query to query the database, and it’s also frequently used directly in code.
Query “statements” (objects, really) are made in CQL (Card Query Language). Because CQL is used by Sharks, the primary CQL Syntax documentation is on decko.org. Note that the examples there are in JSON, like Search card content, but statements in Card::Query are in ruby form.
In Decko’s current form, Card::Query generates and executes SQL statements. However, the SQL generation is largely (not yet fully) separated from the CQL statement interpretation.
The most common way to use Card::Query is as follows:
list_of_cards = Card::Query.run(statement)
This is equivalent to:
query = Card::Query.new(statement)
list_of_cards = query.run
Upon initiation, the query is interpreted, and the following key objects are populated:
- @join - an Array of Card::Query::Join objects
- @conditions - an Array of conditions
- @mod - a Hash of other query-altering keys
- @subqueries - a list of other queries nested within this one
Each condition is either a SQL-ready string (boo) or an Array in this form:
[field_string_or_sym, (Card::Value::Query object)]
Defined Under Namespace
Modules: CardClass, Clause Classes: AbstractQuery, ActQuery, ActionQuery, CardQuery, Join, ReferenceQuery, SqlStatement, Value
Constant Summary collapse
- CONJUNCTIONS =
{ any: :or, in: :or, or: :or, all: :and, and: :and }.freeze
- MODIFIERS =
“dir” is DEPRECATED in favor of sort_dir “sort” is DEPRECATED in favor of sort_by, except in cases where sort’s value is a hash
%i[conj return sort_by sort_as sort_dir sort dir group limit offset] .each_with_object({}) { |v, h| h[v] = nil }
- OPERATORS =
%w[!= = =~ < > in ~ is].each_with_object({}) { |v, h| h[v] = v }.merge( { eq: "=", gt: ">", lt: "<", match: "~", ne: "!=", "not in": "not in", "is not": "is not", "!": "is not" }.stringify_keys )
- DEFAULT_ORDER_DIRS =
{ update: "desc", relevance: "desc" }.freeze
Class Attribute Summary collapse
-
.attributes ⇒ Object
Returns the value of attribute attributes.
Class Method Summary collapse
-
.class_for(type) ⇒ Object
-
.new(statement, comment = nil) ⇒ Object
-
.run(statement, comment = nil) ⇒ Object
-
.safe_sql(txt) ⇒ Object
Class Attribute Details
.attributes ⇒ Object
Returns the value of attribute attributes.
98 99 100 |
# File 'card/lib/card/query.rb', line 98 def attributes @attributes end |
Class Method Details
.class_for(type) ⇒ Object
108 109 110 |
# File 'card/lib/card/query.rb', line 108 def class_for type const_get "#{type.capitalize}Query" end |
.new(statement, comment = nil) ⇒ Object
100 101 102 |
# File 'card/lib/card/query.rb', line 100 def new statement, comment=nil Query::CardQuery.new statement, comment end |
.run(statement, comment = nil) ⇒ Object
104 105 106 |
# File 'card/lib/card/query.rb', line 104 def run statement, comment=nil new(statement, comment).run end |
.safe_sql(txt) ⇒ Object
112 113 114 115 116 117 |
# File 'card/lib/card/query.rb', line 112 def safe_sql txt txt = txt.to_s raise "CQL contains disallowed characters: #{txt}" if txt.match?(/[^\w\s*().,]/) txt end |