From 890c7ac6ad27168e4397f26a9ec59d250a925f6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20S=C3=A9verin?= Date: Tue, 19 Nov 2024 00:08:52 +0000 Subject: [PATCH 1/3] Added missing special casing for encoding arrays of custom types --- sqlx-postgres/src/types/record.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sqlx-postgres/src/types/record.rs b/sqlx-postgres/src/types/record.rs index c4eb639368..6e37182c40 100644 --- a/sqlx-postgres/src/types/record.rs +++ b/sqlx-postgres/src/types/record.rs @@ -41,13 +41,13 @@ impl<'a> PgRecordEncoder<'a> { { let ty = value.produces().unwrap_or_else(T::type_info); - if let PgType::DeclareWithName(name) = ty.0 { + match ty.0 { // push a hole for this type ID // to be filled in on query execution - self.buf.patch_type_by_name(&name); - } else { + PgType::DeclareWithName(name) => self.buf.patch_type_by_name(&name), + PgType::DeclareArrayOf(array) => self.buf.patch_array_type(array), // write type id - self.buf.extend(&ty.0.oid().0.to_be_bytes()); + pg_type => self.buf.extend(&pg_type.oid().0.to_be_bytes()), } self.buf.encode(value)?; From 53d30f0da3a58403298ee2fcdb2aeee777e4443c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20S=C3=A9verin?= Date: Tue, 19 Nov 2024 00:57:03 +0000 Subject: [PATCH 2/3] Added the matching test --- tests/postgres/derives.rs | 66 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/tests/postgres/derives.rs b/tests/postgres/derives.rs index dada74fe4d..cd09f6f14c 100644 --- a/tests/postgres/derives.rs +++ b/tests/postgres/derives.rs @@ -810,3 +810,69 @@ async fn test_custom_pg_array() -> anyhow::Result<()> { } Ok(()) } + +#[sqlx_macros::test] +async fn test_record_array_type() -> anyhow::Result<()> { + let mut conn = new::().await?; + + conn.execute( + r#" +DROP TABLE IF EXISTS responses; + +DROP TYPE IF EXISTS http_response CASCADE; +DROP TYPE IF EXISTS header_pair CASCADE; + +CREATE TYPE header_pair AS ( + name TEXT, + value TEXT +); + +CREATE TYPE http_response AS ( + headers header_pair[] +); + +CREATE TABLE responses ( + response http_response NOT NULL +); + "#, + ) + .await?; + + #[derive(Debug, sqlx::Type)] + #[sqlx(type_name = "http_response")] + struct HttpResponseRecord { + headers: Vec, + } + + #[derive(Debug, sqlx::Type)] + #[sqlx(type_name = "header_pair")] + struct HeaderPairRecord { + name: String, + value: String, + } + + let value = HttpResponseRecord { + headers: vec![ + HeaderPairRecord { + name: "Content-Type".to_owned(), + value: "text/html; charset=utf-8".to_owned() + }, + HeaderPairRecord { + name: "Cache-Control".to_owned(), + value: "max-age=0".to_owned() + } + ], + }; + + sqlx::query( + " +INSERT INTO responses (response) +VALUES ($1) + ", + ) + .bind(&value) + .execute(&mut conn) + .await?; + + Ok(()) +} From 91140f3ec6cf0e0493719def54e2806e4f5af1fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20S=C3=A9verin?= Date: Sun, 26 Jan 2025 11:55:59 +0000 Subject: [PATCH 3/3] Formatting --- tests/postgres/derives.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/postgres/derives.rs b/tests/postgres/derives.rs index cd09f6f14c..13f9bf1d5d 100644 --- a/tests/postgres/derives.rs +++ b/tests/postgres/derives.rs @@ -855,12 +855,12 @@ CREATE TABLE responses ( headers: vec![ HeaderPairRecord { name: "Content-Type".to_owned(), - value: "text/html; charset=utf-8".to_owned() + value: "text/html; charset=utf-8".to_owned(), }, HeaderPairRecord { name: "Cache-Control".to_owned(), - value: "max-age=0".to_owned() - } + value: "max-age=0".to_owned(), + }, ], };