Struct spacetimedb_sats::db::def::TableSchema
source · pub struct TableSchema {
pub table_id: TableId,
pub table_name: String,
pub indexes: Vec<IndexSchema>,
pub constraints: Vec<ConstraintSchema>,
pub sequences: Vec<SequenceSchema>,
pub table_type: StTableType,
pub table_access: StAccess,
/* private fields */
}
Expand description
A data structure representing the schema of a database table.
This struct holds information about the table, including its identifier, name, columns, indexes, constraints, sequences, type, and access rights.
Fields§
§table_id: TableId
§table_name: String
§indexes: Vec<IndexSchema>
§constraints: Vec<ConstraintSchema>
§sequences: Vec<SequenceSchema>
§table_type: StTableType
§table_access: StAccess
Implementations§
source§impl TableSchema
impl TableSchema
pub fn new( table_id: TableId, table_name: String, columns: Vec<ColumnSchema>, indexes: Vec<IndexSchema>, constraints: Vec<ConstraintSchema>, sequences: Vec<SequenceSchema>, table_type: StTableType, table_access: StAccess ) -> Self
sourcepub fn columns(&self) -> &[ColumnSchema]
pub fn columns(&self) -> &[ColumnSchema]
IMPORTANT: Ban changes from outside so Self::row_type won’t get invalidated.
sourcepub fn clear_adjacent_schemas(&mut self)
pub fn clear_adjacent_schemas(&mut self)
Clear all the Self::indexes, Self::sequences & Self::constraints
sourcepub fn update_sequence(&mut self, of: SequenceSchema)
pub fn update_sequence(&mut self, of: SequenceSchema)
Add OR replace the SequenceSchema
sourcepub fn remove_sequence(&mut self, sequence_id: SequenceId)
pub fn remove_sequence(&mut self, sequence_id: SequenceId)
Removes the given sequence_id
sourcepub fn update_index(&mut self, of: IndexSchema)
pub fn update_index(&mut self, of: IndexSchema)
Add OR replace the IndexSchema
sourcepub fn remove_index(&mut self, index_id: IndexId)
pub fn remove_index(&mut self, index_id: IndexId)
Removes the given index_id
sourcepub fn update_constraint(&mut self, of: ConstraintSchema)
pub fn update_constraint(&mut self, of: ConstraintSchema)
Add OR replace the ConstraintSchema
sourcepub fn remove_constraint(&mut self, constraint_id: ConstraintId)
pub fn remove_constraint(&mut self, constraint_id: ConstraintId)
Removes the given index_id
sourcepub fn get_column_by_field(&self, field: &FieldName) -> Option<&ColumnSchema>
pub fn get_column_by_field(&self, field: &FieldName) -> Option<&ColumnSchema>
Check if the specified field
exists in this TableSchema.
This function can handle both named and positional fields.
§Warning
This function ignores the table_name
when searching for a column.
pub fn get_columns( &self, columns: &ColList ) -> Vec<(ColId, Option<&ColumnSchema>)>
sourcepub fn get_column(&self, pos: usize) -> Option<&ColumnSchema>
pub fn get_column(&self, pos: usize) -> Option<&ColumnSchema>
Get a reference to a column by its position (pos
) in the table.
sourcepub fn get_column_by_name(&self, col_name: &str) -> Option<&ColumnSchema>
pub fn get_column_by_name(&self, col_name: &str) -> Option<&ColumnSchema>
Check if the col_name
exist on this TableSchema
Warning: It ignores the table_name
sourcepub fn get_index_by_field(&self, field: &FieldName) -> Option<&IndexSchema>
pub fn get_index_by_field(&self, field: &FieldName) -> Option<&IndexSchema>
Check if there is an index for this FieldName
Warning: It ignores the table_name
sourcepub fn normalize_field(&self, or_use: &TableField<'_>) -> FieldName
pub fn normalize_field(&self, or_use: &TableField<'_>) -> FieldName
Turn a TableField that could be an unqualified field id
into table.id
sourcepub fn project(
&self,
indexes: impl Iterator<Item = ColId>
) -> Result<Vec<&ColumnSchema>, InvalidFieldError>
pub fn project( &self, indexes: impl Iterator<Item = ColId> ) -> Result<Vec<&ColumnSchema>, InvalidFieldError>
Project the fields from the supplied indexes
.
sourcepub fn project_not_empty(
&self,
indexes: ColList
) -> Result<Vec<&ColumnSchema>, InvalidFieldError>
pub fn project_not_empty( &self, indexes: ColList ) -> Result<Vec<&ColumnSchema>, InvalidFieldError>
Utility for project the fields from the supplied indexes
that is a ColList,
used for when the list of field indexes have at least one value.
sourcepub fn get_row_type(&self) -> &ProductType
pub fn get_row_type(&self) -> &ProductType
IMPORTANT: Is required to have this cached to avoid a perf drop on datastore operations
sourcepub fn into_row_type(self) -> ProductType
pub fn into_row_type(self) -> ProductType
Utility to avoid cloning in row_type_for_table
pub fn get_constraints(&self) -> Vec<(ColList, Constraints)>
sourcepub fn from_def(table_id: TableId, schema: TableDef) -> Self
pub fn from_def(table_id: TableId, schema: TableDef) -> Self
Create a new TableSchema from a TableDef and a table_id
.
§Parameters
table_id
: The unique identifier for the table.schema
: TheTableDef
containing the schema information.
pub fn column_constraints_iter( &self ) -> impl Iterator<Item = (ColList, &Constraints)>
sourcepub fn column_constraints(&self) -> HashMap<ColList, Constraints>
pub fn column_constraints(&self) -> HashMap<ColList, Constraints>
Resolves the constraints per each column. If the column don’t have one, auto-generate Constraints::unset().
This guarantee all columns can be queried for it constraints.
sourcepub fn pk(&self) -> Option<&ColumnSchema>
pub fn pk(&self) -> Option<&ColumnSchema>
Find the pk
column. Because we run Self::validated, only exist one pk
.
sourcepub fn validated(self) -> Result<Self, Vec<SchemaError>>
pub fn validated(self) -> Result<Self, Vec<SchemaError>>
Verify the definitions of this schema are valid:
- Check all names are not empty
- All columns exists
- Only 1 PK
- Only 1 sequence per column
- Only Btree Indexes
Trait Implementations§
source§impl Clone for TableSchema
impl Clone for TableSchema
source§fn clone(&self) -> TableSchema
fn clone(&self) -> TableSchema
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for TableSchema
impl Debug for TableSchema
source§impl From<&TableSchema> for DbTable
impl From<&TableSchema> for DbTable
source§fn from(value: &TableSchema) -> Self
fn from(value: &TableSchema) -> Self
source§impl From<&TableSchema> for Header
impl From<&TableSchema> for Header
source§fn from(value: &TableSchema) -> Self
fn from(value: &TableSchema) -> Self
source§impl From<&TableSchema> for ProductType
impl From<&TableSchema> for ProductType
source§fn from(value: &TableSchema) -> Self
fn from(value: &TableSchema) -> Self
source§impl From<TableSchema> for TableDef
impl From<TableSchema> for TableDef
source§fn from(value: TableSchema) -> Self
fn from(value: TableSchema) -> Self
source§impl PartialEq for TableSchema
impl PartialEq for TableSchema
source§fn eq(&self, other: &TableSchema) -> bool
fn eq(&self, other: &TableSchema) -> bool
self
and other
values to be equal, and is used
by ==
.