rust-postgres/types.rs

360 lines
9.4 KiB
Rust
Raw Normal View History

2013-09-02 20:54:02 +00:00
extern mod extra;
2013-09-08 20:27:15 +00:00
use extra::time::Timespec;
2013-09-02 20:54:02 +00:00
use extra::json;
use extra::json::Json;
use extra::uuid::Uuid;
use std::rt::io::Decorator;
use std::rt::io::extensions::{WriterByteConversions, ReaderByteConversions};
2013-09-04 02:35:28 +00:00
use std::rt::io::mem::{MemWriter, BufReader};
use std::str;
2013-09-30 02:47:30 +00:00
/// A Postgres OID
pub type Oid = i32;
// Values from pg_type.h
static BOOLOID: Oid = 16;
static BYTEAOID: Oid = 17;
2013-09-02 22:14:22 +00:00
static CHAROID: Oid = 18;
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;
static FLOAT4OID: Oid = 700;
static FLOAT8OID: Oid = 701;
2013-09-02 21:52:23 +00:00
static BPCHAROID: Oid = 1042;
2013-09-02 17:27:09 +00:00
static VARCHAROID: Oid = 1043;
2013-09-09 04:33:41 +00:00
static TIMESTAMPOID: Oid = 1114;
static TIMESTAMPZOID: Oid = 1184;
2013-09-02 20:54:02 +00:00
static UUIDOID: Oid = 2950;
2013-09-08 20:27:15 +00:00
static USEC_PER_SEC: i64 = 1_000_000;
static NSEC_PER_USEC: i64 = 1_000;
// Number of seconds from 1970-01-01 to 2000-01-01
static TIME_SEC_CONVERSION: i64 = 946684800;
2013-09-30 02:47:30 +00:00
/// A Postgres type
#[deriving(Eq)]
pub enum PostgresType {
2013-09-30 02:47:30 +00:00
/// BOOL
PgBool,
2013-09-30 02:47:30 +00:00
/// BYTEA
PgByteA,
2013-09-30 02:47:30 +00:00
/// "char"
PgChar,
2013-09-30 02:47:30 +00:00
/// INT8/BIGINT
PgInt8,
2013-09-30 02:47:30 +00:00
/// INT2/SMALLINT
PgInt2,
2013-09-30 02:47:30 +00:00
/// INT4/INT
PgInt4,
2013-09-30 02:47:30 +00:00
/// TEXT
PgText,
2013-09-30 02:47:30 +00:00
/// JSON
PgJson,
2013-09-30 02:47:30 +00:00
/// FLOAT4/REAL
PgFloat4,
2013-09-30 02:47:30 +00:00
/// FLOAT8/DOUBLE PRECISION
PgFloat8,
2013-09-30 02:47:30 +00:00
/// TIMESTAMP
2013-09-08 20:27:15 +00:00
PgTimestamp,
2013-09-30 02:47:30 +00:00
/// TIMESTAMP WITH TIME ZONE
2013-09-09 04:33:41 +00:00
PgTimestampZ,
2013-09-30 02:47:30 +00:00
/// CHAR(n)/CHARACTER(n)
PgCharN,
2013-09-30 02:47:30 +00:00
/// VARCHAR/CHARACTER VARYING
PgVarchar,
2013-09-30 02:47:30 +00:00
/// UUID
PgUuid,
2013-09-30 02:47:30 +00:00
/// An unknown type along with its OID
PgUnknownType(Oid)
}
impl PostgresType {
2013-09-30 02:47:30 +00:00
/// Creates a PostgresType from a Postgres OID.
pub fn from_oid(oid: Oid) -> PostgresType {
match oid {
BOOLOID => PgBool,
BYTEAOID => PgByteA,
CHAROID => PgChar,
INT8OID => PgInt8,
INT2OID => PgInt2,
INT4OID => PgInt4,
TEXTOID => PgText,
JSONOID => PgJson,
FLOAT4OID => PgFloat4,
FLOAT8OID => PgFloat8,
2013-09-08 20:27:15 +00:00
TIMESTAMPOID => PgTimestamp,
2013-09-09 04:33:41 +00:00
TIMESTAMPZOID => PgTimestampZ,
BPCHAROID => PgCharN,
VARCHAROID => PgVarchar,
UUIDOID => PgUuid,
oid => PgUnknownType(oid)
}
}
2013-09-30 02:47:30 +00:00
/// Returns the wire format needed for the value of `self`.
pub fn result_format(&self) -> Format {
match *self {
2013-09-16 02:43:46 +00:00
PgUnknownType(*) => Text,
_ => Binary
}
}
}
2013-09-30 02:47:30 +00:00
/// The wire format of a Postgres value
pub enum Format {
/// A user-readable string format
Text = 0,
/// A machine-readable binary format
Binary = 1
}
2013-09-05 04:30:20 +00:00
macro_rules! check_types(
($($expected:pat)|+, $actual:ident) => (
2013-09-02 20:07:57 +00:00
match $actual {
$($expected)|+ => (),
2013-09-05 04:30:20 +00:00
actual => fail2!("Invalid Postgres type {:?}", actual)
}
)
)
2013-09-30 05:22:10 +00:00
/// A trait for types that can be created from a Postgres value
pub trait FromSql {
2013-09-30 02:47:30 +00:00
/// Creates a new value of this type from a buffer of Postgres data.
///
/// If the value was `NULL`, the buffer will be `None`.
///
2013-10-13 06:19:57 +00:00
/// # Failure
///
2013-09-30 02:47:30 +00:00
/// Fails if this type can not be created from the provided Postgres type.
fn from_sql(ty: PostgresType, raw: &Option<~[u8]>) -> Self;
}
2013-09-02 21:06:22 +00:00
macro_rules! from_map_impl(
2013-09-05 04:30:20 +00:00
($($expected:pat)|+, $t:ty, $blk:expr) => (
impl FromSql for Option<$t> {
fn from_sql(ty: PostgresType, raw: &Option<~[u8]>) -> Option<$t> {
2013-09-05 04:30:20 +00:00
check_types!($($expected)|+, ty)
2013-10-11 03:50:39 +00:00
raw.as_ref().map($blk)
}
}
)
)
2013-09-02 21:06:22 +00:00
macro_rules! from_conversions_impl(
2013-09-05 04:30:20 +00:00
($expected:pat, $t:ty, $f:ident) => (
from_map_impl!($expected, $t, |buf| {
2013-09-04 02:35:28 +00:00
let mut reader = BufReader::new(buf.as_slice());
2013-09-02 21:06:22 +00:00
reader.$f()
})
)
)
macro_rules! from_option_impl(
($t:ty) => (
impl FromSql for $t {
fn from_sql(ty: PostgresType, raw: &Option<~[u8]>) -> $t {
// FIXME when you can specify Self types properly
let ret: Option<$t> = FromSql::from_sql(ty, raw);
ret.unwrap()
}
}
)
)
from_map_impl!(PgBool, bool, |buf| { buf[0] != 0 })
from_option_impl!(bool)
from_conversions_impl!(PgChar, i8, read_i8_)
2013-09-02 22:14:22 +00:00
from_option_impl!(i8)
from_conversions_impl!(PgInt2, i16, read_be_i16_)
from_option_impl!(i16)
from_conversions_impl!(PgInt4, i32, read_be_i32_)
from_option_impl!(i32)
from_conversions_impl!(PgInt8, i64, read_be_i64_)
from_option_impl!(i64)
from_conversions_impl!(PgFloat4, f32, read_be_f32_)
from_option_impl!(f32)
from_conversions_impl!(PgFloat8, f64, read_be_f64_)
from_option_impl!(f64)
from_map_impl!(PgVarchar | PgText | PgCharN, ~str, |buf| {
str::from_utf8(buf.as_slice())
2013-09-02 21:06:22 +00:00
})
from_option_impl!(~str)
impl FromSql for Option<~[u8]> {
fn from_sql(ty: PostgresType, raw: &Option<~[u8]>) -> Option<~[u8]> {
2013-09-05 04:30:20 +00:00
check_types!(PgByteA, ty)
raw.clone()
}
}
from_option_impl!(~[u8])
from_map_impl!(PgJson, Json, |buf| {
json::from_str(str::from_utf8_slice(buf.as_slice())).unwrap()
2013-09-02 21:06:22 +00:00
})
2013-09-02 20:54:02 +00:00
from_option_impl!(Json)
from_map_impl!(PgUuid, Uuid, |buf| {
2013-09-07 22:45:11 +00:00
Uuid::from_bytes(buf.as_slice()).unwrap()
2013-09-02 21:06:22 +00:00
})
2013-09-02 20:54:02 +00:00
from_option_impl!(Uuid)
2013-09-09 04:33:41 +00:00
from_map_impl!(PgTimestamp | PgTimestampZ, Timespec, |buf| {
2013-09-08 20:27:15 +00:00
let mut rdr = BufReader::new(buf.as_slice());
let t = rdr.read_be_i64_();
let mut sec = t / USEC_PER_SEC + TIME_SEC_CONVERSION;
let mut usec = t % USEC_PER_SEC;
if usec < 0 {
sec -= 1;
usec = USEC_PER_SEC + usec;
}
Timespec::new(sec, (usec * NSEC_PER_USEC) as i32)
})
from_option_impl!(Timespec)
2013-09-30 02:47:30 +00:00
/// A trait for types that can be converted into Postgres values
pub trait ToSql {
2013-09-30 02:47:30 +00:00
/// Converts the value of `self` into a format appropriate for the Postgres
/// backend.
///
2013-10-13 06:19:57 +00:00
/// # Failure
///
2013-09-30 02:47:30 +00:00
/// Fails if this type cannot be converted into the specified Postgres
/// type.
fn to_sql(&self, ty: PostgresType) -> (Format, Option<~[u8]>);
}
macro_rules! to_option_impl(
2013-09-02 20:07:57 +00:00
($($oid:ident)|+, $t:ty) => (
impl ToSql for Option<$t> {
fn to_sql(&self, ty: PostgresType) -> (Format, Option<~[u8]>) {
2013-09-05 04:30:20 +00:00
check_types!($($oid)|+, ty)
2013-09-02 17:27:09 +00:00
match *self {
None => (Text, None),
2013-09-02 17:27:09 +00:00
Some(ref val) => val.to_sql(ty)
}
}
}
2013-09-02 20:54:02 +00:00
);
(self, $($oid:ident)|+, $t:ty) => (
impl<'self> ToSql for Option<$t> {
fn to_sql(&self, ty: PostgresType) -> (Format, Option<~[u8]>) {
2013-09-05 04:30:20 +00:00
check_types!($($oid)|+, ty)
2013-09-02 20:54:02 +00:00
match *self {
None => (Text, None),
Some(ref val) => val.to_sql(ty)
}
}
}
)
)
macro_rules! to_conversions_impl(
2013-09-02 20:07:57 +00:00
($($oid:ident)|+, $t:ty, $f:ident) => (
impl ToSql for $t {
fn to_sql(&self, ty: PostgresType) -> (Format, Option<~[u8]>) {
2013-09-05 04:30:20 +00:00
check_types!($($oid)|+, ty)
2013-09-02 17:27:09 +00:00
let mut writer = MemWriter::new();
writer.$f(*self);
(Binary, Some(writer.inner()))
}
}
)
)
impl ToSql for bool {
fn to_sql(&self, ty: PostgresType) -> (Format, Option<~[u8]>) {
2013-09-05 04:30:20 +00:00
check_types!(PgBool, ty)
2013-09-02 17:27:09 +00:00
(Binary, Some(~[*self as u8]))
}
}
to_option_impl!(PgBool, bool)
to_conversions_impl!(PgChar, i8, write_i8_)
to_option_impl!(PgChar, i8)
to_conversions_impl!(PgInt2, i16, write_be_i16_)
to_option_impl!(PgInt2, i16)
to_conversions_impl!(PgInt4, i32, write_be_i32_)
to_option_impl!(PgInt4, i32)
to_conversions_impl!(PgInt8, i64, write_be_i64_)
to_option_impl!(PgInt8, i64)
to_conversions_impl!(PgFloat4, f32, write_be_f32_)
to_option_impl!(PgFloat4, f32)
to_conversions_impl!(PgFloat8, f64, write_be_f64_)
to_option_impl!(PgFloat8, f64)
2013-09-03 06:19:03 +00:00
impl ToSql for ~str {
fn to_sql(&self, ty: PostgresType) -> (Format, Option<~[u8]>) {
2013-09-05 04:30:20 +00:00
check_types!(PgVarchar | PgText | PgCharN, ty)
2013-09-03 06:19:03 +00:00
(Text, Some(self.as_bytes().to_owned()))
}
}
impl<'self> ToSql for &'self str {
fn to_sql(&self, ty: PostgresType) -> (Format, Option<~[u8]>) {
2013-09-05 04:30:20 +00:00
check_types!(PgVarchar | PgText | PgCharN, ty)
(Text, Some(self.as_bytes().to_owned()))
}
}
to_option_impl!(PgVarchar | PgText | PgCharN, ~str)
to_option_impl!(self, PgVarchar | PgText | PgCharN, &'self str)
2013-09-03 06:19:03 +00:00
impl ToSql for ~[u8] {
fn to_sql(&self, ty: PostgresType) -> (Format, Option<~[u8]>) {
2013-09-05 04:30:20 +00:00
check_types!(PgByteA, ty)
2013-09-03 06:19:03 +00:00
(Binary, Some(self.to_owned()))
}
}
impl<'self> ToSql for &'self [u8] {
fn to_sql(&self, ty: PostgresType) -> (Format, Option<~[u8]>) {
2013-09-05 04:30:20 +00:00
check_types!(PgByteA, ty)
(Binary, Some(self.to_owned()))
}
}
to_option_impl!(PgByteA, ~[u8])
to_option_impl!(self, PgByteA, &'self [u8])
2013-09-02 20:54:02 +00:00
impl ToSql for Json {
fn to_sql(&self, ty: PostgresType) -> (Format, Option<~[u8]>) {
2013-09-05 04:30:20 +00:00
check_types!(PgJson, ty)
2013-09-02 20:54:02 +00:00
(Text, Some(self.to_str().into_bytes()))
}
}
to_option_impl!(PgJson, Json)
2013-09-02 20:54:02 +00:00
impl ToSql for Uuid {
fn to_sql(&self, ty: PostgresType) -> (Format, Option<~[u8]>) {
2013-09-05 04:30:20 +00:00
check_types!(PgUuid, ty)
2013-09-02 20:54:02 +00:00
(Binary, Some(self.to_bytes().to_owned()))
}
}
2013-09-02 20:54:02 +00:00
to_option_impl!(PgUuid, Uuid)
2013-09-08 20:27:15 +00:00
impl ToSql for Timespec {
fn to_sql(&self, ty: PostgresType) -> (Format, Option<~[u8]>) {
2013-09-09 04:33:41 +00:00
check_types!(PgTimestamp | PgTimestampZ, ty)
2013-09-08 20:27:15 +00:00
let t = (self.sec - TIME_SEC_CONVERSION) * USEC_PER_SEC
+ self.nsec as i64 / NSEC_PER_USEC;
let mut buf = MemWriter::new();
buf.write_be_i64_(t);
(Binary, Some(buf.inner()))
}
}
2013-09-09 04:33:41 +00:00
to_option_impl!(PgTimestamp | PgTimestampZ, Timespec)