proper implementation of executemany(); drop executescript()

This commit is contained in:
Damien Elmes 2020-03-06 10:05:58 +10:00
parent 14546c8a8b
commit e14c5e4745
3 changed files with 34 additions and 11 deletions

View File

@ -100,12 +100,11 @@ class DBProxy:
# Updates
################
def executemany(self, sql: str, args: Iterable[Iterable[ValueForDB]]) -> None:
def executemany(self, sql: str, args: Iterable[Sequence[ValueForDB]]) -> None:
self.mod = True
# fixme
for row in args:
self.execute(sql, *row)
def executescript(self, sql: str) -> None:
self.mod = True
raise Exception("fixme")
assert ":" not in sql
if isinstance(args, list):
list_args = args
else:
list_args = list(args)
self._backend.db_execute_many(sql, list_args)

View File

@ -386,9 +386,12 @@ class RustBackend:
def restore_trash(self):
self._run_command(pb.BackendInput(restore_trash=pb.Empty()))
def db_query(self, sql: str, args: Iterable[ValueForDB]) -> List[DBRow]:
def db_query(self, sql: str, args: List[ValueForDB]) -> List[DBRow]:
return self._db_command(dict(kind="query", sql=sql, args=args))
def db_execute_many(self, sql: str, args: List[List[ValueForDB]]) -> List[DBRow]:
return self._db_command(dict(kind="executemany", sql=sql, args=args))
def db_begin(self) -> None:
return self._db_command(dict(kind="begin"))

View File

@ -9,10 +9,17 @@ use serde_derive::{Deserialize, Serialize};
#[derive(Deserialize)]
#[serde(tag = "kind", rename_all = "lowercase")]
pub(super) enum DBRequest {
Query { sql: String, args: Vec<SqlValue> },
Query {
sql: String,
args: Vec<SqlValue>,
},
Begin,
Commit,
Rollback,
ExecuteMany {
sql: String,
args: Vec<Vec<SqlValue>>,
},
}
#[derive(Serialize)]
@ -74,13 +81,13 @@ pub(super) fn db_command_bytes(ctx: &StorageContext, input: &[u8]) -> Result<Str
ctx.rollback_trx()?;
DBResult::None
}
DBRequest::ExecuteMany { sql, args } => db_execute_many(ctx, &sql, &args)?,
};
Ok(serde_json::to_string(&resp)?)
}
pub(super) fn db_query(ctx: &StorageContext, sql: &str, args: &[SqlValue]) -> Result<DBResult> {
let mut stmt = ctx.db.prepare_cached(sql)?;
let columns = stmt.column_count();
let res: std::result::Result<Vec<Vec<_>>, rusqlite::Error> = stmt
@ -96,3 +103,17 @@ pub(super) fn db_query(ctx: &StorageContext, sql: &str, args: &[SqlValue]) -> Re
Ok(DBResult::Rows(res?))
}
pub(super) fn db_execute_many(
ctx: &StorageContext,
sql: &str,
args: &[Vec<SqlValue>],
) -> Result<DBResult> {
let mut stmt = ctx.db.prepare_cached(sql)?;
for params in args {
stmt.execute(params)?;
}
Ok(DBResult::None)
}