class Rasti::DB::Query

Constants

DATASET_CHAINED_METHODS

Attributes

collection_class[R]
dataset[R]
environment[R]
relations_graph[R]

Public Class Methods

new(environment:, collection_class:, dataset:, relations_graph:nil) click to toggle source
# File lib/rasti/db/query.rb, line 9
def initialize(environment:, collection_class:, dataset:, relations_graph:nil)
  @environment = environment
  @collection_class = collection_class
  @dataset = dataset.qualify collection_class.collection_name
  @relations_graph = relations_graph || Relations::Graph.new(environment, collection_class)
end

Public Instance Methods

all() click to toggle source
# File lib/rasti/db/query.rb, line 68
def all
  with_graph(dataset.all).map do |row|
    collection_class.model.new row
  end
end
Also aliased as: to_a
all_attributes() click to toggle source
# File lib/rasti/db/query.rb, line 44
def all_attributes
  build_query dataset: dataset.select_all(collection_class.collection_name)
end
all_graph_attributes(*relations) click to toggle source
# File lib/rasti/db/query.rb, line 56
def all_graph_attributes(*relations)
  build_query relations_graph: relations_graph.with_all_attributes_for(relations)
end
any?() click to toggle source
# File lib/rasti/db/query.rb, line 108
def any?
  count > 0
end
count() click to toggle source
# File lib/rasti/db/query.rb, line 104
def count
  dataset.count
end
detect(*args, &block) click to toggle source
# File lib/rasti/db/query.rb, line 126
def detect(*args, &block)
  where(*args, &block).first
end
each(batch_size:nil, &block) click to toggle source
# File lib/rasti/db/query.rb, line 75
def each(batch_size:nil, &block)
  if batch_size.nil?
    all.each(&block)
  else
    each_batch(size: batch_size) do |models|
      models.each { |model| block.call model }
    end
  end
end
each_batch(size:, &block) click to toggle source
# File lib/rasti/db/query.rb, line 85
def each_batch(size:, &block)
  primary_keys.each_slice(size) do |pks|
    query = where(collection_class.primary_key => pks)
    block.call query.all
  end
end
empty?() click to toggle source
# File lib/rasti/db/query.rb, line 112
def empty?
  !any?
end
exclude_attributes(*excluded_attributes) click to toggle source
# File lib/rasti/db/query.rb, line 39
def exclude_attributes(*excluded_attributes)
  attributes = collection_class.collection_attributes - excluded_attributes
  select_attributes(*attributes)
end
exclude_graph_attributes(excluded_attributes) click to toggle source
# File lib/rasti/db/query.rb, line 52
def exclude_graph_attributes(excluded_attributes)
  build_query relations_graph: relations_graph.merge(excluded_attributes: excluded_attributes)
end
first() click to toggle source
# File lib/rasti/db/query.rb, line 116
def first
  row = dataset.first
  row ? build_model(row) : nil
end
graph(*relations) click to toggle source
# File lib/rasti/db/query.rb, line 92
def graph(*relations)
  build_query relations_graph: relations_graph.merge(relations: relations)
end
inspect()
Alias for: to_s
join(*relations) click to toggle source
# File lib/rasti/db/query.rb, line 96
def join(*relations)
  graph = Relations::Graph.new environment, collection_class, relations

  ds = graph.add_joins(dataset).distinct

  build_query dataset: ds
end
last() click to toggle source
# File lib/rasti/db/query.rb, line 121
def last
  row = dataset.last
  row ? build_model(row) : nil
end
nql(filter_expression) click to toggle source
# File lib/rasti/db/query.rb, line 135
def nql(filter_expression)
  sentence = nql_parser.parse filter_expression

  raise NQL::InvalidExpressionError.new(filter_expression) if sentence.nil?

  ds = sentence.computed_attributes(collection_class).inject(dataset) do |ds, name|
    collection_class.computed_attributes[name].apply_join ds, environment
  end
  query = build_query dataset: ds

  dependency_tables = sentence.dependency_tables
  query = query.join(*dependency_tables) unless dependency_tables.empty?

  query.where sentence.filter_condition(collection_class)
end
pluck(*attributes) click to toggle source
# File lib/rasti/db/query.rb, line 26
def pluck(*attributes)
  ds = dataset.select(*attributes.map { |a| Sequel[collection_class.collection_name][a] })
  attributes.count == 1 ? ds.map { |r| r[attributes.first] } : ds.map(&:values)
end
primary_keys() click to toggle source
# File lib/rasti/db/query.rb, line 31
def primary_keys
  pluck collection_class.primary_key
end
raw() click to toggle source
# File lib/rasti/db/query.rb, line 22
def raw
  dataset.all
end
select_attributes(*attributes) click to toggle source
# File lib/rasti/db/query.rb, line 35
def select_attributes(*attributes)
  build_query dataset: dataset.select(*attributes.map { |a| Sequel[collection_class.collection_name][a] })
end
select_computed_attributes(*computed_attributes) click to toggle source
# File lib/rasti/db/query.rb, line 60
def select_computed_attributes(*computed_attributes)
  ds = computed_attributes.inject(dataset) do |ds, name|
    computed_attribute = collection_class.computed_attributes[name]
    computed_attribute.apply_join(ds, environment).select_append(computed_attribute.identifier.as(name))
  end
  build_query dataset: ds
end
select_graph_attributes(selected_attributes) click to toggle source
# File lib/rasti/db/query.rb, line 48
def select_graph_attributes(selected_attributes)
  build_query relations_graph: relations_graph.merge(selected_attributes: selected_attributes)
end
to_a()
Alias for: all
to_s() click to toggle source
# File lib/rasti/db/query.rb, line 130
def to_s
  "#<#{self.class.name}: \"#{dataset.sql}\">"
end
Also aliased as: inspect

Private Instance Methods

build_model(row) click to toggle source
# File lib/rasti/db/query.rb, line 166
def build_model(row)
  collection_class.model.new with_graph(row)
end
build_query(**args) click to toggle source
# File lib/rasti/db/query.rb, line 155
def build_query(**args)
  current_args = {
    environment: environment,
    collection_class: collection_class,
    dataset: dataset,
    relations_graph: relations_graph
  }

  Query.new(**current_args.merge(args))
end
chainable(&block) click to toggle source
# File lib/rasti/db/query.rb, line 170
def chainable(&block)
  build_query dataset: instance_eval(&block)
end
method_missing(method, *args, &block) click to toggle source
Calls superclass method
# File lib/rasti/db/query.rb, line 194
def method_missing(method, *args, &block)
  if collection_class.queries.key? method
    instance_exec(*args, &collection_class.queries.fetch(method))
  else
    super
  end
end
nql_parser() click to toggle source
# File lib/rasti/db/query.rb, line 190
def nql_parser
  NQL::SyntaxParser.new
end
qualify(collection_name, data_source_name: nil) click to toggle source
# File lib/rasti/db/query.rb, line 185
def qualify(collection_name, data_source_name: nil)
  data_source_name ||= collection_class.data_source_name
  environment.qualify data_source_name, collection_name
end
respond_to_missing?(method, include_private=false) click to toggle source
Calls superclass method
# File lib/rasti/db/query.rb, line 202
def respond_to_missing?(method, include_private=false)
  collection_class.queries.key?(method) || super
end
with_graph(data) click to toggle source
# File lib/rasti/db/query.rb, line 179
def with_graph(data)
  rows = data.is_a?(Array) ? data : [data]
  relations_graph.fetch_graph rows
  data
end