class DBX::ORM::ModelQuery(Model)

Overview

Generic ModelQuery class.

Automatically injected into the models.

If you want to customize the queries of a model, you can define you own ModelQuery into this model.

class User < DBX::ORM::Model
  # ...

  class ModelQuery < DBX::ORM::ModelQuery(User)
    # A custom `select`
    def select_custom
      self.select({:id, :name, :about, :age})
    end
  end
end

In the model example above, we have added a new method (select_custom) to `ModelQuery', which can be used in each query.

user = User.find(id).select_custom.to_o
users = User.find.select_custom.to_a

Defined in:

orm/model_query.cr

Constructors

Instance Method Summary

Instance methods inherited from class DBX::Query

build : Tuple build, builder : DBX::QueryBuilder builder, create!(data : Hash | NamedTuple, as types, returning : DBX::QueryBuilder::OneOrMoreFieldsType = "*", pk_name : DBX::QueryBuilder::FieldType = :id, pk_type = ::Union(Int64, ::Nil)) create!, exec exec, exec! exec!, query
query(&)
query
, query_all(as types)
query_all(&)
query_all
, query_each(&) query_each, query_one(as types)
query_one(&)
query_one
, query_one!(as types)
query_one!(&)
query_one!
, raw_query(&) : Query raw_query, scalar scalar, scalar! scalar!, to_a(as types)
to_a(&)
to_a
, to_o(as types)
to_o(&)
to_o
, to_o!(as types)
to_o!(&)
to_o!

Constructor methods inherited from class DBX::Query

new(adapter : DBX::Adapter::Base) new

Constructor Detail

def self.new(adapter : DBX::Adapter::Base) #

Instance Method Detail

def create!(data, returning : DBX::QueryBuilder::OneOrMoreFieldsType = "*") #

Creates a new record and returns.

test = Test.create!(data)
puts test.id

def query_all #

Executes current query using current Model::Schema.


def query_one #

Executes current query using current Model::Schema.


def query_one! #

Executes current query using current Model::Schema.


def rel(path : Symbol | String, table_alias : String | Symbol | Nil = nil) : ModelQuery(Model) #

Refers to the result of a join in a defined relation property path.

users = User
  .find
  .rel("groups")
  .left_join("groups", "groups.id", "users.group_id")
  .to_a

def select_all(model_class : DBX::ORM::Model.class, table_alias : String | Symbol | Nil = nil) : ModelQuery(Model) #

Selects the relation fields. This method is automatically called by the methods related to the joins with a model class.


def select_all : ModelQuery(Model) #

Selects all SQL fields.


def selected_all?(model_class : DBX::ORM::Model.class, table_alias : String | Symbol | Nil = nil) : Bool #

Returns true if the relation fields are selected, false otherwise. See select_rel_fields


def selected_all? : Bool #

Returns true if the model fields are selected, false otherwise. See #select_all


def to_a(as types) #

Shortcut, same as query_all(types).


def to_a #

Shortcut, same as #query_all.


def to_o(as types) #

Shortcut, same as query_one(types).


def to_o #

Shortcut, same as #query_one.


def to_o!(as types) #

Shortcut, same as query_one!(types).


def to_o! #

Shortcut, same as #query_one!.