Skip to main content

Table

Struct Table 

Source
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>

Source

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).

Source

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).

Source

pub fn definition(&self) -> &TableDefinition<S>

Everything the blueprint holds.

PHP: the table’s public properties.

Source

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.

Source

pub fn name(&self) -> &str

The table name.

PHP: $table->name.

Source

pub fn columns(&self) -> &[Column<S>]

The columns added so far.

PHP: $table->columns.

Source

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.

Source

pub fn foreign_keys(&self) -> &[ForeignKey]

The foreign keys declared so far.

PHP: $table->foreignKeys.

Source

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.

Source

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().

Source

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().

Source

pub fn text(&mut self, name: impl Into<String>) -> &mut Column<S>

Adds a text column.

PHP: text().

Source

pub fn medium_text(&mut self, name: impl Into<String>) -> &mut Column<S>

Adds a medium text column.

PHP: mediumText().

Source

pub fn long_text(&mut self, name: impl Into<String>) -> &mut Column<S>

Adds a long text column.

PHP: longText().

Source

pub fn tiny_integer(&mut self, name: impl Into<String>) -> &mut Column<S>

Adds an 8-bit integer column.

PHP: tinyInteger().

Source

pub fn small_integer(&mut self, name: impl Into<String>) -> &mut Column<S>

Adds a 16-bit integer column.

PHP: smallInteger().

Source

pub fn integer(&mut self, name: impl Into<String>) -> &mut Column<S>

Adds a 32-bit integer column.

PHP: integer().

Source

pub fn big_integer(&mut self, name: impl Into<String>) -> &mut Column<S>

Adds a 64-bit integer column.

PHP: bigInteger().

Source

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().

Source

pub fn uuid(&mut self, name: impl Into<String>) -> &mut Column<S>

Adds a UUID column (native where the dialect has one).

PHP: uuid().

Source

pub fn float(&mut self, name: impl Into<String>) -> &mut Column<S>

Adds a double-precision float column.

PHP: float().

Source

pub fn boolean(&mut self, name: impl Into<String>) -> &mut Column<S>

Adds a boolean column.

PHP: boolean().

Source

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().

Source

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().

Source

pub fn json(&mut self, name: impl Into<String>) -> &mut Column<S>

Adds a JSON column.

PHP: json().

Source

pub fn binary(&mut self, name: impl Into<String>) -> &mut Column<S>

Adds a binary column.

PHP: binary().

Source

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().

Source

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().

Source

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().

Source

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().

Source

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().

Source

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().

Source

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().

Source

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).

Source

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().

Source

pub fn add_index(&mut self, index: Index) -> Result<&mut Self>

Adds index, after Index::validated.

PHP: addIndex() (and the options of index() / uniqueIndex()).

Source

pub fn drop_index(&mut self, name: impl Into<String>) -> &mut Self

Drops the index name (in an ALTER).

PHP: dropIndex().

Source

pub fn raw_column(&mut self, definition: impl Into<String>) -> &mut Self

Adds a raw column definition, emitted verbatim.

PHP: rawColumn().

Source

pub fn raw_index(&mut self, definition: impl Into<String>) -> &mut Self

Adds a raw index definition, emitted verbatim.

PHP: rawIndex().

Source§

impl<S: Schema> Table<S>

Source

pub fn create(&self) -> Result<Statement>

CREATE TABLE.

PHP: create().

Source

pub fn create_if_not_exists(&self) -> Result<Statement>

CREATE TABLE IF NOT EXISTS.

PHP: createIfNotExists() / create(true).

Source

pub fn alter(&self) -> Result<Statement>

ALTER TABLE with the columns, renames, drops, indexes and keys collected.

PHP: alter().

Source

pub fn drop(&self) -> Result<Statement>

DROP TABLE.

PHP: drop().

Source

pub fn drop_if_exists(&self) -> Result<Statement>

DROP TABLE IF EXISTS.

PHP: dropIfExists().

Source

pub fn truncate(&self) -> Result<Statement>

Empties the table.

PHP: truncate().

Source

pub fn rename(&self, to: &str) -> Result<Statement>

Renames the table to to.

PHP: rename().

Source§

impl<S: Serial> Table<S>

Source

