Boolean parameters transmitted as binary

Work towards #7.
This commit is contained in:
Steven Fackler 2013-08-30 02:28:46 -04:00
parent ace9ffcec4
commit f56c519097
2 changed files with 55 additions and 0 deletions

View File

@ -68,6 +68,30 @@ fn test_nulls() {
};
}
#[test]
fn test_binary_bool_params() {
let conn = PostgresConnection::connect("postgres://postgres@127.0.0.1:5432");
do conn.in_transaction |trans| {
trans.prepare("CREATE TABLE foo (
id BIGINT PRIMARY KEY,
b BOOL
)").update([]);
trans.prepare("INSERT INTO foo (id, b) VALUES
($1, $2), ($3, $4), ($5, $6)")
.update([&1 as &ToSql, &true as &ToSql,
&2 as &ToSql, &false as &ToSql,
&3 as &ToSql, &None::<bool> as &ToSql]);
let stmt = trans.prepare("SELECT b FROM foo ORDER BY id");
let result = stmt.query([]);
assert_eq!(~[Some(true), Some(false), None],
result.iter().map(|row| { row[0] }).collect());
trans.set_rollback();
}
}
#[test]
fn test_wrong_num_params() {
let conn = PostgresConnection::connect("postgres://postgres@127.0.0.1:5432");

View File

@ -2,6 +2,9 @@ use std::str;
pub type Oid = i32;
// Values from pg_type.h
static BOOLOID: Oid = 16;
pub enum Format {
Text = 0,
Binary = 1
@ -39,6 +42,23 @@ macro_rules! from_option_impl(
)
)
impl FromSql for Option<bool> {
fn from_sql(raw: &Option<~[u8]>) -> Option<bool> {
match *raw {
None => None,
Some(ref buf) => {
assert_eq!(1, buf.len());
match buf[0] as char {
't' => Some(true),
'f' => Some(false),
byte => fail!("Invalid byte: %?", byte)
}
}
}
}
}
from_option_impl!(bool)
from_str_impl!(int)
from_option_impl!(int)
from_str_impl!(i8)
@ -102,6 +122,17 @@ macro_rules! to_option_impl(
)
)
impl ToSql for bool {
fn to_sql(&self, ty: Oid) -> (Format, Option<~[u8]>) {
if ty == BOOLOID {
(Binary, Some(~[*self as u8]))
} else {
(Text, Some(self.to_str().into_bytes()))
}
}
}
to_option_impl!(bool)
to_str_impl!(int)
to_option_impl!(int)
to_str_impl!(i8)