pub struct Table<S> { /* private fields */ }Expand description
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`))"
);use Utopia\Query\Schema\MySQL;
$users = (new MySQL())->table('users');
$users->id();
$users->string('email')->unique();
$users->foreignKey('team_id')->references('id')->on('teams');
echo $users->create()->query, "\n";PHP: Utopia\Query\Schema\Table and its dialect classes (Table\MySQL,
Table\PostgreSQL, Table\SQLite, Table\ClickHouse, Table\MongoDB).
Implementations§
Source§impl<S> Table<S>
impl<S> Table<S>
Sourcepub fn new(name: impl Into<String>) -> Self
pub fn new(name: impl Into<String>) -> Self
A blueprint of table name without a schema: it collects columns and
alterations but cannot compile (its terminal methods fail).
PHP: new Table(null, $name).
Sourcepub fn with_schema(schema: S, name: impl Into<String>) -> Self
pub fn with_schema(schema: S, name: impl Into<String>) -> Self
A blueprint of table name compiled by schema (what
Schema::table returns).
PHP: new Table($schema, $name).
Sourcepub fn definition(&self) -> &TableDefinition<S>
pub fn definition(&self) -> &TableDefinition<S>
Everything the blueprint holds.
PHP: the table’s public properties.
Sourcepub fn definition_mut(&mut self) -> &mut TableDefinition<S>
pub fn definition_mut(&mut self) -> &mut TableDefinition<S>
Everything the blueprint holds, to set directly: nothing here is validated, as nothing is when PHP sets a table’s properties.
Sourcepub fn column_mut(&mut self, index: usize) -> Option<&mut Column<S>>
pub fn column_mut(&mut self, index: usize) -> Option<&mut Column<S>>
The column at index (in the order they were added), to modify it later.
Sourcepub fn foreign_keys(&self) -> &[ForeignKey]
pub fn foreign_keys(&self) -> &[ForeignKey]
The foreign keys declared so far.
PHP: $table->foreignKeys.
Sourcepub fn foreign_key_mut(&mut self, index: usize) -> Option<&mut ForeignKey>
pub fn foreign_key_mut(&mut self, index: usize) -> Option<&mut ForeignKey>
The foreign key at index (in the order they were declared), to modify it later.
Sourcepub fn id(&mut self, name: impl Into<String>) -> &mut Column<S>
pub fn id(&mut self, name: impl Into<String>) -> &mut Column<S>
Adds an unsigned auto-incrementing BIGINT primary key name
(PHP’s default name is id).
PHP: id().
Sourcepub fn string(&mut self, name: impl Into<String>, length: i64) -> &mut Column<S>
pub fn string(&mut self, name: impl Into<String>, length: i64) -> &mut Column<S>
Adds a string column of length characters (PHP’s default is 255).
PHP: string().
Sourcepub fn text(&mut self, name: impl Into<String>) -> &mut Column<S>
pub fn text(&mut self, name: impl Into<String>) -> &mut Column<S>
Adds a text column.
PHP: text().
Sourcepub fn medium_text(&mut self, name: impl Into<String>) -> &mut Column<S>
pub fn medium_text(&mut self, name: impl Into<String>) -> &mut Column<S>
Adds a medium text column.
PHP: mediumText().
Sourcepub fn long_text(&mut self, name: impl Into<String>) -> &mut Column<S>
pub fn long_text(&mut self, name: impl Into<String>) -> &mut Column<S>
Adds a long text column.
PHP: longText().
Sourcepub fn tiny_integer(&mut self, name: impl Into<String>) -> &mut Column<S>
pub fn tiny_integer(&mut self, name: impl Into<String>) -> &mut Column<S>
Adds an 8-bit integer column.
PHP: tinyInteger().
Sourcepub fn small_integer(&mut self, name: impl Into<String>) -> &mut Column<S>
pub fn small_integer(&mut self, name: impl Into<String>) -> &mut Column<S>
Adds a 16-bit integer column.
PHP: smallInteger().
Sourcepub fn integer(&mut self, name: impl Into<String>) -> &mut Column<S>
pub fn integer(&mut self, name: impl Into<String>) -> &mut Column<S>
Adds a 32-bit integer column.
PHP: integer().
Sourcepub fn big_integer(&mut self, name: impl Into<String>) -> &mut Column<S>
pub fn big_integer(&mut self, name: impl Into<String>) -> &mut Column<S>
Adds a 64-bit integer column.
PHP: bigInteger().
Sourcepub fn decimal(
&mut self,
name: impl Into<String>,
precision: i64,
scale: i64,
) -> Result<&mut Column<S>>
pub fn decimal( &mut self, name: impl Into<String>, precision: i64, scale: i64, ) -> Result<&mut Column<S>>
Adds a DECIMAL(precision, scale) column (PHP’s defaults are 10 and
0); the precision is at least 1 and the scale between 0 and the
precision.
PHP: decimal().
Sourcepub fn uuid(&mut self, name: impl Into<String>) -> &mut Column<S>
pub fn uuid(&mut self, name: impl Into<String>) -> &mut Column<S>
Adds a UUID column (native where the dialect has one).
PHP: uuid().
Sourcepub fn float(&mut self, name: impl Into<String>) -> &mut Column<S>
pub fn float(&mut self, name: impl Into<String>) -> &mut Column<S>
Adds a double-precision float column.
PHP: float().
Sourcepub fn boolean(&mut self, name: impl Into<String>) -> &mut Column<S>
pub fn boolean(&mut self, name: impl Into<String>) -> &mut Column<S>
Adds a boolean column.
PHP: boolean().
Sourcepub fn datetime(
&mut self,
name: impl Into<String>,
precision: i64,
) -> &mut Column<S>
pub fn datetime( &mut self, name: impl Into<String>, precision: i64, ) -> &mut Column<S>
Adds a date-time column with fractional-second precision (PHP’s
default is 0).
PHP: datetime().
Sourcepub fn timestamp(
&mut self,
name: impl Into<String>,
precision: i64,
) -> &mut Column<S>
pub fn timestamp( &mut self, name: impl Into<String>, precision: i64, ) -> &mut Column<S>
Adds a timestamp column with fractional-second precision (PHP’s
default is 0).
PHP: timestamp().
Sourcepub fn json(&mut self, name: impl Into<String>) -> &mut Column<S>
pub fn json(&mut self, name: impl Into<String>) -> &mut Column<S>
Adds a JSON column.
PHP: json().
Sourcepub fn binary(&mut self, name: impl Into<String>) -> &mut Column<S>
pub fn binary(&mut self, name: impl Into<String>) -> &mut Column<S>
Adds a binary column.
PHP: binary().
Sourcepub fn enumeration<V: Into<String>>(
&mut self,
name: impl Into<String>,
values: impl IntoIterator<Item = V>,
) -> Result<&mut Column<S>>
pub fn enumeration<V: Into<String>>( &mut self, name: impl Into<String>, values: impl IntoIterator<Item = V>, ) -> Result<&mut Column<S>>
Adds an enum column of values (one at least).
PHP: enum().
Sourcepub fn point(&mut self, name: impl Into<String>, srid: i64) -> &mut Column<S>
pub fn point(&mut self, name: impl Into<String>, srid: i64) -> &mut Column<S>
Adds a point column with srid (PHP’s default is 4326).
PHP: point().
Sourcepub fn linestring(
&mut self,
name: impl Into<String>,
srid: i64,
) -> &mut Column<S>
pub fn linestring( &mut self, name: impl Into<String>, srid: i64, ) -> &mut Column<S>
Adds a line string column with srid (PHP’s default is 4326).
PHP: linestring().
Sourcepub fn polygon(&mut self, name: impl Into<String>, srid: i64) -> &mut Column<S>
pub fn polygon(&mut self, name: impl Into<String>, srid: i64) -> &mut Column<S>
Adds a polygon column with srid (PHP’s default is 4326).
PHP: polygon().
Sourcepub fn timestamps(&mut self, precision: i64) -> &mut Self
pub fn timestamps(&mut self, precision: i64) -> &mut Self
Adds created_at and updated_at date-times with precision (PHP’s
default is 3).
PHP: timestamps().
Sourcepub fn add_column(
&mut self,
name: impl Into<String>,
kind: ColumnType,
length_or_precision: Option<i64>,
) -> &mut Column<S>
pub fn add_column( &mut self, name: impl Into<String>, kind: ColumnType, length_or_precision: Option<i64>, ) -> &mut Column<S>
Adds a column of any type; length_or_precision is the length of a
string and the precision of anything else.
PHP: addColumn().
Sourcepub fn modify_column(
&mut self,
name: impl Into<String>,
kind: ColumnType,
length_or_precision: Option<i64>,
) -> &mut Column<S>
pub fn modify_column( &mut self, name: impl Into<String>, kind: ColumnType, length_or_precision: Option<i64>, ) -> &mut Column<S>
Modifies a column (an ALTER emits MODIFY / ALTER COLUMN).
PHP: modifyColumn().
Sourcepub fn index<C: Into<String>>(
&mut self,
columns: impl IntoIterator<Item = C>,
) -> Result<&mut Self>
pub fn index<C: Into<String>>( &mut self, columns: impl IntoIterator<Item = C>, ) -> Result<&mut Self>
Adds an index over columns, named idx_<columns>.
PHP: index() (the other options: Table::add_index).
Sourcepub fn unique_index<C: Into<String>>(
&mut self,
columns: impl IntoIterator<Item = C>,
) -> Result<&mut Self>
pub fn unique_index<C: Into<String>>( &mut self, columns: impl IntoIterator<Item = C>, ) -> Result<&mut Self>
Adds a unique index over columns, named uniq_<columns>.
PHP: uniqueIndex().
Sourcepub fn add_index(&mut self, index: Index) -> Result<&mut Self>
pub fn add_index(&mut self, index: Index) -> Result<&mut Self>
Adds index, after Index::validated.
PHP: addIndex() (and the options of index() / uniqueIndex()).
Sourcepub fn drop_index(&mut self, name: impl Into<String>) -> &mut Self
pub fn drop_index(&mut self, name: impl Into<String>) -> &mut Self
Drops the index name (in an ALTER).
PHP: dropIndex().
Sourcepub fn raw_column(&mut self, definition: impl Into<String>) -> &mut Self
pub fn raw_column(&mut self, definition: impl Into<String>) -> &mut Self
Adds a raw column definition, emitted verbatim.
PHP: rawColumn().
Source§impl<S: Schema> Table<S>
impl<S: Schema> Table<S>
Sourcepub fn create_if_not_exists(&self) -> Result<Statement>
pub fn create_if_not_exists(&self) -> Result<Statement>
CREATE TABLE IF NOT EXISTS.
PHP: createIfNotExists() / create(true).
Sourcepub fn alter(&self) -> Result<Statement>
pub fn alter(&self) -> Result<Statement>
ALTER TABLE with the columns, renames, drops, indexes and keys
collected.
PHP: alter().
Sourcepub fn drop_if_exists(&self) -> Result<Statement>
pub fn drop_if_exists(&self) -> Result<Statement>
DROP TABLE IF EXISTS.
PHP: dropIfExists().
Source§impl<S: Serial> Table<S>
impl<S: Serial> Table<S>
Sourcepub fn serial(&mut self, name: impl Into<String>) -> &mut Column<S>
pub fn serial(&mut self, name: impl Into<String>) -> &mut Column<S>
Adds an auto-incrementing integer column.
PHP: serial().
Sourcepub fn big_serial(&mut self, name: impl Into<String>) -> &mut Column<S>
pub fn big_serial(&mut self, name: impl Into<String>) -> &mut Column<S>
Adds an auto-incrementing big integer column.
PHP: bigSerial().
Sourcepub fn small_serial(&mut self, name: impl Into<String>) -> &mut Column<S>
pub fn small_serial(&mut self, name: impl Into<String>) -> &mut Column<S>
Adds an auto-incrementing small integer column.
PHP: smallSerial().
Source§impl<S: ColumnAlterations> Table<S>
impl<S: ColumnAlterations> Table<S>
Sourcepub fn rename_column(
&mut self,
from: impl Into<String>,
to: impl Into<String>,
) -> &mut Self
pub fn rename_column( &mut self, from: impl Into<String>, to: impl Into<String>, ) -> &mut Self
Renames the column from to to (in an ALTER).
PHP: renameColumn().
Sourcepub fn drop_column(&mut self, name: impl Into<String>) -> &mut Self
pub fn drop_column(&mut self, name: impl Into<String>) -> &mut Self
Drops the column name (in an ALTER).
PHP: dropColumn().
Source§impl<S: CompositePrimary> Table<S>
impl<S: CompositePrimary> Table<S>
Sourcepub fn primary<C: Into<String>>(
&mut self,
columns: impl IntoIterator<Item = C>,
) -> Result<&mut Self>
pub fn primary<C: Into<String>>( &mut self, columns: impl IntoIterator<Item = C>, ) -> Result<&mut Self>
Declares a primary key over two columns or more, each an identifier
(a single-column key is Column::primary).
PHP: primary() with an array.
Source§impl<S: InlineForeignKey> Table<S>
impl<S: InlineForeignKey> Table<S>
Sourcepub fn foreign_key(&mut self, column: impl Into<String>) -> &mut ForeignKey
pub fn foreign_key(&mut self, column: impl Into<String>) -> &mut ForeignKey
Declares a foreign key on column.
PHP: foreignKey().
Source§impl<S: ForeignKeys> Table<S>
impl<S: ForeignKeys> Table<S>
Sourcepub fn add_foreign_key(&mut self, column: impl Into<String>) -> &mut ForeignKey
pub fn add_foreign_key(&mut self, column: impl Into<String>) -> &mut ForeignKey
Declares a foreign key to add in an ALTER (the same as
Table::foreign_key).
PHP: addForeignKey().
Sourcepub fn drop_foreign_key(&mut self, name: impl Into<String>) -> &mut Self
pub fn drop_foreign_key(&mut self, name: impl Into<String>) -> &mut Self
Drops the foreign key name (in an ALTER).
PHP: dropForeignKey().
Source§impl<S: FulltextSpatialIndex> Table<S>
impl<S: FulltextSpatialIndex> Table<S>
Sourcepub fn fulltext_index<C: Into<String>>(
&mut self,
columns: impl IntoIterator<Item = C>,
) -> Result<&mut Self>
pub fn fulltext_index<C: Into<String>>( &mut self, columns: impl IntoIterator<Item = C>, ) -> Result<&mut Self>
Adds a full-text index over columns, named ft_<columns>.
PHP: fulltextIndex().
Sourcepub fn spatial_index<C: Into<String>>(
&mut self,
columns: impl IntoIterator<Item = C>,
) -> Result<&mut Self>
pub fn spatial_index<C: Into<String>>( &mut self, columns: impl IntoIterator<Item = C>, ) -> Result<&mut Self>
Adds a spatial index over columns, named sp_<columns>.
PHP: spatialIndex().
Source§impl<S: StandardPartitioning> Table<S>
impl<S: StandardPartitioning> Table<S>
Sourcepub fn partition_by_range(&mut self, expression: impl Into<String>) -> &mut Self
pub fn partition_by_range(&mut self, expression: impl Into<String>) -> &mut Self
Partitions by range of expression.
PHP: partitionByRange().
Sourcepub fn partition_by_list(&mut self, expression: impl Into<String>) -> &mut Self
pub fn partition_by_list(&mut self, expression: impl Into<String>) -> &mut Self
Partitions by list of expression.
PHP: partitionByList().
Source§impl Table<PostgreSql>
impl Table<PostgreSql>
Source§impl Table<ClickHouse>
impl Table<ClickHouse>
Sourcepub fn vector(&mut self, name: impl Into<String>) -> &mut Column<ClickHouse>
pub fn vector(&mut self, name: impl Into<String>) -> &mut Column<ClickHouse>
Adds an Array(Float64) vector column.
PHP: Table\ClickHouse::vector().
Sourcepub fn array(
&mut self,
name: impl Into<String>,
element: ColumnType,
) -> &mut Column<ClickHouse>
pub fn array( &mut self, name: impl Into<String>, element: ColumnType, ) -> &mut Column<ClickHouse>
Adds an Array(element) column.
PHP: array().
Sourcepub fn tuple(
&mut self,
name: impl Into<String>,
elements: Vec<ColumnType>,
) -> Result<&mut Column<ClickHouse>>
pub fn tuple( &mut self, name: impl Into<String>, elements: Vec<ColumnType>, ) -> Result<&mut Column<ClickHouse>>
Adds a Tuple(elements...) column (one element at least).
PHP: tuple().
Sourcepub fn fixed_string(
&mut self,
name: impl Into<String>,
length: i64,
) -> Result<&mut Column<ClickHouse>>
pub fn fixed_string( &mut self, name: impl Into<String>, length: i64, ) -> Result<&mut Column<ClickHouse>>
Adds a FixedString(length) column (length at least 1).
PHP: fixedString().
Sourcepub fn engine(&mut self, engine: Engine, args: &[&str]) -> Result<&mut Self>
pub fn engine(&mut self, engine: Engine, args: &[&str]) -> Result<&mut Self>
Selects the engine; CollapsingMergeTree needs a sign column and
ReplicatedMergeTree a ZooKeeper path and a replica name.
PHP: engine().
Sourcepub fn order_by<C: Into<String>>(
&mut self,
columns: impl IntoIterator<Item = C>,
) -> Result<&mut Self>
pub fn order_by<C: Into<String>>( &mut self, columns: impl IntoIterator<Item = C>, ) -> Result<&mut Self>
Sets the ORDER BY columns (identifiers); without one, the primary
key orders the table.
PHP: orderBy().
Sourcepub fn order_by_raw(&mut self, expression: &str) -> Result<&mut Self>
pub fn order_by_raw(&mut self, expression: &str) -> Result<&mut Self>
Sets a raw ORDER BY expression (trimmed, not empty, no ;), which
takes precedence over Table::order_by.
PHP: orderByRaw().
Sourcepub fn ttl(&mut self, expression: &str) -> Result<&mut Self>
pub fn ttl(&mut self, expression: &str) -> Result<&mut Self>
Sets the table TTL (trimmed, not empty, no ;).
PHP: Table\ClickHouse::ttl().
Sourcepub fn settings<K: Into<String>>(
&mut self,
settings: impl IntoIterator<Item = (K, Value)>,
) -> Result<&mut Self>
pub fn settings<K: Into<String>>( &mut self, settings: impl IntoIterator<Item = (K, Value)>, ) -> Result<&mut Self>
Replaces the engine SETTINGS: names are identifiers; booleans
become 1 / 0, numbers are printed, strings must match
[A-Za-z0-9_.\-+/]+.
PHP: settings().
Sourcepub fn partition_by(&mut self, expression: impl Into<String>) -> &mut Self
pub fn partition_by(&mut self, expression: impl Into<String>) -> &mut Self
Sets the PARTITION BY expression.
PHP: partitionBy().