We derive the SQLite schema from the AlgebraicType of the table.
(
&mut self,
index_strategy: crate::schemas::IndexStrategy,
)
| 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};"))?; |