Skip to content

Commit

Permalink
added database connection execute batch support
Browse files Browse the repository at this point in the history
  • Loading branch information
Horusiath committed Nov 12, 2023
1 parent 19d0f19 commit 08d8070
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
6 changes: 3 additions & 3 deletions libsql-server/tests/hrana/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,17 @@ fn execute_batch() {
r#"
begin;
create table t(x text);
insert into t(x) values('hello');
insert into t(x) values('hello; world');
end;"#,
)
.await?;
let mut rows = conn
.query("select * from t where x = ?", params!["hello"])
.query("select * from t where x = ?", params!["hello; world"])
.await?;

assert_eq!(rows.column_count(), 1);
assert_eq!(rows.column_name(0), Some("x"));
assert_eq!(rows.next()?.unwrap().get::<String>(0)?, "hello");
assert_eq!(rows.next()?.unwrap().get::<String>(0)?, "hello; world");
assert!(rows.next()?.is_none());

Ok(())
Expand Down
2 changes: 2 additions & 0 deletions libsql/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ hrana = [
"serde",
"dep:base64",
"dep:serde_json",
"dep:sqlite3-parser",
"dep:fallible-iterator"
]
serde = ["dep:serde"]
remote = [
Expand Down
2 changes: 2 additions & 0 deletions libsql/src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(dead_code)]

use crate::{Error, Result};
use fallible_iterator::FallibleIterator;
use sqlite3_parser::ast::{Cmd, PragmaBody, QualifiedName, Stmt, TransactionType};
Expand Down
14 changes: 14 additions & 0 deletions libsql/src/wasm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,20 @@ where
Ok(rows as u64)
}

pub async fn execute_batch(&self, sql: &str) -> crate::Result<()> {
let mut statements = Vec::new();
let stmts = crate::parser::Statement::parse(sql);
for s in stmts {
let s = s?;
statements.push(crate::hrana::proto::Stmt::new(s.stmt, false));
}
self.conn
.raw_batch(statements)
.await
.map_err(|e| crate::Error::Hrana(e.into()))?;
Ok(())
}

pub async fn query(&self, sql: &str, params: impl IntoParams) -> crate::Result<Rows> {
tracing::trace!("querying `{}`", sql);
let mut stmt = crate::hrana::Statement::new(self.conn.clone(), sql.to_string(), true);
Expand Down

0 comments on commit 08d8070

Please sign in to comment.