class Prostore::Query::Builder(T)

Overview

Chainable query builder. Each chained call returns a new builder with one extra clause; the original is unchanged. Materialization happens via #to_a, #each, #first, #count, etc.

Generic over T (the model instance type the builder yields). The macro query :name, ->(...) { ... } emits a class method that constructs a Builder(self) and runs the lambda body inside a with builder block, so #where(...), order_by(...) etc. inside the lambda dispatch to the builder.

Defined in:

prostore/query/builder.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(table : String, field_names : Array(String), materializer : DB::ResultSet -> T, adapter : Adapter::Base, db : DB::Database, plan : Renderer::Plan = Renderer::Plan.new, partial_materializer : DB::ResultSet, Array(String) -> T | Nil = nil) #

[View source]

Instance Method Detail

def adapter : Adapter::Base #

[View source]
def count : Int64 #

[View source]
def db : DB::Database #

[View source]
def each(&) : Nil #

[View source]
def empty? : Bool #

[View source]
def exists? : Bool #

[View source]
def field_names : Array(String) #

[View source]
def first : T | Nil #

[View source]
def first! : T #

[View source]
def in_query(&) #

Inside-block builder for the macro-emitted class methods. The with builder yield makes naked #where(...) etc. inside the lambda body resolve to instance methods on the builder.


[View source]
def joins(target_table : String, source_columns : Array(String), target_columns : Array(String)) : Builder(T) #

Explicit join with caller-supplied source/target column lists.


[View source]
def joins(target_class : Prostore::Model.class, fk_tag : Int32 | Nil = nil) : Builder(T) #

FK-resolved join. Looks for a foreign key linking T (the source model) and target_class. If multiple FKs exist between the two, fk_tag disambiguates.

Resolution rules:

  1. FK on the target references the source (e.g., User.joins(Order) where Order has foreign_key … references: User): ON source.pk = target.fk_columns
  2. FK on the source references the target (e.g., Order.joins(User)): ON source.fk_columns = target.pk
  3. Neither found → raise Prostore::Error.

[View source]
def limit(n : Int32) : Builder(T) #

[View source]
def offset(n : Int32) : Builder(T) #

[View source]
def order_by(field : Symbol | String, desc : Bool = false) : Builder(T) #

[View source]
def select(*fields : Symbol | String) : Builder(T) #

Project only the named columns in the resulting SELECT (ADR-0014). Materialized instances will have only the projected fields populated; other ivars stay nil and accessing a non-projected non-nullable field raises (per the model's standard accessor semantics).


[View source]
def table : String #

[View source]
def to_a : Array(T) #

[View source]
def where(predicate : AST::Predicate) : Builder(T) #

[View source]
def where(**kwargs) : Builder(T) #

[View source]