Class: NoSE::Backend::PreparedQuery

Inherits:
Object
  • Object
show all
Defined in:
lib/nose/backend.rb

Overview

A prepared query which can be executed against the backend

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query, steps) ⇒ PreparedQuery

Returns a new instance of PreparedQuery.



364
365
366
367
# File 'lib/nose/backend.rb', line 364

def initialize(query, steps)
  @query = query
  @steps = steps
end

Instance Attribute Details

#queryObject (readonly)

Returns the value of attribute query.



362
363
364
# File 'lib/nose/backend.rb', line 362

def query
  @query
end

#stepsObject (readonly)

Returns the value of attribute steps.



362
363
364
# File 'lib/nose/backend.rb', line 362

def steps
  @steps
end

Instance Method Details

#execute(conditions) ⇒ Array<Hash>

Execute the query for the given set of conditions

Returns:



371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# File 'lib/nose/backend.rb', line 371

def execute(conditions)
  results = nil

  @steps.each do |step|
    if step.is_a?(Backend::IndexLookupStatementStep)
      field_ids = step.index.all_fields.map(&:id)
      field_conds = conditions.select { |key| field_ids.include? key }
    else
      field_conds = conditions
    end
    results = step.process field_conds, results

    # The query can't return any results at this point, so we're done
    break if results.empty?
  end

  # Only return fields selected by the query if one is given
  # (we have no query to refer to for manually-defined plans)
  unless @query.nil?
    select_ids = @query.select.map(&:id).to_set
    results.map { |row| row.select! { |k, _| select_ids.include? k } }
  end

  results
end