Skip to content

Commit 0c3f3b1

Browse files
committed
More docs + return copy count from execute
1 parent 91a90f0 commit 0c3f3b1

3 files changed

Lines changed: 20 additions & 11 deletions

File tree

src/lib.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ use openssl::ssl::SslContext;
7272
use serialize::hex::ToHex;
7373
use std::cell::{Cell, RefCell};
7474
use std::collections::HashMap;
75-
use std::from_str::FromStr;
7675
use std::io::{BufferedStream, IoResult, MemWriter};
7776
use std::io::net::ip::Port;
7877
use std::mem;
@@ -614,7 +613,6 @@ impl InnerPostgresConnection {
614613
conn: conn,
615614
name: stmt_name,
616615
column_types: column_types,
617-
next_portal_id: Cell::new(0),
618616
finished: false,
619617
})
620618
}
@@ -1256,8 +1254,7 @@ impl<'conn> PostgresStatement<'conn> {
12561254
return Err(PgDbError(PostgresDbError::new(fields)));
12571255
}
12581256
CommandComplete { tag } => {
1259-
let s = tag.as_slice().split(' ').last().unwrap();
1260-
num = FromStr::from_str(s).unwrap_or(0);
1257+
num = util::parse_update_count(tag);
12611258
break;
12621259
}
12631260
EmptyQueryResponse => {
@@ -1568,11 +1565,11 @@ impl<'trans, 'stmt> Iterator<PostgresResult<PostgresRow<'stmt>>>
15681565
}
15691566
}
15701567

1568+
/// A prepared COPY FROM STDIN statement
15711569
pub struct PostgresCopyInStatement<'a> {
15721570
conn: &'a PostgresConnection,
15731571
name: String,
15741572
column_types: Vec<PostgresType>,
1575-
next_portal_id: Cell<uint>,
15761573
finished: bool,
15771574
}
15781575

@@ -1592,7 +1589,13 @@ impl<'a> PostgresCopyInStatement<'a> {
15921589
conn.close_statement(self.name.as_slice())
15931590
}
15941591

1595-
pub fn execute<'b, I, J>(&self, mut rows: I) -> PostgresResult<()>
1592+
/// Executes the prepared statement.
1593+
///
1594+
/// Each iterator retuned by the `rows` iterator will be interpreted as
1595+
/// providing a single result row.
1596+
///
1597+
/// Returns the number of rows copied.
1598+
pub fn execute<'b, I, J>(&self, mut rows: I) -> PostgresResult<uint>
15961599
where I: Iterator<J>, J: Iterator<&'b ToSql + 'b> {
15971600
let mut conn = self.conn.conn.borrow_mut();
15981601

@@ -1680,8 +1683,8 @@ impl<'a> PostgresCopyInStatement<'a> {
16801683
try_pg!(conn.stream.write_message(&Sync));
16811684
try_pg!(conn.stream.flush());
16821685

1683-
match try_pg!(conn.read_message_()) {
1684-
CommandComplete { .. } => {},
1686+
let num = match try_pg!(conn.read_message_()) {
1687+
CommandComplete { tag } => util::parse_update_count(tag),
16851688
ErrorResponse { fields } => {
16861689
try!(conn.wait_for_ready());
16871690
return Err(PgDbError(PostgresDbError::new(fields)));
@@ -1690,9 +1693,10 @@ impl<'a> PostgresCopyInStatement<'a> {
16901693
conn.desynchronized = true;
16911694
return Err(PgBadResponse);
16921695
}
1693-
}
1696+
};
16941697

1695-
conn.wait_for_ready()
1698+
try!(conn.wait_for_ready());
1699+
Ok(num)
16961700
}
16971701

16981702
/// Consumes the statement, clearing it from the Postgres session.

src/util.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,8 @@ pub fn comma_join<'a, W, I>(writer: &mut W, mut strs: I) -> IoResult<()>
1212
}
1313
Ok(())
1414
}
15+
16+
pub fn parse_update_count(tag: String) -> uint {
17+
let s = tag.as_slice().split(' ').last().unwrap();
18+
from_str(s).unwrap_or(0)
19+
}

tests/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,7 @@ fn test_copy_in() {
717717
let stmt = or_fail!(conn.prepare_copy_in("foo", ["id", "name"]));
718718
let data: &[&[&ToSql]] = &[&[&0i32, &"Steven".to_string()], &[&1i32, &None::<String>]];
719719

720-
or_fail!(stmt.execute(data.iter().map(|r| r.iter().map(|&e| e))));
720+
assert_eq!(Ok(2), stmt.execute(data.iter().map(|r| r.iter().map(|&e| e))));
721721

722722
let stmt = or_fail!(conn.prepare("SELECT id, name FROM foo ORDER BY id"));
723723
assert_eq!(vec![(0i32, Some("Steven".to_string())), (1, None)],

0 commit comments

Comments
 (0)