SqlType impls
This commit is contained in:
@@ -237,20 +237,11 @@ impl<'self> Iterator<Row<'self>> 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<uint, T> 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<int> {
|
||||
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<int> {
|
||||
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)
|
||||
|
||||
@@ -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])
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user