2020-06-12 22:20:24 +00:00
|
|
|
use futures::TryStreamExt;
|
|
|
|
use sqlx::{Connection, Executor, FromRow, Postgres};
|
|
|
|
use sqlx_core::postgres::types::PgRange;
|
2020-03-24 04:22:25 +00:00
|
|
|
use sqlx_test::{new, test_type};
|
2020-03-18 02:26:59 +00:00
|
|
|
use std::fmt::Debug;
|
2020-06-12 22:20:24 +00:00
|
|
|
use std::ops::Bound;
|
2020-03-18 02:26:59 +00:00
|
|
|
|
|
|
|
// Transparent types are rust-side wrappers over DB types
|
|
|
|
#[derive(PartialEq, Debug, sqlx::Type)]
|
|
|
|
#[sqlx(transparent)]
|
|
|
|
struct Transparent(i32);
|
|
|
|
|
|
|
|
// "Weak" enums map to an integer type indicated by #[repr]
|
|
|
|
#[derive(PartialEq, Copy, Clone, Debug, sqlx::Type)]
|
|
|
|
#[repr(i32)]
|
|
|
|
enum Weak {
|
|
|
|
One = 0,
|
|
|
|
Two = 2,
|
|
|
|
Three = 4,
|
|
|
|
}
|
|
|
|
|
2020-04-10 20:37:08 +00:00
|
|
|
// "Strong" enums can map to TEXT (25)
|
2020-03-18 02:26:59 +00:00
|
|
|
#[derive(PartialEq, Debug, sqlx::Type)]
|
2020-03-27 02:16:49 +00:00
|
|
|
#[sqlx(rename = "text")]
|
2020-03-18 02:26:59 +00:00
|
|
|
#[sqlx(rename_all = "lowercase")]
|
|
|
|
enum Strong {
|
|
|
|
One,
|
|
|
|
Two,
|
|
|
|
|
|
|
|
#[sqlx(rename = "four")]
|
|
|
|
Three,
|
|
|
|
}
|
|
|
|
|
2020-05-18 04:15:05 +00:00
|
|
|
// rename_all variants
|
|
|
|
#[derive(PartialEq, Debug, sqlx::Type)]
|
2020-05-19 03:09:51 +00:00
|
|
|
#[sqlx(rename = "color_lower")]
|
2020-05-18 04:15:05 +00:00
|
|
|
#[sqlx(rename_all = "lowercase")]
|
|
|
|
enum ColorLower {
|
|
|
|
Red,
|
|
|
|
Green,
|
|
|
|
Blue,
|
|
|
|
}
|
2020-06-12 22:20:24 +00:00
|
|
|
|
2020-05-18 04:15:05 +00:00
|
|
|
#[derive(PartialEq, Debug, sqlx::Type)]
|
2020-05-19 03:09:51 +00:00
|
|
|
#[sqlx(rename = "color_snake")]
|
2020-05-18 04:15:05 +00:00
|
|
|
#[sqlx(rename_all = "snake_case")]
|
|
|
|
enum ColorSnake {
|
|
|
|
RedGreen,
|
|
|
|
BlueBlack,
|
|
|
|
}
|
2020-06-12 22:20:24 +00:00
|
|
|
|
2020-05-18 04:15:05 +00:00
|
|
|
#[derive(PartialEq, Debug, sqlx::Type)]
|
2020-05-19 03:09:51 +00:00
|
|
|
#[sqlx(rename = "color_upper")]
|
2020-05-18 04:15:05 +00:00
|
|
|
#[sqlx(rename_all = "uppercase")]
|
|
|
|
enum ColorUpper {
|
|
|
|
Red,
|
|
|
|
Green,
|
|
|
|
Blue,
|
|
|
|
}
|
|
|
|
|
2020-04-10 20:37:08 +00:00
|
|
|
// "Strong" enum can map to a custom type
|
|
|
|
#[derive(PartialEq, Debug, sqlx::Type)]
|
|
|
|
#[sqlx(rename = "mood")]
|
|
|
|
#[sqlx(rename_all = "lowercase")]
|
|
|
|
enum Mood {
|
|
|
|
Ok,
|
|
|
|
Happy,
|
|
|
|
Sad,
|
|
|
|
}
|
|
|
|
|
2020-03-18 02:26:59 +00:00
|
|
|
// Records must map to a custom type
|
|
|
|
// Note that all types are types in Postgres
|
2020-04-01 23:40:04 +00:00
|
|
|
#[derive(PartialEq, Debug, sqlx::Type)]
|
|
|
|
#[sqlx(rename = "inventory_item")]
|
|
|
|
struct InventoryItem {
|
|
|
|
name: String,
|
|
|
|
supplier_id: Option<i32>,
|
|
|
|
price: Option<i64>,
|
|
|
|
}
|
2020-03-18 02:26:59 +00:00
|
|
|
|
2020-06-12 22:20:24 +00:00
|
|
|
// Custom range type
|
|
|
|
#[derive(sqlx::Type, Debug, PartialEq)]
|
|
|
|
#[sqlx(rename = "float_range")]
|
|
|
|
struct FloatRange(PgRange<f64>);
|
|
|
|
|
|
|
|
// Custom domain type
|
|
|
|
#[derive(sqlx::Type, Debug)]
|
|
|
|
#[sqlx(rename = "int4rangeL0pC")]
|
|
|
|
struct RangeInclusive(PgRange<i32>);
|
|
|
|
|
|
|
|
test_type!(transparent<Transparent>(Postgres,
|
2020-03-18 02:26:59 +00:00
|
|
|
"0" == Transparent(0),
|
|
|
|
"23523" == Transparent(23523)
|
|
|
|
));
|
|
|
|
|
2020-06-12 22:20:24 +00:00
|
|
|
test_type!(weak_enum<Weak>(Postgres,
|
2020-03-18 02:26:59 +00:00
|
|
|
"0::int4" == Weak::One,
|
|
|
|
"2::int4" == Weak::Two,
|
|
|
|
"4::int4" == Weak::Three
|
|
|
|
));
|
|
|
|
|
2020-06-12 22:20:24 +00:00
|
|
|
test_type!(strong_enum<Strong>(Postgres,
|
2020-03-18 02:26:59 +00:00
|
|
|
"'one'::text" == Strong::One,
|
|
|
|
"'two'::text" == Strong::Two,
|
|
|
|
"'four'::text" == Strong::Three
|
|
|
|
));
|
|
|
|
|
2020-06-12 22:20:24 +00:00
|
|
|
test_type!(floatrange<FloatRange>(Postgres,
|
|
|
|
"'[1.234, 5.678]'::float_range" == FloatRange(PgRange::from((Bound::Included(1.234), Bound::Included(5.678)))),
|
|
|
|
));
|
|
|
|
|
2020-05-30 21:51:57 +00:00
|
|
|
#[sqlx_macros::test]
|
2020-04-10 20:37:08 +00:00
|
|
|
async fn test_enum_type() -> anyhow::Result<()> {
|
|
|
|
let mut conn = new::<Postgres>().await?;
|
|
|
|
|
|
|
|
conn.execute(
|
|
|
|
r#"
|
2020-04-10 21:22:18 +00:00
|
|
|
DROP TABLE IF EXISTS people;
|
|
|
|
|
|
|
|
DROP TYPE IF EXISTS mood CASCADE;
|
2020-04-10 20:37:08 +00:00
|
|
|
|
2020-04-10 21:22:18 +00:00
|
|
|
CREATE TYPE mood AS ENUM ( 'ok', 'happy', 'sad' );
|
2020-04-10 20:37:08 +00:00
|
|
|
|
2020-05-19 03:09:51 +00:00
|
|
|
DROP TYPE IF EXISTS color_lower CASCADE;
|
|
|
|
DROP TYPE IF EXISTS color_snake CASCADE;
|
|
|
|
DROP TYPE IF EXISTS color_upper CASCADE;
|
|
|
|
|
|
|
|
CREATE TYPE color_lower AS ENUM ( 'red', 'green', 'blue' );
|
|
|
|
CREATE TYPE color_snake AS ENUM ( 'red_green', 'blue_black' );
|
|
|
|
CREATE TYPE color_upper AS ENUM ( 'RED', 'GREEN', 'BLUE' );
|
|
|
|
|
2020-04-10 21:22:18 +00:00
|
|
|
CREATE TABLE people (
|
2020-04-10 20:37:08 +00:00
|
|
|
id serial PRIMARY KEY,
|
|
|
|
mood mood not null
|
|
|
|
);
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
// Drop and re-acquire the connection
|
|
|
|
conn.close().await?;
|
|
|
|
let mut conn = new::<Postgres>().await?;
|
|
|
|
|
|
|
|
// Select from table test
|
|
|
|
let (people_id,): (i32,) = sqlx::query_as(
|
|
|
|
"
|
|
|
|
INSERT INTO people (mood)
|
|
|
|
VALUES ($1)
|
|
|
|
RETURNING id
|
|
|
|
",
|
|
|
|
)
|
|
|
|
.bind(Mood::Sad)
|
|
|
|
.fetch_one(&mut conn)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
// Drop and re-acquire the connection
|
|
|
|
conn.close().await?;
|
|
|
|
let mut conn = new::<Postgres>().await?;
|
|
|
|
|
|
|
|
#[derive(sqlx::FromRow)]
|
|
|
|
struct PeopleRow {
|
|
|
|
id: i32,
|
|
|
|
mood: Mood,
|
|
|
|
}
|
|
|
|
|
|
|
|
let rec: PeopleRow = sqlx::query_as(
|
|
|
|
"
|
|
|
|
SELECT id, mood FROM people WHERE id = $1
|
2020-06-12 22:20:24 +00:00
|
|
|
",
|
2020-04-10 20:37:08 +00:00
|
|
|
)
|
|
|
|
.bind(people_id)
|
|
|
|
.fetch_one(&mut conn)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
assert_eq!(rec.id, people_id);
|
|
|
|
assert_eq!(rec.mood, Mood::Sad);
|
|
|
|
|
|
|
|
// Drop and re-acquire the connection
|
|
|
|
conn.close().await?;
|
|
|
|
let mut conn = new::<Postgres>().await?;
|
|
|
|
|
|
|
|
let stmt = format!("SELECT id, mood FROM people WHERE id = {}", people_id);
|
|
|
|
dbg!(&stmt);
|
2020-06-12 22:20:24 +00:00
|
|
|
|
2020-04-10 20:37:08 +00:00
|
|
|
let mut cursor = conn.fetch(&*stmt);
|
|
|
|
|
2020-06-12 22:20:24 +00:00
|
|
|
let row = cursor.try_next().await?.unwrap();
|
2020-04-10 20:37:08 +00:00
|
|
|
let rec = PeopleRow::from_row(&row)?;
|
|
|
|
|
|
|
|
assert_eq!(rec.id, people_id);
|
|
|
|
assert_eq!(rec.mood, Mood::Sad);
|
|
|
|
|
2020-06-12 22:20:24 +00:00
|
|
|
drop(cursor);
|
|
|
|
|
2020-04-10 20:37:08 +00:00
|
|
|
// Normal type equivalency test
|
|
|
|
|
|
|
|
let rec: (bool, Mood) = sqlx::query_as(
|
|
|
|
"
|
2020-06-12 22:20:24 +00:00
|
|
|
SELECT $1 = 'happy'::mood, $1
|
|
|
|
",
|
2020-04-10 20:37:08 +00:00
|
|
|
)
|
|
|
|
.bind(&Mood::Happy)
|
|
|
|
.fetch_one(&mut conn)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
assert!(rec.0);
|
|
|
|
assert_eq!(rec.1, Mood::Happy);
|
|
|
|
|
2020-05-19 03:32:05 +00:00
|
|
|
let rec: (bool, ColorLower) = sqlx::query_as(
|
|
|
|
"
|
2020-06-12 22:20:24 +00:00
|
|
|
SELECT $1 = 'green'::color_lower, $1
|
|
|
|
",
|
2020-05-19 03:32:05 +00:00
|
|
|
)
|
|
|
|
.bind(&ColorLower::Green)
|
|
|
|
.fetch_one(&mut conn)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
assert!(rec.0);
|
|
|
|
assert_eq!(rec.1, ColorLower::Green);
|
|
|
|
|
|
|
|
let rec: (bool, ColorSnake) = sqlx::query_as(
|
|
|
|
"
|
2020-06-12 22:20:24 +00:00
|
|
|
SELECT $1 = 'red_green'::color_snake, $1
|
|
|
|
",
|
2020-05-19 03:32:05 +00:00
|
|
|
)
|
|
|
|
.bind(&ColorSnake::RedGreen)
|
|
|
|
.fetch_one(&mut conn)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
assert!(rec.0);
|
|
|
|
assert_eq!(rec.1, ColorSnake::RedGreen);
|
|
|
|
|
|
|
|
let rec: (bool, ColorUpper) = sqlx::query_as(
|
|
|
|
"
|
2020-06-12 22:20:24 +00:00
|
|
|
SELECT $1 = 'RED'::color_upper, $1
|
|
|
|
",
|
2020-05-19 03:32:05 +00:00
|
|
|
)
|
|
|
|
.bind(&ColorUpper::Red)
|
|
|
|
.fetch_one(&mut conn)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
assert!(rec.0);
|
|
|
|
assert_eq!(rec.1, ColorUpper::Red);
|
|
|
|
|
2020-04-10 20:37:08 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-05-30 21:51:57 +00:00
|
|
|
#[sqlx_macros::test]
|
2020-04-01 23:40:04 +00:00
|
|
|
async fn test_record_type() -> anyhow::Result<()> {
|
|
|
|
let mut conn = new::<Postgres>().await?;
|
|
|
|
|
|
|
|
let value = InventoryItem {
|
|
|
|
name: "fuzzy dice".to_owned(),
|
|
|
|
supplier_id: Some(42),
|
|
|
|
price: Some(199),
|
|
|
|
};
|
|
|
|
|
|
|
|
let rec: (bool, InventoryItem) = sqlx::query_as(
|
|
|
|
"
|
2020-06-12 22:20:24 +00:00
|
|
|
SELECT $1 = ROW('fuzzy dice', 42, 199)::inventory_item, $1
|
2020-04-01 23:40:04 +00:00
|
|
|
",
|
|
|
|
)
|
|
|
|
.bind(&value)
|
|
|
|
.fetch_one(&mut conn)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
assert!(rec.0);
|
|
|
|
assert_eq!(rec.1, value);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-03-24 04:22:25 +00:00
|
|
|
|
|
|
|
#[cfg(feature = "macros")]
|
2020-05-30 21:51:57 +00:00
|
|
|
#[sqlx_macros::test]
|
2020-03-24 04:22:25 +00:00
|
|
|
async fn test_from_row() -> anyhow::Result<()> {
|
|
|
|
let mut conn = new::<Postgres>().await?;
|
|
|
|
|
|
|
|
#[derive(sqlx::FromRow)]
|
|
|
|
struct Account {
|
|
|
|
id: i32,
|
|
|
|
name: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
let account: Account = sqlx::query_as(
|
|
|
|
"SELECT * from (VALUES (1, 'Herp Derpinson')) accounts(id, name) where id = $1",
|
|
|
|
)
|
|
|
|
.bind(1_i32)
|
2020-03-24 04:24:28 +00:00
|
|
|
.fetch_one(&mut conn)
|
|
|
|
.await?;
|
2020-03-24 04:22:25 +00:00
|
|
|
|
|
|
|
assert_eq!(account.id, 1);
|
|
|
|
assert_eq!(account.name, "Herp Derpinson");
|
|
|
|
|
|
|
|
// A _single_ lifetime may be used but only when using the lowest-level API currently (Query::fetch)
|
|
|
|
|
|
|
|
#[derive(sqlx::FromRow)]
|
|
|
|
struct RefAccount<'a> {
|
|
|
|
id: i32,
|
|
|
|
name: &'a str,
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut cursor = sqlx::query(
|
|
|
|
"SELECT * from (VALUES (1, 'Herp Derpinson')) accounts(id, name) where id = $1",
|
|
|
|
)
|
|
|
|
.bind(1_i32)
|
|
|
|
.fetch(&mut conn);
|
|
|
|
|
2020-06-12 22:20:24 +00:00
|
|
|
let row = cursor.try_next().await?.unwrap();
|
|
|
|
let account = RefAccount::from_row(&row)?;
|
2020-03-24 04:22:25 +00:00
|
|
|
|
|
|
|
assert_eq!(account.id, 1);
|
|
|
|
assert_eq!(account.name, "Herp Derpinson");
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-03-30 13:44:48 +00:00
|
|
|
|
|
|
|
#[cfg(feature = "macros")]
|
2020-05-30 21:51:57 +00:00
|
|
|
#[sqlx_macros::test]
|
2020-03-30 13:44:48 +00:00
|
|
|
async fn test_from_row_with_keyword() -> anyhow::Result<()> {
|
|
|
|
#[derive(Debug, sqlx::FromRow)]
|
|
|
|
struct AccountKeyword {
|
|
|
|
r#type: i32,
|
|
|
|
r#static: String,
|
|
|
|
r#let: Option<String>,
|
|
|
|
r#struct: Option<String>,
|
|
|
|
name: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut conn = new::<Postgres>().await?;
|
|
|
|
|
|
|
|
let account: AccountKeyword = sqlx::query_as(
|
|
|
|
r#"SELECT * from (VALUES (1, 'foo', 'bar', null, null)) accounts(type, static, let, struct, name)"#
|
|
|
|
)
|
|
|
|
.fetch_one(&mut conn)
|
|
|
|
.await?;
|
|
|
|
println!("{:?}", account);
|
|
|
|
|
|
|
|
assert_eq!(1, account.r#type);
|
|
|
|
assert_eq!("foo", account.r#static);
|
|
|
|
assert_eq!(None, account.r#struct);
|
|
|
|
assert_eq!(Some("bar".to_owned()), account.r#let);
|
|
|
|
assert_eq!(None, account.name);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-04-04 12:18:07 +00:00
|
|
|
|
|
|
|
#[cfg(feature = "macros")]
|
2020-05-30 21:51:57 +00:00
|
|
|
#[sqlx_macros::test]
|
2020-04-04 12:18:07 +00:00
|
|
|
async fn test_from_row_with_rename() -> anyhow::Result<()> {
|
|
|
|
#[derive(Debug, sqlx::FromRow)]
|
|
|
|
struct AccountKeyword {
|
|
|
|
#[sqlx(rename = "type")]
|
|
|
|
own_type: i32,
|
|
|
|
|
|
|
|
#[sqlx(rename = "static")]
|
|
|
|
my_static: String,
|
|
|
|
|
|
|
|
#[sqlx(rename = "let")]
|
|
|
|
custom_let: Option<String>,
|
|
|
|
|
|
|
|
#[sqlx(rename = "struct")]
|
|
|
|
def_struct: Option<String>,
|
|
|
|
|
|
|
|
name: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut conn = new::<Postgres>().await?;
|
|
|
|
|
|
|
|
let account: AccountKeyword = sqlx::query_as(
|
|
|
|
r#"SELECT * from (VALUES (1, 'foo', 'bar', null, null)) accounts(type, static, let, struct, name)"#
|
|
|
|
)
|
|
|
|
.fetch_one(&mut conn)
|
|
|
|
.await?;
|
|
|
|
println!("{:?}", account);
|
|
|
|
|
|
|
|
assert_eq!(1, account.own_type);
|
|
|
|
assert_eq!("foo", account.my_static);
|
|
|
|
assert_eq!(None, account.def_struct);
|
|
|
|
assert_eq!(Some("bar".to_owned()), account.custom_let);
|
|
|
|
assert_eq!(None, account.name);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-07-07 19:51:30 +00:00
|
|
|
|
|
|
|
#[cfg(feature = "macros")]
|
|
|
|
#[sqlx_macros::test]
|
|
|
|
async fn test_default() -> anyhow::Result<()> {
|
|
|
|
#[derive(Debug, sqlx::FromRow)]
|
|
|
|
struct HasDefault {
|
|
|
|
not_default: i32,
|
|
|
|
#[sqlx(default)]
|
|
|
|
default: Option<i32>,
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut conn = new::<Postgres>().await?;
|
|
|
|
|
|
|
|
let has_default: HasDefault = sqlx::query_as(r#"SELECT 1 AS not_default"#)
|
|
|
|
.fetch_one(&mut conn)
|
|
|
|
.await?;
|
|
|
|
println!("{:?}", has_default);
|
|
|
|
|
|
|
|
assert_eq!(has_default.not_default, 1);
|
|
|
|
assert_eq!(has_default.default, None);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|