class Clear::Reflection::Table
- Clear::Reflection::Table
- Reference
- Object
Overview
Reflection of the tables using information_schema in postgreSQL.
TODO Usage of view instead of model
Included Modules
- Clear::Model
- Clear::Model::Relations::BelongsToMacro
- Clear::Model::Relations::HasManyMacro
- Clear::Model::Relations::HasManyThroughMacro
- Clear::Model::Relations::HasOneMacro
Extended Modules
Defined in:
clear/model/reflection/table.crConstant Summary
-
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(**args) : self
Build and new model and save it.
- .create(x : NamedTuple) : self
- .create(x : Hash) : self
-
.create!(**args) : self
Build and new model and save it.
- .create!(x : NamedTuple) : self
- .create!(a : Hash) : self
- .new(h : Hash(String, _), cache : Clear::Model::QueryCache? = nil, persisted = false, fetch_columns = false)
- .new(json : JSON::Any, cache : Clear::Model::QueryCache? = nil, persisted = false)
- .new(t : NamedTuple, persisted = false)
- .new
Class Method Summary
- .__call_relation_filter__(name : String, query : Clear::SQL::SelectBuilder)
- .__relation_filter_columns__(query)
-
.build(**x : **T) forall T
Build a new empty model and fill the columns using the NamedTuple in argument.
- .columns
-
.connection : String
Define on which connection the model is living.
-
.connection=(connection : String)
Define on which connection the model is living.
-
.create(x : Array(NamedTuple)) : Array(self)
Multi-models creation.
-
.create!(x : Array(NamedTuple)) : Array(self)
Multi-models creation.
-
.create_from_json(string_or_io : String | IO, trusted : Bool = false)
Create a new model from json and save it.
-
.create_from_json!(string_or_io : String | IO, trusted : Bool = false)
Create a new model from json and save it.
-
.find(x)
Returns a model using primary key equality Returns
nil
if not found. -
.find!(x)
Returns a model using primary key equality.
-
.from_json(string_or_io : String | IO, trusted : Bool = false)
Create a new empty model and fill the columns from json.
-
.full_table_name
returns the fully qualified and escaped name for this table.
-
.import(array : Enumerable(self), on_conflict : Clear::SQL::InsertQuery -> ? = nil)
Import a bulk of models in one SQL insert query.
- .polymorphic? : Bool
- .public
-
.query
Return a new empty query
SELECT * FROM [my_model_table]
. - .read_only=(read_only : Bool)
- .read_only? : Bool
-
.schema : Clear::SQL::Symbolic?
Define the current schema used in PostgreSQL.
-
.schema=(schema : Clear::SQL::Symbolic?)
Define the current schema used in PostgreSQL.
-
.table : Clear::SQL::Symbolic
Return the table name setup for this model.
-
.table=(table : Clear::SQL::Symbolic)
Return the table name setup for this model.
- .tables_only
- .views_only
Instance Method Summary
- #_cached_columns : Array(Clear::Reflection::Column)?
-
#attributes : Hash(String, Clear::SQL::Any)
Attributes, used when fetch_columns is true
- #cache : Clear::Model::QueryCache?
-
#changed?
Return
true
if the model is dirty (e.g. -
#clear_change_flags
Reset the
#changed?
flag on all columns -
#columns : Clear::Reflection::Column::Collection
The method columns is a
has_many
relation to Clear::Reflection::Column -
#indexes : Hash(String, Array(String))
List all the indexes related to the current table.
-
#invalidate_caches
Invalidate local-to-relation cache and eager-loading cache.
-
#reset(**t : **T) forall T
reset flavors
- #reset(from_json : JSON::Any)
- #reset(t : NamedTuple)
-
#reset(h : Hash(String, _))
Set the model fields from hash
-
#reset(h : Hash(Symbol, _))
Set the columns from hash
- #set(t : NamedTuple)
-
#set(**t : **T) forall T
Set one or multiple columns to a specific value This two are equivalents:
- #set(from_json : JSON::Any)
-
#set(h : Hash(Symbol, _))
Set the columns from hash
-
#set(h : Hash(String, _))
Set the model fields from hash
-
#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 : String
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 : Clear::Model::Column(String, Clear::Model::Converter::StringConverter)
Returns the column object used to manage
#table_catalog
field -
#table_name : String
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 : Clear::Model::Column(String, Clear::Model::Converter::StringConverter)
Returns the column object used to manage
#table_name
field -
#table_schema : String
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 : Clear::Model::Column(String, Clear::Model::Converter::StringConverter)
Returns the column object used to manage
#table_schema
field -
#table_type : String
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 : Clear::Model::Column(String, Clear::Model::Converter::StringConverter)
Returns the column object used to manage
#table_type
field -
#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 : Hash(String, Clear::SQL::Any)
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.
Instance methods inherited from module Clear::Model
==(model : self)
==,
__pkey__
__pkey__,
cache : Clear::Model::QueryCache?
cache
Class methods inherited from module Clear::Model::FullTextSearchable
to_tsq(text)
to_tsq
Instance methods inherited from module Clear::Model::HasValidation
add_error(column, reason)add_error(reason) add_error, clear_errors clear_errors, error? error?, errors : Array(Error) errors, print_errors print_errors, valid! valid!, valid? valid?, validate validate
Instance methods inherited from module Clear::Model::HasSaving
delete
delete,
persisted? : Bool
persisted?,
reload : self
reload,
save(on_conflict : Clear::SQL::InsertQuery -> ? = nil)save(&block) save, save!(on_conflict : Clear::SQL::InsertQuery -> ? = nil)
save!(&block : Clear::SQL::InsertQuery -> ) save!, update(**args) update, update!(**args) update!
Instance methods inherited from module Clear::Model::HasColumns
[](x) : Clear::SQL::Any
[],
[]?(x) : Clear::SQL::Any
[]?,
reset(h : Hash(String, _))reset(h : Hash(Symbol, _))
reset(**t : **T) forall T reset, set(h : Hash(String, _))
set(h : Hash(Symbol, _))
set(**t : **T) forall T set, to_h(full = false) to_h, update_h update_h
Instance methods inherited from module Clear::Model::HasHooks
has_trigger?(event_name : Symbol, direction : Symbol)
has_trigger?,
trigger_after_events(event_name)
trigger_after_events,
trigger_before_events(event_name)
trigger_before_events,
with_triggers(event_name, &)
with_triggers
Instance methods inherited from module Clear::ErrorMessages
build_error_message(message : String, ways_to_resolve : Tuple | Array = Tuple.new, manual_pages : Tuple | Array = Tuple.new)
build_error_message,
converter_error(from, to)
converter_error,
format_width(x, w = 80)
format_width,
illegal_setter_access_to_undefined_column(name)
illegal_setter_access_to_undefined_column,
lack_of_primary_key(model_name)
lack_of_primary_key,
migration_already_down(number)
migration_already_down,
migration_already_up(number)
migration_already_up,
migration_drop_irreversible(name)
migration_drop_irreversible,
migration_irreversible(name = nil, operation = nil)
migration_irreversible,
migration_not_found(number)
migration_not_found,
migration_not_unique(numbers)
migration_not_unique,
no_migration_yet(version)
no_migration_yet,
null_column_mapping_error(name, type)
null_column_mapping_error,
order_by_error_invalid_order(current_order)
order_by_error_invalid_order,
polymorphic_nil(through)
polymorphic_nil,
polymorphic_unknown_class(class_name)
polymorphic_unknown_class,
query_building_error(message)
query_building_error,
uid_not_found(class_name)
uid_not_found,
uninitialized_db_connection(connection)
uninitialized_db_connection
Constructor Detail
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.
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.
Class Method Detail
Build a new empty model and fill the columns using the NamedTuple in argument.
Returns the new model
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
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
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.
Multi-models creation. See Collection#create!(**args)
Returns the list of newly created model. Raises exception if any of the model has validation error.
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 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
Returns a model using primary key equality. Raises error if the model is not found.
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
returns the fully qualified and escaped name for this table. add schema if schema is different from 'public' (default schema)
ex: "schema"."table"
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)
Return a new empty query SELECT * FROM [my_model_table]
. Can be refined after that.
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"
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"
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"
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"
Instance Method Detail
Return true
if the model is dirty (e.g. one or more fields
have been changed.). Return false
otherwise.
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
The method columns is a has_many
relation
to Clear::Reflection::Column
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.
Invalidate local-to-relation cache and eager-loading cache. Useful to forcefully query again when calling relation defined method
Set one or multiple columns to a specific value This two are equivalents:
model.set(a: 1)
model.a = 1
Set the fields from json passed as argument Trusted flag set to true will allow mass assignment without protection, FALSE by default
Returns the value of #table_catalog
column or throw an exception if the column is not defined.
Returns the value of #table_name
column or throw an exception if the column is not defined.
Returns the value of #table_schema
column or throw an exception if the column is not defined.
Returns the value of #table_type
column or throw an exception if the column is not defined.
Return a hash version of the columns of this model.
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
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
Generate the hash for update request (like during save)
For each column, ensure than when needed the column has present information into it.
This method is called on validation.