class
CQL::ActiveRecord::Queryable::QueryBuilder(T)
- CQL::ActiveRecord::Queryable::QueryBuilder(T)
- Reference
- Object
Overview
The QueryBuilder class provides a chainable interface for building queries while maintaining type safety and integration with the CQL::Query system.
Direct Known Subclasses
Defined in:
active_record/query_builder.crConstructors
-
.new(query : CQL::Query, model_class : T.class = T)
Initialize a new QueryBuilder with the given query - @param query [CQL::Query] The underlying query object - @param model_class [T.class] The model class
Class Method Summary
-
.from_model(model_class : T.class) : QueryBuilder(T) forall T
Create a QueryBuilder for the given model type - @param model_class [Class] The model class - @return [QueryBuilder(T)] A new query builder instance
Instance Method Summary
-
#all(as as_kind)
Execute the query and return all results as a custom type Note: Preloading is not applied when using a custom type.
-
#all
Execute the query and return all results When preloads are configured, automatically applies them to the results.
-
#any?
Check if any records exist - @return [Bool] True if any records exist
-
#average(column : Symbol)
Get average value of a column - @param column [Symbol] The column to average - @return [Float64?] The average value or nil
-
#avg(column : Symbol)
Add avg aggregate - @param column [Symbol] The column to average - @return [Float64] The average result
-
#count(column : Symbol = :*)
Counts the rows the query returns, or the non-NULL values of column.
-
#count_query(column : Symbol = :*) : CQL::Query
The
CQL::Querythat#countruns, on the same executor as this query. -
#delete_all : Int64
Deletes the matching records and returns how many.
-
#distinct
Set distinct flag - @return [QueryBuilder(T)] Self for chaining
-
#distinct_values(column : Symbol, as as_kind = DB::Any)
Get distinct values for a column - @param column [Symbol] The column to get distinct values - @return [Array(DB::Any)] Array of distinct values
-
#each(& : T -> )
Iterate over each result - @yield [T] Each model instance
-
#empty?
Check if no records exist - @return [Bool] True if no records exist
-
#exists? : Bool
Check if any records exist - @return [Bool] True if records exist
- #exists?(**conditions)
-
#find(id)
Find record by primary key - @param id [Int32 | Int64] The primary key value - @return [T?] The record or nil if not found
-
#find!(id)
Find record by primary key, raises if not found - @param id [Int32 | Int64] The primary key value - @return [T] The record
-
#find_by(conditions : Hash(String | Symbol, DB::Any | Array(DB::Any)))
Find record by attributes - @param conditions [Hash] The conditions to search by - @return [T?] The first matching record or nil
-
#find_by(**fields)
Find record by attributes with hash syntax - @param fields [Hash] The conditions using hash syntax - @return [T?] The first matching record or nil
-
#find_by!(conditions : Hash(String | Symbol, DB::Any | Array(DB::Any)))
Find record by attributes, raises if not found - @param conditions [Hash] The conditions to search by - @return [T] The first matching record
-
#find_by!(**fields)
Find record by attributes, raises if not found (hash syntax) - @param fields [Hash] The conditions using hash syntax - @return [T] The first matching record
-
#find_each(batch_size : Int32 = 1000, & : T -> )
Yields each matching record, loading them
batch_sizeat a time (see#find_in_batches). -
#find_in_batches(batch_size : Int32 = 1000, & : Array(T) -> )
Yields the matching records in batches of up to
batch_size, ignoring the query's limit and offset. -
#first(as as_kind = T)
Execute the query and return the first result - @return [T?] First model instance or nil
-
#first!(as as_kind = T)
Execute the query and return the first result, raises if not found - @return [T] First model instance
-
#get(as as_kind)
Execute the query and return a scalar value - @param as [Type] The type to cast the result to - @return [Type] The scalar result
-
#group(*columns : Symbol | String)
Add group by clauses - @param columns [Symbol*] The columns to group by - @return [QueryBuilder(T)] Self for chaining
-
#group_by(*columns : Symbol | String)
Alias for group method to match ActiveRecord naming - @param columns [Symbol*] The columns to group by - @return [QueryBuilder(T)] Self for chaining
-
#having(&)
Add having conditions - @yield [HavingBuilder] The block to build having conditions - @return [QueryBuilder(T)] Self for chaining
-
#ids(as as_kind = Int64)
Get array of primary key values - @return [Array] Array of primary key values
-
#join(table_or_alias : Symbol)
Execute automatic inner join - @param table_or_alias [Symbol | Hash] The table or alias mapping - @return [QueryBuilder(T)] Self for chaining
-
#join(table_or_alias : Symbol, &block : Expression::FilterBuilder -> Expression::ConditionBuilder)
Execute inner join with block conditions - @param table_or_alias [Symbol | Hash] The table or alias mapping - @yield [FilterBuilder] The block to build join conditions - @return [QueryBuilder(T)] Self for chaining
-
#join(**tables_with_aliases)
Execute automatic inner join using named arguments for table aliasing - @param tables_with_aliases [NamedTuple] Table name => alias - @return [QueryBuilder(T)] Self for chaining
-
#joins(association : Symbol) : self
Joins an association declared on the model (
has_one,has_many,belongs_to). - #joins(association : Symbol, &) : self
-
#last(as as_kind = T)
Execute the query and return the last result (using reverse order) - @return [T?] Last model instance or nil
-
#last!(as as_kind = T)
Execute the query and return the last result, raises if not found - @return [T] Last model instance
-
#left(table_or_alias : Symbol)
Execute automatic left join - @param table_or_alias [Symbol | Hash] The table or alias mapping - @return [QueryBuilder(T)] Self for chaining
-
#left(table_or_alias : Symbol, &block : Expression::FilterBuilder -> Expression::ConditionBuilder)
Execute left join with block conditions - @param table_or_alias [Symbol | Hash] The table or alias mapping - @yield [FilterBuilder] The block to build join conditions - @return [QueryBuilder(T)] Self for chaining
-
#left(**tables_with_aliases)
Execute automatic left join using named arguments for table aliasing - @param tables_with_aliases [NamedTuple] Table name => alias - @return [QueryBuilder(T)] Self for chaining
- #left_joins(association : Symbol) : self
- #left_joins(association : Symbol, &) : self
-
#limit(value : Int32)
Set limit for the query - @param value [Int32] The limit value - @return [QueryBuilder(T)] Self for chaining
-
#many?
Check if many records exist (more than one) - @return [Bool] True if more than one record exists
-
#max(column : Symbol)
Add max aggregate - @param column [Symbol] The column to find maximum - @return [DB::Any] The maximum result
-
#maximum(column : Symbol)
Get maximum value of a column - @param column [Symbol] The column to find maximum - @return [DB::Any?] The maximum value or nil
-
#merge(other : QueryBuilder(T))
Merge with another QueryBuilder - @param other [QueryBuilder(T)] The other query builder to merge - @return [QueryBuilder(T)] A new merged query builder
-
#min(column : Symbol)
Add min aggregate - @param column [Symbol] The column to find minimum - @return [DB::Any] The minimum result
-
#minimum(column : Symbol)
Get minimum value of a column - @param column [Symbol] The column to find minimum - @return [DB::Any?] The minimum value or nil
-
#model_class : T.class
Get the model class
-
#none
Return a QueryBuilder that will return no results - @return [QueryBuilder(T)] A query builder that returns no results
-
#offset(value : Int32)
Set offset for the query - @param value [Int32] The offset value - @return [QueryBuilder(T)] Self for chaining
-
#order(*fields : Symbol | String)
Add order by clauses - @param fields [Symbol*] The fields to order by - @return [QueryBuilder(T)] Self for chaining
-
#order(**fields)
Add order by clauses with hash syntax - @param fields [Hash] The fields and directions to order by - @return [QueryBuilder(T)] Self for chaining
-
#order_by(**fields : **N) : self forall N
Order by typed columns; unknown column names are compile-time errors.
-
#pick(column : Symbol, as as_kind = DB::Any)
Extract a single column value from the first matching record - @param column [Symbol] The column to extract - @return [DB::Any?] The value or nil if no records
-
#pluck(*columns : Symbol, as as_kind)
Extract column values from all matching records - @param columns [Symbol*] The columns to extract - @return [Array] Array of values or arrays if multiple columns
-
#preload(*associations : Symbol) : self
Add associations to preload (eager load) to avoid N+1 queries.
-
#query
Get the underlying CQL::Query object - @return [CQL::Query] The query object
-
#reorder(*fields : Symbol | String)
Replace existing order clause - @param fields [Symbol*] The fields to order by - @return [QueryBuilder(T)] Self for chaining
-
#reorder(**fields)
Replace existing order clause with hash syntax - @param fields [Hash] The fields and directions to order by - @return [QueryBuilder(T)] Self for chaining
-
#reverse_order
Reverse the order of records - @return [QueryBuilder(T)] Self for chaining
-
#right(table_or_alias : Symbol)
Execute automatic right join - @param table_or_alias [Symbol | Hash] The table or alias mapping - @return [QueryBuilder(T)] Self for chaining
-
#right(table_or_alias : Symbol, &block : Expression::FilterBuilder -> Expression::ConditionBuilder)
Execute right join with block conditions - @param table_or_alias [Symbol | Hash] The table or alias mapping - @yield [FilterBuilder] The block to build join conditions - @return [QueryBuilder(T)] Self for chaining
-
#right(**tables_with_aliases)
Execute automatic right join using named arguments for table aliasing - @param tables_with_aliases [NamedTuple] Table name => alias - @return [QueryBuilder(T)] Self for chaining
- #select(*columns : Symbol) : PartialBuilder(T)
-
#select(*columns : Symbol | String)
Add columns to select - @param columns [Symbol*] The columns to select - @return [QueryBuilder(T)] Self for chaining
-
#select(**fields)
Add columns to select with hash syntax - @param fields [Hash] The fields to select using hash syntax - @return [QueryBuilder(T)] Self for chaining
-
#size
Get the count of records - @return [Int64] The number of records
-
#sum(column : Symbol)
Add sum aggregate - @param column [Symbol] The column to sum - @return [Float64 | Int64] The sum result
-
#to_a
Alias of
#all. -
#to_sql
Convert to SQL string for debugging - @return [String] The SQL query string
-
#to_sql_with_params
Get the SQL query and parameters - @return [Tuple(String, Array)] The SQL query and parameters
-
#unscope(scope_type : Symbol)
Remove specific scopes - @param scope_type [Symbol] The type of scope to remove - @return [QueryBuilder(T)] Self for chaining
-
#using(connection : DB::Connection) : self
Runs this query on an explicit connection (see
Schema#with_connection). -
#using(executor : CQL::Executor) : self
Runs this query on another executor, e.g.
-
#where(conditions : Hash(String | Symbol, DB::Any | Array(DB::Any)))
Add where conditions - @param conditions [Hash] The conditions to filter by - @return [QueryBuilder(T)] Self for chaining
-
#where(__sql sql : String, *positional, **named)
A raw SQL fragment with bound
?/:namevalues (sqlmust be a literal). -
#where(**fields)
Add where conditions with hash syntax - @param fields [Hash] The conditions using hash syntax - @return [QueryBuilder(T)] Self for chaining
-
#where(&) : self
Add where conditions with block - @yield [FilterBuilder] The block to build conditions - @return [QueryBuilder(T)] Self for chaining
-
#where_gt(field : Symbol | String, value : DB::Any)
Add greater-than condition Useful for cursor-based pagination and range queries.
-
#where_like(field : Symbol | String, pattern : String)
Add LIKE conditions - @param field [Symbol | String] The field to match - @param pattern [String] The LIKE pattern - @return [QueryBuilder(T)] Self for chaining
-
#where_lt(field : Symbol | String, value : DB::Any)
Add less-than condition Useful for cursor-based pagination and range queries.
-
#where_primary_key(key : DB::Any | UUID) : QueryBuilder(T)
Rows whose primary key (
T.primary_key_column) is key. -
#where_primary_key(keys : Array) : QueryBuilder(T)
Rows whose primary key is any of keys; none for an empty array.
-
#with_query(&block : CQL::Query -> CQL::Query)
Create a new QueryBuilder with a modified query using a block - @yield [CQL::Query] The query to modify - @return [QueryBuilder(T)] A new query builder instance
Constructor Detail
Initialize a new QueryBuilder with the given query
- @param query [CQL::Query] The underlying query object
- @param model_class [T.class] The model class
Class Method Detail
Create a QueryBuilder for the given model type
- @param model_class [Class] The model class
- @return [QueryBuilder(T)] A new query builder instance
Instance Method Detail
Execute the query and return all results as a custom type Note: Preloading is not applied when using a custom type.
- @param as_kind [Class] The type to deserialize results as
- @return [Array] Array of instances of the specified type
Execute the query and return all results When preloads are configured, automatically applies them to the results.
- @return [Array(T)] Array of model instances
Get average value of a column
- @param column [Symbol] The column to average
- @return [Float64?] The average value or nil
Add avg aggregate
- @param column [Symbol] The column to average
- @return [Float64] The average result
Counts the rows the query returns, or the non-NULL values of column.
Without LIMIT or OFFSET, ORDER BY is dropped. With either it counts a derived
table (SELECT COUNT(*) FROM (...) AS counted_rows); ORDER BY stays inside it
when counting a column, since it decides which rows fall in the window, and
#group counts the groups in the window.
#distinct with joins plus LIMIT or OFFSET keeps every joined column inside
the derived table (MySQL rejects duplicate column names there), and unaliased
aggregate selects cannot be counted with LIMIT or OFFSET.
- @param column [Symbol] The column to count (defaults to *)
- @return [Int64] The count result
The CQL::Query that #count runs, on the same executor as this query.
Deletes the matching records and returns how many. A model with destroy callbacks or
dependent: strategies loads and deletes each record so they run, all in one transaction;
others delete by id in bulk (soft-deletable models set deleted_at).
Get distinct values for a column
- @param column [Symbol] The column to get distinct values
- @return [Array(DB::Any)] Array of distinct values
Find record by primary key
- @param id [Int32 | Int64] The primary key value
- @return [T?] The record or nil if not found
Find record by primary key, raises if not found
- @param id [Int32 | Int64] The primary key value
- @return [T] The record
Find record by attributes
- @param conditions [Hash] The conditions to search by
- @return [T?] The first matching record or nil
Find record by attributes with hash syntax
- @param fields [Hash] The conditions using hash syntax
- @return [T?] The first matching record or nil
Find record by attributes, raises if not found
- @param conditions [Hash] The conditions to search by
- @return [T] The first matching record
Find record by attributes, raises if not found (hash syntax)
- @param fields [Hash] The conditions using hash syntax
- @return [T] The first matching record
Yields each matching record, loading them batch_size at a time (see #find_in_batches).
Yields the matching records in batches of up to batch_size, ignoring the query's
limit and offset. An unordered, unshaped query pages by primary key
(WHERE pk > last ORDER BY pk LIMIT n), so rows deleted mid-iteration never shift
later rows; any other query pages by OFFSET in its order, ties broken by primary key.
Execute the query and return the first result
- @return [T?] First model instance or nil
Execute the query and return the first result, raises if not found
- @return [T] First model instance
Execute the query and return a scalar value
- @param as [Type] The type to cast the result to
- @return [Type] The scalar result
Add group by clauses
- @param columns [Symbol*] The columns to group by
- @return [QueryBuilder(T)] Self for chaining
Alias for group method to match ActiveRecord naming
- @param columns [Symbol*] The columns to group by
- @return [QueryBuilder(T)] Self for chaining
Add having conditions
- @yield [HavingBuilder] The block to build having conditions
- @return [QueryBuilder(T)] Self for chaining
The block may use implicit self (having { count(:id) > 1 }) or take
the builder (having { |h| h.count(:id) > 1 }).
Get array of primary key values
- @return [Array] Array of primary key values
Execute automatic inner join
- @param table_or_alias [Symbol | Hash] The table or alias mapping
- @return [QueryBuilder(T)] Self for chaining
Execute inner join with block conditions
- @param table_or_alias [Symbol | Hash] The table or alias mapping
- @yield [FilterBuilder] The block to build join conditions
- @return [QueryBuilder(T)] Self for chaining
Execute automatic inner join using named arguments for table aliasing
- @param tables_with_aliases [NamedTuple] Table name => alias
- @return [QueryBuilder(T)] Self for chaining
Joins an association declared on the model (has_one, has_many, belongs_to).
The optional block adds typed conditions to the join, written against the
model's columns and its associations (profile.country == "US").
Execute the query and return the last result (using reverse order)
- @return [T?] Last model instance or nil
Execute the query and return the last result, raises if not found
- @return [T] Last model instance
Execute automatic left join
- @param table_or_alias [Symbol | Hash] The table or alias mapping
- @return [QueryBuilder(T)] Self for chaining
Execute left join with block conditions
- @param table_or_alias [Symbol | Hash] The table or alias mapping
- @yield [FilterBuilder] The block to build join conditions
- @return [QueryBuilder(T)] Self for chaining
Execute automatic left join using named arguments for table aliasing
- @param tables_with_aliases [NamedTuple] Table name => alias
- @return [QueryBuilder(T)] Self for chaining
Set limit for the query
- @param value [Int32] The limit value
- @return [QueryBuilder(T)] Self for chaining
Check if many records exist (more than one)
- @return [Bool] True if more than one record exists
Add max aggregate
- @param column [Symbol] The column to find maximum
- @return [DB::Any] The maximum result
Get maximum value of a column
- @param column [Symbol] The column to find maximum
- @return [DB::Any?] The maximum value or nil
Merge with another QueryBuilder
- @param other [QueryBuilder(T)] The other query builder to merge
- @return [QueryBuilder(T)] A new merged query builder
Add min aggregate
- @param column [Symbol] The column to find minimum
- @return [DB::Any] The minimum result
Get minimum value of a column
- @param column [Symbol] The column to find minimum
- @return [DB::Any?] The minimum value or nil
Return a QueryBuilder that will return no results
- @return [QueryBuilder(T)] A query builder that returns no results
Set offset for the query
- @param value [Int32] The offset value
- @return [QueryBuilder(T)] Self for chaining
Add order by clauses
- @param fields [Symbol*] The fields to order by
- @return [QueryBuilder(T)] Self for chaining
Add order by clauses with hash syntax
- @param fields [Hash] The fields and directions to order by
- @return [QueryBuilder(T)] Self for chaining
Order by typed columns; unknown column names are compile-time errors.
User.query.order_by(created_at: :desc, name: :asc)
Extract a single column value from the first matching record
- @param column [Symbol] The column to extract
- @return [DB::Any?] The value or nil if no records
Extract column values from all matching records
- @param columns [Symbol*] The columns to extract
- @return [Array] Array of values or arrays if multiple columns
Add associations to preload (eager load) to avoid N+1 queries. Preloaded associations are fetched in separate batch queries after the main query executes, then injected into the parent records.
- @param associations [Symbol*] The association names to preload
- @return [QueryBuilder(T)] A new query builder with preload configured
Example
User.preload(:posts).all # Preload an association
User.where(active: true).preload(:posts).all # Chain with where
Replace existing order clause
- @param fields [Symbol*] The fields to order by
- @return [QueryBuilder(T)] Self for chaining
Replace existing order clause with hash syntax
- @param fields [Hash] The fields and directions to order by
- @return [QueryBuilder(T)] Self for chaining
Execute automatic right join
- @param table_or_alias [Symbol | Hash] The table or alias mapping
- @return [QueryBuilder(T)] Self for chaining
Execute right join with block conditions
- @param table_or_alias [Symbol | Hash] The table or alias mapping
- @yield [FilterBuilder] The block to build join conditions
- @return [QueryBuilder(T)] Self for chaining
Execute automatic right join using named arguments for table aliasing
- @param tables_with_aliases [NamedTuple] Table name => alias
- @return [QueryBuilder(T)] Self for chaining
Select only these columns; the result rows (#to_a, #first, ...) are
Model::Row records whose unselected columns are nil.
Add columns to select
- @param columns [Symbol*] The columns to select
- @return [QueryBuilder(T)] Self for chaining
Add columns to select with hash syntax
- @param fields [Hash] The fields to select using hash syntax
- @return [QueryBuilder(T)] Self for chaining
Add sum aggregate
- @param column [Symbol] The column to sum
- @return [Float64 | Int64] The sum result
Get the SQL query and parameters
- @return [Tuple(String, Array)] The SQL query and parameters
Remove specific scopes
- @param scope_type [Symbol] The type of scope to remove
- @return [QueryBuilder(T)] Self for chaining
Runs this query on an explicit connection (see Schema#with_connection).
Runs this query on another executor, e.g. a CQL::Postgres::AsyncEngine.
Add where conditions
- @param conditions [Hash] The conditions to filter by
- @return [QueryBuilder(T)] Self for chaining
A raw SQL fragment with bound ? / :name values (sql must be a literal).
Add where conditions with hash syntax
- @param fields [Hash] The conditions using hash syntax
- @return [QueryBuilder(T)] Self for chaining
Add where conditions with block
- @yield [FilterBuilder] The block to build conditions
- @return [QueryBuilder(T)] Self for chaining
Add greater-than condition Useful for cursor-based pagination and range queries.
- @param field [Symbol | String] The field to compare
- @param value [DB::Any] The value to compare against
- @return [QueryBuilder(T)] Self for chaining
Add LIKE conditions
- @param field [Symbol | String] The field to match
- @param pattern [String] The LIKE pattern
- @return [QueryBuilder(T)] Self for chaining
Add less-than condition Useful for cursor-based pagination and range queries.
- @param field [Symbol | String] The field to compare
- @param value [DB::Any] The value to compare against
- @return [QueryBuilder(T)] Self for chaining
Rows whose primary key (T.primary_key_column) is key. A UUID is bound as text.
Rows whose primary key is any of keys; none for an empty array.
Create a new QueryBuilder with a modified query using a block
- @yield [CQL::Query] The query to modify
- @return [QueryBuilder(T)] A new query builder instance