Overhaul the copy_out API

Returning a Reader ends up with a really weird user experience where you
have to make sure to drop it before making any other calls and it has to
internally fast forward to the end of the data even if the user drops it
early. Simply taking a Writer that all data is pushed into is
sigificantly more straightforward.
This commit is contained in:
Steven Fackler
2015-09-15 23:11:14 -07:00
parent 5fe76e2dec
commit 03150f4cae
2 changed files with 74 additions and 134 deletions

View File

@@ -778,26 +778,10 @@ fn test_copy_out() {
CREATE TEMPORARY TABLE foo (id INT);
INSERT INTO foo (id) VALUES (0), (1), (2), (3)"));
let stmt = or_panic!(conn.prepare("COPY (SELECT id FROM foo ORDER BY id) TO STDOUT"));
let mut reader = or_panic!(stmt.copy_out(&[]));
let mut out = vec![];
or_panic!(reader.read_to_end(&mut out));
assert_eq!(out, b"0\n1\n2\n3\n");
drop(reader);
or_panic!(conn.batch_execute("SELECT 1"));
}
#[test]
fn test_copy_out_partial_read() {
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
or_panic!(conn.batch_execute("
CREATE TEMPORARY TABLE foo (id INT);
INSERT INTO foo (id) VALUES (0), (1), (2), (3)"));
let stmt = or_panic!(conn.prepare("COPY (SELECT id FROM foo ORDER BY id) TO STDOUT"));
let mut reader = or_panic!(stmt.copy_out(&[]));
let mut out = vec![];
or_panic!(reader.by_ref().take(5).read_to_end(&mut out));
assert_eq!(out, b"0\n1\n2");
drop(reader);
let mut buf = vec![];
let count = or_panic!(stmt.copy_out(&[], &mut buf));
assert_eq!(count, 4);
assert_eq!(buf, b"0\n1\n2\n3\n");
or_panic!(conn.batch_execute("SELECT 1"));
}