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
use std::collections::HashMap;
use std::iter::IntoIterator;
use base64::Engine;
use worker::*;
#[derive(Clone, Debug, Default)]
pub struct Meta {
pub duration: u64,
}
#[derive(Clone, Debug)]
pub enum CellValue {
Text(String),
Float(f64),
Number(i64),
Bool(bool),
}
#[derive(Clone, Debug)]
pub struct Row {
pub cells: HashMap<String, Option<CellValue>>,
}
#[derive(Clone, Debug)]
pub struct Rows {
pub columns: Vec<String>,
pub rows: Vec<Row>,
}
#[derive(Clone, Debug)]
pub enum ResultSet {
Error((String, Meta)),
Success((Rows, Meta)),
}
#[derive(Clone, Debug)]
pub struct Session {
url: String,
auth: String,
}
fn parse_columns(columns: Vec<serde_json::Value>, result_idx: usize) -> Result<Vec<String>> {
let mut result = Vec::with_capacity(columns.len());
for (idx, column) in columns.into_iter().enumerate() {
match column {
serde_json::Value::String(column) => result.push(column),
_ => {
return Err(worker::Error::from(format!(
"Result {} column name {} not a string",
result_idx, idx
)))
}
}
}
Ok(result)
}
fn parse_value(
cell: serde_json::Value,
result_idx: usize,
row_idx: usize,
cell_idx: usize,
) -> Result<Option<CellValue>> {
match cell {
serde_json::Value::Null => Ok(None),
serde_json::Value::Bool(v) => Ok(Some(CellValue::Bool(v))),
serde_json::Value::Number(v) => match v.as_i64() {
Some(v) => Ok(Some(CellValue::Number(v))),
None => match v.as_f64() {
Some(v) => Ok(Some(CellValue::Float(v))),
None => Err(worker::Error::from(format!(
"Result {} row {} cell {} had unknown number value: {}",
result_idx, row_idx, cell_idx, v
))),
},
},
serde_json::Value::String(v) => Ok(Some(CellValue::Text(v))),
_ => Err(worker::Error::from(format!(
"Result {} row {} cell {} had unknown type",
result_idx, row_idx, cell_idx
))),
}
}
fn parse_rows(
rows: Vec<serde_json::Value>,
columns: &Vec<String>,
result_idx: usize,
) -> Result<Vec<Row>> {
let mut result = Vec::with_capacity(rows.len());
for (idx, row) in rows.into_iter().enumerate() {
match row {
serde_json::Value::Array(row) => {
if row.len() != columns.len() {
return Err(worker::Error::from(format!(
"Result {} row {} had wrong number of cells",
result_idx, idx
)));
}
let mut cells = HashMap::with_capacity(columns.len());
for (cell_idx, value) in row.into_iter().enumerate() {
cells.insert(
columns[cell_idx].clone(),
parse_value(value, result_idx, idx, cell_idx)?,
);
}
result.push(Row { cells })
}
_ => {
return Err(worker::Error::from(format!(
"Result {} row {} was not an array",
result_idx, idx
)))
}
}
}
Ok(result)
}
fn parse_result_set(result: serde_json::Value, idx: usize) -> Result<ResultSet> {
match result {
serde_json::Value::Object(obj) => {
if let Some(err) = obj.get("error") {
return match err {
serde_json::Value::Object(obj) => match obj.get("message") {
Some(serde_json::Value::String(msg)) => {
Ok(ResultSet::Error((msg.clone(), Meta::default())))
}
_ => Err(worker::Error::from(format!(
"Result {} error message was not a string",
idx
))),
},
_ => Err(worker::Error::from(format!(
"Result {} results was not an object",
idx
))),
};
}
let results = obj.get("results");
match results {
Some(serde_json::Value::Object(obj)) => {
let columns = obj.get("columns").ok_or_else(|| {
worker::Error::from(format!("Result {} had no columns", idx))
})?;
let rows = obj.get("rows").ok_or_else(|| {
worker::Error::from(format!("Result {} had no rows", idx))
})?;
match (rows, columns) {
(serde_json::Value::Array(rows), serde_json::Value::Array(columns)) => {
let columns = parse_columns(columns.to_vec(), idx)?;
let rows = parse_rows(rows.to_vec(), &columns, idx)?;
Ok(ResultSet::Success((
Rows { columns, rows },
Meta::default(),
)))
}
_ => Err(worker::Error::from(format!(
"Result {} had rows or columns that were not an array",
idx
))),
}
}
Some(_) => Err(worker::Error::from(format!(
"Result {} was not an object",
idx
))),
None => Err(worker::Error::from(format!(
"Result {} did not contain results or error",
idx
))),
}
}
_ => Err(worker::Error::from(format!(
"Result {} was not an object",
idx
))),
}
}
impl Session {
pub fn connect(url: impl Into<String>, username: &str, pass: &str) -> Self {
Self {
url: url.into(),
auth: format!(
"Basic {}",
base64::engine::general_purpose::STANDARD.encode(format!("{}:{}", username, pass))
),
}
}
pub async fn execute(&self, stmt: impl Into<String>) -> Result<ResultSet> {
let mut results = self.batch(std::iter::once(stmt)).await?;
Ok(results.remove(0))
}
pub async fn batch(
&self,
stmts: impl IntoIterator<Item = impl Into<String>>,
) -> Result<Vec<ResultSet>> {
let mut headers = Headers::new();
headers.append("Authorization", &self.auth).ok();
let stmts: Vec<String> = stmts
.into_iter()
.map(|s| format!("\"{}\"", s.into()))
.collect();
let request_init = RequestInit {
body: Some(wasm_bindgen::JsValue::from_str(&format!(
"{{\"statements\": [{}]}}",
stmts.join(",")
))),
headers,
cf: CfProperties::new(),
method: Method::Post,
redirect: RequestRedirect::Follow,
};
let req = Request::new_with_init(&self.url, &request_init)?;
let response = Fetch::Request(req).send().await;
let resp: String = response?.text().await?;
let response_json: serde_json::Value = serde_json::from_str(&resp)?;
match response_json {
serde_json::Value::Array(results) => {
if results.len() != stmts.len() {
Err(worker::Error::from(format!(
"Response array did not contain expected {} results",
stmts.len()
)))
} else {
let mut result_sets: Vec<ResultSet> = Vec::with_capacity(stmts.len());
for (idx, result) in results.into_iter().enumerate() {
result_sets.push(parse_result_set(result, idx)?);
}
Ok(result_sets)
}
}
e => Err(worker::Error::from(format!(
"Error: {} ({:?})",
e, request_init.body
))),
}
}
pub async fn transaction(
&self,
stmts: impl IntoIterator<Item = impl Into<String>>,
) -> Result<Vec<ResultSet>> {
self.batch(
std::iter::once("BEGIN".to_string())
.chain(stmts.into_iter().map(|s| s.into()))
.chain(std::iter::once("END".to_string())),
)
.await
}
}