Skip to content

Commit 72512f7

Browse files
authored
Support PgHstore by default in macros (launchbadge#3514)
* Support PgHstore in macros * Change tests * Remove unused import
1 parent 5b8bb3b commit 72512f7

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed

sqlx-postgres/src/type_checking.rs

+3
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ impl_type_checking!(
8383
#[cfg(feature = "bit-vec")]
8484
sqlx::types::BitVec,
8585

86+
sqlx::postgres::types::PgHstore,
8687
// Arrays
8788

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

143+
Vec<sqlx::postgres::types::PgHstore> | &[sqlx::postgres::types::PgHstore],
144+
142145
// Ranges
143146

144147
sqlx::postgres::types::PgRange<i32>,

sqlx-postgres/src/types/hstore.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::{
1010
encode::{Encode, IsNull},
1111
error::BoxDynError,
1212
types::Type,
13-
PgArgumentBuffer, PgTypeInfo, PgValueRef, Postgres,
13+
PgArgumentBuffer, PgHasArrayType, PgTypeInfo, PgValueRef, Postgres,
1414
};
1515
use serde::{Deserialize, Serialize};
1616
use sqlx_core::bytes::Buf;
@@ -138,6 +138,12 @@ impl Type<Postgres> for PgHstore {
138138
}
139139
}
140140

141+
impl PgHasArrayType for PgHstore {
142+
fn array_type_info() -> PgTypeInfo {
143+
PgTypeInfo::array_of("hstore")
144+
}
145+
}
146+
141147
impl<'r> Decode<'r, Postgres> for PgHstore {
142148
fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError> {
143149
let mut buf = <&[u8] as Decode<Postgres>>::decode(value)?;

tests/postgres/macros.rs

+23
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use sqlx::{Connection, PgConnection, Postgres, Transaction};
2+
use sqlx_postgres::types::PgHstore;
23
use sqlx_test::new;
34

45
use futures::TryStreamExt;
@@ -636,3 +637,25 @@ async fn test_to_from_citext() -> anyhow::Result<()> {
636637

637638
Ok(())
638639
}
640+
641+
#[sqlx_macros::test]
642+
async fn pghstore_tests() -> anyhow::Result<()> {
643+
let mut conn = new::<Postgres>().await?;
644+
645+
let mut store = PgHstore::default();
646+
let stores = vec![store.clone(), store.clone()];
647+
648+
store.insert("key".into(), Some("value".to_string()));
649+
sqlx::query!(" insert into mytable(f) values ($1)", store)
650+
.execute(&mut conn)
651+
.await?;
652+
653+
sqlx::query!(
654+
" insert into mytable(f) select * from unnest($1::hstore[])",
655+
&stores
656+
)
657+
.execute(&mut conn)
658+
.await?;
659+
660+
Ok(())
661+
}

tests/postgres/setup.sql

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ CREATE EXTENSION IF NOT EXISTS cube;
77
-- https://www.postgresql.org/docs/current/citext.html
88
CREATE EXTENSION IF NOT EXISTS citext;
99

10+
-- https://www.postgresql.org/docs/current/hstore.html
11+
CREATE EXTENSION IF NOT EXISTS hstore;
12+
1013
-- https://www.postgresql.org/docs/current/sql-createtype.html
1114
CREATE TYPE status AS ENUM ('new', 'open', 'closed');
1215

@@ -58,3 +61,5 @@ CREATE TABLE test_citext (
5861
CREATE SCHEMA IF NOT EXISTS foo;
5962

6063
CREATE TYPE foo."Foo" as ENUM ('Bar', 'Baz');
64+
65+
CREATE TABLE mytable(f HSTORE);

0 commit comments

Comments
 (0)