diff --git a/bindings/rust/examples/example_struct.rs b/bindings/rust/examples/example_struct.rs new file mode 100644 index 0000000000..a4001fdf9a --- /dev/null +++ b/bindings/rust/examples/example_struct.rs @@ -0,0 +1,60 @@ +use turso::{transaction::Transaction, Builder, Connection, Error}; + +#[derive(Debug)] +struct User { + email: String, + age: i32, +} + +async fn create_tables(conn: &Connection) -> Result<(), Error> { + conn.execute( + "CREATE TABLE IF NOT EXISTS users (email TEXT, age INTEGER)", + (), + ) + .await?; + Ok(()) +} + +async fn insert_users(tx: &Transaction<'_>) -> Result<(), Error> { + let mut stmt = tx + .prepare("INSERT INTO users (email, age) VALUES (?1, ?2)") + .await?; + stmt.execute(["foo@example.com", &21.to_string()]).await?; + stmt.execute(["bar@example.com", &22.to_string()]).await?; + Ok(()) +} + +async fn list_users(conn: &Connection) -> Result<(), Error> { + let mut stmt = conn + .prepare("SELECT * FROM users WHERE email like ?1") + .await?; + + let mut rows = stmt.query(["%@example.com"]).await?; + + while let Some(row) = rows.next().await? { + let u: User = User { + email: row.get(0)?, + age: row.get(1)?, + }; + println!("Row: {} {}", u.email, u.age); + } + Ok(()) +} + +#[tokio::main] +async fn main() -> Result<(), Error> { + let db = Builder::new_local(":memory:") + .build() + .await + .expect("Turso Failed to Build memory db"); + + let mut conn = db.connect()?; + + create_tables(&conn).await?; + let tx = conn.transaction().await?; + insert_users(&tx).await?; + tx.commit().await?; + list_users(&conn).await?; + + Ok(()) +} diff --git a/bindings/rust/src/transaction.rs b/bindings/rust/src/transaction.rs index dd77ed0d8f..af22c1f741 100644 --- a/bindings/rust/src/transaction.rs +++ b/bindings/rust/src/transaction.rs @@ -1,6 +1,6 @@ use std::{ops::Deref, sync::atomic::Ordering}; -use crate::{Connection, Result}; +use crate::{Connection, Result, Statement}; /// Options for transaction behavior. See [BEGIN /// TRANSACTION](http://www.sqlite.org/lang_transaction.html) for details. @@ -126,6 +126,14 @@ impl Transaction<'_> { }) } + // Use the Connection to Prepare a statement. + // This allows a database update function to be passed a transaction, + // prepare a statement, and use it without needing direct access to the + // Connection + pub async fn prepare(&self, sql: &str) -> Result { + self.conn.prepare(sql).await + } + /// Get the current setting for what happens to the transaction when it is /// dropped. #[inline]