-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathinsert.rs
More file actions
107 lines (100 loc) · 4.48 KB
/
insert.rs
File metadata and controls
107 lines (100 loc) · 4.48 KB
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
use sqlparser::ast::{Expr, Insert, SetExpr, Statement, Value, Values};
use crate::error::{Result, SQLRiteError};
/// The following structure represents a INSERT query already parsed
/// and broken down into `table_name` a `Vec<String>` representing the `Columns`
/// and `Vec<Vec<String>>` representing the list of `Rows` to be inserted
#[derive(Debug)]
pub struct InsertQuery {
pub table_name: String,
pub columns: Vec<String>,
pub rows: Vec<Vec<String>>,
}
impl InsertQuery {
pub fn new(statement: &Statement) -> Result<InsertQuery> {
let tname: Option<String>;
let mut columns: Vec<String> = vec![];
let mut all_values: Vec<Vec<String>> = vec![];
match statement {
Statement::Insert(Insert {
table,
columns: cols,
source,
..
}) => {
tname = Some(table.to_string());
for col in cols {
columns.push(col.to_string());
}
let source = source.as_ref().ok_or_else(|| {
SQLRiteError::Internal(
"INSERT statement is missing a source expression".to_string(),
)
})?;
if let SetExpr::Values(Values { rows, .. }) = source.body.as_ref() {
for row in rows {
let mut value_set: Vec<String> = vec![];
for e in row {
match e {
Expr::Value(v) => match &v.value {
Value::Number(n, _) => {
value_set.push(n.to_string());
}
Value::Boolean(b) => {
if *b {
value_set.push("true".to_string());
} else {
value_set.push("false".to_string());
}
}
Value::SingleQuotedString(sqs) => {
value_set.push(sqs.to_string());
}
Value::Null => {
value_set.push("Null".to_string());
}
_ => {}
},
Expr::Identifier(i) => {
// Phase 7a — sqlparser parses bracket-array
// literals like `[0.1, 0.2, 0.3]` as
// bracket-quoted identifiers (it inherits
// MSSQL-style `[name]` quoting). Detect
// that by `quote_style == Some('[')` and
// re-wrap with brackets so the
// `parse_vector_literal` helper at
// insert_row time can recognize and parse
// it. Regular unquoted identifiers (column
// refs, which don't make sense in INSERT
// VALUES anyway) keep the existing
// pass-through-as-string behavior.
if i.quote_style == Some('[') {
value_set.push(format!("[{}]", i.value));
} else {
value_set.push(i.to_string());
}
}
_ => {}
}
}
all_values.push(value_set);
}
}
}
_ => {
return Err(SQLRiteError::Internal(
"Error parsing insert query".to_string(),
));
}
}
match tname {
Some(t) => Ok(InsertQuery {
table_name: t,
columns,
rows: all_values,
}),
None => Err(SQLRiteError::Internal(
"Error parsing insert query".to_string(),
)),
}
}
}