From 1dac0b5c291c9a9fa97a28fa0c6f5bb01aa21650 Mon Sep 17 00:00:00 2001 From: Ryan Leckey Date: Wed, 10 Jun 2020 04:21:09 -0700 Subject: [PATCH] fix(postgres): invert the type description logic to allow all "simple" type categories closes #379 --- sqlx-core/src/postgres/connection/describe.rs | 37 ++++++++++++++++--- sqlx-core/src/postgres/connection/executor.rs | 21 ----------- 2 files changed, 31 insertions(+), 27 deletions(-) diff --git a/sqlx-core/src/postgres/connection/describe.rs b/sqlx-core/src/postgres/connection/describe.rs index 0ab74756..4700df40 100644 --- a/sqlx-core/src/postgres/connection/describe.rs +++ b/sqlx-core/src/postgres/connection/describe.rs @@ -134,16 +134,21 @@ impl PgConnection { .await?; match category as u8 { - b'S' => Ok(PgTypeInfo(PgType::Custom(Arc::new(PgCustomType { + b'A' => Err(err_protocol!("user-defined array types are unsupported")), + + b'P' => Err(err_protocol!("pseudo types are unsupported")), + + b'R' => Err(err_protocol!("user-defined range types are unsupported")), + + b'E' => self.fetch_enum_by_oid(oid, name).await, + + b'C' => self.fetch_composite_by_oid(oid, relation_id, name).await, + + _ => Ok(PgTypeInfo(PgType::Custom(Arc::new(PgCustomType { kind: PgTypeKind::Simple, name: name.into(), oid, })))), - - b'E' => self.fetch_enum_by_oid(oid, name).await, - b'C' => self.fetch_composite_by_oid(oid, relation_id, name).await, - - c => Err(err_protocol!("unknown type category: {:?}", c as char)), } } @@ -204,6 +209,26 @@ ORDER BY attnum }) } + pub(crate) async fn fetch_type_id_by_name(&mut self, name: &str) -> Result { + if let Some(oid) = self.cache_type_oid.get(name) { + return Ok(*oid); + } + + // language=SQL + let (oid,): (u32,) = query_as( + " +SELECT oid FROM pg_catalog.pg_type WHERE typname ILIKE $1 + ", + ) + .bind(name) + .fetch_one(&mut *self) + .await?; + + self.cache_type_oid.insert(name.to_string().into(), oid); + + Ok(oid) + } + pub(crate) async fn map_result_columns( &mut self, columns: &[PgColumn], diff --git a/sqlx-core/src/postgres/connection/executor.rs b/sqlx-core/src/postgres/connection/executor.rs index 7fe944e7..2c1b5d85 100644 --- a/sqlx-core/src/postgres/connection/executor.rs +++ b/sqlx-core/src/postgres/connection/executor.rs @@ -14,7 +14,6 @@ use crate::postgres::message::{ }; use crate::postgres::type_info::PgType; use crate::postgres::{PgArguments, PgConnection, PgRow, PgValueFormat, Postgres}; -use crate::query_as::query_as; async fn prepare( conn: &mut PgConnection, @@ -95,26 +94,6 @@ impl PgConnection { Ok(statement) } - pub(crate) async fn fetch_type_id_by_name(&mut self, name: &str) -> Result { - if let Some(oid) = self.cache_type_oid.get(name) { - return Ok(*oid); - } - - // language=SQL - let (oid,): (u32,) = query_as( - " -SELECT oid FROM pg_catalog.pg_type WHERE typname ILIKE $1 - ", - ) - .bind(name) - .fetch_one(&mut *self) - .await?; - - self.cache_type_oid.insert(name.to_string().into(), oid); - - Ok(oid) - } - async fn run( &mut self, query: &str,