1use sqlx::FromRow;
7
8use crate::Value;
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
12pub enum Driver {
13 #[cfg(feature = "mysql")]
15 MySql,
16 #[cfg(feature = "postgres")]
18 Postgres,
19 #[cfg(feature = "sqlite")]
21 Sqlite,
22}
23
24impl Driver {
25 pub fn name(self) -> &'static str {
27 match self {
28 #[cfg(feature = "mysql")]
29 Self::MySql => "MySQL",
30 #[cfg(feature = "postgres")]
31 Self::Postgres => "PostgreSQL",
32 #[cfg(feature = "sqlite")]
33 Self::Sqlite => "SQLite",
34 }
35 }
36
37 pub(crate) fn quote(self, name: &str) -> String {
40 match self {
41 #[cfg(feature = "mysql")]
42 Self::MySql => format!("`{name}`"),
43 #[cfg(any(feature = "postgres", feature = "sqlite"))]
44 _ => format!("\"{name}\""),
45 }
46 }
47
48 #[cfg_attr(not(feature = "postgres"), allow(unused_variables))]
51 pub(crate) fn placeholder(self, n: usize) -> String {
52 match self {
53 #[cfg(feature = "mysql")]
54 Self::MySql => "?".to_owned(),
55 #[cfg(feature = "postgres")]
56 Self::Postgres => format!("${n}"),
57 #[cfg(feature = "sqlite")]
58 Self::Sqlite => "?".to_owned(),
59 }
60 }
61
62 pub(crate) fn lock_clause(self) -> &'static str {
66 match self {
67 #[cfg(feature = "mysql")]
68 Self::MySql => " FOR UPDATE",
69 #[cfg(feature = "postgres")]
70 Self::Postgres => " FOR UPDATE",
71 #[cfg(feature = "sqlite")]
72 Self::Sqlite => "",
73 }
74 }
75
76 pub(crate) fn offset_only_limit(self) -> Option<&'static str> {
79 match self {
80 #[cfg(feature = "mysql")]
81 Self::MySql => Some("18446744073709551615"),
82 #[cfg(feature = "postgres")]
83 Self::Postgres => None,
84 #[cfg(feature = "sqlite")]
85 Self::Sqlite => Some("-1"),
86 }
87 }
88
89 pub(crate) fn empty_insert(self) -> &'static str {
91 match self {
92 #[cfg(feature = "mysql")]
93 Self::MySql => "() VALUES ()",
94 #[cfg(any(feature = "postgres", feature = "sqlite"))]
95 _ => "DEFAULT VALUES",
96 }
97 }
98
99 pub(crate) fn bigint(self) -> &'static str {
101 match self {
102 #[cfg(feature = "mysql")]
103 Self::MySql => "SIGNED",
104 #[cfg(feature = "postgres")]
105 Self::Postgres => "BIGINT",
106 #[cfg(feature = "sqlite")]
107 Self::Sqlite => "INTEGER",
108 }
109 }
110
111 pub(crate) fn like(self, column: &str) -> String {
115 match self {
116 #[cfg(feature = "mysql")]
117 Self::MySql => format!("{column} LIKE"),
118 #[cfg(feature = "postgres")]
119 Self::Postgres => format!("{column}::text ILIKE"),
120 #[cfg(feature = "sqlite")]
121 Self::Sqlite => format!("{column} LIKE"),
122 }
123 }
124
125 pub(crate) fn supports_replace(self) -> bool {
128 match self {
129 #[cfg(feature = "mysql")]
130 Self::MySql => true,
131 #[cfg(feature = "postgres")]
132 Self::Postgres => false,
133 #[cfg(feature = "sqlite")]
134 Self::Sqlite => true,
135 }
136 }
137
138 pub(crate) fn inlines_null(self) -> bool {
141 match self {
142 #[cfg(feature = "mysql")]
143 Self::MySql => false,
144 #[cfg(feature = "postgres")]
145 Self::Postgres => true,
146 #[cfg(feature = "sqlite")]
147 Self::Sqlite => false,
148 }
149 }
150
151 pub(crate) fn insert_returns_id(self) -> bool {
153 match self {
154 #[cfg(feature = "mysql")]
155 Self::MySql => false,
156 #[cfg(feature = "postgres")]
157 Self::Postgres => true,
158 #[cfg(feature = "sqlite")]
159 Self::Sqlite => false,
160 }
161 }
162
163 pub(crate) fn prepare(self, sql: &str, binds: &[Value]) -> (String, Vec<Value>) {
167 match self {
168 #[cfg(feature = "mysql")]
169 Self::MySql => (sql.to_owned(), binds.to_vec()),
170 #[cfg(feature = "postgres")]
171 Self::Postgres => prepare_postgres(sql, binds),
172 #[cfg(feature = "sqlite")]
173 Self::Sqlite => (sql.to_owned(), binds.to_vec()),
174 }
175 }
176}
177
178#[cfg(feature = "postgres")]
181fn prepare_postgres(sql: &str, binds: &[Value]) -> (String, Vec<Value>) {
182 let mut out = String::with_capacity(sql.len() + 8);
183 let mut kept = Vec::new();
184 let mut seen = 0usize;
185 let mut chars = sql.chars().peekable();
186 let mut quote: Option<char> = None;
187 while let Some(c) = chars.next() {
188 match quote {
189 Some(q) => {
190 out.push(c);
191 if c == q {
192 quote = None;
193 }
194 }
195 None => match c {
196 '\'' | '"' => {
197 quote = Some(c);
198 out.push(c);
199 }
200 '-' if chars.peek() == Some(&'-') => {
201 out.push(c);
202 for next in chars.by_ref() {
203 out.push(next);
204 if next == '\n' {
205 break;
206 }
207 }
208 }
209 '?' => {
210 let value = binds.get(seen).cloned().unwrap_or(Value::Null);
211 seen += 1;
212 if matches!(value, Value::Null) {
213 out.push_str("NULL");
214 } else {
215 kept.push(value);
216 out.push_str(&format!("${}", kept.len()));
217 }
218 }
219 _ => out.push(c),
220 },
221 }
222 }
223 (out, kept)
224}
225
226#[doc(hidden)]
230pub mod rows {
231 #[cfg(feature = "mysql")]
232 pub trait RowMySql: for<'r> super::FromRow<'r, sqlx::mysql::MySqlRow> {}
233 #[cfg(feature = "mysql")]
234 impl<T: for<'r> super::FromRow<'r, sqlx::mysql::MySqlRow>> RowMySql for T {}
235 #[cfg(not(feature = "mysql"))]
236 pub trait RowMySql {}
237 #[cfg(not(feature = "mysql"))]
238 impl<T> RowMySql for T {}
239
240 #[cfg(feature = "postgres")]
241 pub trait RowPostgres: for<'r> super::FromRow<'r, sqlx::postgres::PgRow> {}
242 #[cfg(feature = "postgres")]
243 impl<T: for<'r> super::FromRow<'r, sqlx::postgres::PgRow>> RowPostgres for T {}
244 #[cfg(not(feature = "postgres"))]
245 pub trait RowPostgres {}
246 #[cfg(not(feature = "postgres"))]
247 impl<T> RowPostgres for T {}
248
249 #[cfg(feature = "sqlite")]
250 pub trait RowSqlite: for<'r> super::FromRow<'r, sqlx::sqlite::SqliteRow> {}
251 #[cfg(feature = "sqlite")]
252 impl<T: for<'r> super::FromRow<'r, sqlx::sqlite::SqliteRow>> RowSqlite for T {}
253 #[cfg(not(feature = "sqlite"))]
254 pub trait RowSqlite {}
255 #[cfg(not(feature = "sqlite"))]
256 impl<T> RowSqlite for T {}
257
258 pub trait DriverRows: RowMySql + RowPostgres + RowSqlite {}
260 impl<T: RowMySql + RowPostgres + RowSqlite> DriverRows for T {}
261}
262
263#[cfg(not(any(feature = "mysql", feature = "postgres", feature = "sqlite")))]
264compile_error!("orbit-orm needs at least one database feature: mysql, postgres or sqlite");