Expand description
Schema builders: DDL for tables, columns, indexes, views, procedures and triggers, per dialect.
A schema (MySql, PostgreSql, Sqlite, ClickHouse,
MongoDb) compiles statements; Schema::table starts a fluent
Table blueprint whose terminal methods (Table::create,
Table::alter, Table::drop, …) compile it. Each dialect only has
the column and table methods it can express (marker traits in
column and table), and the statements beyond tables are
capability traits in feature.
use utopia_query::schema::{ForeignKeyAction, PostgreSql, Schema};
let mut posts = PostgreSql::new().table("posts");
posts.id("id");
posts.string("title", 120);
posts.integer("author_id");
posts.foreign_key("author_id").references("id").on("users").on_delete(ForeignKeyAction::Cascade);
assert_eq!(
posts.create()?.query,
"CREATE TABLE \"posts\" (\"id\" BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL, \
\"title\" VARCHAR(120) NOT NULL, \"author_id\" INTEGER NOT NULL, PRIMARY KEY (\"id\"), \
FOREIGN KEY (\"author_id\") REFERENCES \"users\" (\"id\") ON DELETE CASCADE)"
);use Utopia\Query\Schema\ForeignKeyAction;
use Utopia\Query\Schema\PostgreSQL;
$posts = (new PostgreSQL())->table('posts');
$posts->id();
$posts->string('title', 120);
$posts->integer('author_id');
$posts->foreignKey('author_id')->references('id')->on('users')->onDelete(ForeignKeyAction::Cascade);
echo $posts->create()->query, "\n";PHP: Utopia\Query\Schema, Utopia\Query\Schema\*.
| PHP | Rust |
|---|---|
Schema (abstract), Schema\SQL | Schema (trait) |
Schema\MySQL, PostgreSQL, SQLite, ClickHouse, MongoDB | MySql, PostgreSql, Sqlite, ClickHouse, MongoDb |
Schema\Table, Table\* | Table (Table<MySql>, …), TableDefinition (its properties) |
Table\Trait\* | marker traits in table (table::Serial, table::Checks, …) |
Schema\Column, Column\* | Column (Column<MySql>, …), ColumnDefinition (its properties) |
Column\Trait\* | marker traits in column (column::Unique, column::Collation, …) |
Schema\ForeignKey, ForeignKey\* | ForeignKey |
Schema\Forwarder\* | none: a column or key handle borrows its table, so table methods are called on the table |
Schema\Index, CheckConstraint, RenameColumn | Index, CheckConstraint, RenameColumn |
createIndex() options | IndexOptions |
Schema\Feature\* | traits in feature |
Schema\Trait\* | the dialects’ implementations of those traits |
ColumnType, IndexType, ForeignKeyAction, Order, ParameterDirection, PartitionType, TriggerEvent, TriggerTiming | ColumnType, IndexType, ForeignKeyAction, Order, ParameterDirection, PartitionType, TriggerEvent, TriggerTiming |
Schema\ClickHouse\Engine, ClickHouse\IndexAlgorithm | Engine, IndexAlgorithm |
Re-exports§
pub use column::Column;pub use column::ColumnDefinition;pub use feature::ViewBody;pub use table::ForeignKey;pub use table::Table;pub use table::TableDefinition;
Modules§
- column
Column: a column of a table blueprint, with the modifiers its dialect supports.- feature
- What a schema can do beyond tables and indexes: each trait is one capability a dialect may implement (foreign keys, procedures, triggers, views, partitions, …), so callers can require it in a bound.
- table
Table: a fluent table blueprint, compiled by its schema intoCREATE TABLE,ALTER TABLEand the other table statements; andForeignKey, a foreign key declared on it.
Structs§
- Check
Constraint - A named table-level
CHECKconstraint. - Click
House - The ClickHouse schema: backtick quoting,
Nullable(...)/LowCardinality(...)wrapping, engines withORDER BY,PARTITION BY,SAMPLE BY, TTL andSETTINGS, data-skipping indexes, codecs and materialized views. - Index
- An index of a table: its columns (with per-column lengths, orders and collations), raw expressions, method and operator class, and for ClickHouse data-skipping indexes the algorithm, its arguments and the granularity.
- Index
Options - The options of a standalone
CREATE INDEXstatement. - MongoDb
- The MongoDB schema: collections with
$jsonSchemavalidators, indexes, views and databases as JSON commands ({"command": "createCollection", ...}). - MySql
- The MySQL schema: backtick quoting,
AUTO_INCREMENT, inline indexes,PARTITION BY ... PARTITIONS n. - Postgre
Sql - The PostgreSQL schema: double-quote quoting, identity columns,
SERIALtypes,CHECK-emulated enums, standalone index statements, PL/pgSQL procedures and triggers, types, sequences, extensions and collations. - Rename
Column - A column rename of an
ALTER TABLE. - Sqlite
- The SQLite schema: backtick quoting,
INTEGER PRIMARY KEY AUTOINCREMENTinline,TEXTfor most types,DELETE FROMfor truncation.
Enums§
- Column
Type - The portable type of a column; each dialect compiles it to its own SQL type (or BSON type).
- Engine
- A ClickHouse table engine.
- Foreign
KeyAction - What a foreign key does when the referenced row is deleted or updated.
- Index
Algorithm - The algorithm of a ClickHouse data-skipping index.
- Index
Type - The kind of an index.
- Order
- The order of an index column.
- Parameter
Direction - The direction of a stored procedure parameter.
- Partition
Type - How a table is partitioned.
- Trigger
Event - The statement a trigger fires on.
- Trigger
Timing - When a trigger fires.
Traits§
- Schema
- A dialect’s DDL compiler: quoting, column types, and the table, index and drop statements. Dialects override the steps they write differently; the provided methods are the shared (MySQL-flavoured) compilation.