ORM-07: Add raw SQL helpers to Database #7

Closed
opened 2026-10-13 12:49:38 +01:00 by NoctisWolfshadow · 1 comment
Owner

Depends on: #6

Context

Database should also let you send instructions to the database directly (DDL, ad-hoc statements, test setup) without reaching for rusqlite.

Tasks

  • Database::execute(sql, params) -> Result<usize>, where params are impl rusqlite::Params.
  • Database::execute_batch(sql) -> Result<()>.
  • Database::last_insert_rowid() -> Result<i64>.

Example

Database::execute_batch("CREATE TABLE notes (id INTEGER PRIMARY KEY, body TEXT)")?;

let changed = Database::execute("INSERT INTO notes (body) VALUES (?1)", ["hello"])?;
assert_eq!(changed, 1);

let id = Database::last_insert_rowid()?;
#[test]
fn bad_sql_surfaces_as_error_sqlite() {
    Database::connect_thread(Config::memory()).unwrap();
    assert!(matches!(Database::execute_batch("NOT SQL"), Err(Error::Sqlite(_))));
}

Acceptance criteria

  • Tests create a table, insert with params and check the changed-row count.
  • execute_batch runs several statements in one call.
  • Failures surface as Error::Sqlite.
**Depends on:** #6 ## Context `Database` should also let you send instructions to the database directly (DDL, ad-hoc statements, test setup) without reaching for rusqlite. ## Tasks - [x] `Database::execute(sql, params) -> Result<usize>`, where params are `impl rusqlite::Params`. - [x] `Database::execute_batch(sql) -> Result<()>`. - [ ] `Database::last_insert_rowid() -> Result<i64>`. ## Example ```rust Database::execute_batch("CREATE TABLE notes (id INTEGER PRIMARY KEY, body TEXT)")?; let changed = Database::execute("INSERT INTO notes (body) VALUES (?1)", ["hello"])?; assert_eq!(changed, 1); let id = Database::last_insert_rowid()?; ``` ```rust #[test] fn bad_sql_surfaces_as_error_sqlite() { Database::connect_thread(Config::memory()).unwrap(); assert!(matches!(Database::execute_batch("NOT SQL"), Err(Error::Sqlite(_)))); } ``` ## Acceptance criteria - [x] Tests create a table, insert with params and check the changed-row count. - [x] `execute_batch` runs several statements in one call. - [x] Failures surface as `Error::Sqlite`.
Author
Owner

Database::last_insert_rowid() makes no sense it can just be called in the transaction itself and then returned there

Database::last_insert_rowid() makes no sense it can just be called in the transaction itself and then returned there
Sign in to join this conversation.
No description provided.