#![cfg(feature = "sqlite")]
use qraft::quex;
async fn setup_pool() -> qraft::Result<quex::Pool> {
let pool = quex::Pool::connect("sqlite::memory:")?.build().await?;
quex::query("create table users(id integer primary key, name text not null)")
.execute(&pool)
.await?;
for (id, name) in [(1_i64, "ada"), (2, "grace")] {
quex::query("insert into users(id, name) values(?, ?)")
.bind(id)
.bind(name)
.execute(&pool)
.await?;
}
Ok(pool)
}
#[tokio::test]
async fn borrowed_sqlite_pool_works_across_quex_shim_queries() {
let pool = setup_pool().await.expect("sqlite pool");
let row = quex::query("select id, name from users where id = ?")
.bind(1_i64)
.fetch_one(&pool)
.await
.expect("fetch borrowed row");
assert_eq!(row.get::<i64>(0).expect("id"), 1);
assert_eq!(row.get::<String>(1).expect("name"), "ada");
let typed: (i64, String) = quex::query_as("select id, name from users where id = ?")
.bind(2_i64)
.fetch_one(&pool)
.await
.expect("fetch borrowed typed row");
assert_eq!(typed, (2, "grace".into()));
let count: i64 = quex::query_scalar("select count(*) from users")
.fetch_one(&pool)
.await
.expect("fetch borrowed scalar");
assert_eq!(count, 2);
}
#[tokio::test]
async fn owned_sqlite_pool_clone_can_execute_queries_too() {
let pool = setup_pool().await.expect("sqlite pool");
let count: i64 = quex::query_scalar("select count(*) from users")
.fetch_one(pool.clone())
.await
.expect("fetch scalar from owned pool handle");
assert_eq!(count, 2);
}