Skip to content

Commit bd67e43

Browse files
committed
Add a Domain Kind
This could break downstream code that expects a domain type to explicitly be a Simple Kind, but I doubt that exists. cc #153
1 parent 2d4916e commit bd67e43

File tree

4 files changed

+62
-8
lines changed

4 files changed

+62
-8
lines changed

src/lib.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ impl InnerConnection {
462462
#[cfg_attr(rustfmt, rustfmt_skip)]
463463
fn setup_typeinfo_query(&mut self) -> result::Result<(), ConnectError> {
464464
match self.raw_prepare(TYPEINFO_QUERY,
465-
"SELECT t.typname, t.typelem, r.rngsubtype, n.nspname \
465+
"SELECT t.typname, t.typelem, r.rngsubtype, t.typbasetype, n.nspname \
466466
FROM pg_catalog.pg_type t \
467467
LEFT OUTER JOIN pg_catalog.pg_range r ON \
468468
r.rngtypid = t.oid \
@@ -478,7 +478,7 @@ impl InnerConnection {
478478
}
479479

480480
match self.raw_prepare(TYPEINFO_QUERY,
481-
"SELECT t.typname, t.typelem, NULL::OID, n.nspname \
481+
"SELECT t.typname, t.typelem, NULL::OID, t.typbasetype, n.nspname \
482482
FROM pg_catalog.pg_type t \
483483
INNER JOIN pg_catalog.pg_namespace n \
484484
ON t.typnamespace = n.oid \
@@ -749,7 +749,7 @@ impl InnerConnection {
749749
}
750750
_ => bad_response!(self),
751751
}
752-
let (name, elem_oid, rngsubtype, schema) = match try!(self.read_message()) {
752+
let (name, elem_oid, rngsubtype, basetype, schema) = match try!(self.read_message()) {
753753
DataRow { row } => {
754754
let ctx = SessionInfo::new(self);
755755
let name = try!(String::from_sql(&Type::Name,
@@ -762,10 +762,13 @@ impl InnerConnection {
762762
Some(ref data) => try!(Option::<Oid>::from_sql(&Type::Oid, &mut &**data, &ctx)),
763763
None => try!(Option::<Oid>::from_sql_null(&Type::Oid, &ctx)),
764764
};
765+
let basetype = try!(Oid::from_sql(&Type::Oid,
766+
&mut &**row[3].as_ref().unwrap(),
767+
&ctx));
765768
let schema = try!(String::from_sql(&Type::Name,
766-
&mut &**row[3].as_ref().unwrap(),
769+
&mut &**row[4].as_ref().unwrap(),
767770
&ctx));
768-
(name, elem_oid, rngsubtype, schema)
771+
(name, elem_oid, rngsubtype, basetype, schema)
769772
}
770773
ErrorResponse { fields } => {
771774
try!(self.wait_for_ready());
@@ -783,7 +786,9 @@ impl InnerConnection {
783786
}
784787
try!(self.wait_for_ready());
785788

786-
let kind = if elem_oid != 0 {
789+
let kind = if basetype != 0 {
790+
Kind::Domain(try!(self.get_type(basetype)))
791+
} else if elem_oid != 0 {
787792
Kind::Array(try!(self.get_type(elem_oid)))
788793
} else {
789794
match rngsubtype {

src/types/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ pub enum Kind {
107107
Array(Type),
108108
/// A range type along with the type of its elements.
109109
Range(Type),
110+
/// Domain type along with its underlying type.
111+
Domain(Type),
110112
#[doc(hidden)]
111113
__PseudoPrivateForExtensibility,
112114
}

tests/test.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#[macro_use]
12
extern crate postgres;
23
extern crate url;
34
#[cfg(feature = "openssl")]

tests/types/mod.rs

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ use std::collections::HashMap;
22
use std::f32;
33
use std::f64;
44
use std::fmt;
5+
use std::io::{Read, Write};
56

6-
use postgres::{Connection, SslMode};
7+
use postgres::{Connection, SslMode, Result};
78
use postgres::error::Error;
8-
use postgres::types::{ToSql, FromSql, Slice, WrongType};
9+
use postgres::types::{ToSql, FromSql, Slice, WrongType, Type, IsNull, Kind, SessionInfo};
910

1011
#[cfg(feature = "bit-vec")]
1112
mod bit_vec;
@@ -233,3 +234,48 @@ fn test_slice_range() {
233234
Err(e) => panic!("Unexpected error {:?}", e),
234235
};
235236
}
237+
238+
#[test]
239+
fn domain() {
240+
#[derive(Debug, PartialEq)]
241+
struct SessionId(Vec<u8>);
242+
243+
impl ToSql for SessionId {
244+
fn to_sql<W: ?Sized>(&self, ty: &Type, out: &mut W, ctx: &SessionInfo) -> Result<IsNull>
245+
where W: Write
246+
{
247+
let inner = match *ty.kind() {
248+
Kind::Domain(ref inner) => inner,
249+
_ => unreachable!(),
250+
};
251+
self.0.to_sql(inner, out, ctx)
252+
}
253+
254+
fn accepts(ty: &Type) -> bool {
255+
ty.name() == "session_id" && match *ty.kind() { Kind::Domain(_) => true, _ => false }
256+
}
257+
258+
to_sql_checked!();
259+
}
260+
261+
impl FromSql for SessionId {
262+
fn from_sql<R: Read>(ty: &Type, raw: &mut R, ctx: &SessionInfo) -> Result<Self> {
263+
Vec::<u8>::from_sql(ty, raw, ctx).map(SessionId)
264+
}
265+
266+
fn accepts(ty: &Type) -> bool {
267+
// This is super weird!
268+
<Vec<u8> as FromSql>::accepts(ty)
269+
}
270+
}
271+
272+
let conn = Connection::connect("postgres://postgres@localhost", SslMode::None).unwrap();
273+
conn.batch_execute("CREATE DOMAIN pg_temp.session_id AS bytea CHECK(octet_length(VALUE) = 16);
274+
CREATE TABLE pg_temp.foo (id pg_temp.session_id);")
275+
.unwrap();
276+
277+
let id = SessionId(b"0123456789abcdef".to_vec());
278+
conn.execute("INSERT INTO pg_temp.foo (id) VALUES ($1)", &[&id]).unwrap();
279+
let rows = conn.query("SELECT id FROM pg_temp.foo", &[]).unwrap();
280+
assert_eq!(id, rows.get(0).get(0));
281+
}

0 commit comments

Comments
 (0)