Class: Guacamole::AqlQuery

Inherits:
Query
  • Object
show all
Defined in:
lib/guacamole/aql_query.rb

Overview

Build an AQL query for ArangoDB

Instance Attribute Summary collapse

Attributes inherited from Query

#connection, #example, #mapper

Instance Method Summary collapse

Methods inherited from Query

#==, #each, #limit, #skip

Constructor Details

#initialize(collection, mapper, options = {}) ⇒ AqlQuery

Create a new AqlQuery

Parameters:

  • collection (Guacamole::Collection)

    The collection class to be used

  • mapper (Class)

    The class of the mapper to use

  • options (Hash) (defaults to: {})

    Additional options for query execution

Options Hash (options):

  • :return_as (String) — default: 'RETURN #{model_name}'

    A custom RETURN statement

  • :mapping (Boolean) — default: true

    Should the mapping be performed?



22
23
24
25
26
# File 'lib/guacamole/aql_query.rb', line 22

def initialize(collection, mapper, options = {})
  @collection = collection
  super(collection.connection.query, mapper)
  @options = default_options.merge(options)
end

Instance Attribute Details

#aql_fragmentObject

The AQL fragment to be added into the complete query



7
8
9
# File 'lib/guacamole/aql_query.rb', line 7

def aql_fragment
  @aql_fragment
end

#collectionObject (readonly)

The associated collection



10
11
12
# File 'lib/guacamole/aql_query.rb', line 10

def collection
  @collection
end

#optionsObject (readonly)

The additional options



13
14
15
# File 'lib/guacamole/aql_query.rb', line 13

def options
  @options
end

Instance Method Details

#aql_stringString

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

Returns:

  • (String)

    An AQL string ready to be send to Arango



46
47
48
49
50
# File 'lib/guacamole/aql_query.rb', line 46

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_parametersHash

Get the bind parameters

Returns:

  • (Hash)

    All the bind parameters



38
39
40
# File 'lib/guacamole/aql_query.rb', line 38

def bind_parameters
  @options[:bind_vars]
end

#bind_parameters=(bind_parameters) ⇒ Object

Set the bind parameters

Parameters:

  • bind_parameters (Hash)

    All the bind parameters



31
32
33
# File 'lib/guacamole/aql_query.rb', line 31

def bind_parameters=(bind_parameters)
  @options[:bind_vars] = bind_parameters
end

#default_optionsHash

The default options to be set for the query

Returns:

  • (Hash)

    The default options



73
74
75
76
77
78
79
# File 'lib/guacamole/aql_query.rb', line 73

def default_options
  {
    return_as: "RETURN #{model_name}",
    for_in:    "FOR #{model_name} IN #{collection_name}",
    mapping:   true
  }
end

#for_inObject



59
60
61
# File 'lib/guacamole/aql_query.rb', line 59

def for_in
  options[:for_in]
end

#perform_mapping?Boolean

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

Returns:

  • (Boolean)

    Either if the mapping should be perfomed or not



66
67
68
# File 'lib/guacamole/aql_query.rb', line 66

def perform_mapping?
  options[:mapping]
end

#return_asString

The RETURN part of the query

Returns:

  • (String)

    Either the default RETURN model_name or a custom string



55
56
57
# File 'lib/guacamole/aql_query.rb', line 55

def return_as
  options[:return_as]
end