Skip to content

Commit 7edf66f

Browse files
committed
Add batch_execute
1 parent 0c56a5e commit 7edf66f

File tree

10 files changed

+356
-168
lines changed

10 files changed

+356
-168
lines changed

postgres-shared/src/error/mod.rs

Lines changed: 15 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
//! Error types.
2-
31
use fallible_iterator::FallibleIterator;
42
use postgres_protocol::message::backend::ErrorFields;
53
use std::error;
@@ -141,7 +139,7 @@ pub struct DbError {
141139

142140
impl DbError {
143141
#[doc(hidden)]
144-
pub fn new_raw(fields: &mut ErrorFields) -> io::Result<DbError> {
142+
pub fn new(fields: &mut ErrorFields) -> io::Result<DbError> {
145143
let mut severity = None;
146144
let mut parsed_severity = None;
147145
let mut code = None;
@@ -245,22 +243,6 @@ impl DbError {
245243
_p: (),
246244
})
247245
}
248-
249-
#[doc(hidden)]
250-
pub fn new_connect<T>(fields: &mut ErrorFields) -> Result<T, ConnectError> {
251-
match DbError::new_raw(fields) {
252-
Ok(err) => Err(ConnectError::Db(Box::new(err))),
253-
Err(e) => Err(ConnectError::Io(e)),
254-
}
255-
}
256-
257-
#[doc(hidden)]
258-
pub fn new<T>(fields: &mut ErrorFields) -> Result<T, Error> {
259-
match DbError::new_raw(fields) {
260-
Ok(err) => Err(Error::Db(Box::new(err))),
261-
Err(e) => Err(Error::Io(e)),
262-
}
263-
}
264246
}
265247

266248
// manual impl to leave out _p
@@ -299,6 +281,20 @@ impl error::Error for DbError {
299281
}
300282
}
301283

284+
/// Represents the position of an error in a query.
285+
#[derive(Clone, PartialEq, Eq, Debug)]
286+
pub enum ErrorPosition {
287+
/// A position in the original query.
288+
Normal(u32),
289+
/// A position in an internally generated query.
290+
Internal {
291+
/// The byte position.
292+
position: u32,
293+
/// A query generated by the Postgres server.
294+
query: String,
295+
},
296+
}
297+
302298
/// Reasons a new Postgres connection could fail.
303299
#[derive(Debug)]
304300
pub enum ConnectError {
@@ -355,75 +351,3 @@ impl From<DbError> for ConnectError {
355351
ConnectError::Db(Box::new(err))
356352
}
357353
}
358-
359-
/// Represents the position of an error in a query.
360-
#[derive(Clone, PartialEq, Eq, Debug)]
361-
pub enum ErrorPosition {
362-
/// A position in the original query.
363-
Normal(u32),
364-
/// A position in an internally generated query.
365-
Internal {
366-
/// The byte position.
367-
position: u32,
368-
/// A query generated by the Postgres server.
369-
query: String,
370-
},
371-
}
372-
373-
/// An error encountered when communicating with the Postgres server.
374-
#[derive(Debug)]
375-
pub enum Error {
376-
/// An error reported by the Postgres server.
377-
Db(Box<DbError>),
378-
/// An error communicating with the Postgres server.
379-
Io(io::Error),
380-
/// An error converting between Postgres and Rust types.
381-
Conversion(Box<error::Error + Sync + Send>),
382-
}
383-
384-
impl fmt::Display for Error {
385-
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
386-
try!(fmt.write_str(error::Error::description(self)));
387-
match *self {
388-
Error::Db(ref err) => write!(fmt, ": {}", err),
389-
Error::Io(ref err) => write!(fmt, ": {}", err),
390-
Error::Conversion(ref err) => write!(fmt, ": {}", err),
391-
}
392-
}
393-
}
394-
395-
impl error::Error for Error {
396-
fn description(&self) -> &str {
397-
match *self {
398-
Error::Db(_) => "Error reported by Postgres",
399-
Error::Io(_) => "Error communicating with the server",
400-
Error::Conversion(_) => "Error converting between Postgres and Rust types",
401-
}
402-
}
403-
404-
fn cause(&self) -> Option<&error::Error> {
405-
match *self {
406-
Error::Db(ref err) => Some(&**err),
407-
Error::Io(ref err) => Some(err),
408-
Error::Conversion(ref err) => Some(&**err),
409-
}
410-
}
411-
}
412-
413-
impl From<DbError> for Error {
414-
fn from(err: DbError) -> Error {
415-
Error::Db(Box::new(err))
416-
}
417-
}
418-
419-
impl From<io::Error> for Error {
420-
fn from(err: io::Error) -> Error {
421-
Error::Io(err)
422-
}
423-
}
424-
425-
impl From<Error> for io::Error {
426-
fn from(err: Error) -> io::Error {
427-
io::Error::new(io::ErrorKind::Other, err)
428-
}
429-
}

