Skip to content

Commit f135d22

Browse files
committed
remove Frontend messages
1 parent ad6fa4d commit f135d22

File tree

4 files changed

+32
-104
lines changed

4 files changed

+32
-104
lines changed

src/lib.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ use postgres_protocol::message::frontend;
6565

6666
use error::{Error, ConnectError, SqlState, DbError};
6767
use io::{TlsStream, TlsHandshake};
68-
use message::{Frontend, Backend, RowDescriptionEntry};
69-
use message::{WriteMessage, ReadMessage};
68+
use message::{Backend, RowDescriptionEntry, ReadMessage};
7069
use notification::{Notifications, Notification};
7170
use params::{ConnectParams, IntoConnectParams, UserInfo};
7271
use rows::{Rows, LazyRows};
@@ -306,17 +305,11 @@ impl InnerConnection {
306305
fn write_message<M>(&mut self, message: &M) -> std_io::Result<()>
307306
where M: frontend::Message
308307
{
308+
debug_assert!(!self.desynchronized);
309309
self.io_buf.clear();
310310
try!(message.write(&mut self.io_buf));
311-
self.stream.write_all(&self.io_buf)
312-
}
313-
314-
fn write_messages(&mut self, messages: &[Frontend]) -> std_io::Result<()> {
315-
debug_assert!(!self.desynchronized);
316-
for message in messages {
317-
try_desync!(self, self.stream.write_message(message));
318-
}
319-
Ok(try_desync!(self, self.stream.flush()))
311+
try_desync!(self, self.stream.write_all(&self.io_buf));
312+
Ok(())
320313
}
321314

322315
fn read_message_with_notification(&mut self) -> std_io::Result<Backend> {

src/message.rs

Lines changed: 1 addition & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::io;
22
use std::io::prelude::*;
33
use std::mem;
44
use std::time::Duration;
5-
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
5+
use byteorder::{BigEndian, ReadBytesExt};
66

77
use types::Oid;
88
use priv_io::StreamOptions;
@@ -81,77 +81,6 @@ pub struct RowDescriptionEntry {
8181
pub format: i16,
8282
}
8383

84-
pub enum Frontend<'a> {
85-
CopyData {
86-
data: &'a [u8],
87-
},
88-
CopyDone,
89-
CopyFail {
90-
message: &'a str,
91-
},
92-
Execute {
93-
portal: &'a str,
94-
max_rows: i32,
95-
},
96-
Sync,
97-
}
98-
99-
#[doc(hidden)]
100-
trait WriteCStr {
101-
fn write_cstr(&mut self, s: &str) -> io::Result<()>;
102-
}
103-
104-
impl<W: Write> WriteCStr for W {
105-
fn write_cstr(&mut self, s: &str) -> io::Result<()> {
106-
try!(self.write_all(s.as_bytes()));
107-
Ok(try!(self.write_u8(0)))
108-
}
109-
}
110-
111-
#[doc(hidden)]
112-
pub trait WriteMessage {
113-
fn write_message(&mut self, &Frontend) -> io::Result<()>;
114-
}
115-
116-
impl<W: Write> WriteMessage for W {
117-
#[allow(cyclomatic_complexity)]
118-
fn write_message(&mut self, message: &Frontend) -> io::Result<()> {
119-
let mut buf = vec![];
120-
let ident;
121-
122-
match *message {
123-
Frontend::CopyData { data } => {
124-
ident = Some(b'd');
125-
try!(buf.write_all(data));
126-
}
127-
Frontend::CopyDone => ident = Some(b'c'),
128-
Frontend::CopyFail { message } => {
129-
ident = Some(b'f');
130-
try!(buf.write_cstr(message));
131-
}
132-
Frontend::Execute { portal, max_rows } => {
133-
ident = Some(b'E');
134-
try!(buf.write_cstr(portal));
135-
try!(buf.write_i32::<BigEndian>(max_rows));
136-
}
137-
Frontend::Sync => ident = Some(b'S'),
138-
}
139-
140-
if let Some(ident) = ident {
141-
try!(self.write_u8(ident));
142-
}
143-
144-
// add size of length value
145-
if buf.len() > u32::max_value() as usize - mem::size_of::<u32>() {
146-
return Err(io::Error::new(io::ErrorKind::InvalidInput, "value too large to transmit"));
147-
}
148-
try!(self.write_u32::<BigEndian>((buf.len() + mem::size_of::<u32>()) as u32));
149-
try!(self.write_all(&*buf));
150-
151-
Ok(())
152-
}
153-
}
154-
15584
#[doc(hidden)]
15685
trait ReadCStr {
15786
fn read_cstr(&mut self) -> io::Result<String>;

src/rows.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@
33
use std::ascii::AsciiExt;
44
use std::borrow::Cow;
55
use std::collections::VecDeque;
6+
use std::io::Write;
67
use std::fmt;
78
use std::ops::Deref;
89
use std::slice;
10+
use postgres_protocol::message::frontend;
911

1012
use {Result, SessionInfoNew, RowsNew, LazyRowsNew, StatementInternals, WrongTypeNew};
1113
use transaction::Transaction;
1214
use types::{FromSql, SessionInfo, WrongType};
1315
use stmt::{Statement, Column};
1416
use error::Error;
15-
use message::Frontend;
1617

1718
enum StatementContainer<'a> {
1819
Borrowed(&'a Statement<'a>),
@@ -350,11 +351,12 @@ impl<'trans, 'stmt> LazyRows<'trans, 'stmt> {
350351
fn execute(&mut self) -> Result<()> {
351352
let mut conn = self.stmt.conn().conn.borrow_mut();
352353

353-
try!(conn.write_messages(&[Frontend::Execute {
354-
portal: &self.name,
355-
max_rows: self.row_limit,
356-
},
357-
Frontend::Sync]));
354+
try!(conn.write_message(&frontend::Execute {
355+
portal: &self.name,
356+
max_rows: self.row_limit,
357+
}));
358+
try!(conn.write_message(&frontend::Sync));
359+
try!(conn.stream.flush());
358360
conn.read_rows(&mut self.data).map(|more_rows| self.more_rows = more_rows)
359361
}
360362

src/stmt.rs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ use std::collections::VecDeque;
55
use std::fmt;
66
use std::io::{self, Read, Write};
77
use std::sync::Arc;
8+
use postgres_protocol::message::frontend;
89

910
use error::{Error, DbError};
1011
use types::{SessionInfo, Type, ToSql};
11-
use message::{WriteMessage, Backend, Frontend};
12+
use message::Backend;
1213
use rows::{Rows, LazyRows};
1314
use transaction::Transaction;
1415
use {bad_response, Connection, StatementInternals, Result, RowsNew, InnerConnection, SessionInfoNew,
@@ -146,11 +147,11 @@ impl<'conn> Statement<'conn> {
146147
break;
147148
}
148149
Backend::CopyInResponse { .. } => {
149-
try!(conn.write_messages(&[Frontend::CopyFail {
150-
message: "COPY queries cannot be directly \
151-
executed",
152-
},
153-
Frontend::Sync]));
150+
try!(conn.write_message(&frontend::CopyFail {
151+
message: "COPY queries cannot be directly executed",
152+
}));
153+
try!(conn.write_message(&frontend::Sync));
154+
try!(conn.stream.flush());
154155
}
155156
Backend::CopyOutResponse { .. } => {
156157
loop {
@@ -296,13 +297,13 @@ impl<'conn> Statement<'conn> {
296297
match fill_copy_buf(&mut buf, r, &info) {
297298
Ok(0) => break,
298299
Ok(len) => {
299-
try_desync!(info.conn,
300-
info.conn.stream.write_message(&Frontend::CopyData { data: &buf[..len] }));
300+
try!(info.conn.write_message(&frontend::CopyData { data: &buf[..len] }));
301301
}
302302
Err(err) => {
303-
try!(info.conn.write_messages(&[Frontend::CopyFail { message: "" },
304-
Frontend::CopyDone,
305-
Frontend::Sync]));
303+
try!(info.conn.write_message(&frontend::CopyFail { message: "" }));
304+
try!(info.conn.write_message(&frontend::CopyDone));
305+
try!(info.conn.write_message(&frontend::Sync));
306+
try!(info.conn.stream.flush());
306307
match try!(info.conn.read_message()) {
307308
Backend::ErrorResponse { .. } => {
308309
// expected from the CopyFail
@@ -318,7 +319,9 @@ impl<'conn> Statement<'conn> {
318319
}
319320
}
320321

321-
try!(info.conn.write_messages(&[Frontend::CopyDone, Frontend::Sync]));
322+
try!(info.conn.write_message(&frontend::CopyDone));
323+
try!(info.conn.write_message(&frontend::Sync));
324+
try!(info.conn.stream.flush());
322325

323326
let num = match try!(info.conn.read_message()) {
324327
Backend::CommandComplete { tag } => parse_update_count(tag),
@@ -365,9 +368,10 @@ impl<'conn> Statement<'conn> {
365368
let (format, column_formats) = match try!(conn.read_message()) {
366369
Backend::CopyOutResponse { format, column_formats } => (format, column_formats),
367370
Backend::CopyInResponse { .. } => {
368-
try!(conn.write_messages(&[Frontend::CopyFail { message: "" },
369-
Frontend::CopyDone,
370-
Frontend::Sync]));
371+
try!(conn.write_message(&frontend::CopyFail { message: "" }));
372+
try!(conn.write_message(&frontend::CopyDone));
373+
try!(conn.write_message(&frontend::Sync));
374+
try!(conn.stream.flush());
371375
match try!(conn.read_message()) {
372376
Backend::ErrorResponse { .. } => {
373377
// expected from the CopyFail

0 commit comments

Comments
 (0)