|
1 | 1 | //! Traits dealing with Postgres data types
|
2 | 2 | #![macro_escape]
|
3 | 3 |
|
| 4 | +#[cfg(feature = "uuid_type")] |
| 5 | +extern crate uuid; |
| 6 | + |
4 | 7 | use serialize::json;
|
5 | 8 | use std::collections::HashMap;
|
6 | 9 | use std::io::{AsRefReader, MemWriter, BufReader};
|
@@ -49,6 +52,8 @@ const TIMESTAMPOID: Oid = 1114;
|
49 | 52 | const TIMESTAMPARRAYOID: Oid = 1115;
|
50 | 53 | const TIMESTAMPZOID: Oid = 1184;
|
51 | 54 | const TIMESTAMPZARRAYOID: Oid = 1185;
|
| 55 | +const UUIDOID: Oid = 2950; |
| 56 | +const UUIDARRAYOID: Oid = 2951; |
52 | 57 | const INT4RANGEOID: Oid = 3904;
|
53 | 58 | const INT4RANGEARRAYOID: Oid = 3905;
|
54 | 59 | const TSRANGEOID: Oid = 3908;
|
@@ -175,6 +180,10 @@ make_postgres_type!(
|
175 | 180 | TIMESTAMPZOID => TimestampTZ,
|
176 | 181 | #[doc="TIMESTAMP WITH TIME ZONE[]"]
|
177 | 182 | TIMESTAMPZARRAYOID => TimestampTZArray member TimestampTZ,
|
| 183 | + #[doc="UUID"] |
| 184 | + UUIDOID => Uuid, |
| 185 | + #[doc="UUID[]"] |
| 186 | + UUIDARRAYOID => UuidArray member Uuid, |
178 | 187 | #[doc="CHAR(n)/CHARACTER(n)"]
|
179 | 188 | BPCHAROID => CharN,
|
180 | 189 | #[doc="VARCHAR/CHARACTER VARYING"]
|
@@ -269,6 +278,16 @@ impl RawFromSql for Timespec {
|
269 | 278 | }
|
270 | 279 | }
|
271 | 280 |
|
| 281 | +#[cfg(feature = "uuid_type")] |
| 282 | +impl RawFromSql for uuid::Uuid { |
| 283 | + fn raw_from_sql<R: Reader>(raw: &mut R) -> Result<uuid::Uuid> { |
| 284 | + match uuid::Uuid::from_bytes(try_pg!(raw.read_to_end())[]) { |
| 285 | + Some(u) => Ok(u), |
| 286 | + None => Err(PgBadData), |
| 287 | + } |
| 288 | + } |
| 289 | +} |
| 290 | + |
272 | 291 | macro_rules! from_range_impl(
|
273 | 292 | ($t:ty) => (
|
274 | 293 | impl RawFromSql for Range<$t> {
|
@@ -372,6 +391,8 @@ from_raw_from_impl!(Int4, i32)
|
372 | 391 | from_raw_from_impl!(Int8, i64)
|
373 | 392 | from_raw_from_impl!(Float4, f32)
|
374 | 393 | from_raw_from_impl!(Float8, f64)
|
| 394 | +#[cfg(feature = "uuid_type")] |
| 395 | +from_raw_from_impl!(Uuid, uuid::Uuid) |
375 | 396 | from_raw_from_impl!(Json, json::Json)
|
376 | 397 |
|
377 | 398 | from_raw_from_impl!(Timestamp | TimestampTZ, Timespec)
|
@@ -422,6 +443,8 @@ from_array_impl!(Int4Array, i32)
|
422 | 443 | from_array_impl!(TextArray | CharNArray | VarcharArray | NameArray, String)
|
423 | 444 | from_array_impl!(Int8Array, i64)
|
424 | 445 | from_array_impl!(TimestampArray | TimestampTZArray, Timespec)
|
| 446 | +#[cfg(feature = "uuid_type")] |
| 447 | +from_array_impl!(UuidArray, uuid::Uuid) |
425 | 448 | from_array_impl!(JsonArray, json::Json)
|
426 | 449 | from_array_impl!(Float4Array, f32)
|
427 | 450 | from_array_impl!(Float8Array, f64)
|
@@ -540,6 +563,13 @@ impl RawToSql for Timespec {
|
540 | 563 | }
|
541 | 564 | }
|
542 | 565 |
|
| 566 | +#[cfg(feature = "uuid_type")] |
| 567 | +impl RawToSql for uuid::Uuid { |
| 568 | + fn raw_to_sql<W: Writer>(&self, w: &mut W) -> Result<()> { |
| 569 | + Ok(try_pg!(w.write(self.as_bytes()))) |
| 570 | + } |
| 571 | +} |
| 572 | + |
543 | 573 | macro_rules! to_range_impl(
|
544 | 574 | ($t:ty) => (
|
545 | 575 | impl RawToSql for Range<$t> {
|
@@ -648,6 +678,8 @@ macro_rules! to_raw_to_impl(
|
648 | 678 | to_raw_to_impl!(Bool, bool)
|
649 | 679 | to_raw_to_impl!(ByteA, Vec<u8>)
|
650 | 680 | to_raw_to_impl!(Varchar | Text | CharN | Name, String)
|
| 681 | +#[cfg(feature = "uuid_type")] |
| 682 | +to_raw_to_impl!(Uuid, uuid::Uuid) |
651 | 683 | to_raw_to_impl!(Json, json::Json)
|
652 | 684 | to_raw_to_impl!(Char, i8)
|
653 | 685 | to_raw_to_impl!(Int2, i16)
|
@@ -729,6 +761,8 @@ to_array_impl!(Float8Array, f64)
|
729 | 761 | to_array_impl!(Int4RangeArray, Range<i32>)
|
730 | 762 | to_array_impl!(TsRangeArray | TstzRangeArray, Range<Timespec>)
|
731 | 763 | to_array_impl!(Int8RangeArray, Range<i64>)
|
| 764 | +#[cfg(feature = "uuid_type")] |
| 765 | +to_array_impl!(UuidArray, uuid::Uuid) |
732 | 766 | to_array_impl!(JsonArray, json::Json)
|
733 | 767 |
|
734 | 768 | impl ToSql for HashMap<String, Option<String>> {
|
|
0 commit comments