Clear::SQL
Inherits Clear::SQL::Transaction < Clear::SQL::Logger
Clear::SQL
Clear is made like an onion:
+------------------------------------+
| THE ORM STACK +
+------------------------------------+
| Model | DB Views | Migrations | < High level things
+---------------+--------------------+
| Columns | Validation | Converters | < Mapping stuff
+---------------+--------------------+
| Clear::SQL | Clear::Expression | < Low level SQL builder
+------------------------------------+
| Crystal DB | Crystal PG | < Libs we deal with
+------------------------------------+
On the bottom stack, Clear offer SQL query building. Features provided are then used by top level parts of the engine.
The SQL module provide a simple API to generate delete, insert, select
and update methods.
Each requests can be duplicated then modified and executed.
Note: Each request object is mutable. Therefore, to keep a request prior to modification,
you must use manually the dup method.
Class methods
Lock completetly a table.
Clear::SQL.lock("my_table") do
end
Optional parameter mode allow you to decide over the lock level
Modes are:
- ACCESS EXCLUSIVE (default)
- ACCESS SHARE
- ROW SHARE
- ROW EXCLUSIVE
- SHARE UPDATE EXCLUSIVE
- SHARE
- SHARE ROW EXCLUSIVE
- EXCLUSIVE
Truncate a table or a model
User.query.count # => 200
Clear::SQL.truncate(User) # equivalent to Clear::SQL.truncate(User.table, connection_name: User.connection)
User.query.count # => 0
SEE https://www.postgresql.org/docs/current/sql-truncate.html for more information.
restart_sequenceset to true will appendRESTART IDENTITYto the querycascadeset to true will appendCASCADEto the querytruncate_inheritedset to false will appendONLYto the queryconnection_namewill be:Model.connectionordefaultunless optionally defined.
Instance methods
connect through a hash/named tuple of connections:
Clear::SQL.init(
default: "postgres://postgres@localhost:5432/database",
secondary: "postgres://postgres@localhost:5432/secondary"
)
Escape the expression, double quoting it.
It allows use of reserved keywords as table or column name NOTE: Escape is used for escaping postgresql keyword. For example if you have a column named order (which is a reserved word), you want to escape it by double-quoting it.
For escaping STRING value, please use Clear::SQL.sanitize
Execute a SQL statement without returning a result set
Usage:
Clear::SQL.execute("NOTIFY listener")
Execute a SQL statement without returning a result set
Usage:
Clear::SQL.execute("NOTIFY listener")
Initialize a new connection to a specific database Use "default" connection if no name is provided:
init("postgres://postgres@localhost:5432/database") # use "default" connection
init("secondary_db", "postgres://postgres@localhost:5432/secondary_db")
Initialize a new connection to a specific database Use "default" connection if no name is provided:
init("postgres://postgres@localhost:5432/database") # use "default" connection
init("secondary_db", "postgres://postgres@localhost:5432/secondary_db")
connect through a hash/named tuple of connections:
Clear::SQL.init(
default: "postgres://postgres@localhost:5432/database",
secondary: "postgres://postgres@localhost:5432/secondary"
)
connect through a hash/named tuple of connections:
Clear::SQL.init(
default: "postgres://postgres@localhost:5432/database",
secondary: "postgres://postgres@localhost:5432/secondary"
)
Alias of insert_into, for developers in hurry
:ditto:
Start an INSERT INTO table query
Clear::SQL.insert_into("table", {id: 1, name: "hello"}, {id: 2, name: "World"})
This provide a fast way to create SQL fragment while escaping items, both with ? and :key system:
query = Mode.query.select(Clear::SQL.raw("CASE WHEN x=:x THEN 1 ELSE 0 END as check", x: "blabla"))
query = Mode.query.select(Clear::SQL.raw("CASE WHEN x=? THEN 1 ELSE 0 END as check", "blabla"))
note than returned string is tagged as unsafe and SQL inject is possible (so beware!)
This provide a fast way to create SQL fragment while escaping items, both with ? and :key system:
query = Mode.query.select(Clear::SQL.raw("CASE WHEN x=:x THEN 1 ELSE 0 END as check", x: "blabla"))
query = Mode.query.select(Clear::SQL.raw("CASE WHEN x=? THEN 1 ELSE 0 END as check", "blabla"))
note than returned string is tagged as unsafe and SQL inject is possible (so beware!)
Create an unsafe expression, which can be used in many places in Clear as substitute for string
select.where("x = ?", Clear::SQL.unsafe("y")) # SELECT ... WHERE x = y
Nested types
- Clear::SQL::Any
- Clear::SQL::CancelTransactionError
- Clear::SQL::Column
- Clear::SQL::ConnectionPool
- Clear::SQL::DeleteQuery
- Clear::SQL::Error
- Clear::SQL::ExecutionError
- Clear::SQL::Fragment
- Clear::SQL::From
- Clear::SQL::InsertQuery
- Clear::SQL::JSONB
- Clear::SQL::Join
- Clear::SQL::Logger
- Clear::SQL::OperationNotPermittedError
- Clear::SQL::Query
- Clear::SQL::QueryBuildingError
- Clear::SQL::RecordNotFoundError
- Clear::SQL::RollbackError
- Clear::SQL::SelectBuilder
- Clear::SQL::SelectQuery
- Clear::SQL::Selectable
- Clear::SQL::Symbolic
- Clear::SQL::Transaction
- Clear::SQL::UpdateQuery