Skip to content

Commit

Permalink
Support PgHstore in macros
Browse files Browse the repository at this point in the history
  • Loading branch information
joeydewaal committed Sep 20, 2024
1 parent ebf04ff commit bf1ef50
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
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: 22 additions & 1 deletion 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::{Connection, PgConnection, PgPool, Postgres, Transaction};
use sqlx_postgres::types::PgHstore;
use sqlx_test::new;

use futures::TryStreamExt;
Expand Down Expand Up @@ -636,3 +637,23 @@ async fn test_to_from_citext() -> anyhow::Result<()> {

Ok(())
}

#[sqlx_macros::test(migrations = "tests/postgres/migrations")]
async fn pghstore_tests(pool: PgPool) -> Result<(), sqlx::Error> {
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(&pool)
.await?;

sqlx::query!(
" insert into mytable(f) select * from unnest($1::hstore[])",
&stores
)
.execute(&pool)
.await?;

Ok(())
}
2 changes: 2 additions & 0 deletions tests/postgres/migrations/4_hstore.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CREATE EXTENSION hstore WITH SCHEMA public;
CREATE TABLE mytable(f HSTORE);

0 comments on commit bf1ef50

Please sign in to comment.