MCPcopy Create free account
hub / github.com/clockworklabs/SpacetimeDB / create_table

Method create_table

crates/bench/src/sqlite.rs:54–93  ·  view source on GitHub ↗

We derive the SQLite schema from the AlgebraicType of the table.

(
        &mut self,
        index_strategy: crate::schemas::IndexStrategy,
    )

Source from the content-addressed store, hash-verified

52
53 /// We derive the SQLite schema from the AlgebraicType of the table.
54 fn create_table<T: BenchTable>(
55 &mut self,
56 index_strategy: crate::schemas::IndexStrategy,
57 ) -> ResultBench<Self::TableId> {
58 let mut statement = String::new();
59 let table_name = table_name::<T>(index_strategy);
60 write!(&mut statement, "CREATE TABLE {table_name} (")?;
61 for (i, column) in T::product_type().elements.iter().enumerate() {
62 let column_name = column.name.clone().unwrap();
63 let type_ = match column.algebraic_type {
64 AlgebraicType::U32 | AlgebraicType::U64 => "INTEGER",
65 AlgebraicType::String => "TEXT",
66 _ => unimplemented!(),
67 };
68 let extra = if index_strategy == IndexStrategy::Unique0 && i == 0 {
69 " PRIMARY KEY"
70 } else {
71 ""
72 };
73 let comma = if i == 0 { "" } else { ", " };
74 write!(&mut statement, "{comma}{column_name} {type_}{extra}")?;
75 }
76 writeln!(&mut statement, ");")?;
77
78 if index_strategy == IndexStrategy::BTreeEachColumn {
79 for column in T::product_type().elements.iter() {
80 let column_name = column.name.clone().unwrap();
81
82 writeln!(
83 &mut statement,
84 "CREATE INDEX index_{table_name}_{column_name} ON {table_name}({column_name});"
85 )?;
86 }
87 }
88
89 log::info!("SQLITE: `{statement}`");
90 self.db.execute_batch(&statement)?;
91
92 Ok(table_name)
93 }
94
95 fn clear_table(&mut self, table_id: &Self::TableId) -> ResultBench<()> {
96 self.db.execute_batch(&format!("DELETE FROM {table_id};"))?;

Callers

nothing calls this directly

Calls 5

iterMethod · 0.65
newFunction · 0.50
OkFunction · 0.50
unwrapMethod · 0.45
cloneMethod · 0.45

Tested by

no test coverage detected