pub struct TableDefinition {
pub name: String,
pub schema: Option<String>,
pub database: Option<String>,
pub columns: Vec<ColumnDefinition>,
/* private fields */
}Expand description
A table definition.
This struct defines the schema of a table including its name, optional schema and database names, and column definitions.
§Example
Using the fluent builder pattern:
use hyperdb_api::{TableDefinition, Result};
use hyperdb_api_core::types::{SqlType, Nullability};
let table = TableDefinition::new("users")
.add_required_column("id", SqlType::int())
.add_nullable_column("name", SqlType::text());
let sql = table.to_create_sql(true)?;
assert!(sql.contains("CREATE TABLE"));Fields§
§name: StringTable name.
schema: Option<String>Schema name.
database: Option<String>Database name.
columns: Vec<ColumnDefinition>Column definitions.
Implementations§
Source§impl TableDefinition
impl TableDefinition
Sourcepub fn from_table_name<T>(table_name: T) -> Result<Self>
pub fn from_table_name<T>(table_name: T) -> Result<Self>
Creates a table definition from a validated TableName.
This constructor uses a pre-validated TableName, ensuring all name components
have already passed validation (non-empty, within length limits).
§Example
use hyperdb_api::{TableDefinition, TableName};
use hyperdb_api_core::types::{SqlType, Nullability};
// First create a validated TableName
let table_name = TableName::try_new("users")?
.with_schema("public")?
.with_database("mydb")?;
// Then create TableDefinition from it
let table = TableDefinition::from_table_name(table_name)?
.add_required_column("id", SqlType::int());
assert_eq!(table.name, "users");
assert_eq!(table.schema, Some("public".to_string()));
assert_eq!(table.database, Some("mydb".to_string()));
// Direct conversion from string also works
let table2 = TableDefinition::from_table_name("public.users")?;
assert_eq!(table2.schema, Some("public".to_string()));§Errors
Returns the conversion error (typically Error::InvalidName) if
table_name cannot be parsed into a
TableName.
Sourcepub fn with_schema(self, schema: impl Into<String>) -> Self
pub fn with_schema(self, schema: impl Into<String>) -> Self
Sets the schema name (fluent builder pattern).
§Example
use hyperdb_api::TableDefinition;
use hyperdb_api_core::types::{SqlType, Nullability};
let table = TableDefinition::new("Extract")
.with_schema("Extract")
.add_required_column("id", SqlType::int());Sourcepub fn with_database(self, database: impl Into<String>) -> Self
pub fn with_database(self, database: impl Into<String>) -> Self
Sets the database name (fluent builder pattern).
Sourcepub fn with_persistence(self, persistence: Persistence) -> Self
pub fn with_persistence(self, persistence: Persistence) -> Self
Sets the persistence (fluent builder pattern).
§Example
use hyperdb_api::{TableDefinition, Persistence};
use hyperdb_api_core::types::SqlType;
let temp_table = TableDefinition::new("temp_data")
.with_persistence(Persistence::Temporary)
.add_required_column("id", SqlType::int());
assert_eq!(temp_table.get_persistence(), Persistence::Temporary);Sourcepub fn get_persistence(&self) -> Persistence
pub fn get_persistence(&self) -> Persistence
Returns the persistence setting.
Sourcepub fn set_persistence(&mut self, persistence: Persistence)
pub fn set_persistence(&mut self, persistence: Persistence)
Sets the persistence.
Sourcepub fn add_nullable_column(
self,
name: impl Into<String>,
sql_type: SqlType,
) -> Self
pub fn add_nullable_column( self, name: impl Into<String>, sql_type: SqlType, ) -> Self
Adds a nullable column using SqlType (fluent builder pattern).
§Example
use hyperdb_api::TableDefinition;
use hyperdb_api_core::types::SqlType;
let table = TableDefinition::new("products")
.add_nullable_column("name", SqlType::text())
.add_nullable_column("price", SqlType::numeric(18, 2));Sourcepub fn add_required_column(
self,
name: impl Into<String>,
sql_type: SqlType,
) -> Self
pub fn add_required_column( self, name: impl Into<String>, sql_type: SqlType, ) -> Self
Adds a required (non-nullable) column using SqlType (fluent builder pattern).
§Example
use hyperdb_api::TableDefinition;
use hyperdb_api_core::types::SqlType;
let table = TableDefinition::new("products")
.add_required_column("id", SqlType::int())
.add_required_column("name", SqlType::text());Sourcepub fn add_nullable_column_with_collation(
self,
name: impl Into<String>,
sql_type: SqlType,
collation: impl Into<String>,
) -> Self
pub fn add_nullable_column_with_collation( self, name: impl Into<String>, sql_type: SqlType, collation: impl Into<String>, ) -> Self
Adds a nullable column with a collation (fluent builder pattern).
§Example
use hyperdb_api::TableDefinition;
use hyperdb_api_core::types::SqlType;
let table = TableDefinition::new("products")
.add_nullable_column_with_collation("name", SqlType::text(), "en_US");Sourcepub fn add_required_column_with_collation(
self,
name: impl Into<String>,
sql_type: SqlType,
collation: impl Into<String>,
) -> Self
pub fn add_required_column_with_collation( self, name: impl Into<String>, sql_type: SqlType, collation: impl Into<String>, ) -> Self
Adds a required (non-nullable) column with a collation (fluent builder pattern).
§Example
use hyperdb_api::TableDefinition;
use hyperdb_api_core::types::SqlType;
let table = TableDefinition::new("products")
.add_required_column_with_collation("name", SqlType::text(), "en_US");Sourcepub fn add_column_def(self, column: ColumnDefinition) -> Self
pub fn add_column_def(self, column: ColumnDefinition) -> Self
Adds a ColumnDefinition directly (fluent builder pattern).
Sourcepub fn column_count(&self) -> usize
pub fn column_count(&self) -> usize
Returns the number of columns.
Sourcepub fn columns(&self) -> &[ColumnDefinition]
pub fn columns(&self) -> &[ColumnDefinition]
Returns the column definitions.
Sourcepub fn column(&self, index: usize) -> &ColumnDefinition
pub fn column(&self, index: usize) -> &ColumnDefinition
Sourcepub fn column_by_name(&self, name: &str) -> Option<&ColumnDefinition>
pub fn column_by_name(&self, name: &str) -> Option<&ColumnDefinition>
Returns the column with the given name, if it exists.
Sourcepub fn column_position_by_name(&self, name: &str) -> Option<usize>
pub fn column_position_by_name(&self, name: &str) -> Option<usize>
Returns the position of the column with the given name.
Sourcepub fn table_name(&self) -> String
pub fn table_name(&self) -> String
Returns the table name (unqualified, escaped).
This returns just the table name portion, properly escaped for use in SQL.
§Example
use hyperdb_api::TableDefinition;
let table = TableDefinition::new("Extract").with_schema("Extract");
// "Extract" is quoted because it contains uppercase letters (to preserve case)
assert_eq!(table.table_name(), "\"Extract\"");Sourcepub fn schema_name(&self) -> Option<String>
pub fn schema_name(&self) -> Option<String>
Returns the schema name (escaped), if set.
Sourcepub fn database_name(&self) -> Option<String>
pub fn database_name(&self) -> Option<String>
Returns the database name (escaped), if set.
Sourcepub fn qualified_name(&self) -> String
pub fn qualified_name(&self) -> String
Returns the qualified table name (escaped).
Format: database.schema.table (if all parts are set, unquoted if valid identifiers)
Sourcepub fn set_table_name(&mut self, name: impl Into<String>)
pub fn set_table_name(&mut self, name: impl Into<String>)
Sets the table name.
Sourcepub fn to_table_name(&self) -> Result<TableName>
pub fn to_table_name(&self) -> Result<TableName>
Converts this TableDefinition to a validated TableName.
This method validates all name components (table, schema, database) and returns
a type-safe TableName. Use this when you need to ensure the names are valid.
§Errors
Returns an error if any name component is empty or exceeds the PostgreSQL identifier limit.
§Example
use hyperdb_api::TableDefinition;
let table = TableDefinition::new("users")
.with_schema("public")
.with_database("mydb");
// Validate all names
let table_name = table.to_table_name()?;
assert_eq!(table_name.to_string(), "\"mydb\".\"public\".\"users\"");Sourcepub fn to_create_sql(&self, fail_if_exists: bool) -> Result<String>
pub fn to_create_sql(&self, fail_if_exists: bool) -> Result<String>
Generates CREATE TABLE SQL.
§Arguments
fail_if_exists- If true, the statement will fail if the table exists. If false, uses CREATE TABLE IF NOT EXISTS.
§Example
use hyperdb_api::{TableDefinition, Result};
use hyperdb_api_core::types::{SqlType, Nullability};
let table = TableDefinition::new("users")
.add_required_column("id", SqlType::int());
let sql = table.to_create_sql(true)?;
assert_eq!(sql, r#"CREATE TABLE users (id INTEGER NOT NULL)"#);§Errors
Returns Error::Other with message
"Table must have at least one column" if this definition has no
columns.
Sourcepub fn to_drop_sql(&self, fail_if_not_exists: bool) -> String
pub fn to_drop_sql(&self, fail_if_not_exists: bool) -> String
Generates DROP TABLE SQL.
§Arguments
fail_if_not_exists- If true, the statement will fail if the table doesn’t exist. If false, uses DROP TABLE IF EXISTS.
Trait Implementations§
Source§impl Clone for TableDefinition
impl Clone for TableDefinition
Source§fn clone(&self) -> TableDefinition
fn clone(&self) -> TableDefinition
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for TableDefinition
impl Debug for TableDefinition
Source§impl Default for TableDefinition
impl Default for TableDefinition
Source§impl From<&str> for TableDefinition
impl From<&str> for TableDefinition
Auto Trait Implementations§
impl Freeze for TableDefinition
impl RefUnwindSafe for TableDefinition
impl Send for TableDefinition
impl Sync for TableDefinition
impl Unpin for TableDefinition
impl UnsafeUnpin for TableDefinition
impl UnwindSafe for TableDefinition
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request