Support UUID[]

This commit is contained in:
Steven Fackler 2013-12-08 14:16:37 -08:00
parent 8753cee219
commit 5471b7d6ae
3 changed files with 38 additions and 13 deletions

View File

@ -298,6 +298,10 @@ types. The driver currently supports the following conversions:
<td>types::array::ArrayBase&lt;Option&lt;f64&gt;&gt;</td>
<td>FLOAT8[], FLOAT8[][], ...</td>
</tr>
<tr>
<td>types::array::ArrayBase&lt;Option&lt;extra::uuid::Uuid&gt;&gt;</td>
<td>UUID[], UUID[][], ...</td>
</tr>
<tr>
<td>std::hashmap::HashMap&lt;~str, Option&lt;~str&gt;&gt;</td>
<td>HSTORE</td>

11
test.rs
View File

@ -502,6 +502,17 @@ fn test_float8array_params() {
test_array_params!("FLOAT8", 0f64, "0", 1.5f64, "1.5", 0.009f64, ".009");
}
#[test]
fn test_uuidarray_params() {
fn make_check<'a>(uuid: &'a str) -> (Uuid, &'a str) {
(Uuid::parse_string(uuid).unwrap(), uuid)
}
let (v1, s1) = make_check("a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11");
let (v2, s2) = make_check("00000000-0000-0000-0000-000000000000");
let (v3, s3) = make_check("a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11");
test_array_params!("UUID", v1, s1, v2, s2, v3, s3);
}
#[test]
fn test_hstore_params() {
macro_rules! make_map(

View File

@ -51,6 +51,7 @@ static TIMESTAMPARRAYOID: Oid = 1115;
static TIMESTAMPZOID: Oid = 1184;
static TIMESTAMPZARRAYOID: Oid = 1185;
static UUIDOID: Oid = 2950;
static UUIDARRAYOID: Oid = 2951;
static INT4RANGEOID: Oid = 3904;
static TSRANGEOID: Oid = 3908;
static TSTZRANGEOID: Oid = 3910;
@ -183,6 +184,8 @@ make_postgres_type!(
VARCHAROID => PgVarchar,
#[doc="UUID"]
UUIDOID => PgUuid,
#[doc="UUID[]"]
UUIDARRAYOID => PgUuidArray member PgUuid,
#[doc="INT4RANGE"]
INT4RANGEOID => PgInt4Range,
#[doc="INT8RANGE"]
@ -276,6 +279,12 @@ impl RawFromSql for Timespec {
}
}
impl RawFromSql for Uuid {
fn raw_from_sql<R: Reader>(len: uint, raw: &mut R) -> Uuid {
Uuid::from_bytes(raw.read_bytes(len)).unwrap()
}
}
macro_rules! from_map_impl(
($($expected:pat)|+, $t:ty, $blk:expr) => (
impl FromSql for Option<$t> {
@ -313,15 +322,12 @@ from_raw_from_impl!(PgInt4, i32)
from_raw_from_impl!(PgInt8, i64)
from_raw_from_impl!(PgFloat4, f32)
from_raw_from_impl!(PgFloat8, f64)
from_raw_from_impl!(PgUuid, Uuid)
from_map_impl!(PgJson, Json, |buf| {
json::from_str(str::from_utf8(buf.as_slice())).unwrap()
})
from_map_impl!(PgUuid, Uuid, |buf| {
Uuid::from_bytes(buf.as_slice()).unwrap()
})
from_raw_from_impl!(PgTimestamp | PgTimestampTZ, Timespec)
macro_rules! from_range_impl(
@ -412,6 +418,7 @@ from_array_impl!(PgInt8Array, i64)
from_array_impl!(PgTimestampArray | PgTimestampTZArray, Timespec)
from_array_impl!(PgFloat4Array, f32)
from_array_impl!(PgFloat8Array, f64)
from_array_impl!(PgUuidArray, Uuid)
from_map_impl!(PgUnknownType { name: ~"hstore", .. },
HashMap<~str, Option<~str>>, |buf| {
@ -518,6 +525,16 @@ impl RawToSql for Timespec {
}
}
impl RawToSql for Uuid {
fn raw_to_sql<W: Writer>(&self, w: &mut W) {
w.write(self.to_bytes())
}
fn raw_size(&self) -> uint {
self.to_bytes().len()
}
}
macro_rules! to_option_impl(
($($oid:pat)|+, $t:ty) => (
impl ToSql for Option<$t> {
@ -601,16 +618,8 @@ impl ToSql for Json {
to_option_impl!(PgJson, Json)
impl ToSql for Uuid {
fn to_sql(&self, ty: &PostgresType) -> (Format, Option<~[u8]>) {
check_types!(PgUuid, ty)
(Binary, Some(self.to_bytes().to_owned()))
}
}
to_option_impl!(PgUuid, Uuid)
to_raw_to_impl!(PgTimestamp | PgTimestampTZ, Timespec)
to_raw_to_impl!(PgUuid, Uuid)
macro_rules! to_range_impl(
($($oid:ident)|+, $t:ty) => (
@ -710,6 +719,7 @@ to_array_impl!(PgInt8Array, i64)
to_array_impl!(PgTimestampArray | PgTimestampTZArray, Timespec)
to_array_impl!(PgFloat4Array, f32)
to_array_impl!(PgFloat8Array, f64)
to_array_impl!(PgUuidArray, Uuid)
impl<'self> ToSql for HashMap<~str, Option<~str>> {
fn to_sql(&self, ty: &PostgresType) -> (Format, Option<~[u8]>) {