From 81190721d06c3660cc74a7059d8e71dbde304b2d Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Sat, 27 Jul 2013 17:41:53 -0400 Subject: [PATCH] SqlType impls --- src/sqlite3/lib.rs | 110 ++++++++++++++++++++++++++++++++------------ src/sqlite3/test.rs | 10 ++-- 2 files changed, 85 insertions(+), 35 deletions(-) diff --git a/src/sqlite3/lib.rs b/src/sqlite3/lib.rs index 65115757..d2184bd9 100644 --- a/src/sqlite3/lib.rs +++ b/src/sqlite3/lib.rs @@ -237,20 +237,11 @@ impl<'self> Iterator> for ResultIterator<'self> { } pub struct Row<'self> { + // Only here to enforce lifetime restrictions priv stmt: &'self PreparedStatement<'self>, priv cols: ~[Option<~str>] } -impl<'self> Container for Row<'self> { - fn len(&self) -> uint { - unsafe { ffi::sqlite3_column_count(self.stmt.stmt) as uint } - } - - fn is_empty(&self) -> bool { - self.len() == 0 - } -} - impl<'self> Row<'self> { fn new(stmt: &'self PreparedStatement<'self>) -> Row<'self> { let count = unsafe { ffi::sqlite3_column_count(stmt.stmt) as uint}; @@ -280,33 +271,92 @@ impl<'self> Row<'self> { } } +impl<'self> Container for Row<'self> { + fn len(&self) -> uint { + self.cols.len() + } +} + +impl<'self, T: SqlType> Index for Row<'self> { + fn index(&self, idx: &uint) -> T { + self.get(*idx) + } +} + pub trait SqlType { fn to_sql_str(&self) -> Option<~str>; fn from_sql_str(sql_str: &Option<~str>) -> Self; } -impl SqlType for int { +// See #8075 +macro_rules! to_from_str_impl( + ($t:ty) => ( + impl SqlType for $t { + fn to_sql_str(&self) -> Option<~str> { + Some(self.to_str()) + } + + fn from_sql_str(sql_str: &Option<~str>) -> $t { + FromStr::from_str(*sql_str.get_ref()).get() + } + } + ) +) + +macro_rules! option_impl( + ($t:ty) => ( + impl SqlType for Option<$t> { + fn to_sql_str(&self) -> Option<~str> { + match *self { + None => None, + Some(ref v) => Some(v.to_sql_str().get()) + } + } + + fn from_sql_str(sql_str: &Option<~str>) -> Option<$t> { + match *sql_str { + None => None, + Some(_) => Some(SqlType::from_sql_str(sql_str)) + } + } + } + ) +) + +to_from_str_impl!(int) +option_impl!(int) +to_from_str_impl!(i8) +option_impl!(i8) +to_from_str_impl!(i16) +option_impl!(i16) +to_from_str_impl!(i32) +option_impl!(i32) +to_from_str_impl!(i64) +option_impl!(i64) +to_from_str_impl!(uint) +option_impl!(uint) +to_from_str_impl!(u8) +option_impl!(u8) +to_from_str_impl!(u16) +option_impl!(u16) +to_from_str_impl!(u32) +option_impl!(u32) +to_from_str_impl!(u64) +option_impl!(u64) +to_from_str_impl!(float) +option_impl!(float) +to_from_str_impl!(f32) +option_impl!(f32) +to_from_str_impl!(f64) +option_impl!(f64) + +impl SqlType for ~str { fn to_sql_str(&self) -> Option<~str> { - Some(self.to_str()) + Some(self.clone()) } - fn from_sql_str(sql_str: &Option<~str>) -> int { - FromStr::from_str(*sql_str.get_ref()).get() - } -} - -impl SqlType for Option { - fn to_sql_str(&self) -> Option<~str> { - match *self { - None => None, - Some(v) => Some(v.to_str()) - } - } - - fn from_sql_str(sql_str: &Option<~str>) -> Option { - match *sql_str { - None => None, - Some(ref s) => Some(FromStr::from_str(*s).get()) - } + fn from_sql_str(sql_str: &Option<~str>) -> ~str { + sql_str.get_ref().clone() } } +option_impl!(~str) diff --git a/src/sqlite3/test.rs b/src/sqlite3/test.rs index 48b22204..ffab6eae 100644 --- a/src/sqlite3/test.rs +++ b/src/sqlite3/test.rs @@ -21,7 +21,7 @@ fn test_basic() { do conn.query("SELECT id FROM foo") |it| { for it.advance |row| { - printfln!("%u %d", row.len(), row.get(0)); + printfln!("%u %d", row.len(), row[0]); } }; } @@ -37,7 +37,7 @@ fn test_trans() { Err::<(), ~str>(~"") }; assert_eq!(0, chk!(conn.query("SELECT COUNT(*) FROM bar", |it| { - it.next().get().get(0) + it.next().get()[0] }))); do conn.in_transaction |conn| { @@ -46,7 +46,7 @@ fn test_trans() { }; assert_eq!(1, chk!(conn.query("SELECT COUNT(*) FROM bar", |it| { - it.next().get().get(0) + it.next().get()[0] }))); } @@ -60,7 +60,7 @@ fn test_params() { &[@100 as @SqlType, @101 as @SqlType])); assert_eq!(2, chk!(conn.query("SELECT COUNT(*) FROM foo", |it| { - it.next().get().get(0) + it.next().get()[0] }))); } @@ -80,6 +80,6 @@ fn test_null() { }; do conn.query("SELECT n FROM foo WHERE id = 101") |it| { - assert_eq!(Some(1), it.next().get().get(0)) + assert_eq!(Some(1), it.next().get()[0]) }; }