schema
ModuleSchema 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)"
);Contents
10 submodules, and what the module defines itself.
use utopia_query::schema::Schema;pub trait Schema: Clone + Send + Sync + Sized + 'staticA 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.
Implement it (and the feature traits) to add a dialect.
schema::clickhouse
Own page[ClickHouse]: the ClickHouse schema.
use utopia_query::schema::clickhouse::ClickHouse;pub struct ClickHouseThe 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.
use utopia_query::schema::{ClickHouse, Engine, Schema};
let mut table = ClickHouse::new().table("events");
table.big_integer("id").unsigned().primary();
table.string("country", 255).low_cardinality();
table.datetime("at", 3);
table.engine(Engine::MergeTree, &[])?.partition_by("toYYYYMM(at)");
assert_eq!(
table.create()?.query,
"CREATE TABLE `events` (`id` UInt64, `country` LowCardinality(String), `at` DateTime64(3)) \
ENGINE = MergeTree() PARTITION BY toYYYYMM(at) ORDER BY (`id`)"
);Debug.schema::column
Own page[Column]: a column of a table blueprint, with the modifiers its
dialect supports.
use utopia_query::schema::column::Column;pub struct Column<S>A column of a table blueprint for the dialect S.
Built by the table's factories (Table::string,
Table::integer, ...), which return it to
chain modifiers: table.string("email", 255).unique().nullable().
Clone, Debug, PartialEq.use utopia_query::schema::column::ColumnDefinition;pub struct ColumnDefinitionWhat a column is: its name, type, sizes and every modifier set on it.
use utopia_query::schema::column::AutoIncrement;pub trait AutoIncrement: SchemaDialects whose columns can be server-incremented.
use utopia_query::schema::column::Collation;pub trait Collation: SchemaDialects with column-level collations.
use utopia_query::schema::column::Comment;pub trait Comment: SchemaDialects with inline column comments.
use utopia_query::schema::column::Dimensions;pub trait Dimensions: SchemaDialects whose vector columns have a declared width.
use utopia_query::schema::column::Generated;pub trait Generated: SchemaDialects with stored generated columns.
use utopia_query::schema::column::Positioning;pub trait Positioning: SchemaDialects that place columns with AFTER.
use utopia_query::schema::column::Srid;pub trait Srid: SchemaDialects whose geometry columns carry an SRID.
use utopia_query::schema::column::Unique;pub trait Unique: SchemaDialects with column-level UNIQUE.
use utopia_query::schema::column::VirtualGenerated;pub trait VirtualGenerated: SchemaDialects with virtual generated columns.
schema::enums
Own pageThe schema enumerations: column and index types, foreign-key actions, partitioning, triggers, procedure parameters and ClickHouse engines.
schema::feature
Own pageWhat 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.
use utopia_query::schema::feature::ViewBody;pub enum ViewBody<'a>The body of a materialized view: a builder (its SQL inlined, its bindings carried) or raw SQL, emitted verbatim (trusted input only).
use utopia_query::schema::feature::AnalyzeTable;pub trait AnalyzeTable: SchemaRefreshing table statistics.
use utopia_query::schema::feature::ColumnComments;pub trait ColumnComments: SchemaColumn comments as separate statements.
use utopia_query::schema::feature::CreatePartition;pub trait CreatePartition: SchemaAdding partitions.
use utopia_query::schema::feature::Databases;pub trait Databases: SchemaDatabases (schemas on PostgreSQL).
use utopia_query::schema::feature::DropPartition;pub trait DropPartition: SchemaDropping partitions.
use utopia_query::schema::feature::ForeignKeys;pub trait ForeignKeys: SchemaAdding and dropping foreign-key constraints on existing tables.
use utopia_query::schema::feature::MaterializedViews;pub trait MaterializedViews: SchemaMaterialized views.
use utopia_query::schema::feature::Partitioning;pub trait Partitioning: SchemaPARTITION BY in CREATE TABLE.
use utopia_query::schema::feature::Procedures;pub trait Procedures: SchemaStored procedures.
use utopia_query::schema::feature::RenameIndex;pub trait RenameIndex: SchemaRenaming indexes.
use utopia_query::schema::feature::ReplaceView;pub trait ReplaceView: SchemaCREATE OR REPLACE VIEW.
use utopia_query::schema::feature::Sequences;pub trait Sequences: SchemaSequences.
use utopia_query::schema::feature::TableComments;pub trait TableComments: SchemaTable comments.
use utopia_query::schema::feature::Triggers;pub trait Triggers: SchemaRow triggers.
use utopia_query::schema::feature::Types;pub trait Types: SchemaUser-defined enum types.
use utopia_query::schema::feature::Views;pub trait Views: SchemaViews.
schema::index
Own pageThe value parts of a table blueprint: indexes, check constraints and
column renames, and the options of a standalone CREATE INDEX.
use utopia_query::schema::index::CheckConstraint;pub struct CheckConstraintA named table-level CHECK constraint.
use utopia_query::schema::index::Index;pub struct IndexAn 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.
use utopia_query::schema::index::IndexOptions;pub struct IndexOptionsThe options of a standalone CREATE INDEX statement.
use utopia_query::schema::index::RenameColumn;pub struct RenameColumnA column rename of an ALTER TABLE.
schema::mongodb
Own page[MongoDb]: the MongoDB schema, whose statements are JSON commands.
use utopia_query::schema::mongodb::MongoDb;pub struct MongoDbThe MongoDB schema: collections with $jsonSchema validators, indexes,
views and databases as JSON commands ({"command": "createCollection", ...}).
use utopia_query::schema::{MongoDb, Schema};
let mut users = MongoDb::new().table("users");
users.string("email", 255);
users.integer("age").nullable();
assert_eq!(
users.create()?.query,
r#"{"command":"createCollection","collection":"users","validator":{"$jsonSchema":{"bsonType":"object","properties":{"email":{"bsonType":"string"},"age":{"bsonType":"int"}},"required":["email"]}}}"#
);Debug.schema::mysql
Own page[MySql]: the MySQL / MariaDB schema.
use utopia_query::schema::mysql::MySql;pub struct MySqlThe MySQL schema: backtick quoting, AUTO_INCREMENT, inline indexes,
PARTITION BY ... PARTITIONS n.
use utopia_query::schema::{MySql, Schema};
let schema = MySql::new();
let mut table = schema.table("events");
table.id("id");
table.json("payload").nullable();
table.partition_by_hash("id", Some(4))?;
assert_eq!(
table.create()?.query,
"CREATE TABLE `events` (`id` BIGINT UNSIGNED AUTO_INCREMENT NOT NULL, `payload` JSON NULL, \
PRIMARY KEY (`id`)) PARTITION BY HASH(id) PARTITIONS 4"
);Debug.schema::postgresql
Own page[PostgreSql]: the PostgreSQL schema.
use utopia_query::schema::postgresql::PostgreSql;pub struct PostgreSqlThe 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.
use utopia_query::schema::{PostgreSql, Schema};
let mut table = PostgreSql::new().table("users");
table.add_column("bio", utopia_query::schema::ColumnType::Text, None).nullable();
table.drop_column("legacy");
table.index(["email"])?;
assert_eq!(
table.alter()?.query,
"ALTER TABLE \"users\" ADD COLUMN \"bio\" TEXT NULL, DROP COLUMN \"legacy\"; \
CREATE INDEX \"idx_email\" ON \"users\" (\"email\")"
);Debug.schema::sqlite
Own page[Sqlite]: the SQLite schema.
use utopia_query::schema::sqlite::Sqlite;pub struct SqliteThe SQLite schema: backtick quoting, INTEGER PRIMARY KEY AUTOINCREMENT
inline, TEXT for most types, DELETE FROM for truncation.
use utopia_query::schema::{Schema, Sqlite};
let mut table = Sqlite::new().table("notes");
table.id("id");
table.text("body");
assert_eq!(
table.create()?.query,
"CREATE TABLE `notes` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `body` TEXT NOT NULL)"
);Debug.schema::table
Own page[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.
use utopia_query::schema::table::ForeignKey;pub struct ForeignKeyA foreign key: FOREIGN KEY (column) REFERENCES table (column) with
optional ON DELETE / ON UPDATE actions.
use utopia_query::schema::table::Table;pub struct Table<S>A table blueprint for the dialect S: columns, indexes, keys and
alterations, compiled by the schema it came from.
use utopia_query::schema::{MySql, Schema};
let mut users = MySql::new().table("users");
users.id("id");
users.string("email", 255).unique();
users.foreign_key("team_id").references("id").on("teams");
assert_eq!(
users.create()?.query,
"CREATE TABLE `users` (`id` BIGINT UNSIGNED AUTO_INCREMENT NOT NULL, `email` VARCHAR(255) NOT NULL, \
PRIMARY KEY (`id`), UNIQUE (`email`), FOREIGN KEY (`team_id`) REFERENCES `teams` (`id`))"
);Clone, Debug.use utopia_query::schema::table::TableDefinition;pub struct TableDefinition<S>Everything a table blueprint holds: added columns, indexes, keys, and the alterations, partitioning and engine options set on it.
Clone, Debug.use utopia_query::schema::table::Checks;pub trait Checks: SchemaDialects with named table-level CHECK constraints (and column-level ones).
use utopia_query::schema::table::ColumnAlterations;pub trait ColumnAlterations: SchemaDialects that drop and rename columns in ALTER TABLE.
use utopia_query::schema::table::CompositePrimary;pub trait CompositePrimary: SchemaDialects with composite primary keys.
use utopia_query::schema::table::ForeignKeys;pub trait ForeignKeys: InlineForeignKeyDialects that also add and drop foreign keys in ALTER TABLE.
use utopia_query::schema::table::FulltextSpatialIndex;pub trait FulltextSpatialIndex: SchemaDialects with full-text and spatial indexes.
use utopia_query::schema::table::InlineForeignKey;pub trait InlineForeignKey: SchemaDialects with inline foreign keys in CREATE TABLE.
use utopia_query::schema::table::Serial;pub trait Serial: SchemaDialects with SERIAL column factories.
use utopia_query::schema::table::StandardPartitioning;pub trait StandardPartitioning: SchemaDialects with PARTITION BY RANGE / LIST / HASH.