class

Clear::Reflection::Table

Inherits Clear::Model::Relations::HasManyThroughMacro < Clear::Model::Relations::HasManyMacro < Clear::Model::Relations::HasOneMacro < Clear::Model::Relations::BelongsToMacro < Clear::Model < Clear::Model::FullTextSearchable < Clear::Model::JSONDeserialize < Clear::Model::Initializer < Clear::Model::HasFactory < Clear::Model::ClassMethods < Clear::Model::HasScope < Clear::Model::HasRelations < Clear::Model::HasValidation < Clear::Validation::Helper < Clear::Model::HasSaving < Clear::Model::HasSerialPkey < Clear::Model::HasTimestamps < Clear::Model::HasColumns < Clear::Model::HasHooks < Clear::Model::Connection < Clear::ErrorMessages < Reference < Object

Reflection of the tables using information_schema in postgreSQL.

Constants

COLUMNS = {"table_name" => {type: String, primary: true, converter: "String", db_column_name: "table_name", crystal_variable_name: table_name, presence: true, mass_assign: true}, "table_catalog" => {type: String, primary: false, converter: "String", db_column_name: "table_catalog", crystal_variable_name: table_catalog, presence: true, mass_assign: true}, "table_schema" => {type: String, primary: false, converter: "String", db_column_name: "table_schema", crystal_variable_name: table_schema, presence: true, mass_assign: true}, "table_type" => {type: String, primary: false, converter: "String", db_column_name: "table_type", crystal_variable_name: table_type, presence: true, mass_assign: true}} of Nil => Nil
POLYMORPHISM_SETTINGS = {} of Nil => Nil
RELATION_FILTERS = {} of String => (Clear::SQL::SelectBuilder ->)

Constructors

create(x : Hash) : self
Source
create(x : NamedTuple) : self
Source
create

Build and new model and save it. Returns the model.

The model may not be saved due to validation failure; check the returned model errors? and persisted? flags.

Source
create!(a : Hash) : self
Source
create!(x : NamedTuple) : self
Source
create!

Build and new model and save it. Returns the model.

Returns the newly inserted model Raises an exception if validation failed during the saving process.

Source
new(h : Hash(String, _), cache : Clear::Model::QueryCache | Nil = nil, persisted = false, fetch_columns = false)
Source
new(json : JSON::Any, cache : Clear::Model::QueryCache | Nil = nil, persisted = false)
Source
new(t : NamedTuple, persisted = false)
Source

Class methods

__call_relation_filter__(name : String, query : Clear::SQL::SelectBuilder)
Source
__relation_filter_columns__(query)
build

Build a new empty model and fill the columns using the NamedTuple in argument.

Returns the new model

Source
columns
Source
connection

Define on which connection the model is living. Useful in case of models living in different databases.

Is set to "default" by default.

See Clear::SQL#init(URI, *opts) for more information about multi-connections.

Example:

Clear::SQL.init("postgres://postgres@localhost/database_1", connection_pool_size: 5)
Clear::SQL.init("secondary", "postgres://postgres@localhost/database_2", connection_pool_size: 5)

class ModelA
  include Clear::Model

  # Performs all the queries on `database_1`
  # self.connection = "default"
  column id : Int32, primary: true, presence: false
  column title : String
end

class ModelB
  include Clear::Model

  # Performs all the queries on `database_2`
  self.connection = "secondary"

  column id : Int32, primary: true, presence: false
end
connection=(connection : String)

Define on which connection the model is living. Useful in case of models living in different databases.

Is set to "default" by default.

See Clear::SQL#init(URI, *opts) for more information about multi-connections.

Example:

Clear::SQL.init("postgres://postgres@localhost/database_1", connection_pool_size: 5)
Clear::SQL.init("secondary", "postgres://postgres@localhost/database_2", connection_pool_size: 5)

class ModelA
  include Clear::Model

  # Performs all the queries on `database_1`
  # self.connection = "default"
  column id : Int32, primary: true, presence: false
  column title : String
end

class ModelB
  include Clear::Model

  # Performs all the queries on `database_2`
  self.connection = "secondary"

  column id : Int32, primary: true, presence: false
end
create(x : Array(NamedTuple)) : Array(self)

Multi-models creation. See Collection#create(**args)

Returns the list of newly created model.

Each model will call an INSERT query. You may want to use Collection#import to insert multiple model more efficiently in one query.

Source
create!(x : Array(NamedTuple)) : Array(self)

Multi-models creation. See Collection#create!(**args)

Returns the list of newly created model. Raises exception if any of the model has validation error.

Source
create_from_json(string_or_io : String | IO, trusted : Bool = false)

Create a new model from json and save it. Returns the model.

The model may not be saved due to validation failure; check the returned model errors? and persisted? flags. Trusted flag set to true will allow mass assignment without protection, FALSE by default

create_from_json!(string_or_io : String | IO, trusted : Bool = false)

Create a new model from json and save it. Returns the model.

Returns the newly inserted model Raises an exception if validation failed during the saving process. Trusted flag set to true will allow mass assignment without protection, FALSE by default

find(x)

Returns a model using primary key equality Returns nil if not found.

Source
find!(x)

Returns a model using primary key equality. Raises error if the model is not found.

Source
from_json(string_or_io : String | IO, trusted : Bool = false)

Create a new empty model and fill the columns from json. Returns the new model

