Skip to content

Commit f551a86

Browse files
committed
Expose containing schema for custom types
1 parent 3305bef commit f551a86

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

src/lib.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -455,10 +455,12 @@ impl InnerConnection {
455455

456456
fn setup_typeinfo_query(&mut self) -> result::Result<(), ConnectError> {
457457
match self.raw_prepare(TYPEINFO_QUERY,
458-
"SELECT t.typname, t.typelem, r.rngsubtype \
458+
"SELECT t.typname, t.typelem, r.rngsubtype, n.nspname \
459459
FROM pg_catalog.pg_type t \
460460
LEFT OUTER JOIN pg_catalog.pg_range r \
461461
ON r.rngtypid = t.oid \
462+
INNER JOIN pg_catalog.pg_namespace n \
463+
ON t.typnamespace = n.oid \
462464
WHERE t.oid = $1") {
463465
Ok(..) => return Ok(()),
464466
Err(Error::IoError(e)) => return Err(ConnectError::IoError(e)),
@@ -469,9 +471,11 @@ impl InnerConnection {
469471
}
470472

471473
match self.raw_prepare(TYPEINFO_QUERY,
472-
"SELECT typname, typelem, NULL::OID \
473-
FROM pg_catalog.pg_type \
474-
WHERE oid = $1") {
474+
"SELECT t.typname, t.typelem, NULL::OID, n.nspname \
475+
FROM pg_catalog.pg_type t \
476+
INNER JOIN pg_catalog.pg_namespace n \
477+
ON t.typnamespace = n.oid \
478+
WHERE t.oid = $1") {
475479
Ok(..) => Ok(()),
476480
Err(Error::IoError(e)) => Err(ConnectError::IoError(e)),
477481
Err(Error::DbError(e)) => Err(ConnectError::DbError(e)),
@@ -696,7 +700,7 @@ impl InnerConnection {
696700
}
697701
_ => bad_response!(self)
698702
}
699-
let (name, elem_oid, rngsubtype): (String, Oid, Option<Oid>) =
703+
let (name, elem_oid, rngsubtype, schema): (String, Oid, Option<Oid>, String) =
700704
match try!(self.read_message()) {
701705
DataRow { row } => {
702706
let ctx = SessionInfo::new(self);
@@ -708,6 +712,9 @@ impl InnerConnection {
708712
&ctx)),
709713
try!(FromSql::from_sql_nullable(&Type::Oid,
710714
row[2].as_ref().map(|r| &**r).as_mut(),
715+
&ctx)),
716+
try!(FromSql::from_sql_nullable(&Type::Name,
717+
row[3].as_ref().map(|r| &**r).as_mut(),
711718
&ctx)))
712719
}
713720
ErrorResponse { fields } => {
@@ -735,7 +742,7 @@ impl InnerConnection {
735742
}
736743
};
737744

738-
let type_ = Type::Other(Box::new(Other::new(name, oid, kind)));
745+
let type_ = Type::Other(Box::new(Other::new(name, oid, kind, schema)));
739746
self.unknown_types.insert(oid, type_.clone());
740747
Ok(type_)
741748
}
@@ -1337,7 +1344,7 @@ impl<'a> GenericConnection for Transaction<'a> {
13371344
}
13381345

13391346
trait OtherNew {
1340-
fn new(name: String, oid: Oid, kind: Kind) -> Other;
1347+
fn new(name: String, oid: Oid, kind: Kind, schema: String) -> Other;
13411348
}
13421349

13431350
trait DbErrorNew {

src/types/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,14 +476,16 @@ pub struct Other {
476476
name: String,
477477
oid: Oid,
478478
kind: Kind,
479+
schema: String,
479480
}
480481

481482
impl OtherNew for Other {
482-
fn new(name: String, oid: Oid, kind: Kind) -> Other {
483+
fn new(name: String, oid: Oid, kind: Kind, schema: String) -> Other {
483484
Other {
484485
name: name,
485486
oid: oid,
486487
kind: kind,
488+
schema: schema,
487489
}
488490
}
489491
}
@@ -503,6 +505,11 @@ impl Other {
503505
pub fn kind(&self) -> &Kind {
504506
&self.kind
505507
}
508+
509+
/// The schema of this type.
510+
pub fn schema(&self) -> &str {
511+
&self.schema
512+
}
506513
}
507514

508515
/// An error indicating that a `NULL` Postgres value was passed to a `FromSql`

tests/test.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,7 @@ fn test_custom_range_element_type() {
812812
&Type::Other(ref u) => {
813813
assert_eq!("floatrange", u.name());
814814
assert_eq!(&Kind::Range(Type::Float8), u.kind());
815+
assert_eq!("public", u.schema());
815816
}
816817
t => panic!("Unexpected type {:?}", t)
817818
}

0 commit comments

Comments
 (0)