bottle_orm/model.rs
1use std::collections::HashMap;
2
3/// Metadata information about a database column.
4///
5/// This structure is used internally to generate table schemas and map Rust types to SQL types.
6/// It is usually populated automatically by the `#[derive(Model)]` macro.
7pub struct ColumnInfo {
8 /// The column name in the database.
9 pub name: &'static str,
10 /// The SQL type of the column (e.g., "TEXT", "INTEGER", "TIMESTAMPTZ").
11 pub sql_type: &'static str,
12 /// Whether this column is a Primary Key.
13 pub is_primary_key: bool,
14 /// Whether this column allows NULL values.
15 pub is_nullable: bool,
16 /// Whether this column should be automatically populated with the creation timestamp.
17 pub create_time: bool,
18 /// Whether this column should be automatically updated on modification (feature in progress).
19 pub update_time: bool,
20 /// Whether this column has a UNIQUE constraint.
21 pub unique: bool,
22 /// Whether an index should be created for this column.
23 pub index: bool,
24 /// The name of the foreign table, if this is a Foreign Key.
25 pub foreign_table: Option<&'static str>,
26 /// The name of the foreign column, if this is a Foreign Key.
27 pub foreign_key: Option<&'static str>,
28}
29
30/// The core trait defining a Database Model (Table) in Bottle ORM.
31///
32/// This trait is typically implemented automatically via the `#[derive(Model)]` macro.
33///
34/// # Example
35///
36/// ```rust,ignore
37/// use bottle_orm::Model;
38///
39/// #[derive(Model)]
40/// struct User {
41/// #[orm(primary_key)]
42/// id: i32,
43/// username: String,
44/// }
45/// ```
46pub trait Model {
47 /// Returns the table name associated with this model.
48 /// usually converted from CamelCase struct name to snake_case.
49 fn table_name() -> &'static str;
50
51 /// Returns the list of column definitions for this model.
52 fn columns() -> Vec<ColumnInfo>;
53
54 /// Returns the names of active columns (struct fields).
55 fn active_columns() -> Vec<&'static str>;
56
57 /// Converts the model instance into a value map (Column Name -> String Value).
58 /// Used primarily for INSERT operations.
59 fn to_map(&self) -> HashMap<String, String>;
60}