journio_sqlite/
dialect.rs1use journio_core::dialect::{Dialect, DialectName};
2
3#[derive(Debug, Default, Clone, Copy)]
4pub struct SqliteDialect;
5
6impl Dialect for SqliteDialect {
7 fn name(&self) -> DialectName {
8 DialectName::Sqlite
9 }
10
11 fn schema_prefix(&self, _schema: &str) -> String {
12 String::new()
13 }
14
15 fn rewrite_query(&self, query: &str) -> String {
16 rewrite_postgres_placeholders(query)
17 }
18
19 fn lock_skip_locked(&self) -> &str {
20 ""
21 }
22
23 fn lock_nowait(&self) -> &str {
24 ""
25 }
26
27 fn supports_listen_notify(&self) -> bool {
28 false
29 }
30
31 fn supports_array_parameters(&self) -> bool {
32 false
33 }
34
35 fn supports_data_modifying_cte(&self) -> bool {
36 true
37 }
38}
39
40fn rewrite_postgres_placeholders(query: &str) -> String {
41 let mut out = String::with_capacity(query.len());
42 let bytes = query.as_bytes();
43 let mut i = 0;
44 while i < bytes.len() {
45 if bytes[i] == b'$' {
46 let start = i + 1;
47 let mut end = start;
48 while end < bytes.len() && bytes[end].is_ascii_digit() {
49 end += 1;
50 }
51 if end > start {
52 out.push('?');
53 out.push_str(&query[start..end]);
54 i = end;
55 continue;
56 }
57 }
58 out.push(bytes[i] as char);
59 i += 1;
60 }
61 out
62}
63
64#[cfg(test)]
65mod tests {
66 use super::rewrite_postgres_placeholders;
67
68 #[test]
69 fn rewrites_numbered_placeholders() {
70 let query = "UPDATE t SET a = $1, b = $2 WHERE id = $3 AND a = CAST($1 AS TEXT)";
71 assert_eq!(
72 rewrite_postgres_placeholders(query),
73 "UPDATE t SET a = ?1, b = ?2 WHERE id = ?3 AND a = CAST(?1 AS TEXT)"
74 );
75 }
76}