pub struct AsyncSqliteIo<S: AsyncConnectionState> { /* private fields */ }Expand description
Async interface for page-level SQLite database access.
This struct wraps the blocking SqliteIo and executes all blocking SQLite
operations via tokio::task::spawn_blocking.
§Important Notes
- The connection is opened in-memory, and the actual database file is attached
- All page operations use the ‘target’ schema name
- The connection is configured with special permissions for page-level access
§Typestate Pattern
This connection uses a typestate pattern. When you start a transaction with
transaction, the connection moves to the
AsyncInTransaction state. You must call commit() or rollback() to
return to the normal state.
Implementations§
Source§impl AsyncSqliteIo<AsyncNormal>
impl AsyncSqliteIo<AsyncNormal>
Sourcepub async fn open(uri: &str) -> Result<Self, Error>
pub async fn open(uri: &str) -> Result<Self, Error>
Opens a database for page-level access asynchronously.
This creates an in-memory SQLite connection and attaches the database
using the provided URI. The URI can be a simple path or include query
parameters like ?vfs=reverb.
§Arguments
uri- URI string for the database. Can be:- A simple path:
/path/to/db.sqlite - A file URI:
file:/path/to/db.sqlite - A URI with parameters:
file:/path/to/db.sqlite?vfs=reverb
- A simple path:
§Errors
Returns an error if:
- The database path is invalid
- The database cannot be opened or attached
- The specified VFS is not registered
- Required database configurations cannot be set
Sourcepub async fn transaction(
self,
transaction_type: TransactionType,
) -> Result<AsyncSqliteIo<AsyncInTransaction>, Error>
pub async fn transaction( self, transaction_type: TransactionType, ) -> Result<AsyncSqliteIo<AsyncInTransaction>, Error>
Starts a new async transaction for page modifications.
This method consumes the connection and returns it in transaction mode.
You must call commit() or rollback() on the returned connection
to get back to normal mode.
§Arguments
transaction_type- The type of transaction to begin. UseTransactionType::Immediate(the default) for write operations to avoidSQLITE_BUSYerrors.
§Errors
Returns an error if the transaction cannot be started.
§Example
use sqlite_pages::{AsyncSqliteIo, TransactionType};
// Create a test database
let tempdir = tempfile::tempdir()?;
let db_path = tempdir.path().join("test.db");
rusqlite::Connection::open(&db_path)?.execute(
"CREATE TABLE test (id INTEGER PRIMARY KEY)",
[],
)?;
let db = AsyncSqliteIo::open(&db_path.to_string_lossy()).await?;
let tx = db.transaction(TransactionType::Immediate).await?;
// Can await between operations
let page = tx.get_page_data(1).await?;
tx.set_page_data(1, page.data()).await?;
let db = tx.commit().await?; // Returns connection back to normal modeSourcepub async fn page_map<R, F>(&self, range: R, fun: F) -> Result<(), Error>
pub async fn page_map<R, F>(&self, range: R, fun: F) -> Result<(), Error>
Maps a function over database pages in the specified range asynchronously.
Page numbers are 1-based (the first page is page 1, not page 0).
§Arguments
range- A range of page numbers (1-based) to processfun- A function that receives the page number (1-based) and page data for each page
§Errors
Returns an error if:
- The range is invalid (start > end)
- The database cannot be queried
- Page data cannot be read
Sourcepub async fn page_count(&self) -> Result<usize, Error>
pub async fn page_count(&self) -> Result<usize, Error>
Returns the number of pages in the database.
§Errors
Returns an error if the page count cannot be retrieved.
Sourcepub async fn with_connection<F, R>(&self, f: F) -> Result<R, Error>
pub async fn with_connection<F, R>(&self, f: F) -> Result<R, Error>
Executes a closure with access to the underlying SqliteIo connection.
This method provides synchronous access to the rusqlite::Connection for
operations that require direct SQL access, such as diagnostic queries,
test setup, or arbitrary SQL execution.
The closure runs inside spawn_blocking to avoid blocking the async runtime.
§Arguments
f- A closure that receives a reference to theSqliteIoand returns a value.
§Errors
Returns an error if the inner connection cannot be accessed or if the spawned task fails.
§Example
use sqlite_pages::AsyncSqliteIo;
let db = AsyncSqliteIo::open(&db_path.to_string_lossy()).await?;
// Execute arbitrary SQL via the connection
let count: i64 = db.with_connection(|conn| {
conn.query_row("SELECT COUNT(*) FROM sqlite_master", [], |row| row.get(0))
.unwrap()
}).await?;Source§impl AsyncSqliteIo<AsyncInTransaction>
impl AsyncSqliteIo<AsyncInTransaction>
Sourcepub async fn page_map<R, F>(&self, range: R, fun: F) -> Result<(), Error>
pub async fn page_map<R, F>(&self, range: R, fun: F) -> Result<(), Error>
Maps a function over database pages in the specified range asynchronously.
Page numbers are 1-based (the first page is page 1, not page 0).
§Arguments
range- A range of page numbers (1-based) to processfun- A function that receives the page number (1-based) and page data for each page
§Errors
Returns an error if the range is invalid or pages cannot be read.
Sourcepub async fn with_connection<F, R>(&self, f: F) -> Result<R, Error>
pub async fn with_connection<F, R>(&self, f: F) -> Result<R, Error>
Executes a closure with access to the underlying rusqlite::Connection.
This method provides synchronous access to the rusqlite::Connection for
operations that require direct SQL access, such as diagnostic queries,
test setup, or arbitrary SQL execution.
The closure runs inside spawn_blocking to avoid blocking the async runtime.
§Arguments
f- A closure that receives a reference to the connection and returns a value.
§Errors
Returns an error if the inner connection cannot be accessed or if the spawned task fails.