Trusted flag set to true will allow mass assignment without protection, FALSE by default

full_table_name

returns the fully qualified and escaped name for this table. add schema if schema is different from 'public' (default schema)

ex: "schema"."table"

Source
import(array : Enumerable(self), on_conflict : Clear::SQL::InsertQuery -> | Nil = nil)

Import a bulk of models in one SQL insert query. Each model must be non-persisted.

on_conflict callback can be optionnaly turned on to manage constraints of the database.

Note: Old models are not modified. This method return a copy of the models as saved in the database.

Example:

users = [User.new(id: 1), User.new(id: 2), User.new(id: 3)]
users = User.import(users)
Source
polymorphic?
public
Source
query

Return a new empty query SELECT * FROM [my_model_table]. Can be refined after that.

Source
read_only=(read_only : Bool)
read_only?
schema

Define the current schema used in PostgreSQL. The value is nil by default, which lead to non-specified schema during the querying, and usage of "public" by PostgreSQL.

This property can be redefined on initialization. Example:

  class MyModel
    include Clear::Model

    self.schema = "my_schema"
  end
  MyModel.query.to_sql # SELECT * FROM "my_schema"."my_models"
schema=(schema : Clear::SQL::Symbolic | Nil)

Define the current schema used in PostgreSQL. The value is nil by default, which lead to non-specified schema during the querying, and usage of "public" by PostgreSQL.

This property can be redefined on initialization. Example:

  class MyModel
    include Clear::Model

    self.schema = "my_schema"
  end
  MyModel.query.to_sql # SELECT * FROM "my_schema"."my_models"
table

Return the table name setup for this model. By convention, the class name is by default equals to the pluralized underscored string form of the model name. Example:

  MyModel => "my_models"
  Person => "people"
  Project::Info => "project_infos"

The property can be updated at initialization to a custom table name:

  class MyModel
    include Clear::Model

    self.table = "another_table_name"
  end
  MyModel.query.to_sql # SELECT * FROM "another_table_name"
table=(table : Clear::SQL::Symbolic)

Return the table name setup for this model. By convention, the class name is by default equals to the pluralized underscored string form of the model name. Example:

  MyModel => "my_models"
  Person => "people"
  Project::Info => "project_infos"

The property can be updated at initialization to a custom table name:

  class MyModel
    include Clear::Model

    self.table = "another_table_name"
  end
  MyModel.query.to_sql # SELECT * FROM "another_table_name"
tables_only
Source
views_only
Source

Instance methods

_cached_columns
attributes

Attributes, used when fetch_columns is true

cache
changed?

Return true if the model is dirty (e.g. one or more fields have been changed.). Return false otherwise.

clear_change_flags

Reset the changed? flag on all columns

The model behave like its not dirty anymore and call to save would apply no changes.

Returns self

columns

The method columns is a has_many relation to Clear::Reflection::Column

indexes

List all the indexes related to the current table. return an hash where the key is the name of the column and the value is an array containing all the indexes related to this specific field.

Source
invalidate_caches

Invalidate local-to-relation cache and eager-loading cache. Useful to forcefully query again when calling relation defined method

reset(h : Hash(Symbol, _))

Set the columns from hash

reset(h : Hash(String, _))

Set the model fields from hash

reset(t : NamedTuple)
reset(from_json : JSON::Any)
reset

reset flavors

set(h : Hash(Symbol, _))

Set the columns from hash

set(h : Hash(String, _))

Set the model fields from hash

set(t : NamedTuple)
set(from_json : JSON::Any)
set

Set one or multiple columns to a specific value This two are equivalents:

model.set(a: 1)
model.a = 1
set_from_json(string_or_io : String | IO, trusted : Bool = false)

Set the fields from json passed as argument Trusted flag set to true will allow mass assignment without protection, FALSE by default

table_catalog

Returns the value of table_catalog column or throw an exception if the column is not defined.

table_catalog=(x : String)

Setter for table_catalog column.

table_catalog_column

Returns the column object used to manage table_catalog field

See Clear::Model::Column

table_name

Returns the value of table_name column or throw an exception if the column is not defined.

table_name=(x : String)

Setter for table_name column.

table_name_column

Returns the column object used to manage table_name field

See Clear::Model::Column

table_schema

Returns the value of table_schema column or throw an exception if the column is not defined.

table_schema=(x : String)

Setter for table_schema column.

table_schema_column

Returns the column object used to manage table_schema field

See Clear::Model::Column

table_type

Returns the value of table_type column or throw an exception if the column is not defined.

table_type=(x : String)

Setter for table_type column.

table_type_column

Returns the column object used to manage table_type field

See Clear::Model::Column

to_h(full = false) : Hash(String, Clear::SQL::Any)

Return a hash version of the columns of this model.

to_json(emit_nulls : Bool = false)
to_json(json, emit_nulls = false)
update_from_json(string_or_io : String | IO, trusted : Bool = false)

Set the fields from json passed as argument and call save on the object Trusted flag set to true will allow mass assignment without protection, FALSE by default

update_from_json!(string_or_io : String | IO, trusted : Bool = false)

Set the fields from json passed as argument and call save! on the object Trusted flag set to true will allow mass assignment without protection, FALSE by default

update_h

Generate the hash for update request (like during save)

validate_fields_presence

For each column, ensure than when needed the column has present information into it.

This method is called on validation.

Nested types