pub struct TableSql<'a> {
pub table: &'a Table,
pub columns: &'a [Column],
pub primary_key: Option<&'a PrimaryKey>,
pub foreign_keys: &'a [ForeignKey],
pub unique_constraints: &'a [UniqueConstraint],
pub check_constraints: &'a [CheckConstraint],
pub indexes: &'a [Index],
pub policies: &'a [Policy],
}Expand description
A table and the entities that belong to it, for rendering
CREATE TABLE and its related statements.
Start with TableSql::new and add the rest with the builder methods.
§Examples
use drizzle_types::postgres::ddl::{ColumnDef, TableDef, TableSql};
let table = TableDef::new("public", "users").into_table();
let columns = [
ColumnDef::new("public", "users", "id", "integer").not_null().into_column(),
ColumnDef::new("public", "users", "name", "text").into_column(),
];
let sql = TableSql::new(&table).columns(&columns).create_table_sql();
assert_eq!(sql, "CREATE TABLE \"users\" (\n\t\"id\" integer NOT NULL,\n\t\"name\" text\n);");Fields§
§table: &'a TableThe table.
columns: &'a [Column]Its columns, in order.
primary_key: Option<&'a PrimaryKey>Its primary key, if any.
foreign_keys: &'a [ForeignKey]Its foreign keys.
unique_constraints: &'a [UniqueConstraint]Its unique constraints.
check_constraints: &'a [CheckConstraint]Its check constraints.
indexes: &'a [Index]Its indexes, rendered by create_indexes_sql.
policies: &'a [Policy]Its policies, rendered by create_policies_sql.
Implementations§
Source§impl<'a> TableSql<'a>
impl<'a> TableSql<'a>
Sourcepub const fn new(table: &'a Table) -> Self
pub const fn new(table: &'a Table) -> Self
Starts with just the table: no columns, constraints, indexes or policies.
Sourcepub const fn primary_key(self, pk: Option<&'a PrimaryKey>) -> Self
pub const fn primary_key(self, pk: Option<&'a PrimaryKey>) -> Self
Sets the primary key.
Sourcepub const fn foreign_keys(self, fks: &'a [ForeignKey]) -> Self
pub const fn foreign_keys(self, fks: &'a [ForeignKey]) -> Self
Sets the foreign keys.
Sourcepub const fn unique_constraints(self, uniques: &'a [UniqueConstraint]) -> Self
pub const fn unique_constraints(self, uniques: &'a [UniqueConstraint]) -> Self
Sets the unique constraints.
Sourcepub const fn check_constraints(self, checks: &'a [CheckConstraint]) -> Self
pub const fn check_constraints(self, checks: &'a [CheckConstraint]) -> Self
Sets the check constraints.
Sourcepub fn create_table_sql(&self) -> String
pub fn create_table_sql(&self) -> String
Renders the CREATE TABLE statement, ending in ;.
Columns come first, then the primary key, foreign keys, unique and
check constraints as table constraints. TEMPORARY or UNLOGGED,
INHERITS and TABLESPACE come from the table. Indexes, comments,
row-level security and policies are separate statements; see the
other methods.
Sourcepub fn drop_table_sql(&self) -> String
pub fn drop_table_sql(&self) -> String
Renders DROP TABLE for the table.
Sourcepub fn create_indexes_sql(&self) -> Vec<String>
pub fn create_indexes_sql(&self) -> Vec<String>
Renders CREATE INDEX for each of the table’s indexes.
Sourcepub fn create_comments_sql(&self) -> Vec<String>
pub fn create_comments_sql(&self) -> Vec<String>
Renders COMMENT ON TABLE / COMMENT ON COLUMN for each comment set
on the table and its columns.
Sourcepub fn enable_rls_sql(&self) -> Option<String>
pub fn enable_rls_sql(&self) -> Option<String>
Renders ALTER TABLE ... ENABLE ROW LEVEL SECURITY;, or None when
the table does not enable it.
Sourcepub fn create_policies_sql(&self) -> Vec<String>
pub fn create_policies_sql(&self) -> Vec<String>
Renders CREATE POLICY for each of the table’s policies.