1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
//! Stataments mod
use std::collections::HashMap;
use std::sync::{Arc, RwLock};
use cdk_common::database::Error;
use once_cell::sync::Lazy;
use crate::database::DatabaseExecutor;
use crate::value::Value;
/// The Column type
pub type Column = Value;
/// Expected response type for a given SQL statement
#[derive(Debug, Clone, Copy, Default)]
pub enum ExpectedSqlResponse {
/// A single row
SingleRow,
/// All the rows that matches a query
#[default]
ManyRows,
/// How many rows were affected by the query
AffectedRows,
/// Return the first column of the first row
Pluck,
/// Batch
Batch,
}
/// Part value
#[derive(Debug, Clone)]
pub enum PlaceholderValue {
/// Value
Value(Value),
/// Set
Set(Vec<Value>),
}
impl From<Value> for PlaceholderValue {
fn from(value: Value) -> Self {
PlaceholderValue::Value(value)
}
}
impl From<Vec<Value>> for PlaceholderValue {
fn from(value: Vec<Value>) -> Self {
PlaceholderValue::Set(value)
}
}
/// SQL Part
#[derive(Debug, Clone)]
pub enum SqlPart {
/// Raw SQL statement
Raw(Arc<str>),
/// Placeholder
Placeholder(Arc<str>, Option<PlaceholderValue>),
}
/// SQL parser error
#[derive(Debug, PartialEq, thiserror::Error)]
pub enum SqlParseError {
/// Invalid SQL
#[error("Unterminated String literal")]
UnterminatedStringLiteral,
/// Invalid placeholder name
#[error("Invalid placeholder name")]
InvalidPlaceholder,
}
/// Rudimentary SQL parser.
///
/// This function does not validate the SQL statement, it only extracts the placeholder to be
/// database agnostic.
pub fn split_sql_parts(input: &str) -> Result<Vec<SqlPart>, SqlParseError> {
let mut parts = Vec::new();
let mut current = String::new();
let mut chars = input.chars().peekable();
while let Some(&c) = chars.peek() {
match c {
'\'' | '"' => {
// Start of string literal
let quote = c;
current.push(
chars
.next()
.ok_or(SqlParseError::UnterminatedStringLiteral)?,
);
let mut closed = false;
while let Some(&next) = chars.peek() {
current.push(
chars
.next()
.ok_or(SqlParseError::UnterminatedStringLiteral)?,
);
if next == quote {
if chars.peek() == Some("e) {
// Escaped quote (e.g. '' inside strings)
current.push(
chars
.next()
.ok_or(SqlParseError::UnterminatedStringLiteral)?,
);
} else {
closed = true;
break;
}
}
}
if !closed {
return Err(SqlParseError::UnterminatedStringLiteral);
}
}
'-' => {
current.push(
chars
.next()
.ok_or(SqlParseError::UnterminatedStringLiteral)?,
);
if chars.peek() == Some(&'-') {
while let Some(&next) = chars.peek() {
current.push(
chars
.next()
.ok_or(SqlParseError::UnterminatedStringLiteral)?,
);
if next == '\n' {
break;
}
}
}
}
':' => {
// Flush current raw SQL
if !current.is_empty() {
parts.push(SqlPart::Raw(current.clone().into()));
current.clear();
}
chars.next(); // consume ':'
let mut name = String::new();
while let Some(&next) = chars.peek() {
if next.is_alphanumeric() || next == '_' {
name.push(
chars
.next()
.ok_or(SqlParseError::UnterminatedStringLiteral)?,
);
} else {
break;
}
}
if name.is_empty() {
return Err(SqlParseError::InvalidPlaceholder);
}
parts.push(SqlPart::Placeholder(name.into(), None));
}
_ => {
current.push(
chars
.next()
.ok_or(SqlParseError::UnterminatedStringLiteral)?,
);
}
}
}
if !current.is_empty() {
parts.push(SqlPart::Raw(current.into()));
}
Ok(parts)
}
type Cache = HashMap<String, (Vec<SqlPart>, Option<Arc<str>>)>;
/// Sql message
#[derive(Debug, Default)]
pub struct Statement {
cache: Arc<RwLock<Cache>>,
cached_sql: Option<Arc<str>>,
sql: Option<String>,
/// The SQL statement
pub parts: Vec<SqlPart>,
/// The expected response type
pub expected_response: ExpectedSqlResponse,
}
impl Statement {
/// Creates a new statement
fn new(sql: &str, cache: Arc<RwLock<Cache>>) -> Result<Self, SqlParseError> {
let parsed = cache
.read()
.map(|cache| cache.get(sql).cloned())
.ok()
.flatten();
if let Some((parts, cached_sql)) = parsed {
Ok(Self {
parts,
cached_sql,
sql: None,
cache,
..Default::default()
})
} else {
let parts = split_sql_parts(sql)?;
if let Ok(mut cache) = cache.write() {
cache.insert(sql.to_owned(), (parts.clone(), None));
} else {
tracing::warn!("Failed to acquire write lock for SQL statement cache");
}
Ok(Self {
parts,
sql: Some(sql.to_owned()),
cache,
..Default::default()
})
}
}
/// Convert Statement into a SQL statement and the list of placeholders
///
/// By default it converts the statement into placeholder using $1..$n placeholders which seems
/// to be more widely supported, although it can be reimplemented with other formats since part
/// is public
pub fn to_sql(self) -> Result<(String, Vec<Value>), Error> {
if let Some(cached_sql) = self.cached_sql {
let sql = cached_sql.to_string();
let values = self
.parts
.into_iter()
.map(|x| match x {
SqlPart::Placeholder(name, value) => {
match value.ok_or(Error::MissingPlaceholder(name.to_string()))? {
PlaceholderValue::Value(value) => Ok(vec![value]),
PlaceholderValue::Set(values) => Ok(values),
}
}
SqlPart::Raw(_) => Ok(vec![]),
})
.collect::<Result<Vec<_>, Error>>()?
.into_iter()
.flatten()
.collect::<Vec<_>>();
return Ok((sql, values));
}
let mut placeholder_values = Vec::new();
let mut can_be_cached = true;
let sql = self
.parts
.into_iter()
.map(|x| match x {
SqlPart::Placeholder(name, value) => {
match value.ok_or(Error::MissingPlaceholder(name.to_string()))? {
PlaceholderValue::Value(value) => {
placeholder_values.push(value);
Ok::<_, Error>(format!("${}", placeholder_values.len()))
}
PlaceholderValue::Set(mut values) => {
can_be_cached = false;
let start_size = placeholder_values.len();
placeholder_values.append(&mut values);
let placeholders = (start_size + 1..=placeholder_values.len())
.map(|i| format!("${i}"))
.collect::<Vec<_>>()
.join(", ");
Ok(placeholders)
}
}
}
SqlPart::Raw(raw) => Ok(raw.trim().to_string()),
})
.collect::<Result<Vec<String>, _>>()?
.join(" ");
if can_be_cached {
if let Some(original_sql) = self.sql {
let _ = self.cache.write().map(|mut cache| {
if let Some((_, cached_sql)) = cache.get_mut(&original_sql) {
*cached_sql = Some(sql.clone().into());
}
});
}
}
Ok((sql, placeholder_values))
}
/// Binds a given placeholder to a value.
#[inline]
pub fn bind<C, V>(mut self, name: C, value: V) -> Self
where
C: ToString,
V: Into<Value>,
{
let name = name.to_string();
let value = value.into();
let value: PlaceholderValue = value.into();
for part in self.parts.iter_mut() {
if let SqlPart::Placeholder(part_name, part_value) = part {
if **part_name == *name.as_str() {
*part_value = Some(value.clone());
}
}
}
self
}
/// Binds a single variable with a vector.
///
/// This will rewrite the function from `:foo` (where value is vec![1, 2, 3]) to `:foo0, :foo1,
/// :foo2` and binds each value from the value vector accordingly.
#[inline]
pub fn bind_vec<C, V>(mut self, name: C, value: Vec<V>) -> Self
where
C: ToString,
V: Into<Value>,
{
let name = name.to_string();
let value: PlaceholderValue = value
.into_iter()
.map(|x| x.into())
.collect::<Vec<Value>>()
.into();
for part in self.parts.iter_mut() {
if let SqlPart::Placeholder(part_name, part_value) = part {
if **part_name == *name.as_str() {
*part_value = Some(value.clone());
}
}
}
self
}
/// Executes a query and returns the affected rows
pub async fn pluck<C>(self, conn: &C) -> Result<Option<Value>, Error>
where
C: DatabaseExecutor,
{
conn.pluck(self).await
}
/// Executes a query and returns the affected rows
pub async fn batch<C>(self, conn: &C) -> Result<(), Error>
where
C: DatabaseExecutor,
{
conn.batch(self).await
}
/// Executes a query and returns the affected rows
pub async fn execute<C>(self, conn: &C) -> Result<usize, Error>
where
C: DatabaseExecutor,
{
conn.execute(self).await
}
/// Runs the query and returns the first row or None
pub async fn fetch_one<C>(self, conn: &C) -> Result<Option<Vec<Column>>, Error>
where
C: DatabaseExecutor,
{
conn.fetch_one(self).await
}
/// Runs the query and returns the first row or None
pub async fn fetch_all<C>(self, conn: &C) -> Result<Vec<Vec<Column>>, Error>
where
C: DatabaseExecutor,
{
conn.fetch_all(self).await
}
}
/// Creates a new query statement
#[inline(always)]
pub fn query(sql: &str) -> Result<Statement, Error> {
static CACHE: Lazy<Arc<RwLock<Cache>>> = Lazy::new(|| Arc::new(RwLock::new(HashMap::new())));
Statement::new(sql, CACHE.clone()).map_err(|e| Error::Database(Box::new(e)))
}