Skip to content

Commit 4f80c32

Browse files
committed
Add optional UUID support
Closes #72
1 parent fb0fbdd commit 4f80c32

File tree

4 files changed

+71
-2
lines changed

4 files changed

+71
-2
lines changed

.travis.yml

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@ language: rust
22
env:
33
global:
44
- secure: Dl8GH7HCtwf0iG0EiBlQprZ5wQ7Wz8lfsMGxCSfTCjWoCbYK5+B5xuPnSaj8UYbrbXHmaZms23kSc5ZgQixuKs3zMCRDuEX1safT+t3AlhY/5/T3uyUWd1bv2Brw4TZg7M0Dubsu+3+3jNcDYv6vp+YWUNlKUcxjcX4EPrY0FKI=
5+
matrix:
6+
- FEATURES=""
7+
- FEATURES="uuid_type"
58
addons:
69
postgresql: 9.3
710
before_script:
811
- ./.travis/setup.sh
912
script:
10-
- cargo test
11-
- cargo doc --no-deps
13+
- cargo test --features "$FEATURES"
14+
- cargo doc --no-deps --features "uuid_type"
1215
after_script:
1316
- mv target/doc .
1417
- curl http://www.rust-ci.org/artifacts/put?t=$RUSTCI_TOKEN | sh

Cargo.toml

+8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ test = false
1212
name = "test"
1313
path = "tests/test.rs"
1414

15+
[features]
16+
uuid_type = ["uuid"]
17+
1518
[dependencies.openssl]
1619
git = "https://github.com/sfackler/rust-openssl"
1720

@@ -21,5 +24,10 @@ git = "https://github.com/sfackler/rust-phf"
2124
[dependencies.phf_mac]
2225
git = "https://github.com/sfackler/rust-phf"
2326

27+
[dependencies.uuid]
28+
git = "https://github.com/rust-lang/uuid"
29+
optional = true
30+
2431
[dev-dependencies.url]
2532
git = "https://github.com/servo/rust-url"
33+

src/types/mod.rs

+34
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
//! Traits dealing with Postgres data types
22
#![macro_escape]
33

