Module: ActiveRecord::ConnectionAdapters::Elasticsearch::DatabaseStatements

Extended by:
ActiveSupport::Concern
Included in:
ActiveRecord::ConnectionAdapters::ElasticsearchAdapter
Defined in:
lib/active_record/connection_adapters/elasticsearch/database_statements.rb

Overview

extend adapter with query-related statements

ORIGINAL methods untouched:

  • to_sql
  • to_sql_and_binds
  • insert
  • create
  • update
  • delete
  • arel_from_relation

SUPPORTED but not used:

  • select
  • select_all
  • select_one
  • select_value
  • select_values

UNSUPPORTED methods that will be +ignored+:

  • build_fixture_sql
  • build_fixture_statements
  • build_truncate_statement
  • build_truncate_statements

UNSUPPORTED methods that will +fail+:

  • insert_fixture
  • insert_fixtures_set
  • execute_batch
  • select_prepared
  • combine_multi_statements

Instance Method Summary collapse

Instance Method Details

#exec_delete(query, name = nil, binds = []) ⇒ Integer

Executes delete +query+ statement in the context of this connection using +binds+ as the bind substitutes. +name+ is logged along with the executed +query+ arguments. expects a integer as return.

Returns:

  • (Integer)


117
118
119
120
121
122
123
124
# File 'lib/active_record/connection_adapters/elasticsearch/database_statements.rb', line 117

def exec_delete(query, name = nil, binds = [])
  result = exec_query(query, name, binds)

  # fetch additional Elasticsearch response result
  # raise ::ElasticsearchRecord::ResponseResultError.new('deleted', result.result) unless result.result == 'deleted'

  result.total
end

#exec_insert(query, name = nil, binds = [], _pk = nil, _sequence_name = nil) ⇒ ElasticsearchRecord::Result

Executes insert +query+ statement in the context of this connection using +binds+ as the bind substitutes. +name+ is logged along with the executed +query+ arguments.



88
89
90
91
92
93
94
95
96
# File 'lib/active_record/connection_adapters/elasticsearch/database_statements.rb', line 88

def exec_insert(query, name = nil, binds = [], _pk = nil, _sequence_name = nil)
  result = exec_query(query, name, binds)

  # fetch additional Elasticsearch response result
  # raise ::ElasticsearchRecord::ResponseResultError.new('created', result.result) unless result.result == 'created'

  # return the result object
  result
end

#exec_query(query, name = "QUERY", binds = [], prepare: false, async: false) ⇒ ElasticsearchRecord::Result

gets called for all queries - a +ElasticsearchRecord::Query+ must be provided.

Parameters:

  • query (ElasticsearchRecord::Query)
  • name (String (frozen)) (defaults to: "QUERY")
  • binds (Array) (defaults to: [])
    • not supported on the top-level and therefore ignored!
  • prepare (Boolean) (defaults to: false)
    • used by the default AbstractAdapter - but not supported and therefore never ignored!
  • async (Boolean) (defaults to: false)

Returns:



77
78
79
80
81
82
# File 'lib/active_record/connection_adapters/elasticsearch/database_statements.rb', line 77

def exec_query(query, name = "QUERY", binds = [], prepare: false, async: false)
  build_result(
    execute(query, name, async: async),
    columns: query.columns
  )
end

#exec_update(query, name = nil, binds = []) ⇒ Integer

Executes update +query+ statement in the context of this connection using +binds+ as the bind substitutes. +name+ is logged along with the executed +query+ arguments. expects a integer as return.

Returns:

  • (Integer)


103
104
105
106
107
108
109
110
# File 'lib/active_record/connection_adapters/elasticsearch/database_statements.rb', line 103

def exec_update(query, name = nil, binds = [])
  result = exec_query(query, name, binds)

  # fetch additional Elasticsearch response result
  # raise ::ElasticsearchRecord::ResponseResultError.new('updated', result.result) unless result.result == 'updated'

  result.total
end

#execute(query, name = nil, async: false) ⇒ ElasticsearchRecord::Result

Executes the query object in the context of this connection and returns the raw result from the connection adapter.

Parameters:

Returns:

Raises:

  • (ActiveRecord::StatementInvalid)


59
60
61
62
63
64
65
66
67
68
# File 'lib/active_record/connection_adapters/elasticsearch/database_statements.rb', line 59

def execute(query, name = nil, async: false)
  # validate the query
  raise ActiveRecord::StatementInvalid, 'Unable to execute! Provided query is not a "ElasticsearchRecord::Query".' unless query.is_a?(ElasticsearchRecord::Query)
  raise ActiveRecord::StatementInvalid, 'Unable to execute! Provided query is invalid.' unless query.valid?

  # checks for write query - raises an exception if connection is locked to readonly ...
  check_if_write_query(query)

  api(*query.gate, query.query_arguments, name, async: async)
end

#last_inserted_id(result) ⇒ Object

returns the last inserted id from the result. called through +#insert+



159
160
161
# File 'lib/active_record/connection_adapters/elasticsearch/database_statements.rb', line 159

def last_inserted_id(result)
  result.response['_id']
end

#select_count(arel, name = "Count", async: false) ⇒ Integer

executes a count query for provided arel

Returns:

  • (Integer)


143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/active_record/connection_adapters/elasticsearch/database_statements.rb', line 143

def select_count(arel, name = "Count", async: false)
  query = to_sql(arel_from_relation(arel))

  # build new count query from existing query
  query = ElasticsearchRecord::Query.new(
    index:     query.index,
    type:      ElasticsearchRecord::Query::TYPE_COUNT,
    body:      query.body,
    status:    query.status,
    arguments: query.arguments)

  exec_query(query, name, async: async).response['count']
end

#select_multiple(arels, name = "Multi", async: false) ⇒ ElasticsearchRecord::Result

executes a msearch for provided arels



128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/active_record/connection_adapters/elasticsearch/database_statements.rb', line 128

def select_multiple(arels, name = "Multi", async: false)
  # transform arels to query objects
  queries = arels.map { |arel| to_sql(arel_from_relation(arel)) }

  # build new msearch query
  query = ElasticsearchRecord::Query.new(
    index: queries.first&.index,
    type:  ElasticsearchRecord::Query::TYPE_MSEARCH,
    body:  queries.map { |q| { search: q.body } })

  exec_query(query, name, async: async)
end

#write_query?(query) ⇒ Boolean

detects if a query is a write query. since we don't provide a simple string / hash we can now access the query-object and ask for it :)

Parameters:

Returns:

  • (Boolean)

See Also:

  • DatabaseStatements#write_query?


49
50
51
# File 'lib/active_record/connection_adapters/elasticsearch/database_statements.rb', line 49

def write_query?(query)
  query.write?
end