Skip to main content

table

Macro table 

Source
macro_rules! table {
    ($schema:literal.$table:literal {
        $($col_name:literal: $col_type:expr, $nullability:ident),* $(,)?
    }) => { ... };
    ($table:literal {
        $($col_name:literal: $col_type:expr, $nullability:ident),* $(,)?
    }) => { ... };
    (@add_column $table_def:expr, $col_name:literal, $col_type:expr, NULLABLE) => { ... };
    (@add_column $table_def:expr, $col_name:literal, $col_type:expr, NOT_NULL) => { ... };
}
Expand description

Macro for creating table definitions with a fluent syntax.

This macro simplifies the common pattern of creating table definitions with multiple columns by providing a more compact syntax.

§Syntax

table! {
    "table_name" {
        "column_name": SqlType::type_name(), NULLABLE | NOT_NULL,
        // ... more columns
    }
}

§Example

let orders = table! {
    "Orders" {
        "Address ID": SqlType::small_int(), NOT_NULL,
        "Customer ID": SqlType::text(), NOT_NULL,
        "Order Date": SqlType::date(), NOT_NULL,
        "Order ID": SqlType::text(), NOT_NULL,
        "Ship Date": SqlType::date(), NULLABLE,
        "Ship Mode": SqlType::text(), NULLABLE,
    }
};

// Equivalent to:
let orders_manual = TableDefinition::new("Orders")
    .add_required_column("Address ID", SqlType::small_int())
    .add_required_column("Customer ID", SqlType::text())
    .add_required_column("Order Date", SqlType::date())
    .add_required_column("Order ID", SqlType::text())
    .add_nullable_column("Ship Date", SqlType::date())
    .add_nullable_column("Ship Mode", SqlType::text());