mirror of
https://github.com/launchbadge/sqlx.git
synced 2025-12-30 05:11:13 +00:00
* refactor: introduce `SqlSafeStr` API * rebase main * Add SqlStr + remove Statement lifetime * Update the definition of Executor and AnyConnectionBackend + update Postgres driver * Update MySql driver * Update Sqlite driver * remove debug clone count * Reduce the amount of SqlStr clones * improve QueryBuilder error message * cargo fmt * fix clippy warnings * fix doc test * Avoid panic in `QueryBuilder::reset` * Use `QueryBuilder` when removing all test db's * Add comment to `SqlStr` Co-authored-by: Austin Bonander <austin.bonander@gmail.com> * Update sqlx-core/src/query_builder.rs Co-authored-by: Austin Bonander <austin.bonander@gmail.com> * Add `Clone` as supertrait to `Statement` * Move `Connection`, `AnyConnectionBackend` and `TransactionManager` to `SqlStr` * Replace `sql_cloned` with `sql` in `Statement` * Update `Executor` trait * Update unit tests + QueryBuilder changes * Remove code in comments * Update comment in `QueryBuilder` * Fix clippy warnings * Update `Migrate` comment * Small changes * Move `Migration` to `SqlStr` --------- Co-authored-by: Austin Bonander <austin.bonander@gmail.com>
100 lines
2.7 KiB
Rust
100 lines
2.7 KiB
Rust
use sqlx::{postgres::Postgres, Column, Executor, SqlSafeStr, TypeInfo};
|
|
use sqlx_test::new;
|
|
|
|
#[sqlx_macros::test]
|
|
async fn it_describes_simple() -> anyhow::Result<()> {
|
|
let mut conn = new::<Postgres>().await?;
|
|
|
|
let d = conn.describe("SELECT * FROM tweet".into_sql_str()).await?;
|
|
|
|
assert_eq!(d.columns()[0].name(), "id");
|
|
assert_eq!(d.columns()[1].name(), "created_at");
|
|
assert_eq!(d.columns()[2].name(), "text");
|
|
assert_eq!(d.columns()[3].name(), "owner_id");
|
|
|
|
assert_eq!(d.nullable(0), Some(false));
|
|
assert_eq!(d.nullable(1), Some(false));
|
|
assert_eq!(d.nullable(2), Some(false));
|
|
assert_eq!(d.nullable(3), Some(true));
|
|
|
|
assert_eq!(d.columns()[0].type_info().name(), "INT8");
|
|
assert_eq!(d.columns()[1].type_info().name(), "TIMESTAMPTZ");
|
|
assert_eq!(d.columns()[2].type_info().name(), "TEXT");
|
|
assert_eq!(d.columns()[3].type_info().name(), "INT8");
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[sqlx_macros::test]
|
|
async fn it_describes_expression() -> anyhow::Result<()> {
|
|
let mut conn = new::<Postgres>().await?;
|
|
|
|
let d = conn.describe("SELECT 1::int8 + 10".into_sql_str()).await?;
|
|
|
|
// ?column? will cause the macro to emit an error ad ask the user to explicitly name the type
|
|
assert_eq!(d.columns()[0].name(), "?column?");
|
|
|
|
// postgres cannot infer nullability from an expression
|
|
// this will cause the macro to emit `Option<_>`
|
|
assert_eq!(d.nullable(0), None);
|
|
assert_eq!(d.columns()[0].type_info().name(), "INT8");
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[sqlx_macros::test]
|
|
async fn it_describes_enum() -> anyhow::Result<()> {
|
|
let mut conn = new::<Postgres>().await?;
|
|
|
|
let d = conn
|
|
.describe("SELECT 'open'::status as _1".into_sql_str())
|
|
.await?;
|
|
|
|
assert_eq!(d.columns()[0].name(), "_1");
|
|
|
|
let ty = d.columns()[0].type_info();
|
|
|
|
assert_eq!(ty.name(), "status");
|
|
|
|
assert_eq!(
|
|
format!("{:?}", ty.kind()),
|
|
r#"Enum(["new", "open", "closed"])"#
|
|
);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[sqlx_macros::test]
|
|
async fn it_describes_record() -> anyhow::Result<()> {
|
|
let mut conn = new::<Postgres>().await?;
|
|
|
|
let d = conn
|
|
.describe("SELECT (true, 10::int2)".into_sql_str())
|
|
.await?;
|
|
|
|
let ty = d.columns()[0].type_info();
|
|
assert_eq!(ty.name(), "RECORD");
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[sqlx_macros::test]
|
|
async fn it_describes_composite() -> anyhow::Result<()> {
|
|
let mut conn = new::<Postgres>().await?;
|
|
|
|
let d = conn
|
|
.describe("SELECT ROW('name',10,500)::inventory_item".into_sql_str())
|
|
.await?;
|
|
|
|
let ty = d.columns()[0].type_info();
|
|
|
|
assert_eq!(ty.name(), "inventory_item");
|
|
|
|
assert_eq!(
|
|
format!("{:?}", ty.kind()),
|
|
r#"Composite([("name", PgTypeInfo(Text)), ("supplier_id", PgTypeInfo(Int4)), ("price", PgTypeInfo(Int8))])"#
|
|
);
|
|
|
|
Ok(())
|
|
}
|