Skip to content

Commit ace9ffc

Browse files
committed
Infrastructure for binary parameter formats
Work towards #7.
1 parent 543af18 commit ace9ffc

File tree

3 files changed

+43
-24
lines changed

3 files changed

+43
-24
lines changed

src/lib.rs

+14-7
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::rt::io::net::ip::SocketAddr;
1010
use std::rt::io::net::tcp::TcpStream;
1111

1212
use message::*;
13-
use types::{ToSql, FromSql};
13+
use types::{Oid, ToSql, FromSql};
1414

1515
mod message;
1616
mod types;
@@ -266,10 +266,10 @@ impl PostgresConnection {
266266
resp => fail!("Bad response: %?", resp.to_str())
267267
})
268268
269-
match_read_message!(self, {
270-
ParameterDescription {_} => (),
269+
let param_types = match_read_message!(self, {
270+
ParameterDescription { types } => types,
271271
resp => fail!("Bad response: %?", resp.to_str())
272-
})
272+
});
273273
274274
match_read_message!(self, {
275275
RowDescription {_} | NoData => (),
@@ -281,6 +281,7 @@ impl PostgresConnection {
281281
Ok(PostgresStatement {
282282
conn: self,
283283
name: stmt_name,
284+
param_types: param_types,
284285
next_portal_id: Cell::new(0)
285286
})
286287
}
@@ -362,6 +363,7 @@ impl<'self> PostgresTransaction<'self> {
362363
pub struct PostgresStatement<'self> {
363364
priv conn: &'self PostgresConnection,
364365
priv name: ~str,
366+
priv param_types: ~[Oid],
365367
priv next_portal_id: Cell<uint>
366368
}
367369
@@ -387,9 +389,14 @@ impl<'self> Drop for PostgresStatement<'self> {
387389
impl<'self> PostgresStatement<'self> {
388390
fn execute(&self, portal_name: &str, params: &[&ToSql])
389391
-> Option<PostgresDbError> {
390-
let formats = [];
391-
let values: ~[Option<~[u8]>] = params.iter().map(|val| val.to_sql())
392-
.collect();
392+
let mut formats = ~[];
393+
let mut values = ~[];
394+
for (&param, &ty) in params.iter().zip(self.param_types.iter()) {
395+
let (format, value) = param.to_sql(ty);
396+
formats.push(format as i16);
397+
values.push(value);
398+
};
399+
393400
let result_formats = [];
394401
395402
self.conn.write_message(&Bind {

src/message.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ use std::rt::io::mem::{MemWriter, MemReader};
66
use std::sys;
77
use std::vec;
88

9+
use types::Oid;
10+
911
pub static PROTOCOL_VERSION: i32 = 0x0003_0000;
1012

1113
#[deriving(ToStr)]
@@ -36,7 +38,7 @@ pub enum BackendMessage {
3638
fields: ~[(u8, ~str)]
3739
},
3840
ParameterDescription {
39-
types: ~[i32]
41+
types: ~[Oid]
4042
},
4143
ParameterStatus {
4244
parameter: ~str,
@@ -54,9 +56,9 @@ pub enum BackendMessage {
5456
#[deriving(ToStr)]
5557
pub struct RowDescriptionEntry {
5658
name: ~str,
57-
table_oid: i32,
59+
table_oid: Oid,
5860
column_id: i16,
59-
type_oid: i32,
61+
type_oid: Oid,
6062
type_size: i16,
6163
type_modifier: i32,
6264
format: i16

src/types.rs

+24-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
use std::str;
22

3+
pub type Oid = i32;
4+
5+
pub enum Format {
6+
Text = 0,
7+
Binary = 1
8+
}
9+
310
pub trait FromSql {
411
fn from_sql(raw: &Option<~[u8]>) -> Self;
512
}
@@ -69,14 +76,14 @@ impl FromSql for Option<~str> {
6976
from_option_impl!(~str)
7077

7178
pub trait ToSql {
72-
fn to_sql(&self) -> Option<~[u8]>;
79+
fn to_sql(&self, ty: Oid) -> (Format, Option<~[u8]>);
7380
}
7481

7582
macro_rules! to_str_impl(
7683
($t:ty) => (
7784
impl ToSql for $t {
78-
fn to_sql(&self) -> Option<~[u8]> {
79-
Some(self.to_str().into_bytes())
85+
fn to_sql(&self, _ty: Oid) -> (Format, Option<~[u8]>) {
86+
(Text, Some(self.to_str().into_bytes()))
8087
}
8188
}
8289
)
@@ -85,9 +92,10 @@ macro_rules! to_str_impl(
8592
macro_rules! to_option_impl(
8693
($t:ty) => (
8794
impl ToSql for Option<$t> {
88-
fn to_sql(&self) -> Option<~[u8]> {
89-
do self.chain |val| {
90-
val.to_sql()
95+
fn to_sql(&self, ty: Oid) -> (Format, Option<~[u8]>) {
96+
match *self {
97+
None => (Text, None),
98+
Some(val) => val.to_sql(ty)
9199
}
92100
}
93101
}
@@ -122,23 +130,25 @@ to_str_impl!(f64)
122130
to_option_impl!(f64)
123131

124132
impl<'self> ToSql for &'self str {
125-
fn to_sql(&self) -> Option<~[u8]> {
126-
Some(self.as_bytes().to_owned())
133+
fn to_sql(&self, _ty: Oid) -> (Format, Option<~[u8]>) {
134+
(Text, Some(self.as_bytes().to_owned()))
127135
}
128136
}
129137

130138
impl ToSql for Option<~str> {
131-
fn to_sql(&self) -> Option<~[u8]> {
132-
do self.chain_ref |val| {
133-
val.to_sql()
139+
fn to_sql(&self, ty: Oid) -> (Format, Option<~[u8]>) {
140+
match *self {
141+
None => (Text, None),
142+
Some(ref val) => val.to_sql(ty)
134143
}
135144
}
136145
}
137146

138147
impl<'self> ToSql for Option<&'self str> {
139-
fn to_sql(&self) -> Option<~[u8]> {
140-
do self.chain |val| {
141-
val.to_sql()
148+
fn to_sql(&self, ty: Oid) -> (Format, Option<~[u8]>) {
149+
match *self {
150+
None => (Text, None),
151+
Some(val) => val.to_sql(ty)
142152
}
143153
}
144154
}

0 commit comments

Comments
 (0)