Pull ToSql and FromSql out into their own module

Their implementations will become significantly more complicated once we
start handling binary data.
This commit is contained in:
Steven Fackler 2013-08-28 00:36:27 -04:00
parent 604b1ddb4d
commit 27bbf6f7fd
4 changed files with 149 additions and 146 deletions

View File

@ -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 $@

View File

@ -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<TcpStream>,
@ -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()
}
}
}

View File

@ -1,6 +1,7 @@
extern mod postgres;
use postgres::{PostgresConnection, ToSql};
use postgres::{PostgresConnection};
use postgres::types::ToSql;
#[test]
fn test_basic() {

144
src/types.rs Normal file
View File

@ -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()
}
}
}