Skip to main content

TableDefinition

Struct TableDefinition 

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

Table name.

§schema: Option<String>

Schema name.

§database: Option<String>

Database name.

§columns: Vec<ColumnDefinition>

Column definitions.

Implementations§

Source§

impl TableDefinition

Source

pub fn new(name: impl Into<String>) -> Self

Creates a new table definition.

Source

pub fn from_table_name<T>(table_name: T) -> Result<Self>
where T: TryInto<TableName>, Error: From<T::Error>,

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.

Source

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());
Source

pub fn with_database(self, database: impl Into<String>) -> Self

Sets the database name (fluent builder pattern).

Source

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

pub fn get_persistence(&self) -> Persistence

Returns the persistence setting.

Source

pub fn set_persistence(&mut self, persistence: Persistence)

Sets the persistence.

Source

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

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());
Source

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");
Source

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");
Source

pub fn add_column_def(self, column: ColumnDefinition) -> Self

Adds a ColumnDefinition directly (fluent builder pattern).

Source

pub fn column_count(&self) -> usize

Returns the number of columns.

Source

pub fn columns(&self) -> &[ColumnDefinition]

Returns the column definitions.

Source

pub fn column(&self, index: usize) -> &ColumnDefinition

Returns the column at the given position.

§Panics

Panics if the index is out of bounds.

Source

pub fn column_by_name(&self, name: &str) -> Option<&ColumnDefinition>

Returns the column with the given name, if it exists.

Source

pub fn column_position_by_name(&self, name: &str) -> Option<usize>

Returns the position of the column with the given name.

Source

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\"");
Source

pub fn schema_name(&self) -> Option<String>

Returns the schema name (escaped), if set.

Source

pub fn database_name(&self) -> Option<String>

Returns the database name (escaped), if set.

Source

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)

Source

pub fn set_table_name(&mut self, name: impl Into<String>)

Sets the table name.

Source

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\"");
Source

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.

Source

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

Source§

fn clone(&self) -> TableDefinition

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 Debug for TableDefinition

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for TableDefinition

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl From<&str> for TableDefinition

Source§

fn from(name: &str) -> Self

Converts to this type from the input type.
Source§

impl From<String> for TableDefinition

Source§

fn from(name: String) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

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> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<L> LayerExt<L> for L

Source§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in Layered.
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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> Allocation for T
where T: RefUnwindSafe + Send + Sync,