rust_ef_sqlite/
sql_generator.rs1use rust_ef::provider::ISqlGenerator;
2
3#[derive(Debug, Clone)]
4pub struct SqliteSqlGenerator;
5
6impl SqliteSqlGenerator {
7 pub fn new() -> Self {
8 Self
9 }
10}
11
12impl Default for SqliteSqlGenerator {
13 fn default() -> Self {
14 Self::new()
15 }
16}
17
18impl ISqlGenerator for SqliteSqlGenerator {
19 fn select(&self, table: &str, columns: &[&str]) -> String {
20 let cols = if columns.is_empty() {
21 "*".to_string()
22 } else {
23 columns
24 .iter()
25 .map(|c| self.quote_identifier(c))
26 .collect::<Vec<_>>()
27 .join(", ")
28 };
29 format!("SELECT {} FROM {}", cols, self.quote_identifier(table))
30 }
31
32 fn insert(&self, table: &str, columns: &[&str], _returning: bool) -> String {
33 let cols = columns
34 .iter()
35 .map(|c| self.quote_identifier(c))
36 .collect::<Vec<_>>()
37 .join(", ");
38 let placeholders = vec!["?"; columns.len()].join(", ");
39 format!(
40 "INSERT INTO {} ({}) VALUES ({})",
41 self.quote_identifier(table),
42 cols,
43 placeholders
44 )
45 }
46
47 fn insert_batch(&self, table: &str, columns: &[&str], row_count: usize) -> String {
48 let cols = columns
49 .iter()
50 .map(|c| self.quote_identifier(c))
51 .collect::<Vec<_>>()
52 .join(", ");
53 let row = format!("({})", vec!["?"; columns.len()].join(", "));
54 let all_rows = vec![row; row_count].join(", ");
55 format!(
56 "INSERT INTO {} ({}) VALUES {}",
57 self.quote_identifier(table),
58 cols,
59 all_rows,
60 )
61 }
62
63 fn update(&self, table: &str, set_columns: &[&str], where_clause: &str) -> String {
64 let sets: Vec<String> = set_columns
65 .iter()
66 .map(|c| format!("{} = ?", self.quote_identifier(c)))
67 .collect();
68 format!(
69 "UPDATE {} SET {} WHERE {}",
70 self.quote_identifier(table),
71 sets.join(", "),
72 where_clause
73 )
74 }
75
76 fn delete(&self, table: &str, where_clause: &str) -> String {
77 format!(
78 "DELETE FROM {} WHERE {}",
79 self.quote_identifier(table),
80 where_clause
81 )
82 }
83
84 fn create_table(&self, table: &str, columns: &[(String, String)]) -> String {
85 let col_defs: Vec<String> = columns
86 .iter()
87 .map(|(name, type_def)| format!("{} {}", self.quote_identifier(name), type_def))
88 .collect();
89 format!(
90 "CREATE TABLE {} (\n {}\n)",
91 self.quote_identifier(table),
92 col_defs.join(",\n ")
93 )
94 }
95
96 fn drop_table(&self, table: &str) -> String {
97 format!("DROP TABLE IF EXISTS {}", self.quote_identifier(table))
98 }
99
100 fn pagination(&self, skip: Option<usize>, take: Option<usize>) -> String {
101 match (skip, take) {
102 (Some(s), Some(t)) => format!("LIMIT {} OFFSET {}", t, s),
103 (None, Some(t)) => format!("LIMIT {}", t),
104 _ => String::new(),
105 }
106 }
107
108 fn parameter_placeholder(&self, _index: usize) -> String {
109 "?".to_string()
110 }
111
112 fn quote_identifier(&self, identifier: &str) -> String {
113 format!("\"{}\"", identifier)
114 }
115
116 fn auto_increment_syntax(&self) -> &'static str {
117 "AUTOINCREMENT"
118 }
119}