class Guacamole::AqlQuery

Build an AQL query for ArangoDB

Attributes

aql_fragment[RW]

The AQL fragment to be added into the complete query

collection[R]

The associated collection

options[R]

The additional options

Public Class Methods

new(collection, mapper, options = {}) click to toggle source

Create a new AqlQuery

@param [Guacamole::Collection] collection The collection class to be used @param [Class] mapper The class of the mapper to use @param [Hash] options Additional options for query execution @option options [String] :return_as (‘RETURN #{model_name}’) A custom ‘RETURN` statement @option options [Boolean] :mapping (true) Should the mapping be performed?

Calls superclass method
# File lib/guacamole/aql_query.rb, line 21
def initialize(collection, mapper, options = {})
  @collection = collection
  super(collection.connection.query, mapper)
  @options = default_options.merge(options)
end

Public Instance Methods

aql_string() click to toggle source

Creates an AQL string based on the ‘aql_fragment`, the collection and model information.

@return [String] An AQL string ready to be send to Arango

# File lib/guacamole/aql_query.rb, line 45
def aql_string
  aql_string = "#{for_in} #{aql_fragment} #{return_as}"
  Guacamole.logger.debug "[AQL] #{aql_string} | bind_parameters: #{bind_parameters}"
  aql_string
end
bind_parameters() click to toggle source

Get the bind parameters

@return [Hash] All the bind parameters

# File lib/guacamole/aql_query.rb, line 37
def bind_parameters
  @options[:bind_vars]
end
bind_parameters=(bind_parameters) click to toggle source

Set the bind parameters

@param [Hash] bind_parameters All the bind parameters

# File lib/guacamole/aql_query.rb, line 30
def bind_parameters=(bind_parameters)
  @options[:bind_vars] = bind_parameters
end
default_options() click to toggle source

The default options to be set for the query

@return [Hash] The default options

# File lib/guacamole/aql_query.rb, line 72
def default_options
  {
    return_as: "RETURN #{model_name}",
    for_in:    "FOR #{model_name} IN #{collection_name}",
    mapping:   true
  }
end
for_in() click to toggle source
# File lib/guacamole/aql_query.rb, line 58
def for_in
  options[:for_in]
end
perform_mapping?() click to toggle source

Should the mapping step be perfomed? If set to false we will return the raw document.

@return [Boolean] Either if the mapping should be perfomed or not

# File lib/guacamole/aql_query.rb, line 65
def perform_mapping?
  options[:mapping]
end
return_as() click to toggle source

The RETURN part of the query

@return [String] Either the default ‘RETURN model_name` or a custom string

# File lib/guacamole/aql_query.rb, line 54
def return_as
  options[:return_as]
end

Private Instance Methods

collection_name() click to toggle source

The name of the collection to be used in the query

# File lib/guacamole/aql_query.rb, line 88
def collection_name
  collection.collection_name
end
iterator_without_mapping(&block) click to toggle source

An iterator to be used if no mapping should be performed

# File lib/guacamole/aql_query.rb, line 101
def iterator_without_mapping(&block)
  ->(document) { block.call document }
end
model_name() click to toggle source

The name of the model to be used in the query

# File lib/guacamole/aql_query.rb, line 83
def model_name
  mapper.model_class.model_name.element
end
perfom_query(iterator_with_mapping, &block) click to toggle source

Executes an AQL query with bind parameters

@see Query#perfom_query

# File lib/guacamole/aql_query.rb, line 95
def perfom_query(iterator_with_mapping, &block)
  iterator = perform_mapping? ? iterator_with_mapping : iterator_without_mapping(&block)
  connection.execute(aql_string, options).each(&iterator)
end