Skip to content

Commit

Permalink
refactor: Bump gluesql to 0.15
Browse files Browse the repository at this point in the history
  • Loading branch information
langyo committed Nov 24, 2023
1 parent de66032 commit c2b988a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 31 deletions.
5 changes: 1 addition & 4 deletions examples/proxy_gluesql_example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ sea-orm = { path = "../../", features = [
"runtime-async-std-native-tls",
"debug-print",
] }
# Since it's newer version (0.14.0) locked the chrono's version to 0.4.23,
# we need to lock it on older version too.
# Related to https://github.com/gluesql/gluesql/pull/1427
gluesql = { version = "0.13", default-features = false, features = [
gluesql = { version = "0.15", default-features = false, features = [
"memory-storage",
] }

Expand Down
60 changes: 33 additions & 27 deletions examples/proxy_gluesql_example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,37 +33,39 @@ impl ProxyDatabaseTrait for ProxyDb {
let sql = statement.sql.clone();

let mut ret: Vec<ProxyRow> = vec![];
for payload in self.mem.lock().unwrap().execute(sql).unwrap().iter() {
match payload {
gluesql::prelude::Payload::Select { labels, rows } => {
for row in rows.iter() {
let mut map = BTreeMap::new();
for (label, column) in labels.iter().zip(row.iter()) {
map.insert(
label.to_owned(),
match column {
gluesql::prelude::Value::I64(val) => {
sea_orm::Value::BigInt(Some(*val))
}
gluesql::prelude::Value::Str(val) => {
sea_orm::Value::String(Some(Box::new(val.to_owned())))
}
_ => unreachable!("Unsupported value: {:?}", column),
},
);
async_std::task::block_on(async {
for payload in self.mem.lock().unwrap().execute(sql).await.unwrap().iter() {
match payload {
gluesql::prelude::Payload::Select { labels, rows } => {
for row in rows.iter() {
let mut map = BTreeMap::new();
for (label, column) in labels.iter().zip(row.iter()) {
map.insert(
label.to_owned(),
match column {
gluesql::prelude::Value::I64(val) => {
sea_orm::Value::BigInt(Some(*val))
}
gluesql::prelude::Value::Str(val) => {
sea_orm::Value::String(Some(Box::new(val.to_owned())))
}
_ => unreachable!("Unsupported value: {:?}", column),
},
);
}
ret.push(map.into());
}
ret.push(map.into());
}
_ => unreachable!("Unsupported payload: {:?}", payload),
}
_ => unreachable!("Unsupported payload: {:?}", payload),
}
}
});

Ok(ret)
}

fn execute(&self, statement: Statement) -> Result<ProxyExecResult, DbErr> {
if let Some(values) = statement.values {
let sql = if let Some(values) = statement.values {
// Replace all the '?' with the statement values
let mut new_sql = statement.sql.clone();
let mark_count = new_sql.matches('?').count();
Expand All @@ -73,12 +75,15 @@ impl ProxyDatabaseTrait for ProxyDb {
}
new_sql = new_sql.replacen('?', &v.to_string(), 1);
}
println!("SQL execute: {}", new_sql);

self.mem.lock().unwrap().execute(new_sql).unwrap();
new_sql
} else {
self.mem.lock().unwrap().execute(statement.sql).unwrap();
}
statement.sql
};

println!("SQL execute: {}", sql);
async_std::task::block_on(async {
self.mem.lock().unwrap().execute(sql).await.unwrap();
});

Ok(ProxyExecResult {
last_insert_id: 1,
Expand All @@ -101,6 +106,7 @@ async fn main() {
)
"#,
)
.await
.unwrap();

let db = Database::connect_proxy(
Expand Down

0 comments on commit c2b988a

Please sign in to comment.