module Rome::Query::Methods(T)

Direct including types

Defined in:

query/methods.cr

Instance Method Summary

Instance Method Detail

def all : self #

Query all records from the database. Doesn't actually issue any SQL query. See Cache#to_a to load all records from the database into an Array.


[View source]
def average(column_name : Symbol | String) : Float64 #

Calculates the average of a column. See #sum for details.


[View source]
def count(column_name : Symbol | String = "*", distinct = builder.distinct?) : Int64 #

Counts how many records match the SQL query.

You can count all columns or a specific column::

User.count
User.count(:name)

You can specify a raw SQL query with a String:

User.count("LENGTH(name)")

[View source]
def delete_all : Nil #

Executes a DELETE SQL query.

User.where(group_id: 1).delete_all
# => DELETE FROM "users" WHERE "group_id" = 1;

[View source]
def distinct(value = true) : self #

Specify a DISTINCT statement for the query.


[View source]
abstract def dup(builder : Builder) #

[View source]
def exists?(id : T::PrimaryKeyType) : Bool #

Returns true when a record identified by primary key exists in the database with the current conditions.

User.where(group_id: 1).exists?(2)
# => SELECT 1 AS one FROM "users" WHERE "group_id" = 1 AND "id" = 2 LIMIT 1;

[View source]
def exists? : Bool #

Returns true if the SQL query has at least one result.

User.where(group_id: 1).exists?
# => SELECT 1 AS one FROM "users" WHERE "group_id" = 1 LIMIT 1;

[View source]
def find(id : T::PrimaryKeyType) : T #

Loads a record by id from the database. Raises a RecordNotFound exception when the record doesn't exist.

user = User.find(1)
# => SELECT * FROM "users" WHERE "id" = 1 LIMIT 1;

[View source]
def find?(id : T::PrimaryKeyType) : T | Nil #

Same as #find but returns nil when the record doesn't exist.


[View source]
def find_by(**args) : T #

Loads a record by arguments from the database. Raises a RecordNotFound exception when the record doesn't exist. For example:

user = User.find_by(name: "julien", group_id: 2)
# => SELECT * FROM "users" WHERE "name" = 'julien' AND "group_id" = 2 LIMIT 1;

See #where for more details on conditions.


[View source]
def find_by?(**args) : T | Nil #

Same as #find_by but returns nil when no record could be found in the database.


[View source]
def first : T #

Loads the first record from the database, ordering by the primary key in ascending order unless an order has been specified.

Merely takes the last entry in the cached result set if the relation was previously loaded.

Prefer #take if you don't need an ordering or already specified one.

user = User.first
# => SELECT * FROM "users" ORDER BY "id" ASC LIMIT 1;

user = User.order(name: :desc).last
# => SELECT * FROM "users" ORDER BY "name" DESC LIMIT 1;

user = User.order("name ASC, group_id DESC").last
# => SELECT * FROM "users" ORDER BY name ASC, group_id DESC LIMIT 1;

[View source]
def first? : T | Nil #

Same as #first? but returns nil when no record could be found in the database.


[View source]
def ids : Array(T::PrimaryKeyType) #

Loads all primary key values of rows matching the SQL query.


[View source]
def last : T #

Loads the last record from the database, ordering by the primary key in ascending order unless an order has been specified.

Merely takes the last entry in the cached result set if the relation was previously loaded.

Prefer #take if you don't need an ordering or already specified one.

user = User.last
# => SELECT * FROM "users" ORDER BY "id" DESC LIMIT 1;

user = User.order(name: :desc).last
# => SELECT * FROM "users" ORDER BY "name" ASC LIMIT 1;

user = User.order("name ASC, group_id DESC").last
# => SELECT * FROM "users" ORDER BY name DESC, group_id ASC LIMIT 1;

[View source]
def last? : T | Nil #

Same as #last? but returns nil when no record could be found in the database.


[View source]
def limit(value : Int32) : self #

Specify a LIMIT for the query.


[View source]
def maximum(column_name : Symbol | String) #

Returns the maximum value for a column. See #sum for details.


[View source]
def minimum(column_name : Symbol | String) #

Returns the minimum value for a column. See #sum for details.


[View source]
def none : self #

Ensures that the query will never return anything from the database.


[View source]
def offset(value : Int32) : self #

Specify an OFFSET for the query.


[View source]
def order(columns : Hash(Symbol, Symbol)) : self #

Specify an ORDER for the query. This is added to any previous order definition. For example:

User.order({ name: :asc, group_id: :desc })
# => SELECT * FROM "users" ORDER BY "name" ASC, "group_id" DESC;

