Skip to main content

Module schema

Module schema 

Source
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\*.

PHPRust
Schema (abstract), Schema\SQLSchema (trait)
Schema\MySQL, PostgreSQL, SQLite, ClickHouse, MongoDBMySql, 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, RenameColumnIndex, CheckConstraint, RenameColumn
createIndex() optionsIndexOptions
Schema\Feature\*traits in feature
Schema\Trait\*the dialects’ implementations of those traits
ColumnType, IndexType, ForeignKeyAction, Order, ParameterDirection, PartitionType, TriggerEvent, TriggerTimingColumnType, IndexType, ForeignKeyAction, Order, ParameterDirection, PartitionType, TriggerEvent, TriggerTiming
Schema\ClickHouse\Engine, ClickHouse\IndexAlgorithmEngine, 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 into CREATE TABLE, ALTER TABLE and the other table statements; and ForeignKey, a foreign key declared on it.

Structs§

CheckConstraint
A named table-level CHECK constraint.
ClickHouse
The ClickHouse schema: backtick quoting, Nullable(...) / LowCardinality(...) wrapping, engines with ORDER BY, PARTITION BY, SAMPLE BY, TTL and SETTINGS, 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.
IndexOptions
The options of a standalone CREATE INDEX statement.
MongoDb
The MongoDB schema: collections with $jsonSchema validators, indexes, views and databases as JSON commands ({"command": "createCollection", ...}).
MySql
The MySQL schema: backtick quoting, AUTO_INCREMENT, inline indexes, PARTITION BY ... PARTITIONS n.
PostgreSql
The PostgreSQL schema: double-quote quoting, identity columns, SERIAL types, CHECK-emulated enums, standalone index statements, PL/pgSQL procedures and triggers, types, sequences, extensions and collations.
RenameColumn
A column rename of an ALTER TABLE.
Sqlite
The SQLite schema: backtick quoting, INTEGER PRIMARY KEY AUTOINCREMENT inline, TEXT for most types, DELETE FROM for truncation.

Enums§

ColumnType
The portable type of a column; each dialect compiles it to its own SQL type (or BSON type).
Engine
A ClickHouse table engine.
ForeignKeyAction
What a foreign key does when the referenced row is deleted or updated.
IndexAlgorithm
The algorithm of a ClickHouse data-skipping index.
IndexType
The kind of an index.
Order
The order of an index column.
ParameterDirection
The direction of a stored procedure parameter.
PartitionType
How a table is partitioned.
TriggerEvent
The statement a trigger fires on.
TriggerTiming
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.