Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support PgHstore by default in macros #3514

Merged
merged 3 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions sqlx-postgres/src/type_checking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ impl_type_checking!(
#[cfg(feature = "bit-vec")]
sqlx::types::BitVec,

sqlx::postgres::types::PgHstore,
// Arrays

Vec<bool> | &[bool],
Expand Down Expand Up @@ -139,6 +140,8 @@ impl_type_checking!(
#[cfg(feature = "json")]
Vec<sqlx::types::JsonValue> | &[sqlx::types::JsonValue],

Vec<sqlx::postgres::types::PgHstore> | &[sqlx::postgres::types::PgHstore],

// Ranges

sqlx::postgres::types::PgRange<i32>,
Expand Down
8 changes: 7 additions & 1 deletion sqlx-postgres/src/types/hstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -138,6 +138,12 @@ impl Type<Postgres> 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<Self, BoxDynError> {
let mut buf = <&[u8] as Decode<Postgres>>::decode(value)?;
Expand Down
23 changes: 23 additions & 0 deletions tests/postgres/macros.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use sqlx::{Connection, PgConnection, Postgres, Transaction};
use sqlx_postgres::types::PgHstore;
use sqlx_test::new;

use futures::TryStreamExt;
Expand Down Expand Up @@ -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::<Postgres>().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(())
}
5 changes: 5 additions & 0 deletions tests/postgres/setup.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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);