Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for UUIDs #50

Merged
merged 3 commits into from
Jun 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]

name = "mysql"
version = "5.0.0"
version = "5.1.0"
authors = ["blackbeam"]
description = "Mysql client library implemented in rust"
license = "MIT"
Expand Down Expand Up @@ -72,3 +72,7 @@ optional = true
[dependencies.named_pipe]
version = "~0.2"
optional = true

[dependencies.uuid]
version = "~0.2"
optional = true
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,16 @@ default-features = false
features = ["pipe"]
```

#### Optional features
You can compile rust-mysql-simple with the `uuid` feature, which makes it possible to use UUIDs with MySQL conveniently.

The `uuid` feature depends on the `uuid` crate and UUIDs are assumed to be binary encoded in MySQL. So make sure that your MySQL fields are binary(16).

To activate the `uuid` feature, add it to the `features` list:

```toml
[dependencies]
mysql = { version = "*", features = ["uuid"] }
```

[Simple example](http://blackbeam.org/doc/mysql/index.html#example)
15 changes: 15 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@
//! features = ["pipe"]
//! ```
//!
//! #### Optional features
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please also copy-paste this section into README.md?

//!
//! You can compile rust-mysql-simple with the `uuid` feature, which makes it possible to use UUIDs with MySQL conveniently.
//!
//! The `uuid` feature depends on the `uuid` crate and UUIDs are assumed to be binary encoded in MySQL. So make sure that your MySQL fields are binary(16).
//!
//! To activate the `uuid` feature, add it to the `features` list:
//!
//! ```toml
//! [dependencies]
//! mysql = { version = "*", features = ["uuid"] }
//! ```
//!
//! #### Example
//!
//! ```rust
Expand Down Expand Up @@ -163,6 +176,8 @@ mod io;
pub mod value;
pub mod conn;
mod named_params;
#[cfg(feature = "uuid")]
mod uuid;

#[doc(inline)]
pub use conn::Column;
Expand Down
72 changes: 72 additions & 0 deletions src/uuid.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
extern crate uuid;

use self::uuid::Uuid;

use super::value::{
Value,
FromValue,
ConvIr,
};
use super::error::{
Error,
Result as MyResult,
};

impl Into<Value> for Uuid {
fn into(self) -> Value {
Value::Bytes(self.as_bytes().to_vec())
}
}

#[derive(Debug)]
pub struct UuidIr {
val: Uuid,
bytes: Vec<u8>,
}

impl ConvIr<Uuid> for UuidIr {
fn new(v: Value) -> MyResult<UuidIr> {
match v {
Value::Bytes(bytes) => match Uuid::from_bytes(bytes.as_slice()) {
Ok(val) => Ok(UuidIr { val: val, bytes: bytes }),
Err(_) => Err(Error::FromValueError(Value::Bytes(bytes))),
},
v => Err(Error::FromValueError(v)),
}
}
fn commit(self) -> Uuid {
self.val
}
fn rollback(self) -> Value {
Value::Bytes(self.bytes)
}
}

impl FromValue for Uuid {
type Intermediate = UuidIr;
}

#[cfg(test)]
mod test {
use super::super::value::Value::Bytes;
use super::super::from_value;
use super::uuid::Uuid;

#[test]
fn should_convert_Bytes_to_Uuid() {
let bytes = vec![0x0e, 0x87, 0x6d, 0x72, 0xc7, 0xb2, 0x4c, 0x00, 0x8c, 0x56, 0x44, 0xee, 0xac, 0x10, 0x15, 0xd1];
assert_eq!(
Uuid::parse_str("0e876d72-c7b2-4c00-8c56-44eeac1015d1").unwrap(),
from_value::<Uuid>(Bytes(bytes))
);
}

#[test]
fn should_convert_Uuid_to_Bytes() {
let bytes = vec![0x0e, 0x87, 0x6d, 0x72, 0xc7, 0xb2, 0x4c, 0x00, 0x8c, 0x56, 0x44, 0xee, 0xac, 0x10, 0x15, 0xd1];
assert_eq!(
Bytes(bytes),
Uuid::parse_str("0e876d72-c7b2-4c00-8c56-44eeac1015d1").unwrap().into()
);
}
}