Skip to content

Commit

Permalink
fix(postgres): derive PgHasArrayType for enums
Browse files Browse the repository at this point in the history
  • Loading branch information
abonander committed May 31, 2024
1 parent d8d9d04 commit 43dd681
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
22 changes: 21 additions & 1 deletion sqlx-macros-core/src/derives/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ fn expand_derive_has_sql_type_weak_enum(
let attr = check_weak_enum_attributes(input, variants)?;
let repr = attr.repr.unwrap();
let ident = &input.ident;
let ts = quote!(
let mut ts = quote!(
#[automatically_derived]
impl<DB: ::sqlx::Database> ::sqlx::Type<DB> for #ident
where
Expand All @@ -146,6 +146,16 @@ fn expand_derive_has_sql_type_weak_enum(
}
);

if cfg!(feature = "postgres") && !attributes.no_pg_array {

Check failure on line 149 in sqlx-macros-core/src/derives/type.rs

View workflow job for this annotation

GitHub Actions / SQLite Examples

cannot find value `attributes` in this scope

Check failure on line 149 in sqlx-macros-core/src/derives/type.rs

View workflow job for this annotation

GitHub Actions / MySQL Examples

cannot find value `attributes` in this scope

Check failure on line 149 in sqlx-macros-core/src/derives/type.rs

View workflow job for this annotation

GitHub Actions / PostgreSQL Examples

cannot find value `attributes` in this scope
ts.extend(quote!(
impl ::sqlx::postgres::PgHasArrayType for #ident {
fn array_type_info() -> ::sqlx::postgres::PgTypeInfo {
<#ty as ::sqlx::postgres::PgHasArrayType>::array_type_info()

Check failure on line 153 in sqlx-macros-core/src/derives/type.rs

View workflow job for this annotation

GitHub Actions / SQLite Examples

cannot find value `ty` in this scope

Check failure on line 153 in sqlx-macros-core/src/derives/type.rs

View workflow job for this annotation

GitHub Actions / MySQL Examples

cannot find value `ty` in this scope

Check failure on line 153 in sqlx-macros-core/src/derives/type.rs

View workflow job for this annotation

GitHub Actions / PostgreSQL Examples

cannot find value `ty` in this scope
}
}
));
}

Ok(ts)
}

Expand Down Expand Up @@ -184,6 +194,16 @@ fn expand_derive_has_sql_type_strong_enum(
}
}
));

if !attributes.no_pg_array {
tts.extend(quote!(
impl ::sqlx::postgres::PgHasArrayType for #ident {
fn array_type_info() -> ::sqlx::postgres::PgTypeInfo {
<#ty as ::sqlx::postgres::PgHasArrayType>::array_type_info()

Check failure on line 202 in sqlx-macros-core/src/derives/type.rs

View workflow job for this annotation

GitHub Actions / SQLite Examples

cannot find value `ty` in this scope

Check failure on line 202 in sqlx-macros-core/src/derives/type.rs

View workflow job for this annotation

GitHub Actions / MySQL Examples

cannot find value `ty` in this scope

Check failure on line 202 in sqlx-macros-core/src/derives/type.rs

View workflow job for this annotation

GitHub Actions / PostgreSQL Examples

cannot find value `ty` in this scope
}
}
));
}
}

if cfg!(feature = "sqlite") {
Expand Down
19 changes: 15 additions & 4 deletions tests/postgres/derives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,15 @@ test_type!(transparent_array<TransparentArray>(Postgres,
test_type!(weak_enum<Weak>(Postgres,
"0::int4" == Weak::One,
"2::int4" == Weak::Two,
"4::int4" == Weak::Three
"4::int4" == Weak::Three,
"'{0, 2, 4}'::int4[]" == vec![Weak::One, Weak::Two, Weak::Three],
));

test_type!(strong_enum<Strong>(Postgres,
"'one'::text" == Strong::One,
"'two'::text" == Strong::Two,
"'four'::text" == Strong::Three
"'four'::text" == Strong::Three,
"'{'one', 'two', 'four'}'::text[]" == vec![Strong::One, Strong::Two, Strong::Three],
));

test_type!(floatrange<FloatRange>(Postgres,
Expand Down Expand Up @@ -739,7 +741,8 @@ async fn test_enum_with_schema() -> anyhow::Result<()> {

let foo: Foo = sqlx::query_scalar("SELECT $1::foo.\"Foo\"")
.bind(Foo::Bar)
.fetch_one(&mut conn).await?;
.fetch_one(&mut conn)
.await?;

assert_eq!(foo, Foo::Bar);

Expand All @@ -749,4 +752,12 @@ async fn test_enum_with_schema() -> anyhow::Result<()> {
.await?;

assert_eq!(foo, Foo::Baz);
}

let foos: Vec<Foo> = sqlx::query_scalar!("SELECT ARRAY($1::foo.\"Foo\", $2::foo.\"Foo\")")
.bind(Foo::Bar)
.bind(Foo::Baz)
.fetch_one(&mut conn)
.await?;

assert_eq!(foos, [Foo::Bar, Foo::Baz]);
}

0 comments on commit 43dd681

Please sign in to comment.