pub fn serial(&mut self, name: impl Into<String>) -> &mut Column<S>

Adds an auto-incrementing integer column.

PHP: serial().

Source

pub fn big_serial(&mut self, name: impl Into<String>) -> &mut Column<S>

Adds an auto-incrementing big integer column.

PHP: bigSerial().

Source

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: Checks> Table<S>

Source

pub fn check( &mut self, name: impl Into<String>, expression: impl Into<String>, ) -> Result<&mut Self>

Adds a named CHECK constraint (name is an identifier).

PHP: check() with two arguments.

Source§

impl<S: ColumnAlterations> Table<S>

Source

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().

Source

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>

Source

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>

Source

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>

Source

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().

Source

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>

Source

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().

Source

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>

Source

pub fn partition_by_range(&mut self, expression: impl Into<String>) -> &mut Self

Partitions by range of expression.

PHP: partitionByRange().

Source

pub fn partition_by_list(&mut self, expression: impl Into<String>) -> &mut Self

Partitions by list of expression.

PHP: partitionByList().

Source

pub fn partition_by_hash( &mut self, expression: impl Into<String>, partitions: Option<i64>, ) -> Result<&mut Self>

Partitions by hash of expression, into partitions (at least 1) when given.

PHP: partitionByHash().

Source§

impl Table<PostgreSql>

Source

pub fn vector( &mut self, name: impl Into<String>, dimensions: i64, ) -> &mut Column<PostgreSql>

Adds a VECTOR(dimensions) column (pgvector).

PHP: Table\PostgreSQL::vector().

Source§

impl Table<MongoDb>

Source

pub fn vector(&mut self, name: impl Into<String>) -> &mut Column<MongoDb>

Adds a vector (array) field.

PHP: Table\MongoDB::vector().

Source§

impl Table<ClickHouse>

Source

pub fn vector(&mut self, name: impl Into<String>) -> &mut Column<ClickHouse>

Adds an Array(Float64) vector column.

PHP: Table\ClickHouse::vector().

Source

pub fn array( &mut self, name: impl Into<String>, element: ColumnType, ) -> &mut Column<ClickHouse>

Adds an Array(element) column.

PHP: array().

Source

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().

Source

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().

Source

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().

Source

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().

Source

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().

Source

pub fn ttl(&mut self, expression: &str) -> Result<&mut Self>

Sets the table TTL (trimmed, not empty, no ;).

PHP: Table\ClickHouse::ttl().

Source

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().

Source

pub fn partition_by(&mut self, expression: impl Into<String>) -> &mut Self

Sets the PARTITION BY expression.

PHP: partitionBy().

Source

pub fn sample_by(&mut self, expression: &str) -> Result<&mut Self>

Sets the SAMPLE BY expression (trimmed, not empty, no ;).

PHP: sampleBy().

Trait Implementations§

Source§

impl<S: Clone> Clone for Table<S>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<S> Debug for Table<S>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<S> Forget for Table<S>

§

impl<S> Freeze for Table<S>

§

impl<S> Move for Table<S>

§

impl<S> RefUnwindSafe for Table<S>

§

impl<S> Send for Table<S>
where S: Send, RawVecInner: Send, &'static [u8]: Send, Bytes: Send, Arc<Repr<Object>>: Send, BoxRaw<Query>: Send,

§

impl<S> Sync for Table<S>
where S: Sync, RawVecInner: Sync, Bytes: Sync, Arc<Repr<Object>>: Sync, BoxRaw<Query>: Sync,

§

impl<S> Unpin for Table<S>
where S: Unpin, *const u8 is !null: Unpin, &'static [u8]: Unpin, *const u8: Unpin, Align8<*mut ()>: Unpin, &'static Vtable: Unpin, Arc<Repr<Object>>: Unpin, Box<Query>: Unpin,

§

impl<S> UnsafeUnpin for Table<S>

§

impl<S> UnwindSafe for Table<S>
where S: UnwindSafe, RawVecInner: UnwindSafe, &'static [u8]: UnwindSafe, *const u8: UnwindSafe, Align8<*mut ()>: UnwindSafe, &'static Vtable: UnwindSafe, Arc<Repr<Object>>: UnwindSafe, BoxRaw<Query>: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.