pub struct DynamicModel { /* private fields */ }Expand description
A dynamically-defined model for tables whose schema is determined at runtime.
Unlike compile-time Model structs, DynamicModel stores column definitions
and values in hash maps, trading type safety for flexibility.
§Example
use sqlmodel_core::dynamic::{DynamicModel, ColumnDef};
use sqlmodel_core::types::SqlType;
use sqlmodel_core::value::Value;
let mut model = DynamicModel::new("users");
model.add_column(ColumnDef::new("id", SqlType::BigInt).primary_key().auto_increment());
model.add_column(ColumnDef::new("name", SqlType::Text));
model.add_column(ColumnDef::new("email", SqlType::Text));
model.set("name", Value::Text("Alice".to_string()));
model.set("email", Value::Text("alice@example.com".to_string()));
assert_eq!(model.get("name").unwrap().as_str(), Some("Alice"));Implementations§
Source§impl DynamicModel
impl DynamicModel
Sourcepub fn new(table_name: impl Into<String>) -> Self
pub fn new(table_name: impl Into<String>) -> Self
Create a new dynamic model for the given table.
Sourcepub fn add_column(&mut self, column: ColumnDef)
pub fn add_column(&mut self, column: ColumnDef)
Add a column definition.
Sourcepub fn table_name(&self) -> &str
pub fn table_name(&self) -> &str
Get the table name.
Sourcepub fn primary_key_columns(&self) -> Vec<&str>
pub fn primary_key_columns(&self) -> Vec<&str>
Get primary key column names.
Sourcepub fn to_insert_pairs(&self) -> Vec<(&str, &Value)>
pub fn to_insert_pairs(&self) -> Vec<(&str, &Value)>
Get all column-value pairs for non-null, non-auto-increment columns.
Suitable for building INSERT statements.
Sourcepub fn primary_key_values(&self) -> Vec<Value>
pub fn primary_key_values(&self) -> Vec<Value>
Get primary key values.
Trait Implementations§
Source§impl Clone for DynamicModel
impl Clone for DynamicModel
Source§fn clone(&self) -> DynamicModel
fn clone(&self) -> DynamicModel
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for DynamicModel
impl RefUnwindSafe for DynamicModel
impl Send for DynamicModel
impl Sync for DynamicModel
impl Unpin for DynamicModel
impl UnwindSafe for DynamicModel
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
Mutably borrows from an owned value. Read more
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: NoopSpan) -> Self
fn instrument(self, _span: NoopSpan) -> Self
Instruments this future with a span (no-op when disabled).
Source§fn in_current_span(self) -> Self
fn in_current_span(self) -> Self
Instruments this future with the current span (no-op when disabled).