| 66 | } |
| 67 | |
| 68 | async fn execute(&self, statement: Statement) -> Result<ProxyExecResult, DbErr> { |
| 69 | let sql = if let Some(values) = statement.values { |
| 70 | // Replace all the '?' with the statement values |
| 71 | use sqlparser::ast::{Expr, Value}; |
| 72 | use sqlparser::dialect::GenericDialect; |
| 73 | use sqlparser::parser::Parser; |
| 74 | |
| 75 | let dialect = GenericDialect {}; |
| 76 | let mut ast = Parser::parse_sql(&dialect, statement.sql.as_str()).unwrap(); |
| 77 | match &mut ast[0] { |
| 78 | sqlparser::ast::Statement::Insert { |
| 79 | columns, source, .. |
| 80 | } => { |
| 81 | for item in columns.iter_mut() { |
| 82 | item.quote_style = Some('"'); |
| 83 | } |
| 84 | |
| 85 | if let Some(obj) = source { |
| 86 | match &mut *obj.body { |
| 87 | sqlparser::ast::SetExpr::Values(obj) => { |
| 88 | for (mut item, val) in obj.rows[0].iter_mut().zip(values.0.iter()) { |
| 89 | match &mut item { |
| 90 | Expr::Value(item) => { |
| 91 | *item = match val { |
| 92 | sea_orm::Value::String(val) => { |
| 93 | Value::SingleQuotedString(match val { |
| 94 | Some(val) => val.to_string(), |
| 95 | None => "".to_string(), |
| 96 | }) |
| 97 | } |
| 98 | sea_orm::Value::BigInt(val) => Value::Number( |
| 99 | val.unwrap_or(0).to_string(), |
| 100 | false, |
| 101 | ), |
| 102 | _ => todo!(), |
| 103 | }; |
| 104 | } |
| 105 | _ => todo!(), |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | _ => todo!(), |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | _ => todo!(), |
| 114 | } |
| 115 | |
| 116 | let statement = &ast[0]; |
| 117 | statement.to_string() |
| 118 | } else { |
| 119 | statement.sql |
| 120 | }; |
| 121 | |
| 122 | println!("SQL execute: {}", sql); |
| 123 | async_std::task::block_on(async { |
| 124 | self.mem.lock().unwrap().execute(sql).await.unwrap(); |
| 125 | }); |