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