2013-09-02 20:54:02 +00:00
|
|
|
extern mod extra;
|
|
|
|
|
|
|
|
use extra::json;
|
|
|
|
use extra::json::Json;
|
|
|
|
use extra::uuid::Uuid;
|
2013-08-31 23:11:42 +00:00
|
|
|
use std::rt::io::Decorator;
|
2013-09-02 19:42:24 +00:00
|
|
|
use std::rt::io::extensions::{WriterByteConversions, ReaderByteConversions};
|
|
|
|
use std::rt::io::mem::{MemWriter, MemReader};
|
2013-08-28 04:36:27 +00:00
|
|
|
use std::str;
|
|
|
|
|
2013-08-30 05:58:26 +00:00
|
|
|
pub type Oid = i32;
|
|
|
|
|
2013-08-30 06:28:46 +00:00
|
|
|
// Values from pg_type.h
|
|
|
|
static BOOLOID: Oid = 16;
|
2013-09-02 19:42:24 +00:00
|
|
|
static BYTEAOID: Oid = 17;
|
2013-08-31 23:11:42 +00:00
|
|
|
static INT8OID: Oid = 20;
|
|
|
|
static INT2OID: Oid = 21;
|
|
|
|
static INT4OID: Oid = 23;
|
2013-09-02 20:07:57 +00:00
|
|
|
static TEXTOID: Oid = 25;
|
2013-09-02 20:54:02 +00:00
|
|
|
static JSONOID: Oid = 114;
|
2013-08-31 23:11:42 +00:00
|
|
|
static FLOAT4OID: Oid = 700;
|
|
|
|
static FLOAT8OID: Oid = 701;
|
2013-09-02 17:27:09 +00:00
|
|
|
static VARCHAROID: Oid = 1043;
|
2013-09-02 20:54:02 +00:00
|
|
|
static UUIDOID: Oid = 2950;
|
2013-08-30 06:28:46 +00:00
|
|
|
|
2013-08-30 05:58:26 +00:00
|
|
|
pub enum Format {
|
|
|
|
Text = 0,
|
|
|
|
Binary = 1
|
|
|
|
}
|
|
|
|
|
2013-09-02 19:42:24 +00:00
|
|
|
pub fn result_format(ty: Oid) -> Format {
|
|
|
|
match ty {
|
|
|
|
BOOLOID |
|
|
|
|
BYTEAOID |
|
|
|
|
INT8OID |
|
|
|
|
INT2OID |
|
|
|
|
INT4OID |
|
|
|
|
FLOAT4OID |
|
2013-09-02 20:54:02 +00:00
|
|
|
FLOAT8OID |
|
|
|
|
UUIDOID => Binary,
|
2013-09-02 19:42:24 +00:00
|
|
|
_ => Text
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! check_oid(
|
2013-09-02 20:07:57 +00:00
|
|
|
($($expected:ident)|+, $actual:ident) => (
|
|
|
|
match $actual {
|
|
|
|
$($expected)|+ => (),
|
|
|
|
actual => fail!("Invalid Oid %?", actual)
|
2013-09-02 19:42:24 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2013-08-28 04:36:27 +00:00
|
|
|
pub trait FromSql {
|
2013-09-02 19:42:24 +00:00
|
|
|
fn from_sql(ty: Oid, raw: &Option<~[u8]>) -> Self;
|
2013-08-28 04:36:27 +00:00
|
|
|
}
|
|
|
|
|
2013-09-02 21:06:22 +00:00
|
|
|
macro_rules! from_map_impl(
|
|
|
|
($($oid:ident)|+, $t:ty, $blk:expr) => (
|
2013-08-28 04:36:27 +00:00
|
|
|
impl FromSql for Option<$t> {
|
2013-09-02 19:42:24 +00:00
|
|
|
fn from_sql(ty: Oid, raw: &Option<~[u8]>) -> Option<$t> {
|
2013-09-02 21:06:22 +00:00
|
|
|
check_oid!($($oid)|+, ty)
|
|
|
|
raw.map($blk)
|
2013-08-28 04:36:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2013-09-02 21:06:22 +00:00
|
|
|
macro_rules! from_conversions_impl(
|
|
|
|
($oid:ident, $t:ty, $f:ident) => (
|
|
|
|
from_map_impl!($oid, $t, |buf| {
|
|
|
|
// TODO change to BufReader when implemented
|
|
|
|
let mut reader = MemReader::new(buf.to_owned());
|
|
|
|
reader.$f()
|
|
|
|
})
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2013-08-28 04:36:27 +00:00
|
|
|
macro_rules! from_option_impl(
|
|
|
|
($t:ty) => (
|
|
|
|
impl FromSql for $t {
|
2013-09-02 19:42:24 +00:00
|
|
|
fn from_sql(ty: Oid, raw: &Option<~[u8]>) -> $t {
|
2013-08-28 04:36:27 +00:00
|
|
|
// FIXME when you can specify Self types properly
|
2013-09-02 19:42:24 +00:00
|
|
|
let ret: Option<$t> = FromSql::from_sql(ty, raw);
|
2013-08-28 04:36:27 +00:00
|
|
|
ret.unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2013-09-02 21:06:22 +00:00
|
|
|
from_map_impl!(BOOLOID, bool, |buf| { buf[0] != 0 })
|
2013-08-30 06:28:46 +00:00
|
|
|
from_option_impl!(bool)
|
|
|
|
|
2013-09-02 19:42:24 +00:00
|
|
|
from_conversions_impl!(INT2OID, i16, read_be_i16_)
|
2013-08-28 04:36:27 +00:00
|
|
|
from_option_impl!(i16)
|
2013-09-02 19:42:24 +00:00
|
|
|
from_conversions_impl!(INT4OID, i32, read_be_i32_)
|
2013-08-28 04:36:27 +00:00
|
|
|
from_option_impl!(i32)
|
2013-09-02 19:42:24 +00:00
|
|
|
from_conversions_impl!(INT8OID, i64, read_be_i64_)
|
2013-08-28 04:36:27 +00:00
|
|
|
from_option_impl!(i64)
|
2013-09-02 19:42:24 +00:00
|
|
|
from_conversions_impl!(FLOAT4OID, f32, read_be_f32_)
|
2013-08-28 04:36:27 +00:00
|
|
|
from_option_impl!(f32)
|
2013-09-02 19:42:24 +00:00
|
|
|
from_conversions_impl!(FLOAT8OID, f64, read_be_f64_)
|
2013-08-28 04:36:27 +00:00
|
|
|
from_option_impl!(f64)
|
|
|
|
|
2013-09-02 21:06:22 +00:00
|
|
|
from_map_impl!(VARCHAROID | TEXTOID, ~str, |buf| {
|
|
|
|
str::from_bytes(buf.as_slice())
|
|
|
|
})
|
2013-08-28 04:36:27 +00:00
|
|
|
from_option_impl!(~str)
|
|
|
|
|
2013-09-02 19:42:24 +00:00
|
|
|
impl FromSql for Option<~[u8]> {
|
|
|
|
fn from_sql(ty: Oid, raw: &Option<~[u8]>) -> Option<~[u8]> {
|
|
|
|
check_oid!(BYTEAOID, ty)
|
|
|
|
raw.clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
from_option_impl!(~[u8])
|
|
|
|
|
2013-09-02 21:06:22 +00:00
|
|
|
from_map_impl!(JSONOID, Json, |buf| {
|
|
|
|
json::from_str(str::from_bytes_slice(buf.as_slice())).unwrap()
|
|
|
|
})
|
2013-09-02 20:54:02 +00:00
|
|
|
from_option_impl!(Json)
|
|
|
|
|
2013-09-02 21:06:22 +00:00
|
|
|
from_map_impl!(UUIDOID, Uuid, |buf| {
|
|
|
|
Uuid::from_bytes(buf.as_slice()).unwrap()
|
|
|
|
})
|
2013-09-02 20:54:02 +00:00
|
|
|
from_option_impl!(Uuid)
|
|
|
|
|
2013-08-28 04:36:27 +00:00
|
|
|
pub trait ToSql {
|
2013-08-30 05:58:26 +00:00
|
|
|
fn to_sql(&self, ty: Oid) -> (Format, Option<~[u8]>);
|
2013-08-28 04:36:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! to_option_impl(
|
2013-09-02 20:07:57 +00:00
|
|
|
($($oid:ident)|+, $t:ty) => (
|
2013-08-28 04:36:27 +00:00
|
|
|
impl ToSql for Option<$t> {
|
2013-08-30 05:58:26 +00:00
|
|
|
fn to_sql(&self, ty: Oid) -> (Format, Option<~[u8]>) {
|
2013-09-02 20:07:57 +00:00
|
|
|
check_oid!($($oid)|+, ty)
|
2013-09-02 17:27:09 +00:00
|
|
|
|
2013-08-30 05:58:26 +00:00
|
|
|
match *self {
|
|
|
|
None => (Text, None),
|
2013-09-02 17:27:09 +00:00
|
|
|
Some(ref val) => val.to_sql(ty)
|
2013-08-28 04:36:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-09-02 20:54:02 +00:00
|
|
|
);
|
|
|
|
(self, $($oid:ident)|+, $t:ty) => (
|
|
|
|
impl<'self> ToSql for Option<$t> {
|
|
|
|
fn to_sql(&self, ty: Oid) -> (Format, Option<~[u8]>) {
|
|
|
|
check_oid!($($oid)|+, ty)
|
|
|
|
|
|
|
|
match *self {
|
|
|
|
None => (Text, None),
|
|
|
|
Some(ref val) => val.to_sql(ty)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-08-28 04:36:27 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2013-08-31 23:11:42 +00:00
|
|
|
macro_rules! to_conversions_impl(
|
2013-09-02 20:07:57 +00:00
|
|
|
($($oid:ident)|+, $t:ty, $f:ident) => (
|
2013-08-31 23:11:42 +00:00
|
|
|
impl ToSql for $t {
|
|
|
|
fn to_sql(&self, ty: Oid) -> (Format, Option<~[u8]>) {
|
2013-09-02 20:07:57 +00:00
|
|
|
check_oid!($($oid)|+, ty)
|
2013-09-02 17:27:09 +00:00
|
|
|
|
|
|
|
let mut writer = MemWriter::new();
|
|
|
|
writer.$f(*self);
|
|
|
|
(Binary, Some(writer.inner()))
|
2013-08-31 23:11:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2013-08-30 06:28:46 +00:00
|
|
|
impl ToSql for bool {
|
|
|
|
fn to_sql(&self, ty: Oid) -> (Format, Option<~[u8]>) {
|
2013-09-02 17:27:09 +00:00
|
|
|
check_oid!(BOOLOID, ty)
|
|
|
|
(Binary, Some(~[*self as u8]))
|
2013-08-30 06:28:46 +00:00
|
|
|
}
|
|
|
|
}
|
2013-09-02 17:27:09 +00:00
|
|
|
to_option_impl!(BOOLOID, bool)
|
2013-08-30 06:28:46 +00:00
|
|
|
|
2013-08-31 23:11:42 +00:00
|
|
|
to_conversions_impl!(INT2OID, i16, write_be_i16_)
|
2013-09-02 17:27:09 +00:00
|
|
|
to_option_impl!(INT2OID, i16)
|
2013-08-31 23:11:42 +00:00
|
|
|
to_conversions_impl!(INT4OID, i32, write_be_i32_)
|
2013-09-02 17:27:09 +00:00
|
|
|
to_option_impl!(INT4OID, i32)
|
2013-08-31 23:11:42 +00:00
|
|
|
to_conversions_impl!(INT8OID, i64, write_be_i64_)
|
2013-09-02 17:27:09 +00:00
|
|
|
to_option_impl!(INT8OID, i64)
|
2013-08-31 23:11:42 +00:00
|
|
|
to_conversions_impl!(FLOAT4OID, f32, write_be_f32_)
|
2013-09-02 17:27:09 +00:00
|
|
|
to_option_impl!(FLOAT4OID, f32)
|
2013-08-31 23:11:42 +00:00
|
|
|
to_conversions_impl!(FLOAT8OID, f64, write_be_f64_)
|
2013-09-02 17:27:09 +00:00
|
|
|
to_option_impl!(FLOAT8OID, f64)
|
2013-08-28 04:36:27 +00:00
|
|
|
|
|
|
|
impl<'self> ToSql for &'self str {
|
2013-09-02 17:27:09 +00:00
|
|
|
fn to_sql(&self, ty: Oid) -> (Format, Option<~[u8]>) {
|
2013-09-02 20:07:57 +00:00
|
|
|
check_oid!(VARCHAROID | TEXTOID, ty)
|
2013-08-30 05:58:26 +00:00
|
|
|
(Text, Some(self.as_bytes().to_owned()))
|
2013-08-28 04:36:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-02 20:07:57 +00:00
|
|
|
to_option_impl!(VARCHAROID | TEXTOID, ~str)
|
2013-09-02 20:54:02 +00:00
|
|
|
to_option_impl!(self, VARCHAROID | TEXTOID, &'self str)
|
2013-09-02 19:42:24 +00:00
|
|
|
|
|
|
|
impl<'self> ToSql for &'self [u8] {
|
|
|
|
fn to_sql(&self, ty: Oid) -> (Format, Option<~[u8]>) {
|
|
|
|
check_oid!(BYTEAOID, ty)
|
|
|
|
(Binary, Some(self.to_owned()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
to_option_impl!(BYTEAOID, ~[u8])
|
2013-09-02 20:54:02 +00:00
|
|
|
to_option_impl!(self, BYTEAOID, &'self [u8])
|
2013-09-02 19:42:24 +00:00
|
|
|
|
2013-09-02 20:54:02 +00:00
|
|
|
impl ToSql for Json {
|
2013-09-02 19:42:24 +00:00
|
|
|
fn to_sql(&self, ty: Oid) -> (Format, Option<~[u8]>) {
|
2013-09-02 20:54:02 +00:00
|
|
|
check_oid!(JSONOID, ty)
|
|
|
|
(Text, Some(self.to_str().into_bytes()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
to_option_impl!(JSONOID, Json)
|
|
|
|
|
|
|
|
impl ToSql for Uuid {
|
|
|
|
fn to_sql(&self, ty: Oid) -> (Format, Option<~[u8]>) {
|
|
|
|
check_oid!(UUIDOID, ty)
|
|
|
|
(Binary, Some(self.to_bytes().to_owned()))
|
2013-09-02 19:42:24 +00:00
|
|
|
}
|
|
|
|
}
|
2013-09-02 20:54:02 +00:00
|
|
|
|
|
|
|
to_option_impl!(UUIDOID, Uuid)
|