Skip to main content

dbx_core/engine/
schema_builder_api.rs

1//! Schema Builder API implementation for Database
2
3use crate::DbxResult;
4use crate::engine::Database;
5use crate::engine::schema_builder::SchemaBuilder;
6
7impl Database {
8    /// Create a table using SchemaBuilder
9    ///
10    /// This method provides a convenient way to create tables using the fluent
11    /// SchemaBuilder API without manually constructing Arrow schemas.
12    ///
13    /// # Example
14    ///
15    /// ```rust
16    /// use dbx_core::Database;
17    ///
18    /// # fn main() -> dbx_core::DbxResult<()> {
19    /// let db = Database::open_in_memory()?;
20    ///
21    /// db.create_table_with_builder("users", |builder| {
22    ///     builder
23    ///         .id("id")
24    ///         .text("name")
25    ///         .text("email")
26    ///         .int32("age").nullable()
27    /// })?;
28    ///
29    /// assert!(db.table_exists("users"));
30    /// # Ok(())
31    /// # }
32    /// ```
33    pub fn create_table_with_builder<F>(&self, name: &str, builder_fn: F) -> DbxResult<()>
34    where
35        F: FnOnce(SchemaBuilder) -> SchemaBuilder,
36    {
37        let schema = builder_fn(SchemaBuilder::new()).build();
38        self.create_table(name, schema)
39    }
40}