More docs + return copy count from execute

This commit is contained in:
Steven Fackler 2014-09-30 00:11:23 -07:00
parent 91a90f0d1c
commit 0c3f3b1e83
3 changed files with 20 additions and 11 deletions

View File

@ -72,7 +72,6 @@ use openssl::ssl::SslContext;
use serialize::hex::ToHex; use serialize::hex::ToHex;
use std::cell::{Cell, RefCell}; use std::cell::{Cell, RefCell};
use std::collections::HashMap; use std::collections::HashMap;
use std::from_str::FromStr;
use std::io::{BufferedStream, IoResult, MemWriter}; use std::io::{BufferedStream, IoResult, MemWriter};
use std::io::net::ip::Port; use std::io::net::ip::Port;
use std::mem; use std::mem;
@ -614,7 +613,6 @@ impl InnerPostgresConnection {
conn: conn, conn: conn,
name: stmt_name, name: stmt_name,
column_types: column_types, column_types: column_types,
next_portal_id: Cell::new(0),
finished: false, finished: false,
}) })
} }
@ -1256,8 +1254,7 @@ impl<'conn> PostgresStatement<'conn> {
return Err(PgDbError(PostgresDbError::new(fields))); return Err(PgDbError(PostgresDbError::new(fields)));
} }
CommandComplete { tag } => { CommandComplete { tag } => {
let s = tag.as_slice().split(' ').last().unwrap(); num = util::parse_update_count(tag);
num = FromStr::from_str(s).unwrap_or(0);
break; break;
} }
EmptyQueryResponse => { EmptyQueryResponse => {
@ -1568,11 +1565,11 @@ impl<'trans, 'stmt> Iterator<PostgresResult<PostgresRow<'stmt>>>
} }
} }
/// A prepared COPY FROM STDIN statement
pub struct PostgresCopyInStatement<'a> { pub struct PostgresCopyInStatement<'a> {
conn: &'a PostgresConnection, conn: &'a PostgresConnection,
name: String, name: String,
column_types: Vec<PostgresType>, column_types: Vec<PostgresType>,
next_portal_id: Cell<uint>,
finished: bool, finished: bool,
} }
@ -1592,7 +1589,13 @@ impl<'a> PostgresCopyInStatement<'a> {
conn.close_statement(self.name.as_slice()) conn.close_statement(self.name.as_slice())
} }
pub fn execute<'b, I, J>(&self, mut rows: I) -> PostgresResult<()> /// Executes the prepared statement.
///
/// Each iterator retuned by the `rows` iterator will be interpreted as
/// providing a single result row.
///
/// Returns the number of rows copied.
pub fn execute<'b, I, J>(&self, mut rows: I) -> PostgresResult<uint>
where I: Iterator<J>, J: Iterator<&'b ToSql + 'b> { where I: Iterator<J>, J: Iterator<&'b ToSql + 'b> {
let mut conn = self.conn.conn.borrow_mut(); let mut conn = self.conn.conn.borrow_mut();
@ -1680,8 +1683,8 @@ impl<'a> PostgresCopyInStatement<'a> {
try_pg!(conn.stream.write_message(&Sync)); try_pg!(conn.stream.write_message(&Sync));
try_pg!(conn.stream.flush()); try_pg!(conn.stream.flush());
match try_pg!(conn.read_message_()) { let num = match try_pg!(conn.read_message_()) {
CommandComplete { .. } => {}, CommandComplete { tag } => util::parse_update_count(tag),
ErrorResponse { fields } => { ErrorResponse { fields } => {
try!(conn.wait_for_ready()); try!(conn.wait_for_ready());
return Err(PgDbError(PostgresDbError::new(fields))); return Err(PgDbError(PostgresDbError::new(fields)));
@ -1690,9 +1693,10 @@ impl<'a> PostgresCopyInStatement<'a> {
conn.desynchronized = true; conn.desynchronized = true;
return Err(PgBadResponse); return Err(PgBadResponse);
} }
} };
conn.wait_for_ready() try!(conn.wait_for_ready());
Ok(num)
} }
/// Consumes the statement, clearing it from the Postgres session. /// Consumes the statement, clearing it from the Postgres session.

View File

@ -12,3 +12,8 @@ pub fn comma_join<'a, W, I>(writer: &mut W, mut strs: I) -> IoResult<()>
} }
Ok(()) Ok(())
} }
pub fn parse_update_count(tag: String) -> uint {
let s = tag.as_slice().split(' ').last().unwrap();
from_str(s).unwrap_or(0)
}

View File

@ -717,7 +717,7 @@ fn test_copy_in() {
let stmt = or_fail!(conn.prepare_copy_in("foo", ["id", "name"])); let stmt = or_fail!(conn.prepare_copy_in("foo", ["id", "name"]));
let data: &[&[&ToSql]] = &[&[&0i32, &"Steven".to_string()], &[&1i32, &None::<String>]]; let data: &[&[&ToSql]] = &[&[&0i32, &"Steven".to_string()], &[&1i32, &None::<String>]];
or_fail!(stmt.execute(data.iter().map(|r| r.iter().map(|&e| e)))); assert_eq!(Ok(2), stmt.execute(data.iter().map(|r| r.iter().map(|&e| e))));
let stmt = or_fail!(conn.prepare("SELECT id, name FROM foo ORDER BY id")); let stmt = or_fail!(conn.prepare("SELECT id, name FROM foo ORDER BY id"));
assert_eq!(vec![(0i32, Some("Steven".to_string())), (1, None)], assert_eq!(vec![(0i32, Some("Steven".to_string())), (1, None)],