Lustra::SQL
Inherits Lustra::SQL::Transaction < Lustra::SQL::Logger
Lustra::SQL
Lustra is made like an onion:
+------------------------------------+
| THE ORM STACK +
+------------------------------------+
| Model | DB Views | Migrations | < High Level Tools
+---------------+--------------------+
| Columns | Validation | Converters | < Mapping system
+---------------+--------------------+
| Lustra::SQL | Lustra::Expression | < Low Level SQL Builder
+------------------------------------+
| Crystal DB | Crystal PG | < Low Level connection
+------------------------------------+
On the bottom stack, Lustra offer SQL query building. Theses features 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 update and store a request,
you must use manually the dup method.
Class methods
Lock completetly a table.
Lustra::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
Lustra::SQL.truncate(User) # equivalent to Lustra::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
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 Lustra::SQL.sanitize
Execute a SQL statement on a specific connection.
Usage: Lustra::SQL.execute("seconddatabase", "SELECT 1 FROM users")
Start an INSERT INTO table query
Lustra::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 = Model.query.select(Lustra::SQL.raw("CASE WHEN x=:x THEN 1 ELSE 0 END AS check", x: "blabla"))
query = Model.query.select(Lustra::SQL.raw("CASE WHEN x=? THEN 1 ELSE 0 END AS check", "blabla"))
Create a transaction, but this one is stackable using savepoints.
Example:
Lustra::SQL.with_savepoint do
# do something
Lustra::SQL.with_savepoint do
rollback # < Rollback only the last `with_savepoint` block
end
end
Nested types
- Lustra::SQL::Any
- Lustra::SQL::CancelTransactionError
- Lustra::SQL::Column
- Lustra::SQL::ConnectionPool
- Lustra::SQL::DeleteQuery
- Lustra::SQL::Error
- Lustra::SQL::ExecutionError
- Lustra::SQL::Fragment
- Lustra::SQL::From
- Lustra::SQL::InsertQuery
- Lustra::SQL::JSONB
- Lustra::SQL::Join
- Lustra::SQL::Logger
- Lustra::SQL::OperationNotPermittedError
- Lustra::SQL::Query
- Lustra::SQL::QueryBuildingError
- Lustra::SQL::RecordNotFoundError
- Lustra::SQL::RollbackError
- Lustra::SQL::SelectBuilder
- Lustra::SQL::SelectQuery
- Lustra::SQL::Selectable
- Lustra::SQL::Symbolic
- Lustra::SQL::Transaction
- Lustra::SQL::UpdateQuery