-
-
Notifications
You must be signed in to change notification settings - Fork 523
/
orm.rs
218 lines (189 loc) Β· 8.61 KB
/
orm.rs
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
use anyhow::{anyhow, Context, Result};
use std::{collections::BTreeMap, sync::Arc};
use wasm_bindgen::JsValue;
use sea_orm::{
ConnectionTrait, Database, DatabaseConnection, DbBackend, DbErr, ProxyDatabaseTrait,
ProxyExecResult, ProxyRow, RuntimeErr, Schema, Statement, Value, Values,
};
use worker::{console_log, Env};
struct ProxyDb {
env: Arc<Env>,
}
impl std::fmt::Debug for ProxyDb {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ProxyDb").finish()
}
}
impl ProxyDb {
async fn do_query(env: Arc<Env>, statement: Statement) -> Result<Vec<ProxyRow>> {
let sql = statement.sql.clone();
let values = match statement.values {
Some(Values(values)) => values
.iter()
.map(|val| match &val {
Value::BigInt(Some(val)) => JsValue::from(val.to_string()),
Value::BigUnsigned(Some(val)) => JsValue::from(val.to_string()),
Value::Int(Some(val)) => JsValue::from(*val),
Value::Unsigned(Some(val)) => JsValue::from(*val),
Value::SmallInt(Some(val)) => JsValue::from(*val),
Value::SmallUnsigned(Some(val)) => JsValue::from(*val),
Value::TinyInt(Some(val)) => JsValue::from(*val),
Value::TinyUnsigned(Some(val)) => JsValue::from(*val),
Value::Float(Some(val)) => JsValue::from_f64(*val as f64),
Value::Double(Some(val)) => JsValue::from_f64(*val),
Value::Bool(Some(val)) => JsValue::from(*val),
Value::Bytes(Some(val)) => JsValue::from(format!(
"X'{}'",
val.iter()
.map(|byte| format!("{:02x}", byte))
.collect::<String>()
)),
Value::Char(Some(val)) => JsValue::from(val.to_string()),
Value::Json(Some(val)) => JsValue::from(val.to_string()),
Value::String(Some(val)) => JsValue::from(val.to_string()),
Value::ChronoDate(Some(val)) => JsValue::from(val.to_string()),
Value::ChronoDateTime(Some(val)) => JsValue::from(val.to_string()),
Value::ChronoDateTimeLocal(Some(val)) => JsValue::from(val.to_string()),
Value::ChronoDateTimeUtc(Some(val)) => JsValue::from(val.to_string()),
Value::ChronoDateTimeWithTimeZone(Some(val)) => JsValue::from(val.to_string()),
_ => JsValue::NULL,
})
.collect(),
None => Vec::new(),
};
console_log!("SQL query values: {:?}", values);
let ret = env.d1("test-d1")?.prepare(sql).bind(&values)?.all().await?;
if let Some(message) = ret.error() {
return Err(anyhow!(message.to_string()));
}
let ret = ret.results::<serde_json::Value>()?;
let ret = ret
.iter()
.map(|row| {
let mut values = BTreeMap::new();
for (key, value) in row.as_object().unwrap() {
values.insert(
key.clone(),
match &value {
serde_json::Value::Bool(val) => Value::Bool(Some(*val)),
serde_json::Value::Number(val) => {
if val.is_i64() {
Value::BigInt(Some(val.as_i64().unwrap()))
} else if val.is_u64() {
Value::BigUnsigned(Some(val.as_u64().unwrap()))
} else {
Value::Double(Some(val.as_f64().unwrap()))
}
}
serde_json::Value::String(val) => {
Value::String(Some(Box::new(val.clone())))
}
_ => unreachable!("Unsupported JSON value"),
},
);
}
ProxyRow { values }
})
.collect();
console_log!("SQL query result: {:?}", ret);
Ok(ret)
}
async fn do_execute(env: Arc<Env>, statement: Statement) -> Result<ProxyExecResult> {
let sql = statement.sql.clone();
let values = match statement.values {
Some(Values(values)) => values
.iter()
.map(|val| match &val {
Value::BigInt(Some(val)) => JsValue::from(val.to_string()),
Value::BigUnsigned(Some(val)) => JsValue::from(val.to_string()),
Value::Int(Some(val)) => JsValue::from(*val),
Value::Unsigned(Some(val)) => JsValue::from(*val),
Value::SmallInt(Some(val)) => JsValue::from(*val),
Value::SmallUnsigned(Some(val)) => JsValue::from(*val),
Value::TinyInt(Some(val)) => JsValue::from(*val),
Value::TinyUnsigned(Some(val)) => JsValue::from(*val),
Value::Float(Some(val)) => JsValue::from_f64(*val as f64),
Value::Double(Some(val)) => JsValue::from_f64(*val),
Value::Bool(Some(val)) => JsValue::from(*val),
Value::Bytes(Some(val)) => JsValue::from(format!(
"X'{}'",
val.iter()
.map(|byte| format!("{:02x}", byte))
.collect::<String>()
)),
Value::Char(Some(val)) => JsValue::from(val.to_string()),
Value::Json(Some(val)) => JsValue::from(val.to_string()),
Value::String(Some(val)) => JsValue::from(val.to_string()),
Value::ChronoDate(Some(val)) => JsValue::from(val.to_string()),
Value::ChronoDateTime(Some(val)) => JsValue::from(val.to_string()),
Value::ChronoDateTimeLocal(Some(val)) => JsValue::from(val.to_string()),
Value::ChronoDateTimeUtc(Some(val)) => JsValue::from(val.to_string()),
Value::ChronoDateTimeWithTimeZone(Some(val)) => JsValue::from(val.to_string()),
_ => JsValue::NULL,
})
.collect(),
None => Vec::new(),
};
let ret = env
.d1("test-d1")?
.prepare(sql)
.bind(&values)?
.run()
.await?
.meta()?;
console_log!("SQL execute result: {:?}", ret);
let last_insert_id = ret
.as_ref()
.map(|meta| meta.last_row_id.unwrap_or(0))
.unwrap_or(0) as u64;
let rows_affected = ret
.as_ref()
.map(|meta| meta.rows_written.unwrap_or(0))
.unwrap_or(0) as u64;
Ok(ProxyExecResult {
last_insert_id,
rows_affected,
})
}
}
#[async_trait::async_trait]
impl ProxyDatabaseTrait for ProxyDb {
async fn query(&self, statement: Statement) -> Result<Vec<ProxyRow>, DbErr> {
console_log!("SQL query: {:?}", statement);
let env = self.env.clone();
let (tx, rx) = oneshot::channel();
wasm_bindgen_futures::spawn_local(async move {
let ret = Self::do_query(env, statement).await;
tx.send(ret).unwrap();
});
let ret = rx.await.unwrap();
ret.map_err(|err| DbErr::Conn(RuntimeErr::Internal(err.to_string())))
}
async fn execute(&self, statement: Statement) -> Result<ProxyExecResult, DbErr> {
console_log!("SQL execute: {:?}", statement);
let env = self.env.clone();
let (tx, rx) = oneshot::channel();
wasm_bindgen_futures::spawn_local(async move {
let ret = Self::do_execute(env, statement).await;
tx.send(ret).unwrap();
});
let ret = rx.await.unwrap();
ret.map_err(|err| DbErr::Conn(RuntimeErr::Internal(err.to_string())))
}
}
pub async fn init_db(env: Arc<Env>) -> Result<DatabaseConnection> {
let db = Database::connect_proxy(DbBackend::Sqlite, Arc::new(Box::new(ProxyDb { env })))
.await
.context("Failed to connect to database")?;
let builder = db.get_database_backend();
console_log!("Connected to database");
db.execute(
builder.build(
Schema::new(builder)
.create_table_from_entity(crate::entity::Entity)
.if_not_exists(),
),
)
.await?;
Ok(db)
}