[View source]
def order(*columns : Symbol | String) : self #

Specify an ORDER column for the query. This is added to any previous order definition. For example:

User.order(:name)
# => SELECT * FROM "users" ORDER BY "name" ASC;

[View source]
def order(**columns) : self #

Specify an ORDER for the query. This is added to any previous order definition. For example:

User.order(name: :asc, group_id: :desc)
# => SELECT * FROM "users" ORDER BY "name" ASC, "group_id" DESC;

[View source]
def pluck(column_name : Symbol | String) : Array(Value) #

Loads values of a single column as an Array.

names = User.pluck(:name)
# => SELECT "name" FROM "users";
# => ["julien", "alice", ...]

[View source]
def reorder(columns : Hash(Symbol, Symbol)) : self #

Specify an ORDER for the query, replacing any previous ORDER definition. See #order for details.


[View source]
def reorder(*columns : Symbol | String) : self #

Specify an ORDER column for the query, replacing any previous ORDER definition. See #order for details.


[View source]
def reorder(**columns) : self #

Specify an ORDER for the query, replacing any previous ORDER definition. See #order for details.


[View source]
def select(sql : String) : self #

Specify a raw SELECT statement for the query.


[View source]
def select(*columns : Symbol) : self #

Specify SELECT columns for the query.


[View source]
def size : Int64 #

Returns how many records match the SQL query. Uses the cached result set if the query was previously loaded, otherwise executes a COUNT SQL query.


[View source]
def sum(column_name : Symbol | String) : Int64 | Float64 #

Calculates the sum of a column.

You can specify a column name:

User.sum(:salary)

You can specify a raw SQL query with a String:

User.sum("LENGTH(name)")

[View source]
def take : T #

Loads one record from the database, without any ordering. Raises a RecordNotFound exception when no record could be found.

user = User.take
# => SELECT * FROM "users" LIMIT 1;

[View source]
def take? : T | Nil #

Same as #take but returns nil when no record could be found in the database.


[View source]
def to_sql : String #

Returns the generated SQL query. Useful for debugging.


[View source]
def unscope(*args) : self #

Resets previously set SQL statement(s). For example:

users = User.where(group_id: 1).limit(10)
users.unscope(:limit)         # == User.where(group_id: 1)
users.unscope(:where)         # == User.limit(10)
users.unscope(:where, :limit) # == User.all

Available properties:

  • :select
  • :where
  • :order
  • :limit
  • :offset

[View source]
def update_all(attributes : Hash | NamedTuple) : Nil #

Executes an UPDATE SQL query.

User.where(id: 1).update_all({ group_id: 2 })
# => UPDATE "users" SET "group_id" = 2 WHERE "id" = 1;

[View source]
def update_all(**attributes) : Nil #

Executes an UPDATE SQL query.

User.where(id: 1).update_all(group_id: 2)
# => UPDATE "users" SET "group_id" = 2 WHERE "id" = 1;

[View source]
def where(conditions : Hash(Symbol, Value | Array(Value)) | NamedTuple) : self #

Specify WHERE conditions for the query. For example:

conditions = {
  :name => "julien",
  :group_id => 2,
}
User.where(conditions)
# => SELECT * FROM "users" WHERE "name" = 'julien' AND "group_id" = 2;

The condition value may be nil:

User.where({ :group_id => nil })
# => SELECT * FROM "users" WHERE "group_id" IS NULL;

The condition value may also be an Array of values:

User.where({ :group_id => [1, 2, 3] })
# => SELECT * FROM "users" WHERE "group_id" IN (1, 2, 3);

[View source]
def where(sql : String, *args : Value) : self #

Specify a raw WHERE condition for the query. You can specify arguments as ? and pass them to the method. For example:

User.where("LENGTH(name) > ?", 10)
# => SELECT * FROM "users" WHERE LENGTH(name) > 10;

[View source]
def where(**conditions) : self #

Specify WHERE conditions for the query. For example:

User.where(name: "julien", group_id: 2)
# => SELECT * FROM "users" WHERE "name" = 'julien' AND "group_id" = 2;

The condition value may be nil:

User.where(group_id: nil)
# => SELECT * FROM "users" WHERE "group_id" IS NULL;

The condition value may also be an Array of values:

User.where(group_id: [1, 2, 3])
# => SELECT * FROM "users" WHERE "group_id" IN (1, 2, 3);

[View source]
def where_not(conditions : Hash(Symbol, Value | Array(Value)) | NamedTuple) : self #

[View source]
def where_not(**conditions) : self #

[View source]