Module: Olaf::QueryDefinition::ClassMethods
- Defined in:
- lib/olaf/query_definition/class_methods.rb
Instance Method Summary collapse
-
#argument(name, options = {}) ⇒ Object
Define an argument for the query matching a placeholder in the template.
-
#arguments ⇒ Object
Returns ALL the arguments defined for the query, including options.
-
#driver(name = nil, **options) ⇒ Object
Define the driver this query runs on, one of the names given to
Olaf.configure. -
#driver_options ⇒ Object
@return Hash of the options given to
driver. -
#prepare(**vars) ⇒ Object
Creates a new instance of the Query defined and validates the parameters passed, leaving the instance in a ready-to-execute state.
-
#row_object(object_class = nil) ⇒ Object
Define the object representing each row of the result.
-
#template(file_name = nil) ⇒ Object
Define the file path to the SQL template for this query.
Instance Method Details
#argument(name, options = {}) ⇒ Object
Define an argument for the query matching a placeholder in the template. The Sequel gem, will fill the placeholders in the SQL query escaping the value passed as an argument. Sometimes, we need to pass an argument as literal to avoid the single quotes added by Sequel (i.e. sending a table_name)
options - Hash config for each argument :as - Argument Type * :literal - Forces a string substitution with the literal value
Example:
class OneQuery
include Snowflake::QueryDefinition
template 'reports/one.sql'
argument :dealersip_id
argument :table_name, as: :literal
end
38 39 40 41 42 43 44 |
# File 'lib/olaf/query_definition/class_methods.rb', line 38 def argument(name, = {}) return if arguments.key?(name) arguments[name] = { literal: [:as] == :literal } name end |
#arguments ⇒ Object
Returns ALL the arguments defined for the query, including options. @return Hash
13 14 15 |
# File 'lib/olaf/query_definition/class_methods.rb', line 13 def arguments @arguments ||= {} end |
#driver(name = nil, **options) ⇒ Object
Define the driver this query runs on, one of the names given to
Olaf.configure. Defaults to the default driver.
Options are driver specific: the BigQuery driver reads :maximum_bytes_billed.
Example:
class OneQuery
include Olaf::QueryDefinition
driver :big_query, maximum_bytes_billed: 5 * Olaf::BigQuery::GIGABYTE
end
76 77 78 79 80 81 82 83 |
# File 'lib/olaf/query_definition/class_methods.rb', line 76 def driver(name = nil, **) return @driver if name.nil? @driver ||= name @driver_options ||= @driver end |
#driver_options ⇒ Object
@return Hash of the options given to driver
86 87 88 |
# File 'lib/olaf/query_definition/class_methods.rb', line 86 def @driver_options ||= {} end |
#prepare(**vars) ⇒ Object
Creates a new instance of the Query defined and validates the parameters passed, leaving the instance in a ready-to-execute state. @return Snowflake::QueryDefinition instance
7 8 9 |
# File 'lib/olaf/query_definition/class_methods.rb', line 7 def prepare(**vars) new(**vars).prepare end |
#row_object(object_class = nil) ⇒ Object
Define the object representing each row of the result. When not specified, each row will be a hash by default.
Example:
class OneQuery
include Snowflake::QueryDefinition
row_object MyOwnObject
end
102 103 104 |
# File 'lib/olaf/query_definition/class_methods.rb', line 102 def row_object(object_class = nil) @row_object ||= object_class end |
#template(file_name = nil) ⇒ Object
Define the file path to the SQL template for this query.
Example:
class OneQuery
include Snowflake::QueryDefinition
template 'reports/one.sql'
end
57 58 59 |
# File 'lib/olaf/query_definition/class_methods.rb', line 57 def template(file_name = nil) @template ||= file_name end |