| 20 | |
| 21 | impl ProxyDb { |
| 22 | async fn do_query(env: Arc<Env>, statement: Statement) -> Result<Vec<ProxyRow>> { |
| 23 | let sql = statement.sql.clone(); |
| 24 | let values = match statement.values { |
| 25 | Some(Values(values)) => values |
| 26 | .iter() |
| 27 | .map(|val| match &val { |
| 28 | Value::BigInt(Some(val)) => JsValue::from(val.to_string()), |
| 29 | Value::BigUnsigned(Some(val)) => JsValue::from(val.to_string()), |
| 30 | Value::Int(Some(val)) => JsValue::from(*val), |
| 31 | Value::Unsigned(Some(val)) => JsValue::from(*val), |
| 32 | Value::SmallInt(Some(val)) => JsValue::from(*val), |
| 33 | Value::SmallUnsigned(Some(val)) => JsValue::from(*val), |
| 34 | Value::TinyInt(Some(val)) => JsValue::from(*val), |
| 35 | Value::TinyUnsigned(Some(val)) => JsValue::from(*val), |
| 36 | |
| 37 | Value::Float(Some(val)) => JsValue::from_f64(*val as f64), |
| 38 | Value::Double(Some(val)) => JsValue::from_f64(*val), |
| 39 | |
| 40 | Value::Bool(Some(val)) => JsValue::from(*val), |
| 41 | Value::Bytes(Some(val)) => JsValue::from(format!( |
| 42 | "X'{}'", |
| 43 | val.iter() |
| 44 | .map(|byte| format!("{:02x}", byte)) |
| 45 | .collect::<String>() |
| 46 | )), |
| 47 | Value::Char(Some(val)) => JsValue::from(val.to_string()), |
| 48 | Value::Json(Some(val)) => JsValue::from(val.to_string()), |
| 49 | Value::String(Some(val)) => JsValue::from(val.to_string()), |
| 50 | |
| 51 | Value::ChronoDate(Some(val)) => JsValue::from(val.to_string()), |
| 52 | Value::ChronoDateTime(Some(val)) => JsValue::from(val.to_string()), |
| 53 | Value::ChronoDateTimeLocal(Some(val)) => JsValue::from(val.to_string()), |
| 54 | Value::ChronoDateTimeUtc(Some(val)) => JsValue::from(val.to_string()), |
| 55 | Value::ChronoDateTimeWithTimeZone(Some(val)) => JsValue::from(val.to_string()), |
| 56 | |
| 57 | _ => JsValue::NULL, |
| 58 | }) |
| 59 | .collect(), |
| 60 | None => Vec::new(), |
| 61 | }; |
| 62 | |
| 63 | console_log!("SQL query values: {:?}", values); |
| 64 | let ret = env.d1("test-d1")?.prepare(sql).bind(&values)?.all().await?; |
| 65 | if let Some(message) = ret.error() { |
| 66 | return Err(anyhow!(message.to_string())); |
| 67 | } |
| 68 | |
| 69 | let ret = ret.results::<serde_json::Value>()?; |
| 70 | let ret = ret |
| 71 | .iter() |
| 72 | .map(|row| { |
| 73 | let mut values = BTreeMap::new(); |
| 74 | for (key, value) in row.as_object().unwrap() { |
| 75 | values.insert( |
| 76 | key.clone(), |
| 77 | match &value { |
| 78 | serde_json::Value::Bool(val) => Value::Bool(Some(*val)), |
| 79 | serde_json::Value::Number(val) => { |