2018-12-23 21:08:02 +00:00
|
|
|
use std::io::Read;
|
2018-12-22 05:01:49 +00:00
|
|
|
use tokio_postgres::types::Type;
|
|
|
|
use tokio_postgres::NoTls;
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn prepare() {
|
|
|
|
let mut client = Client::connect("host=localhost port=5433 user=postgres", NoTls).unwrap();
|
|
|
|
|
|
|
|
let stmt = client.prepare("SELECT 1::INT, $1::TEXT").unwrap();
|
|
|
|
assert_eq!(stmt.params(), &[Type::TEXT]);
|
|
|
|
assert_eq!(stmt.columns().len(), 2);
|
|
|
|
assert_eq!(stmt.columns()[0].type_(), &Type::INT4);
|
|
|
|
assert_eq!(stmt.columns()[1].type_(), &Type::TEXT);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn query_prepared() {
|
|
|
|
let mut client = Client::connect("host=localhost port=5433 user=postgres", NoTls).unwrap();
|
|
|
|
|
|
|
|
let stmt = client.prepare("SELECT $1::TEXT").unwrap();
|
2019-01-30 03:29:33 +00:00
|
|
|
let rows = client.query(&stmt, &[&"hello"]).unwrap().to_vec().unwrap();
|
2018-12-22 05:01:49 +00:00
|
|
|
assert_eq!(rows.len(), 1);
|
|
|
|
assert_eq!(rows[0].get::<_, &str>(0), "hello");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn query_unprepared() {
|
|
|
|
let mut client = Client::connect("host=localhost port=5433 user=postgres", NoTls).unwrap();
|
|
|
|
|
2018-12-29 04:39:32 +00:00
|
|
|
let rows = client
|
|
|
|
.query("SELECT $1::TEXT", &[&"hello"])
|
|
|
|
.unwrap()
|
2019-01-30 03:29:33 +00:00
|
|
|
.to_vec()
|
2018-12-29 04:39:32 +00:00
|
|
|
.unwrap();
|
2018-12-22 05:01:49 +00:00
|
|
|
assert_eq!(rows.len(), 1);
|
|
|
|
assert_eq!(rows[0].get::<_, &str>(0), "hello");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn transaction_commit() {
|
|
|
|
let mut client = Client::connect("host=localhost port=5433 user=postgres", NoTls).unwrap();
|
|
|
|
|
|
|
|
client
|
|
|
|
.batch_execute("CREATE TEMPORARY TABLE foo (id SERIAL PRIMARY KEY)")
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let mut transaction = client.transaction().unwrap();
|
|
|
|
|
|
|
|
transaction
|
|
|
|
.execute("INSERT INTO foo DEFAULT VALUES", &[])
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
transaction.commit().unwrap();
|
|
|
|
|
2018-12-29 04:39:32 +00:00
|
|
|
let rows = client
|
|
|
|
.query("SELECT * FROM foo", &[])
|
|
|
|
.unwrap()
|
2019-01-30 03:29:33 +00:00
|
|
|
.to_vec()
|
2018-12-29 04:39:32 +00:00
|
|
|
.unwrap();
|
2018-12-22 05:01:49 +00:00
|
|
|
assert_eq!(rows.len(), 1);
|
|
|
|
assert_eq!(rows[0].get::<_, i32>(0), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn transaction_rollback() {
|
|
|
|
let mut client = Client::connect("host=localhost port=5433 user=postgres", NoTls).unwrap();
|
|
|
|
|
|
|
|
client
|
|
|
|
.batch_execute("CREATE TEMPORARY TABLE foo (id SERIAL PRIMARY KEY)")
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let mut transaction = client.transaction().unwrap();
|
|
|
|
|
|
|
|
transaction
|
|
|
|
.execute("INSERT INTO foo DEFAULT VALUES", &[])
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
transaction.rollback().unwrap();
|
|
|
|
|
2018-12-29 04:39:32 +00:00
|
|
|
let rows = client
|
|
|
|
.query("SELECT * FROM foo", &[])
|
|
|
|
.unwrap()
|
2019-01-30 03:29:33 +00:00
|
|
|
.to_vec()
|
2018-12-29 04:39:32 +00:00
|
|
|
.unwrap();
|
2018-12-22 05:01:49 +00:00
|
|
|
assert_eq!(rows.len(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn transaction_drop() {
|
|
|
|
let mut client = Client::connect("host=localhost port=5433 user=postgres", NoTls).unwrap();
|
|
|
|
|
|
|
|
client
|
|
|
|
.batch_execute("CREATE TEMPORARY TABLE foo (id SERIAL PRIMARY KEY)")
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let mut transaction = client.transaction().unwrap();
|
|
|
|
|
|
|
|
transaction
|
|
|
|
.execute("INSERT INTO foo DEFAULT VALUES", &[])
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
drop(transaction);
|
|
|
|
|
2018-12-29 04:39:32 +00:00
|
|
|
let rows = client
|
|
|
|
.query("SELECT * FROM foo", &[])
|
|
|
|
.unwrap()
|
2019-01-30 03:29:33 +00:00
|
|
|
.to_vec()
|
2018-12-29 04:39:32 +00:00
|
|
|
.unwrap();
|
2018-12-22 05:01:49 +00:00
|
|
|
assert_eq!(rows.len(), 0);
|
|
|
|
}
|
2018-12-22 21:38:35 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn nested_transactions() {
|
|
|
|
let mut client = Client::connect("host=localhost port=5433 user=postgres", NoTls).unwrap();
|
|
|
|
|
|
|
|
client
|
|
|
|
.batch_execute("CREATE TEMPORARY TABLE foo (id INT PRIMARY KEY)")
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let mut transaction = client.transaction().unwrap();
|
|
|
|
|
|
|
|
transaction
|
|
|
|
.execute("INSERT INTO foo (id) VALUES (1)", &[])
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let mut transaction2 = transaction.transaction().unwrap();
|
|
|
|
|
|
|
|
transaction2
|
|
|
|
.execute("INSERT INTO foo (id) VALUES (2)", &[])
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
transaction2.rollback().unwrap();
|
|
|
|
|
|
|
|
let rows = transaction
|
|
|
|
.query("SELECT id FROM foo ORDER BY id", &[])
|
2018-12-29 04:39:32 +00:00
|
|
|
.unwrap()
|
2019-01-30 03:29:33 +00:00
|
|
|
.to_vec()
|
2018-12-22 21:38:35 +00:00
|
|
|
.unwrap();
|
|
|
|
assert_eq!(rows.len(), 1);
|
|
|
|
assert_eq!(rows[0].get::<_, i32>(0), 1);
|
|
|
|
|
|
|
|
let mut transaction3 = transaction.transaction().unwrap();
|
|
|
|
|
|
|
|
transaction3
|
|
|
|
.execute("INSERT INTO foo (id) VALUES(3)", &[])
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let mut transaction4 = transaction3.transaction().unwrap();
|
|
|
|
|
|
|
|
transaction4
|
|
|
|
.execute("INSERT INTO foo (id) VALUES(4)", &[])
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
transaction4.commit().unwrap();
|
|
|
|
transaction3.commit().unwrap();
|
|
|
|
transaction.commit().unwrap();
|
|
|
|
|
2018-12-29 04:39:32 +00:00
|
|
|
let rows = client
|
|
|
|
.query("SELECT id FROM foo ORDER BY id", &[])
|
|
|
|
.unwrap()
|
2019-01-30 03:29:33 +00:00
|
|
|
.to_vec()
|
2018-12-29 04:39:32 +00:00
|
|
|
.unwrap();
|
2018-12-22 21:38:35 +00:00
|
|
|
assert_eq!(rows.len(), 3);
|
|
|
|
assert_eq!(rows[0].get::<_, i32>(0), 1);
|
|
|
|
assert_eq!(rows[1].get::<_, i32>(0), 3);
|
|
|
|
assert_eq!(rows[2].get::<_, i32>(0), 4);
|
|
|
|
}
|
2018-12-23 05:42:03 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn copy_in() {
|
|
|
|
let mut client = Client::connect("host=localhost port=5433 user=postgres", NoTls).unwrap();
|
|
|
|
|
|
|
|
client
|
|
|
|
.batch_execute("CREATE TEMPORARY TABLE foo (id INT, name TEXT)")
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
client
|
|
|
|
.copy_in(
|
|
|
|
"COPY foo FROM stdin",
|
|
|
|
&[],
|
|
|
|
&mut &b"1\tsteven\n2\ttimothy"[..],
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let rows = client
|
|
|
|
.query("SELECT id, name FROM foo ORDER BY id", &[])
|
2018-12-29 04:39:32 +00:00
|
|
|
.unwrap()
|
2019-01-30 03:29:33 +00:00
|
|
|
.to_vec()
|
2018-12-23 05:42:03 +00:00
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(rows.len(), 2);
|
|
|
|
assert_eq!(rows[0].get::<_, i32>(0), 1);
|
|
|
|
assert_eq!(rows[0].get::<_, &str>(1), "steven");
|
|
|
|
assert_eq!(rows[1].get::<_, i32>(0), 2);
|
|
|
|
assert_eq!(rows[1].get::<_, &str>(1), "timothy");
|
|
|
|
}
|
2018-12-23 21:08:02 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn copy_out() {
|
|
|
|
let mut client = Client::connect("host=localhost port=5433 user=postgres", NoTls).unwrap();
|
|
|
|
|
|
|
|
client
|
|
|
|
.batch_execute(
|
|
|
|
"
|
|
|
|
CREATE TEMPORARY TABLE foo (id INT, name TEXT);
|
|
|
|
|
|
|
|
INSERT INTO foo (id, name) VALUES (1, 'steven'), (2, 'timothy');
|
|
|
|
",
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let mut reader = client
|
|
|
|
.copy_out("COPY foo (id, name) TO STDOUT", &[])
|
|
|
|
.unwrap();
|
|
|
|
let mut s = String::new();
|
|
|
|
reader.read_to_string(&mut s).unwrap();
|
2018-12-23 21:28:13 +00:00
|
|
|
drop(reader);
|
2018-12-23 21:08:02 +00:00
|
|
|
|
|
|
|
assert_eq!(s, "1\tsteven\n2\ttimothy\n");
|
|
|
|
|
|
|
|
client.batch_execute("SELECT 1").unwrap();
|
|
|
|
}
|
2018-12-28 20:59:17 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn portal() {
|
|
|
|
let mut client = Client::connect("host=localhost port=5433 user=postgres", NoTls).unwrap();
|
|
|
|
|
|
|
|
client
|
|
|
|
.batch_execute(
|
|
|
|
"
|
|
|
|
CREATE TEMPORARY TABLE foo (id INT);
|
|
|
|
|
|
|
|
INSERT INTO foo (id) VALUES (1), (2), (3);
|
|
|
|
",
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let mut transaction = client.transaction().unwrap();
|
|
|
|
|
|
|
|
let portal = transaction
|
|
|
|
.bind("SELECT * FROM foo ORDER BY id", &[])
|
|
|
|
.unwrap();
|
|
|
|
|
2018-12-29 04:39:32 +00:00
|
|
|
let rows = transaction
|
|
|
|
.query_portal(&portal, 2)
|
|
|
|
.unwrap()
|
2019-01-30 03:29:33 +00:00
|
|
|
.to_vec()
|
2018-12-29 04:39:32 +00:00
|
|
|
.unwrap();
|
2018-12-28 20:59:17 +00:00
|
|
|
assert_eq!(rows.len(), 2);
|
|
|
|
assert_eq!(rows[0].get::<_, i32>(0), 1);
|
|
|
|
assert_eq!(rows[1].get::<_, i32>(0), 2);
|
|
|
|
|
2018-12-29 04:39:32 +00:00
|
|
|
let rows = transaction
|
|
|
|
.query_portal(&portal, 2)
|
|
|
|
.unwrap()
|
2019-01-30 03:29:33 +00:00
|
|
|
.to_vec()
|
2018-12-29 04:39:32 +00:00
|
|
|
.unwrap();
|
2018-12-28 20:59:17 +00:00
|
|
|
assert_eq!(rows.len(), 1);
|
|
|
|
assert_eq!(rows[0].get::<_, i32>(0), 3);
|
|
|
|
}
|