4+
#[cfg(feature = "uuid_type")]
5+
extern crate uuid;
6+
47
use serialize::json;
58
use std::collections::HashMap;
69
use std::io::{AsRefReader, MemWriter, BufReader};
@@ -49,6 +52,8 @@ const TIMESTAMPOID: Oid = 1114;
4952
const TIMESTAMPARRAYOID: Oid = 1115;
5053
const TIMESTAMPZOID: Oid = 1184;
5154
const TIMESTAMPZARRAYOID: Oid = 1185;
55+
const UUIDOID: Oid = 2950;
56+
const UUIDARRAYOID: Oid = 2951;
5257
const INT4RANGEOID: Oid = 3904;
5358
const INT4RANGEARRAYOID: Oid = 3905;
5459
const TSRANGEOID: Oid = 3908;
@@ -175,6 +180,10 @@ make_postgres_type!(
175180
TIMESTAMPZOID => TimestampTZ,
176181
#[doc="TIMESTAMP WITH TIME ZONE[]"]
177182
TIMESTAMPZARRAYOID => TimestampTZArray member TimestampTZ,
183+
#[doc="UUID"]
184+
UUIDOID => Uuid,
185+
#[doc="UUID[]"]
186+
UUIDARRAYOID => UuidArray member Uuid,
178187
#[doc="CHAR(n)/CHARACTER(n)"]
179188
BPCHAROID => CharN,
180189
#[doc="VARCHAR/CHARACTER VARYING"]
@@ -269,6 +278,16 @@ impl RawFromSql for Timespec {
269278
}
270279
}
271280

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+
272291
macro_rules! from_range_impl(
273292
($t:ty) => (
274293
impl RawFromSql for Range<$t> {
@@ -372,6 +391,8 @@ from_raw_from_impl!(Int4, i32)
372391
from_raw_from_impl!(Int8, i64)
373392
from_raw_from_impl!(Float4, f32)
374393
from_raw_from_impl!(Float8, f64)
394+
#[cfg(feature = "uuid_type")]
395+
from_raw_from_impl!(Uuid, uuid::Uuid)
375396
from_raw_from_impl!(Json, json::Json)
376397

377398
from_raw_from_impl!(Timestamp | TimestampTZ, Timespec)
@@ -422,6 +443,8 @@ from_array_impl!(Int4Array, i32)
422443
from_array_impl!(TextArray | CharNArray | VarcharArray | NameArray, String)
423444
from_array_impl!(Int8Array, i64)
424445
from_array_impl!(TimestampArray | TimestampTZArray, Timespec)
446+
#[cfg(feature = "uuid_type")]
447+
from_array_impl!(UuidArray, uuid::Uuid)
425448
from_array_impl!(JsonArray, json::Json)
426449
from_array_impl!(Float4Array, f32)
427450
from_array_impl!(Float8Array, f64)
@@ -540,6 +563,13 @@ impl RawToSql for Timespec {
540563
}
541564
}
542565

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+
543573
macro_rules! to_range_impl(
544574
($t:ty) => (
545575
impl RawToSql for Range<$t> {
@@ -648,6 +678,8 @@ macro_rules! to_raw_to_impl(
648678
to_raw_to_impl!(Bool, bool)
649679
to_raw_to_impl!(ByteA, Vec<u8>)
650680
to_raw_to_impl!(Varchar | Text | CharN | Name, String)
681+
#[cfg(feature = "uuid_type")]
682+
to_raw_to_impl!(Uuid, uuid::Uuid)
651683
to_raw_to_impl!(Json, json::Json)
652684
to_raw_to_impl!(Char, i8)
653685
to_raw_to_impl!(Int2, i16)
@@ -729,6 +761,8 @@ to_array_impl!(Float8Array, f64)
729761
to_array_impl!(Int4RangeArray, Range<i32>)
730762
to_array_impl!(TsRangeArray | TstzRangeArray, Range<Timespec>)
731763
to_array_impl!(Int8RangeArray, Range<i64>)
764+
#[cfg(feature = "uuid_type")]
765+
to_array_impl!(UuidArray, uuid::Uuid)
732766
to_array_impl!(JsonArray, json::Json)
733767

734768
impl ToSql for HashMap<String, Option<String>> {

tests/types/mod.rs

+24
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#[cfg(feature = "uuid_type")]
2+
extern crate uuid;
3+
14
use serialize::json;
25
use std::collections::HashMap;
36
use std::f32;
@@ -122,6 +125,15 @@ fn test_json_params() {
122125
(None, "NULL")])
123126
}
124127

128+
#[test]
129+
#[cfg(feature = "uuid_type")]
130+
fn test_uuid_params() {
131+
test_type("UUID", [(Some(uuid::Uuid::parse_str("a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11").unwrap()),
132+
"'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11'"),
133+
(None, "NULL")])
134+
}
135+
136+
125137
#[test]
126138
fn test_tm_params() {
127139
fn make_check<'a>(time: &'a str) -> (Option<Timespec>, &'a str) {
@@ -290,6 +302,18 @@ fn test_int4rangearray_params() {
290302
range!('(', 10i32 ')'), "\"(,10)\"");
291303
}
292304

305+
#[test]
306+
#[cfg(feature = "uuid_type")]
307+
fn test_uuidarray_params() {
308+
fn make_check<'a>(uuid: &'a str) -> (uuid::Uuid, &'a str) {
309+
(uuid::Uuid::parse_str(uuid).unwrap(), uuid)
310+
}
311+
let (v1, s1) = make_check("a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11");
312+
let (v2, s2) = make_check("00000000-0000-0000-0000-000000000000");
313+
let (v3, s3) = make_check("a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11");
314+
test_array_params!("UUID", v1, s1, v2, s2, v3, s3);
315+
}
316+
293317
#[test]
294318
fn test_tsrangearray_params() {
295319
fn make_check<'a>(time: &'a str) -> (Timespec, &'a str) {

0 commit comments

Comments
 (0)