From 5fa6026100e34093d5b358abbfbee0d78c97dfc7 Mon Sep 17 00:00:00 2001 From: Yorhel Date: Wed, 10 Jun 2020 10:39:35 +0200 Subject: [PATCH] Implement Readable for Option --- src/statement.rs | 11 +++++++++++ tests/lib.rs | 39 ++++++++++++++++++++++++++++++--------- 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/src/statement.rs b/src/statement.rs index 38c1c22..abff1d1 100644 --- a/src/statement.rs +++ b/src/statement.rs @@ -297,6 +297,17 @@ impl Readable for Vec { } } +impl Readable for Option { + #[inline] + fn read(statement: &Statement, i: usize) -> Result { + if statement.kind(i) == Type::Null { + Ok(None) + } else { + T::read(statement, i).map(Some) + } + } +} + #[inline] pub fn new<'l, T: AsRef>(raw1: *mut ffi::sqlite3, statement: T) -> Result> { let mut raw0 = 0 as *mut _; diff --git a/tests/lib.rs b/tests/lib.rs index ade2d90..b0074c0 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -29,11 +29,12 @@ fn connection_iterate() { let mut done = false; let statement = "SELECT * FROM users"; ok!(connection.iterate(statement, |pairs| { - assert_eq!(pairs.len(), 4); + assert_eq!(pairs.len(), 5); assert_eq!(pairs[0], pair!("id", "1")); assert_eq!(pairs[1], pair!("name", "Alice")); assert_eq!(pairs[2], pair!("age", "42.69")); assert_eq!(pairs[3], pair!("photo", "\x42\x69")); + assert_eq!(pairs[4], ("email", None)); done = true; true })); @@ -71,12 +72,13 @@ fn connection_set_busy_handler() { thread::spawn(move || { let mut connection = ok!(sqlite::open(&path)); ok!(connection.set_busy_handler(|_| true)); - let statement = "INSERT INTO users VALUES (?, ?, ?, ?)"; + let statement = "INSERT INTO users VALUES (?, ?, ?, ?, ?)"; let mut statement = ok!(connection.prepare(statement)); ok!(statement.bind(1, 2i64)); ok!(statement.bind(2, "Bob")); ok!(statement.bind(3, 69.42)); ok!(statement.bind(4, &[0x69u8, 0x42u8][..])); + ok!(statement.bind(5, ())); assert_eq!(ok!(statement.next()), State::Done); true }) @@ -91,7 +93,7 @@ fn connection_set_busy_handler() { #[test] fn cursor_read() { let connection = setup_users(":memory:"); - ok!(connection.execute("INSERT INTO users VALUES (2, 'Bob', NULL, NULL)")); + ok!(connection.execute("INSERT INTO users VALUES (2, 'Bob', NULL, NULL, NULL)")); let statement = "SELECT id, age FROM users ORDER BY 1 DESC"; let statement = ok!(connection.prepare(statement)); @@ -176,35 +178,38 @@ fn cursor_workflow() { #[test] fn statement_bind() { let connection = setup_users(":memory:"); - let statement = "INSERT INTO users VALUES (?, ?, ?, ?)"; + let statement = "INSERT INTO users VALUES (?, ?, ?, ?, ?)"; let mut statement = ok!(connection.prepare(statement)); ok!(statement.bind(1, 2i64)); ok!(statement.bind(2, "Bob")); ok!(statement.bind(3, 69.42)); ok!(statement.bind(4, &[0x69u8, 0x42u8][..])); + ok!(statement.bind(5, ())); assert_eq!(ok!(statement.next()), State::Done); } #[test] fn statement_bind_with_optional() { let connection = setup_users(":memory:"); - let statement = "INSERT INTO users VALUES (?, ?, ?, ?)"; + let statement = "INSERT INTO users VALUES (?, ?, ?, ?, ?)"; let mut statement = ok!(connection.prepare(statement)); ok!(statement.bind(1, None::)); ok!(statement.bind(2, None::<&str>)); ok!(statement.bind(3, None::)); ok!(statement.bind(4, None::<&[u8]>)); + ok!(statement.bind(5, None::<&str>)); assert_eq!(ok!(statement.next()), State::Done); - let statement = "INSERT INTO users VALUES (?, ?, ?, ?)"; + let statement = "INSERT INTO users VALUES (?, ?, ?, ?, ?)"; let mut statement = ok!(connection.prepare(statement)); ok!(statement.bind(1, Some(2i64))); ok!(statement.bind(2, Some("Bob"))); ok!(statement.bind(3, Some(69.42))); ok!(statement.bind(4, Some(&[0x69u8, 0x42u8][..]))); + ok!(statement.bind(5, None::<&str>)); assert_eq!(ok!(statement.next()), State::Done); } @@ -216,7 +221,7 @@ fn statement_count() { assert_eq!(ok!(statement.next()), State::Row); - assert_eq!(statement.count(), 4); + assert_eq!(statement.count(), 5); } #[test] @@ -260,6 +265,22 @@ fn statement_read() { assert_eq!(ok!(statement.read::(1)), String::from("Alice")); assert_eq!(ok!(statement.read::(2)), 42.69); assert_eq!(ok!(statement.read::>(3)), vec![0x42, 0x69]); + assert_eq!(ok!(statement.read::(4)), Value::Null); + assert_eq!(ok!(statement.next()), State::Done); +} + +#[test] +fn statement_read_with_optional() { + let connection = setup_users(":memory:"); + let statement = "SELECT * FROM users"; + let mut statement = ok!(connection.prepare(statement)); + + assert_eq!(ok!(statement.next()), State::Row); + assert_eq!(ok!(statement.read::>(0)), Some(1)); + assert_eq!(ok!(statement.read::>(1)), Some(String::from("Alice"))); + assert_eq!(ok!(statement.read::>(2)), Some(42.69)); + assert_eq!(ok!(statement.read::>>(3)), Some(vec![0x42, 0x69])); + assert_eq!(ok!(statement.read::>(4)), None); assert_eq!(ok!(statement.next()), State::Done); } @@ -311,8 +332,8 @@ fn setup_users>(path: T) -> Connection { let connection = ok!(sqlite::open(path)); ok!(connection.execute( " - CREATE TABLE users (id INTEGER, name TEXT, age REAL, photo BLOB); - INSERT INTO users VALUES (1, 'Alice', 42.69, X'4269'); + CREATE TABLE users (id INTEGER, name TEXT, age REAL, photo BLOB, email TEXT); + INSERT INTO users VALUES (1, 'Alice', 42.69, X'4269', NULL); ", )); connection