2020-06-06 04:16:14 +00:00
|
|
|
use sqlx::Sqlite;
|
|
|
|
use sqlx_test::new;
|
|
|
|
|
|
|
|
#[sqlx_macros::test]
|
|
|
|
async fn macro_select() -> anyhow::Result<()> {
|
|
|
|
let mut conn = new::<Sqlite>().await?;
|
|
|
|
|
|
|
|
let account = sqlx::query!("select id, name, is_active from accounts where id = 1")
|
|
|
|
.fetch_one(&mut conn)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
assert_eq!(1, account.id);
|
|
|
|
assert_eq!("Herp Derpinson", account.name);
|
2020-06-12 02:21:22 +00:00
|
|
|
assert_eq!(account.is_active, Some(true));
|
2020-06-06 04:16:14 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[sqlx_macros::test]
|
|
|
|
async fn macro_select_bind() -> anyhow::Result<()> {
|
|
|
|
let mut conn = new::<Sqlite>().await?;
|
|
|
|
|
|
|
|
let account = sqlx::query!(
|
|
|
|
"select id, name, is_active from accounts where id = ?",
|
|
|
|
1i32
|
|
|
|
)
|
|
|
|
.fetch_one(&mut conn)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
assert_eq!(1, account.id);
|
|
|
|
assert_eq!("Herp Derpinson", account.name);
|
2020-06-12 02:21:22 +00:00
|
|
|
assert_eq!(account.is_active, Some(true));
|
2020-06-06 04:16:14 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct RawAccount {
|
|
|
|
id: i32,
|
|
|
|
name: String,
|
|
|
|
is_active: Option<bool>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[sqlx_macros::test]
|
|
|
|
async fn test_query_as_raw() -> anyhow::Result<()> {
|
|
|
|
let mut conn = new::<Sqlite>().await?;
|
|
|
|
|
|
|
|
let account = sqlx::query_as!(RawAccount, "SELECT id, name, is_active from accounts")
|
|
|
|
.fetch_one(&mut conn)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
assert_eq!(account.id, 1);
|
|
|
|
assert_eq!(account.name, "Herp Derpinson");
|
2020-06-12 02:21:22 +00:00
|
|
|
assert_eq!(account.is_active, Some(true));
|
2020-06-06 04:16:14 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[sqlx_macros::test]
|
|
|
|
async fn macro_select_from_view() -> anyhow::Result<()> {
|
|
|
|
let mut conn = new::<Sqlite>().await?;
|
|
|
|
|
|
|
|
let account = sqlx::query!("SELECT id, name, is_active from accounts_view")
|
|
|
|
.fetch_one(&mut conn)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
// SQLite tells us the true origin of these columns even through the view
|
|
|
|
assert_eq!(account.id, 1);
|
|
|
|
assert_eq!(account.name, "Herp Derpinson");
|
2020-06-12 02:21:22 +00:00
|
|
|
assert_eq!(account.is_active, Some(true));
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[sqlx_macros::test]
|
|
|
|
async fn test_column_override_not_null() -> anyhow::Result<()> {
|
|
|
|
let mut conn = new::<Sqlite>().await?;
|
|
|
|
|
|
|
|
let record = sqlx::query!(r#"select is_active as "is_active!" from accounts"#)
|
|
|
|
.fetch_one(&mut conn)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
assert_eq!(record.is_active, true);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(PartialEq, Eq, Debug, sqlx::Type)]
|
|
|
|
#[sqlx(transparent)]
|
|
|
|
struct MyInt(i32);
|
|
|
|
|
|
|
|
#[sqlx_macros::test]
|
|
|
|
async fn test_column_override_wildcard() -> anyhow::Result<()> {
|
|
|
|
struct Record {
|
|
|
|
id: MyInt,
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut conn = new::<Sqlite>().await?;
|
|
|
|
|
|
|
|
let record = sqlx::query_as!(Record, r#"select id as "id: _" from accounts"#)
|
|
|
|
.fetch_one(&mut conn)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
assert_eq!(record.id, MyInt(1));
|
|
|
|
|
|
|
|
// this syntax is also useful for expressions
|
|
|
|
let record = sqlx::query_as!(Record, r#"select 1 as "id: _""#)
|
|
|
|
.fetch_one(&mut conn)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
assert_eq!(record.id, MyInt(1));
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[sqlx_macros::test]
|
|
|
|
async fn test_column_override_exact() -> anyhow::Result<()> {
|
|
|
|
let mut conn = new::<Sqlite>().await?;
|
|
|
|
|
|
|
|
let record = sqlx::query!(r#"select id as "id: MyInt" from accounts"#)
|
|
|
|
.fetch_one(&mut conn)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
assert_eq!(record.id, MyInt(1));
|
|
|
|
|
|
|
|
// we can also support this syntax for expressions
|
|
|
|
let record = sqlx::query!(r#"select 1 as "id: MyInt""#)
|
|
|
|
.fetch_one(&mut conn)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
assert_eq!(record.id, MyInt(1));
|
2020-06-06 04:16:14 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|