Skip to content

Commit 16ce48a

Browse files
committed
Update for postgres_protocol changes
1 parent b559b98 commit 16ce48a

File tree

5 files changed

+49
-46
lines changed

5 files changed

+49
-46
lines changed

src/lib.rs

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,8 @@ pub fn cancel_query<T>(params: T,
165165
let params = try!(params.into_connect_params().map_err(ConnectError::ConnectParams));
166166
let mut socket = try!(priv_io::initialize_stream(&params, tls));
167167

168-
let message = frontend::CancelRequest {
169-
process_id: data.process_id,
170-
secret_key: data.secret_key,
171-
};
172168
let mut buf = vec![];
173-
try!(frontend::Message::write(&message, &mut buf));
174-
169+
frontend::cancel_request(data.process_id, data.secret_key, &mut buf);
175170
try!(socket.write_all(&buf));
176171
try!(socket.flush());
177172

@@ -516,23 +511,31 @@ impl InnerConnection {
516511
debug!("executing statement {} with parameters: {:?}",
517512
stmt_name,
518513
params);
519-
let mut values = vec![];
520-
for (param, ty) in params.iter().zip(param_types) {
521-
let mut buf = vec![];
522-
match try!(param.to_sql_checked(ty, &mut buf, &SessionInfo::new(self))
523-
.map_err(Error::Conversion)) {
524-
IsNull::Yes => values.push(None),
525-
IsNull::No => values.push(Some(buf)),
514+
515+
{
516+
let info = SessionInfo::new(&self.parameters);
517+
let r = self.stream.write_message2(|buf| {
518+
frontend::bind(portal_name,
519+
&stmt_name,
520+
Some(1),
521+
params.iter().zip(param_types),
522+
|(param, ty), buf| {
523+
match param.to_sql_checked(ty, buf, &info) {
524+
Ok(IsNull::Yes) => Ok(postgres_protocol::IsNull::Yes),
525+
Ok(IsNull::No) => Ok(postgres_protocol::IsNull::No),
526+
Err(e) => Err(e),
527+
}
528+
},
529+
Some(1),
530+
buf)
531+
});
532+
match r {
533+
Ok(()) => {}
534+
Err(frontend::BindError::Conversion(e)) => return Err(Error::Conversion(e)),
535+
Err(frontend::BindError::Serialization(e)) => return Err(Error::Io(e)),
526536
}
527537
}
528538

529-
try!(self.stream.write_message(&frontend::Bind {
530-
portal: portal_name,
531-
statement: &stmt_name,
532-
formats: &[1],
533-
values: &values,
534-
result_formats: &[1],
535-
}));
536539
try!(self.stream.write_message(&frontend::Execute {
537540
portal: portal_name,
538541
max_rows: row_limit,
@@ -592,10 +595,7 @@ impl InnerConnection {
592595
}
593596

594597
fn close_statement(&mut self, name: &str, type_: u8) -> Result<()> {
595-
try!(self.stream.write_message(&frontend::Close {
596-
variant: type_,
597-
name: name,
598-
}));
598+
try!(self.stream.write_message2(|buf| frontend::close(type_, name, buf)));
599599
try!(self.stream.write_message(&frontend::Sync));
600600
try!(self.stream.flush());
601601
let resp = match try!(self.read_message()) {
@@ -662,7 +662,7 @@ impl InnerConnection {
662662
let row = rows.pop_front().unwrap();
663663

664664
let (name, type_, elem_oid, rngsubtype, basetype, schema, relid) = {
665-
let ctx = SessionInfo::new(self);
665+
let ctx = SessionInfo::new(&self.parameters);
666666
let name = try!(String::from_sql(&Type::Name, &mut &**row[0].as_ref().unwrap(), &ctx)
667667
.map_err(Error::Conversion));
668668
let type_ = try!(i8::from_sql(&Type::Char, &mut &**row[1].as_ref().unwrap(), &ctx)
@@ -740,7 +740,7 @@ impl InnerConnection {
740740
let mut rows = VecDeque::new();
741741
try!(self.read_rows(&mut rows));
742742

743-
let ctx = SessionInfo::new(self);
743+
let ctx = SessionInfo::new(&self.parameters);
744744
let mut variants = vec![];
745745
for row in rows {
746746
variants.push(try!(String::from_sql(&Type::Name, &mut &**row[0].as_ref().unwrap(), &ctx)
@@ -776,7 +776,7 @@ impl InnerConnection {
776776
let mut fields = vec![];
777777
for row in rows {
778778
let (name, type_) = {
779-
let ctx = SessionInfo::new(self);
779+
let ctx = SessionInfo::new(&self.parameters);
780780
let name =
781781
try!(String::from_sql(&Type::Name, &mut &**row[0].as_ref().unwrap(), &ctx)
782782
.map_err(Error::Conversion));
@@ -1316,7 +1316,7 @@ trait LazyRowsNew<'trans, 'stmt> {
13161316
}
13171317

13181318
trait SessionInfoNew<'a> {
1319-
fn new(conn: &'a InnerConnection) -> SessionInfo<'a>;
1319+
fn new(params: &'a HashMap<String, String>) -> SessionInfo<'a>;
13201320
}
13211321

13221322
trait StatementInternals<'conn> {

src/priv_io.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ impl MessageStream {
3939
self.stream.get_ref()
4040
}
4141

42+
pub fn write_message2<F, E>(&mut self, f: F) -> Result<(), E>
43+
where F: FnOnce(&mut Vec<u8>) -> Result<(), E>,
44+
E: From<io::Error>
45+
{
46+
self.buf.clear();
47+
try!(f(&mut self.buf));
48+
self.stream.write_all(&self.buf).map_err(From::from)
49+
}
50+
4251
pub fn write_message(&mut self, message: &frontend::Message) -> io::Result<()> {
4352
self.buf.clear();
4453
try!(frontend::Message::write(message, &mut self.buf));

src/rows.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,8 @@ impl<'a> Row<'a> {
237237
}
238238
let conn = self.stmt.conn().conn.borrow();
239239
let value = match self.data[idx] {
240-
Some(ref data) => FromSql::from_sql(ty, &mut &**data, &SessionInfo::new(&*conn)),
241-
None => FromSql::from_sql_null(ty, &SessionInfo::new(&*conn)),
240+
Some(ref data) => FromSql::from_sql(ty, &mut &**data, &SessionInfo::new(&conn.parameters)),
241+
None => FromSql::from_sql_null(ty, &SessionInfo::new(&conn.parameters)),
242242
};
243243
Some(value.map_err(Error::Conversion))
244244
}

src/stmt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ impl<'a> CopyInfo<'a> {
528528

529529
/// Returns session info for the associated connection.
530530
pub fn session_info<'b>(&'b self) -> SessionInfo<'b> {
531-
SessionInfo::new(&*self.conn)
531+
SessionInfo::new(&self.conn.parameters)
532532
}
533533
}
534534

src/types/mod.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Traits dealing with Postgres data types
22
33
use fallible_iterator::FallibleIterator;
4+
use postgres_protocol;
45
use postgres_protocol::types::{self, ArrayDimension};
56
use std::collections::HashMap;
67
use std::error::Error;
@@ -12,7 +13,7 @@ pub use postgres_protocol::Oid;
1213

1314
pub use self::type_gen::Type;
1415
pub use self::special::{Date, Timestamp};
15-
use {SessionInfoNew, InnerConnection, OtherNew, WrongTypeNew, FieldNew};
16+
use {SessionInfoNew, OtherNew, WrongTypeNew, FieldNew};
1617

1718
/// Generates a simple implementation of `ToSql::accepts` which accepts the
1819
/// types passed to it.
@@ -82,29 +83,22 @@ mod special;
8283
mod type_gen;
8384

8485
/// A structure providing information for conversion methods.
86+
#[derive(Debug)]
8587
pub struct SessionInfo<'a> {
86-
conn: &'a InnerConnection,
88+
parameters: &'a HashMap<String, String>,
8789
}
8890

8991
impl<'a> SessionInfoNew<'a> for SessionInfo<'a> {
90-
fn new(conn: &'a InnerConnection) -> SessionInfo<'a> {
91-
SessionInfo { conn: conn }
92+
fn new(parameters: &'a HashMap<String, String>) -> SessionInfo<'a> {
93+
SessionInfo { parameters: parameters }
9294
}
9395
}
9496

9597
impl<'a> SessionInfo<'a> {
9698
/// Returns the value of the specified Postgres backend parameter, such
9799
/// as `timezone` or `server_version`.
98100
pub fn parameter(&self, param: &str) -> Option<&'a str> {
99-
self.conn.parameters.get(param).map(|s| &**s)
100-
}
101-
}
102-
103-
impl<'a> fmt::Debug for SessionInfo<'a> {
104-
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
105-
fmt.debug_struct("SessionInfo")
106-
.field("parameters", &self.conn.parameters)
107-
.finish()
101+
self.parameters.get(param).map(|s| &**s)
108102
}
109103
}
110104

@@ -607,8 +601,8 @@ impl<'a, T: ToSql> ToSql for &'a [T] {
607601
self.iter(),
608602
|e, w| {
609603
match try!(e.to_sql(member_type, w, ctx)) {
610-
IsNull::No => Ok(types::IsNull::No),
611-
IsNull::Yes => Ok(types::IsNull::Yes),
604+
IsNull::No => Ok(postgres_protocol::IsNull::No),
605+
IsNull::Yes => Ok(postgres_protocol::IsNull::Yes),
612606
}
613607
},
614608
w));

0 commit comments

Comments
 (0)