postgres-shared/src/lib.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,51 @@ extern crate fallible_iterator;
55
extern crate phf;
66
extern crate postgres_protocol;
77

8+
use fallible_iterator::{FallibleIterator, FromFallibleIterator};
9+
use std::ops::Range;
10+
811
pub mod error;
912
pub mod params;
13+
14+
pub struct RowData {
15+
buf: Vec<u8>,
16+
indices: Vec<Option<Range<usize>>>,
17+
}
18+
19+
impl<'a> FromFallibleIterator<Option<&'a [u8]>> for RowData {
20+
fn from_fallible_iterator<I>(mut it: I) -> Result<RowData, I::Error>
21+
where I: FallibleIterator<Item = Option<&'a [u8]>>
22+
{
23+
let mut row = RowData {
24+
buf: vec![],
25+
indices: Vec::with_capacity(it.size_hint().0),
26+
};
27+
28+
while let Some(cell) = try!(it.next()) {
29+
let index = match cell {
30+
Some(cell) => {
31+
let base = row.buf.len();
32+
row.buf.extend_from_slice(cell);
33+
Some(base..row.buf.len())
34+
}
35+
None => None,
36+
};
37+
row.indices.push(index);
38+
}
39+
40+
Ok(row)
41+
}
42+
}
43+
44+
impl RowData {
45+
pub fn len(&self) -> usize {
46+
self.indices.len()
47+
}
48+
49+
pub fn get(&self, index: usize) -> Option<&[u8]> {
50+
match &self.indices[index] {
51+
&Some(ref range) => Some(&self.buf[range.clone()]),
52+
&None => None,
53+
}
54+
}
55+
}

postgres-tokio/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ version = "0.1.0"
44
authors = ["Steven Fackler <[email protected]>"]
55

66
[dependencies]
7+
fallible-iterator = "0.1.3"
78
futures = "0.1.7"
89
postgres-shared = { path = "../postgres-shared" }
910
postgres-protocol = "0.2"

postgres-tokio/src/error.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
use std::error;
2+
use std::io;
3+
use std::fmt;
4+
5+
use Connection;
6+
7+
#[doc(inline)]
8+
pub use postgres_shared::error::*;
9+
10+
#[derive(Debug)]
11+
pub enum Error {
12+
Io(io::Error),
13+
Db(Box<DbError>, Connection),
14+
Conversion(Box<error::Error + Sync + Send>, Connection),
15+
}
16+
17+
impl fmt::Display for Error {
18+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
19+
fmt.write_str(error::Error::description(self))?;
20+
match *self {
21+
Error::Db(ref err, _) => write!(fmt, ": {}", err),
22+
Error::Io(ref err) => write!(fmt, ": {}", err),
23+
Error::Conversion(ref err, _) => write!(fmt, ": {}", err),
24+
}
25+
}
26+
}
27+
28+
impl error::Error for Error {
29+
fn description(&self) -> &str {
30+
match *self {
31+
Error::Db(_, _) => "Error reported by Postgres",
32+
Error::Io(_) => "Error communicating with the server",
33+
Error::Conversion(_, _) => "Error converting between Postgres and Rust types",
34+
}
35+
}
36+
37+
fn cause(&self) -> Option<&error::Error> {
38+
match *self {
39+
Error::Db(ref err, _) => Some(&**err),
40+
Error::Io(ref err) => Some(err),
41+
Error::Conversion(ref err, _) => Some(&**err),
42+
}
43+
}
44+
}
45+
46+
impl From<io::Error> for Error {
47+
fn from(err: io::Error) -> Error {
48+
Error::Io(err)
49+
}
50+
}

0 commit comments

Comments
 (0)