From 27bbf6f7fd7b1e15788c0f40a81e7fcfa4542dd1 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Wed, 28 Aug 2013 00:36:27 -0400 Subject: [PATCH] Pull ToSql and FromSql out into their own module Their implementations will become significantly more complicated once we start handling binary data. --- Makefile | 2 +- src/lib.rs | 146 +-------------------------------------------------- src/test.rs | 3 +- src/types.rs | 144 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 149 insertions(+), 146 deletions(-) create mode 100644 src/types.rs diff --git a/Makefile b/Makefile index a85e57d4..9efe6864 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ RUSTFLAGS += -L. --cfg debug -Z debug-info .PHONY: all all: postgres.dummy -postgres.dummy: src/lib.rs src/message.rs +postgres.dummy: src/lib.rs src/message.rs src/types.rs $(RUSTC) $(RUSTFLAGS) --lib src/lib.rs -o $@ touch $@ diff --git a/src/lib.rs b/src/lib.rs index 50c851f8..db765726 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,11 +7,12 @@ use std::cell::Cell; use std::rt::io::io_error; use std::rt::io::net::ip::SocketAddr; use std::rt::io::net::tcp::TcpStream; -use std::str; use message::*; +use types::{ToSql, FromSql}; mod message; +mod types; pub struct PostgresConnection { priv stream: Cell, @@ -485,146 +486,3 @@ impl<'self> PostgresRow<'self> { FromSql::from_sql(&self.result.data[self.row][idx]) } } - -pub trait FromSql { - fn from_sql(raw: &Option<~[u8]>) -> Self; -} - -macro_rules! from_str_impl( - ($t:ty) => ( - impl FromSql for Option<$t> { - fn from_sql(raw: &Option<~[u8]>) -> Option<$t> { - match *raw { - None => None, - Some(ref buf) => { - let s = str::from_bytes_slice(buf.as_slice()); - Some(FromStr::from_str(s).unwrap()) - } - } - } - } - ) -) - -macro_rules! from_option_impl( - ($t:ty) => ( - impl FromSql for $t { - fn from_sql(raw: &Option<~[u8]>) -> $t { - // FIXME when you can specify Self types properly - let ret: Option<$t> = FromSql::from_sql(raw); - ret.unwrap() - } - } - ) -) - -from_str_impl!(int) -from_option_impl!(int) -from_str_impl!(i8) -from_option_impl!(i8) -from_str_impl!(i16) -from_option_impl!(i16) -from_str_impl!(i32) -from_option_impl!(i32) -from_str_impl!(i64) -from_option_impl!(i64) -from_str_impl!(uint) -from_option_impl!(uint) -from_str_impl!(u8) -from_option_impl!(u8) -from_str_impl!(u16) -from_option_impl!(u16) -from_str_impl!(u32) -from_option_impl!(u32) -from_str_impl!(u64) -from_option_impl!(u64) -from_str_impl!(float) -from_option_impl!(float) -from_str_impl!(f32) -from_option_impl!(f32) -from_str_impl!(f64) -from_option_impl!(f64) - -impl FromSql for Option<~str> { - fn from_sql(raw: &Option<~[u8]>) -> Option<~str> { - do raw.chain_ref |buf| { - Some(str::from_bytes(buf.as_slice())) - } - } -} -from_option_impl!(~str) - -pub trait ToSql { - fn to_sql(&self) -> Option<~[u8]>; -} - -macro_rules! to_str_impl( - ($t:ty) => ( - impl ToSql for $t { - fn to_sql(&self) -> Option<~[u8]> { - Some(self.to_str().into_bytes()) - } - } - ) -) - -macro_rules! to_option_impl( - ($t:ty) => ( - impl ToSql for Option<$t> { - fn to_sql(&self) -> Option<~[u8]> { - do self.chain |val| { - val.to_sql() - } - } - } - ) -) - -to_str_impl!(int) -to_option_impl!(int) -to_str_impl!(i8) -to_option_impl!(i8) -to_str_impl!(i16) -to_option_impl!(i16) -to_str_impl!(i32) -to_option_impl!(i32) -to_str_impl!(i64) -to_option_impl!(i64) -to_str_impl!(uint) -to_option_impl!(uint) -to_str_impl!(u8) -to_option_impl!(u8) -to_str_impl!(u16) -to_option_impl!(u16) -to_str_impl!(u32) -to_option_impl!(u32) -to_str_impl!(u64) -to_option_impl!(u64) -to_str_impl!(float) -to_option_impl!(float) -to_str_impl!(f32) -to_option_impl!(f32) -to_str_impl!(f64) -to_option_impl!(f64) - -impl<'self> ToSql for &'self str { - fn to_sql(&self) -> Option<~[u8]> { - Some(self.as_bytes().to_owned()) - } -} - -impl ToSql for Option<~str> { - fn to_sql(&self) -> Option<~[u8]> { - do self.chain_ref |val| { - val.to_sql() - } - } -} - -impl<'self> ToSql for Option<&'self str> { - fn to_sql(&self) -> Option<~[u8]> { - do self.chain |val| { - val.to_sql() - } - } -} diff --git a/src/test.rs b/src/test.rs index 3069371b..389d2a9c 100644 --- a/src/test.rs +++ b/src/test.rs @@ -1,6 +1,7 @@ extern mod postgres; -use postgres::{PostgresConnection, ToSql}; +use postgres::{PostgresConnection}; +use postgres::types::ToSql; #[test] fn test_basic() { diff --git a/src/types.rs b/src/types.rs new file mode 100644 index 00000000..0f603859 --- /dev/null +++ b/src/types.rs @@ -0,0 +1,144 @@ +use std::str; + +pub trait FromSql { + fn from_sql(raw: &Option<~[u8]>) -> Self; +} + +macro_rules! from_str_impl( + ($t:ty) => ( + impl FromSql for Option<$t> { + fn from_sql(raw: &Option<~[u8]>) -> Option<$t> { + match *raw { + None => None, + Some(ref buf) => { + let s = str::from_bytes_slice(buf.as_slice()); + Some(FromStr::from_str(s).unwrap()) + } + } + } + } + ) +) + +macro_rules! from_option_impl( + ($t:ty) => ( + impl FromSql for $t { + fn from_sql(raw: &Option<~[u8]>) -> $t { + // FIXME when you can specify Self types properly + let ret: Option<$t> = FromSql::from_sql(raw); + ret.unwrap() + } + } + ) +) + +from_str_impl!(int) +from_option_impl!(int) +from_str_impl!(i8) +from_option_impl!(i8) +from_str_impl!(i16) +from_option_impl!(i16) +from_str_impl!(i32) +from_option_impl!(i32) +from_str_impl!(i64) +from_option_impl!(i64) +from_str_impl!(uint) +from_option_impl!(uint) +from_str_impl!(u8) +from_option_impl!(u8) +from_str_impl!(u16) +from_option_impl!(u16) +from_str_impl!(u32) +from_option_impl!(u32) +from_str_impl!(u64) +from_option_impl!(u64) +from_str_impl!(float) +from_option_impl!(float) +from_str_impl!(f32) +from_option_impl!(f32) +from_str_impl!(f64) +from_option_impl!(f64) + +impl FromSql for Option<~str> { + fn from_sql(raw: &Option<~[u8]>) -> Option<~str> { + do raw.chain_ref |buf| { + Some(str::from_bytes(buf.as_slice())) + } + } +} +from_option_impl!(~str) + +pub trait ToSql { + fn to_sql(&self) -> Option<~[u8]>; +} + +macro_rules! to_str_impl( + ($t:ty) => ( + impl ToSql for $t { + fn to_sql(&self) -> Option<~[u8]> { + Some(self.to_str().into_bytes()) + } + } + ) +) + +macro_rules! to_option_impl( + ($t:ty) => ( + impl ToSql for Option<$t> { + fn to_sql(&self) -> Option<~[u8]> { + do self.chain |val| { + val.to_sql() + } + } + } + ) +) + +to_str_impl!(int) +to_option_impl!(int) +to_str_impl!(i8) +to_option_impl!(i8) +to_str_impl!(i16) +to_option_impl!(i16) +to_str_impl!(i32) +to_option_impl!(i32) +to_str_impl!(i64) +to_option_impl!(i64) +to_str_impl!(uint) +to_option_impl!(uint) +to_str_impl!(u8) +to_option_impl!(u8) +to_str_impl!(u16) +to_option_impl!(u16) +to_str_impl!(u32) +to_option_impl!(u32) +to_str_impl!(u64) +to_option_impl!(u64) +to_str_impl!(float) +to_option_impl!(float) +to_str_impl!(f32) +to_option_impl!(f32) +to_str_impl!(f64) +to_option_impl!(f64) + +impl<'self> ToSql for &'self str { + fn to_sql(&self) -> Option<~[u8]> { + Some(self.as_bytes().to_owned()) + } +} + +impl ToSql for Option<~str> { + fn to_sql(&self) -> Option<~[u8]> { + do self.chain_ref |val| { + val.to_sql() + } + } +} + +impl<'self> ToSql for Option<&'self str> { + fn to_sql(&self) -> Option<~[u8]> { + do self.chain |val| { + val.to_sql() + } + } +}