Skip to content

Commit 5471b7d

Browse files
committed
Support UUID[]
1 parent 8753cee commit 5471b7d

3 files changed

Lines changed: 38 additions & 13 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,10 @@ types. The driver currently supports the following conversions:
298298
<td>types::array::ArrayBase&lt;Option&lt;f64&gt;&gt;</td>
299299
<td>FLOAT8[], FLOAT8[][], ...</td>
300300
</tr>
301+
<tr>
302+
<td>types::array::ArrayBase&lt;Option&lt;extra::uuid::Uuid&gt;&gt;</td>
303+
<td>UUID[], UUID[][], ...</td>
304+
</tr>
301305
<tr>
302306
<td>std::hashmap::HashMap&lt;~str, Option&lt;~str&gt;&gt;</td>
303307
<td>HSTORE</td>

test.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,17 @@ fn test_float8array_params() {
502502
test_array_params!("FLOAT8", 0f64, "0", 1.5f64, "1.5", 0.009f64, ".009");
503503
}
504504
505+
#[test]
506+
fn test_uuidarray_params() {
507+
fn make_check<'a>(uuid: &'a str) -> (Uuid, &'a str) {
508+
(Uuid::parse_string(uuid).unwrap(), uuid)
509+
}
510+
let (v1, s1) = make_check("a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11");
511+
let (v2, s2) = make_check("00000000-0000-0000-0000-000000000000");
512+
let (v3, s3) = make_check("a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11");
513+
test_array_params!("UUID", v1, s1, v2, s2, v3, s3);
514+
}
515+
505516
#[test]
506517
fn test_hstore_params() {
507518
macro_rules! make_map(

types/mod.rs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ static TIMESTAMPARRAYOID: Oid = 1115;
5151
static TIMESTAMPZOID: Oid = 1184;
5252
static TIMESTAMPZARRAYOID: Oid = 1185;
5353
static UUIDOID: Oid = 2950;
54+
static UUIDARRAYOID: Oid = 2951;
5455
static INT4RANGEOID: Oid = 3904;
5556
static TSRANGEOID: Oid = 3908;
5657
static TSTZRANGEOID: Oid = 3910;
@@ -183,6 +184,8 @@ make_postgres_type!(
183184
VARCHAROID => PgVarchar,
184185
#[doc="UUID"]
185186
UUIDOID => PgUuid,
187+
#[doc="UUID[]"]
188+
UUIDARRAYOID => PgUuidArray member PgUuid,
186189
#[doc="INT4RANGE"]
187190
INT4RANGEOID => PgInt4Range,
188191
#[doc="INT8RANGE"]
@@ -276,6 +279,12 @@ impl RawFromSql for Timespec {
276279
}
277280
}
278281

282+
impl RawFromSql for Uuid {
283+
fn raw_from_sql<R: Reader>(len: uint, raw: &mut R) -> Uuid {
284+
Uuid::from_bytes(raw.read_bytes(len)).unwrap()
285+
}
286+
}
287+
279288
macro_rules! from_map_impl(
280289
($($expected:pat)|+, $t:ty, $blk:expr) => (
281290
impl FromSql for Option<$t> {
@@ -313,15 +322,12 @@ from_raw_from_impl!(PgInt4, i32)
313322
from_raw_from_impl!(PgInt8, i64)
314323
from_raw_from_impl!(PgFloat4, f32)
315324
from_raw_from_impl!(PgFloat8, f64)
325+
from_raw_from_impl!(PgUuid, Uuid)
316326

317327
from_map_impl!(PgJson, Json, |buf| {
318328
json::from_str(str::from_utf8(buf.as_slice())).unwrap()
319329
})
320330

321-
from_map_impl!(PgUuid, Uuid, |buf| {
322-
Uuid::from_bytes(buf.as_slice()).unwrap()
323-
})
324-
325331
from_raw_from_impl!(PgTimestamp | PgTimestampTZ, Timespec)
326332

327333
macro_rules! from_range_impl(
@@ -412,6 +418,7 @@ from_array_impl!(PgInt8Array, i64)
412418
from_array_impl!(PgTimestampArray | PgTimestampTZArray, Timespec)
413419
from_array_impl!(PgFloat4Array, f32)
414420
from_array_impl!(PgFloat8Array, f64)
421+
from_array_impl!(PgUuidArray, Uuid)
415422

416423
from_map_impl!(PgUnknownType { name: ~"hstore", .. },
417424
HashMap<~str, Option<~str>>, |buf| {
@@ -518,6 +525,16 @@ impl RawToSql for Timespec {
518525
}
519526
}
520527
528+
impl RawToSql for Uuid {
529+
fn raw_to_sql<W: Writer>(&self, w: &mut W) {
530+
w.write(self.to_bytes())
531+
}
532+
533+
fn raw_size(&self) -> uint {
534+
self.to_bytes().len()
535+
}
536+
}
537+
521538
macro_rules! to_option_impl(
522539
($($oid:pat)|+, $t:ty) => (
523540
impl ToSql for Option<$t> {
@@ -601,16 +618,8 @@ impl ToSql for Json {
601618
602619
to_option_impl!(PgJson, Json)
603620
604-
impl ToSql for Uuid {
605-
fn to_sql(&self, ty: &PostgresType) -> (Format, Option<~[u8]>) {
606-
check_types!(PgUuid, ty)
607-
(Binary, Some(self.to_bytes().to_owned()))
608-
}
609-
}
610-
611-
to_option_impl!(PgUuid, Uuid)
612-
613621
to_raw_to_impl!(PgTimestamp | PgTimestampTZ, Timespec)
622+
to_raw_to_impl!(PgUuid, Uuid)
614623
615624
macro_rules! to_range_impl(
616625
($($oid:ident)|+, $t:ty) => (
@@ -710,6 +719,7 @@ to_array_impl!(PgInt8Array, i64)
710719
to_array_impl!(PgTimestampArray | PgTimestampTZArray, Timespec)
711720
to_array_impl!(PgFloat4Array, f32)
712721
to_array_impl!(PgFloat8Array, f64)
722+
to_array_impl!(PgUuidArray, Uuid)
713723
714724
impl<'self> ToSql for HashMap<~str, Option<~str>> {
715725
fn to_sql(&self, ty: &PostgresType) -> (Format, Option<~[u8]>) {

0 commit comments

Comments
 (0)