From 72512f7311a8ed720ec370b9908ce085e38940bc Mon Sep 17 00:00:00 2001 From: joeydewaal <99046430+joeydewaal@users.noreply.github.com> Date: Wed, 2 Oct 2024 20:56:13 +0200 Subject: [PATCH] Support PgHstore by default in macros (#3514) * Support PgHstore in macros * Change tests * Remove unused import --- sqlx-postgres/src/type_checking.rs | 3 +++ sqlx-postgres/src/types/hstore.rs | 8 +++++++- tests/postgres/macros.rs | 23 +++++++++++++++++++++++ tests/postgres/setup.sql | 5 +++++ 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/sqlx-postgres/src/type_checking.rs b/sqlx-postgres/src/type_checking.rs index e22d3b900..4fa02289b 100644 --- a/sqlx-postgres/src/type_checking.rs +++ b/sqlx-postgres/src/type_checking.rs @@ -83,6 +83,7 @@ impl_type_checking!( #[cfg(feature = "bit-vec")] sqlx::types::BitVec, + sqlx::postgres::types::PgHstore, // Arrays Vec | &[bool], @@ -139,6 +140,8 @@ impl_type_checking!( #[cfg(feature = "json")] Vec | &[sqlx::types::JsonValue], + Vec | &[sqlx::postgres::types::PgHstore], + // Ranges sqlx::postgres::types::PgRange, diff --git a/sqlx-postgres/src/types/hstore.rs b/sqlx-postgres/src/types/hstore.rs index 84172fcb7..a03970fb3 100644 --- a/sqlx-postgres/src/types/hstore.rs +++ b/sqlx-postgres/src/types/hstore.rs @@ -10,7 +10,7 @@ use crate::{ encode::{Encode, IsNull}, error::BoxDynError, types::Type, - PgArgumentBuffer, PgTypeInfo, PgValueRef, Postgres, + PgArgumentBuffer, PgHasArrayType, PgTypeInfo, PgValueRef, Postgres, }; use serde::{Deserialize, Serialize}; use sqlx_core::bytes::Buf; @@ -138,6 +138,12 @@ impl Type for PgHstore { } } +impl PgHasArrayType for PgHstore { + fn array_type_info() -> PgTypeInfo { + PgTypeInfo::array_of("hstore") + } +} + impl<'r> Decode<'r, Postgres> for PgHstore { fn decode(value: PgValueRef<'r>) -> Result { let mut buf = <&[u8] as Decode>::decode(value)?; diff --git a/tests/postgres/macros.rs b/tests/postgres/macros.rs index cfaaf1349..07ae96201 100644 --- a/tests/postgres/macros.rs +++ b/tests/postgres/macros.rs @@ -1,4 +1,5 @@ use sqlx::{Connection, PgConnection, Postgres, Transaction}; +use sqlx_postgres::types::PgHstore; use sqlx_test::new; use futures::TryStreamExt; @@ -636,3 +637,25 @@ async fn test_to_from_citext() -> anyhow::Result<()> { Ok(()) } + +#[sqlx_macros::test] +async fn pghstore_tests() -> anyhow::Result<()> { + let mut conn = new::().await?; + + let mut store = PgHstore::default(); + let stores = vec![store.clone(), store.clone()]; + + store.insert("key".into(), Some("value".to_string())); + sqlx::query!(" insert into mytable(f) values ($1)", store) + .execute(&mut conn) + .await?; + + sqlx::query!( + " insert into mytable(f) select * from unnest($1::hstore[])", + &stores + ) + .execute(&mut conn) + .await?; + + Ok(()) +} diff --git a/tests/postgres/setup.sql b/tests/postgres/setup.sql index db8364558..e3b35d315 100644 --- a/tests/postgres/setup.sql +++ b/tests/postgres/setup.sql @@ -7,6 +7,9 @@ CREATE EXTENSION IF NOT EXISTS cube; -- https://www.postgresql.org/docs/current/citext.html CREATE EXTENSION IF NOT EXISTS citext; +-- https://www.postgresql.org/docs/current/hstore.html +CREATE EXTENSION IF NOT EXISTS hstore; + -- https://www.postgresql.org/docs/current/sql-createtype.html CREATE TYPE status AS ENUM ('new', 'open', 'closed'); @@ -58,3 +61,5 @@ CREATE TABLE test_citext ( CREATE SCHEMA IF NOT EXISTS foo; CREATE TYPE foo."Foo" as ENUM ('Bar', 'Baz'); + +CREATE TABLE mytable(f HSTORE);