Module: Olaf::QueryDefinition::ClassMethods

Defined in:
lib/olaf/query_definition/class_methods.rb

Instance Method Summary collapse

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, options = {})
  return if arguments.key?(name)

  arguments[name] = { literal: options[:as] == :literal }

  name
end

#argumentsObject

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

#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


73
74
75
# File 'lib/olaf/query_definition/class_methods.rb', line 73

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