libsql icon indicating copy to clipboard operation
libsql copied to clipboard

Batch scopes

Open levydsa opened this issue 1 year ago • 0 comments

Batch statements are quite limited in the current API. You can only execute multiple queries without parameters. And if need to execute a list of statements, you would need to join those strings separated by ";".

Proposal: Create a batch object that acts like regular connection or transaction, and batch all executes and queries when you call .commit() on that object. Rows can be accessed with a handle via a object returned by the commit.

let batch: Batch = conn.batch();

batch.execute("create table if not exists test (i integer)", ())?;

for i in 0..10 {
    batch.execute("insert into test values (?)", params![i])?;
}

// You can't use the Rows from a query before it is batched. To prevent it,
// only give a handle to access rows with the BatchResponse. This should only
// be wrapper of a index (u32) that can only be returned by `.query()`.
let handle: BatchHandle = batch.query("select * from test", ())?;

let batch_res: BatchResponse = batch.commit()?; // Batch all statements

// batch.query/execute(...) cannot be called here, ownership is passed to `.commit()`

let rows = batch_res.get(handle);

levydsa avatar Sep 26 '24 18:09 levydsa