Skip to content

Commit 5aa8c2f

Browse files
committed
feat(Postgres): support nested domain types
1 parent c3fd645 commit 5aa8c2f

File tree

4 files changed

+81
-22
lines changed

4 files changed

+81
-22
lines changed

sqlx-postgres/src/type_info.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -1013,7 +1013,7 @@ impl PgType {
10131013
/// If `soft_eq` is true and `self` or `other` is `DeclareWithOid` but not both, return `true`
10141014
/// before checking names.
10151015
fn eq_impl(&self, other: &Self, soft_eq: bool) -> bool {
1016-
if let (Some(a), Some(b)) = (self.try_oid(), other.try_oid()) {
1016+
if let (Some(a), Some(b)) = (self.base_oid(), other.base_oid()) {
10171017
// If there are OIDs available, use OIDs to perform a direct match
10181018
return a == b;
10191019
}
@@ -1035,6 +1035,17 @@ impl PgType {
10351035
// Otherwise, perform a match on the name
10361036
name_eq(self.name(), other.name())
10371037
}
1038+
1039+
// Returns the OID of the type, returns the OID of the base_type for Domain types
1040+
fn base_oid(&self) -> Option<Oid> {
1041+
match self {
1042+
PgType::Custom(custom) => match &custom.kind {
1043+
PgTypeKind::Domain(domain) => domain.base_oid(),
1044+
_ => Some(custom.oid),
1045+
},
1046+
ty => ty.try_oid(),
1047+
}
1048+
}
10381049
}
10391050

10401051
impl TypeInfo for PgTypeInfo {

sqlx-postgres/src/types/record.rs

+26-21
Original file line numberDiff line numberDiff line change
@@ -103,27 +103,7 @@ impl<'r> PgRecordDecoder<'r> {
103103
match self.fmt {
104104
PgValueFormat::Binary => {
105105
let element_type_oid = Oid(self.buf.get_u32());
106-
let element_type_opt = match self.typ.0.kind() {
107-
PgTypeKind::Simple if self.typ.0 == PgType::Record => {
108-
PgTypeInfo::try_from_oid(element_type_oid)
109-
}
110-
111-
PgTypeKind::Composite(fields) => {
112-
let ty = fields[self.ind].1.clone();
113-
if ty.0.oid() != element_type_oid {
114-
return Err("unexpected mismatch of composite type information".into());
115-
}
116-
117-
Some(ty)
118-
}
119-
120-
_ => {
121-
return Err(
122-
"unexpected non-composite type being decoded as a composite type"
123-
.into(),
124-
);
125-
}
126-
};
106+
let element_type_opt = self.find_type_info(&self.typ, element_type_oid)?;
127107

128108
if let Some(ty) = &element_type_opt {
129109
if !ty.is_null() && !T::compatible(ty) {
@@ -202,4 +182,29 @@ impl<'r> PgRecordDecoder<'r> {
202182
}
203183
}
204184
}
185+
fn find_type_info(
186+
&self,
187+
typ: &PgTypeInfo,
188+
oid: Oid,
189+
) -> Result<Option<PgTypeInfo>, BoxDynError> {
190+
match typ.kind() {
191+
PgTypeKind::Simple if typ.0 == PgType::Record => Ok(PgTypeInfo::try_from_oid(oid)),
192+
193+
PgTypeKind::Composite(fields) => {
194+
let ty = fields[self.ind].1.clone();
195+
if ty.0.oid() != oid {
196+
return Err("unexpected mismatch of composite type information".into());
197+
}
198+
199+
Ok(Some(ty))
200+
}
201+
PgTypeKind::Domain(d) => self.find_type_info(d, oid),
202+
203+
_ => {
204+
return Err(
205+
"unexpected non-composite type being decoded as a composite type".into(),
206+
);
207+
}
208+
}
209+
}
205210
}

tests/postgres/setup.sql

+13
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,16 @@ CREATE SCHEMA IF NOT EXISTS foo;
6363
CREATE TYPE foo."Foo" as ENUM ('Bar', 'Baz');
6464

6565
CREATE TABLE mytable(f HSTORE);
66+
67+
CREATE DOMAIN positive_int AS integer CHECK (VALUE >= 0);
68+
CREATE DOMAIN percentage AS positive_int CHECK (VALUE < 100);
69+
70+
CREATE TYPE person as (
71+
id int,
72+
age positive_int,
73+
percent percentage
74+
);
75+
76+
CREATE TYPE leaf_composite AS (prim integer);
77+
CREATE DOMAIN domain AS leaf_composite;
78+
CREATE TYPE root_composite AS (domain domain);

tests/postgres/types.rs

+30
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,36 @@ test_type!(ltree_vec<Vec<sqlx::postgres::types::PgLTree>>(Postgres,
635635
sqlx::postgres::types::PgLTree::try_from_iter(["Alpha", "Beta", "Delta", "Gamma"]).unwrap()
636636
]
637637
));
638+
#[derive(sqlx::Type, Debug, PartialEq)]
639+
struct Person {
640+
id: i32,
641+
age: i32,
642+
percent: i32,
643+
}
644+
645+
test_type!(nested_domain_types<Person>(Postgres,
646+
"ROW(1, 21::positive_int, 50::percentage)::person" == Person { id: 1, age: 21, percent: 50 })
647+
);
648+
649+
#[derive(sqlx::Type, Debug, PartialEq)]
650+
#[sqlx(type_name = "leaf_composite")]
651+
struct LeafComposite {
652+
prim: i32,
653+
}
654+
655+
#[derive(sqlx::Type, Debug, PartialEq)]
656+
#[sqlx(type_name = "domain")]
657+
struct Domain(LeafComposite);
658+
659+
#[derive(sqlx::Type, Debug, PartialEq)]
660+
#[sqlx(type_name = "root_composite")]
661+
struct RootComposite {
662+
domain: Domain,
663+
}
664+
665+
test_type!(nested_domain_types_2<RootComposite>(Postgres,
666+
"ROW(ROW(1))::root_composite" == RootComposite { domain: Domain(LeafComposite { prim: 1})})
667+
);
638668

639669
#[sqlx_macros::test]
640670
async fn test_text_adapter() -> anyhow::Result<()> {

0 commit comments

Comments
 (0)