2014-07-26 22:35:38 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::f32;
|
|
|
|
use std::f64;
|
2014-11-20 18:54:40 +00:00
|
|
|
use std::fmt;
|
2016-01-23 04:00:42 +00:00
|
|
|
use std::io::{Read, Write};
|
2014-07-26 22:35:38 +00:00
|
|
|
|
2016-01-23 04:00:42 +00:00
|
|
|
use postgres::{Connection, SslMode, Result};
|
2015-05-26 05:47:25 +00:00
|
|
|
use postgres::error::Error;
|
2016-01-23 04:00:42 +00:00
|
|
|
use postgres::types::{ToSql, FromSql, Slice, WrongType, Type, IsNull, Kind, SessionInfo};
|
2014-07-26 22:35:38 +00:00
|
|
|
|
2015-12-06 20:40:00 +00:00
|
|
|
#[cfg(feature = "bit-vec")]
|
|
|
|
mod bit_vec;
|
2016-02-14 05:07:02 +00:00
|
|
|
#[cfg(feature = "eui48")]
|
|
|
|
mod eui48;
|
2014-11-17 17:43:10 +00:00
|
|
|
#[cfg(feature = "uuid")]
|
|
|
|
mod uuid;
|
2015-02-08 03:52:45 +00:00
|
|
|
#[cfg(feature = "time")]
|
2014-11-18 03:11:32 +00:00
|
|
|
mod time;
|
2015-02-08 03:20:18 +00:00
|
|
|
#[cfg(feature = "rustc-serialize")]
|
2015-04-13 04:48:38 +00:00
|
|
|
mod rustc_serialize;
|
2015-11-08 00:28:20 +00:00
|
|
|
#[cfg(feature = "serde_json")]
|
|
|
|
mod serde_json;
|
2015-05-14 05:21:40 +00:00
|
|
|
#[cfg(feature = "chrono")]
|
|
|
|
mod chrono;
|
2014-07-26 22:35:38 +00:00
|
|
|
|
2015-01-23 18:44:15 +00:00
|
|
|
fn test_type<T: PartialEq+FromSql+ToSql, S: fmt::Display>(sql_type: &str, checks: &[(T, S)]) {
|
2015-12-06 00:15:19 +00:00
|
|
|
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", SslMode::None));
|
2014-07-26 22:35:38 +00:00
|
|
|
for &(ref val, ref repr) in checks.iter() {
|
2015-01-09 18:50:54 +00:00
|
|
|
let stmt = or_panic!(conn.prepare(&*format!("SELECT {}::{}", *repr, sql_type)));
|
2015-02-19 07:05:11 +00:00
|
|
|
let result = or_panic!(stmt.query(&[])).iter().next().unwrap().get(0);
|
2015-05-14 05:21:40 +00:00
|
|
|
assert_eq!(val, &result);
|
2014-07-26 22:35:38 +00:00
|
|
|
|
2015-01-09 18:50:54 +00:00
|
|
|
let stmt = or_panic!(conn.prepare(&*format!("SELECT $1::{}", sql_type)));
|
2015-02-19 07:05:11 +00:00
|
|
|
let result = or_panic!(stmt.query(&[val])).iter().next().unwrap().get(0);
|
2015-05-14 05:21:40 +00:00
|
|
|
assert_eq!(val, &result);
|
2014-07-26 22:35:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-01 01:25:41 +00:00
|
|
|
#[test]
|
|
|
|
fn test_ref_tosql() {
|
2015-12-06 00:15:19 +00:00
|
|
|
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", SslMode::None));
|
2015-08-01 01:25:41 +00:00
|
|
|
let stmt = conn.prepare("SELECT $1::Int").unwrap();
|
|
|
|
let num: &ToSql = &&7;
|
|
|
|
stmt.query(&[num]).unwrap();
|
|
|
|
}
|
|
|
|
|
2014-07-26 22:35:38 +00:00
|
|
|
#[test]
|
|
|
|
fn test_bool_params() {
|
2014-11-19 18:20:20 +00:00
|
|
|
test_type("BOOL", &[(Some(true), "'t'"), (Some(false), "'f'"),
|
2014-07-26 22:35:38 +00:00
|
|
|
(None, "NULL")]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_i8_params() {
|
2014-11-19 18:20:20 +00:00
|
|
|
test_type("\"char\"", &[(Some('a' as i8), "'a'"), (None, "NULL")]);
|
2014-07-26 22:35:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_name_params() {
|
2015-09-28 05:19:02 +00:00
|
|
|
test_type("NAME", &[(Some("hello world".to_owned()), "'hello world'"),
|
|
|
|
(Some("イロハニホヘト チリヌルヲ".to_owned()), "'イロハニホヘト チリヌルヲ'"),
|
2014-07-26 22:35:38 +00:00
|
|
|
(None, "NULL")]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_i16_params() {
|
2014-11-19 18:20:20 +00:00
|
|
|
test_type("SMALLINT", &[(Some(15001i16), "15001"),
|
2014-07-26 22:35:38 +00:00
|
|
|
(Some(-15001i16), "-15001"), (None, "NULL")]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_i32_params() {
|
2014-11-19 18:20:20 +00:00
|
|
|
test_type("INT", &[(Some(2147483548i32), "2147483548"),
|
2014-07-26 22:35:38 +00:00
|
|
|
(Some(-2147483548i32), "-2147483548"), (None, "NULL")]);
|
|
|
|
}
|
|
|
|
|
2014-12-15 00:43:17 +00:00
|
|
|
#[test]
|
|
|
|
fn test_oid_params() {
|
|
|
|
test_type("OID", &[(Some(2147483548u32), "2147483548"),
|
|
|
|
(Some(4000000000), "4000000000"), (None, "NULL")]);
|
|
|
|
}
|
|
|
|
|
2014-07-26 22:35:38 +00:00
|
|
|
#[test]
|
|
|
|
fn test_i64_params() {
|
2014-11-19 18:20:20 +00:00
|
|
|
test_type("BIGINT", &[(Some(9223372036854775708i64), "9223372036854775708"),
|
2014-07-26 22:35:38 +00:00
|
|
|
(Some(-9223372036854775708i64), "-9223372036854775708"),
|
|
|
|
(None, "NULL")]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_f32_params() {
|
2014-11-19 18:20:20 +00:00
|
|
|
test_type("REAL", &[(Some(f32::INFINITY), "'infinity'"),
|
2014-07-26 22:35:38 +00:00
|
|
|
(Some(f32::NEG_INFINITY), "'-infinity'"),
|
|
|
|
(Some(1000.55), "1000.55"), (None, "NULL")]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_f64_params() {
|
2014-11-19 18:20:20 +00:00
|
|
|
test_type("DOUBLE PRECISION", &[(Some(f64::INFINITY), "'infinity'"),
|
2014-07-26 22:35:38 +00:00
|
|
|
(Some(f64::NEG_INFINITY), "'-infinity'"),
|
|
|
|
(Some(10000.55), "10000.55"),
|
|
|
|
(None, "NULL")]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_varchar_params() {
|
2015-09-28 05:19:02 +00:00
|
|
|
test_type("VARCHAR", &[(Some("hello world".to_owned()), "'hello world'"),
|
|
|
|
(Some("イロハニホヘト チリヌルヲ".to_owned()), "'イロハニホヘト チリヌルヲ'"),
|
2014-07-26 22:35:38 +00:00
|
|
|
(None, "NULL")]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_text_params() {
|
2015-09-28 05:19:02 +00:00
|
|
|
test_type("TEXT", &[(Some("hello world".to_owned()), "'hello world'"),
|
|
|
|
(Some("イロハニホヘト チリヌルヲ".to_owned()), "'イロハニホヘト チリヌルヲ'"),
|
2014-07-26 22:35:38 +00:00
|
|
|
(None, "NULL")]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_bpchar_params() {
|
2015-12-06 00:15:19 +00:00
|
|
|
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", SslMode::None));
|
2014-10-30 04:26:03 +00:00
|
|
|
or_panic!(conn.execute("CREATE TEMPORARY TABLE foo (
|
2014-07-26 22:35:38 +00:00
|
|
|
id SERIAL PRIMARY KEY,
|
|
|
|
b CHAR(5)
|
2014-11-19 18:20:20 +00:00
|
|
|
)", &[]));
|
2014-10-30 04:26:03 +00:00
|
|
|
or_panic!(conn.execute("INSERT INTO foo (b) VALUES ($1), ($2), ($3)",
|
2014-12-15 00:43:17 +00:00
|
|
|
&[&Some("12345"), &Some("123"), &None::<&'static str>]));
|
2014-10-30 04:26:03 +00:00
|
|
|
let stmt = or_panic!(conn.prepare("SELECT b FROM foo ORDER BY id"));
|
2014-11-19 18:20:20 +00:00
|
|
|
let res = or_panic!(stmt.query(&[]));
|
2014-07-26 22:35:38 +00:00
|
|
|
|
2015-09-28 05:19:02 +00:00
|
|
|
assert_eq!(vec!(Some("12345".to_owned()), Some("123 ".to_owned()), None),
|
2015-02-19 07:05:11 +00:00
|
|
|
res.iter().map(|row| row.get(0)).collect::<Vec<_>>());
|
2014-07-26 22:35:38 +00:00
|
|
|
}
|
|
|
|
|
2014-12-15 00:43:17 +00:00
|
|
|
#[test]
|
|
|
|
fn test_citext_params() {
|
2015-12-06 00:15:19 +00:00
|
|
|
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", SslMode::None));
|
2014-12-15 00:43:17 +00:00
|
|
|
or_panic!(conn.execute("CREATE TEMPORARY TABLE foo (
|
|
|
|
id SERIAL PRIMARY KEY,
|
|
|
|
b CITEXT
|
|
|
|
)", &[]));
|
|
|
|
or_panic!(conn.execute("INSERT INTO foo (b) VALUES ($1), ($2), ($3)",
|
|
|
|
&[&Some("foobar"), &Some("FooBar"), &None::<&'static str>]));
|
|
|
|
let stmt = or_panic!(conn.prepare("SELECT id FROM foo WHERE b = 'FOOBAR' ORDER BY id"));
|
|
|
|
let res = or_panic!(stmt.query(&[]));
|
|
|
|
|
2015-02-19 07:05:11 +00:00
|
|
|
assert_eq!(vec!(Some(1i32), Some(2i32)), res.iter().map(|row| row.get(0)).collect::<Vec<_>>());
|
2014-12-15 00:43:17 +00:00
|
|
|
}
|
|
|
|
|
2014-07-26 22:35:38 +00:00
|
|
|
#[test]
|
|
|
|
fn test_bytea_params() {
|
2014-11-19 18:20:20 +00:00
|
|
|
test_type("BYTEA", &[(Some(vec!(0u8, 1, 2, 3, 254, 255)), "'\\x00010203feff'"),
|
2014-07-26 22:35:38 +00:00
|
|
|
(None, "NULL")]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_hstore_params() {
|
2014-12-19 18:43:42 +00:00
|
|
|
macro_rules! make_map {
|
2014-07-26 22:35:38 +00:00
|
|
|
($($k:expr => $v:expr),+) => ({
|
|
|
|
let mut map = HashMap::new();
|
|
|
|
$(map.insert($k, $v);)+
|
|
|
|
map
|
|
|
|
})
|
2014-12-19 18:43:42 +00:00
|
|
|
}
|
2014-07-26 22:35:38 +00:00
|
|
|
test_type("hstore",
|
2015-09-28 05:19:02 +00:00
|
|
|
&[(Some(make_map!("a".to_owned() => Some("1".to_owned()))), "'a=>1'"),
|
|
|
|
(Some(make_map!("hello".to_owned() => Some("world!".to_owned()),
|
|
|
|
"hola".to_owned() => Some("mundo!".to_owned()),
|
|
|
|
"what".to_owned() => None)),
|
2014-07-26 22:35:38 +00:00
|
|
|
"'hello=>world!,hola=>mundo!,what=>NULL'"),
|
|
|
|
(None, "NULL")]);
|
|
|
|
}
|
|
|
|
|
2015-04-02 18:34:42 +00:00
|
|
|
fn test_nan_param<T: PartialEq+ToSql+FromSql>(sql_type: &str) {
|
2015-12-06 00:15:19 +00:00
|
|
|
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", SslMode::None));
|
2015-01-09 18:50:54 +00:00
|
|
|
let stmt = or_panic!(conn.prepare(&*format!("SELECT 'NaN'::{}", sql_type)));
|
2015-02-19 07:05:11 +00:00
|
|
|
let result = or_panic!(stmt.query(&[]));
|
|
|
|
let val: T = result.iter().next().unwrap().get(0);
|
2015-04-02 18:34:42 +00:00
|
|
|
assert!(val != val);
|
2014-07-26 22:35:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_f32_nan_param() {
|
|
|
|
test_nan_param::<f32>("REAL");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_f64_nan_param() {
|
|
|
|
test_nan_param::<f64>("DOUBLE PRECISION");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_pg_database_datname() {
|
2015-12-06 00:15:19 +00:00
|
|
|
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", SslMode::None));
|
2014-10-30 04:26:03 +00:00
|
|
|
let stmt = or_panic!(conn.prepare("SELECT datname FROM pg_database"));
|
2015-02-19 07:05:11 +00:00
|
|
|
let result = or_panic!(stmt.query(&[]));
|
2014-07-26 22:35:38 +00:00
|
|
|
|
2015-02-19 07:05:11 +00:00
|
|
|
let next = result.iter().next().unwrap();
|
2015-12-26 02:32:51 +00:00
|
|
|
or_panic!(next.get_opt::<_, String>(0).unwrap());
|
|
|
|
or_panic!(next.get_opt::<_, String>("datname").unwrap());
|
2014-07-26 22:35:38 +00:00
|
|
|
}
|
2015-02-13 06:52:55 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_slice() {
|
2015-12-06 00:15:19 +00:00
|
|
|
let conn = Connection::connect("postgres://postgres@localhost", SslMode::None).unwrap();
|
2015-02-13 06:52:55 +00:00
|
|
|
conn.batch_execute("CREATE TEMPORARY TABLE foo (id SERIAL PRIMARY KEY, f VARCHAR);
|
|
|
|
INSERT INTO foo (f) VALUES ('a'), ('b'), ('c'), ('d');").unwrap();
|
|
|
|
|
|
|
|
let stmt = conn.prepare("SELECT f FROM foo WHERE id = ANY($1)").unwrap();
|
|
|
|
let result = stmt.query(&[&Slice(&[1i32, 3, 4])]).unwrap();
|
2015-09-28 05:19:02 +00:00
|
|
|
assert_eq!(vec!["a".to_owned(), "c".to_owned(), "d".to_owned()],
|
2015-02-19 07:05:11 +00:00
|
|
|
result.iter().map(|r| r.get::<_, String>(0)).collect::<Vec<_>>());
|
2015-02-13 06:52:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_slice_wrong_type() {
|
2015-12-06 00:15:19 +00:00
|
|
|
let conn = Connection::connect("postgres://postgres@localhost", SslMode::None).unwrap();
|
2015-02-13 06:52:55 +00:00
|
|
|
conn.batch_execute("CREATE TEMPORARY TABLE foo (id SERIAL PRIMARY KEY)").unwrap();
|
|
|
|
|
|
|
|
let stmt = conn.prepare("SELECT * FROM foo WHERE id = ANY($1)").unwrap();
|
|
|
|
match stmt.query(&[&Slice(&["hi"])]) {
|
|
|
|
Ok(_) => panic!("Unexpected success"),
|
2015-12-27 05:05:23 +00:00
|
|
|
Err(Error::Conversion(ref e)) if e.is::<WrongType>() => {}
|
2015-02-15 23:11:15 +00:00
|
|
|
Err(e) => panic!("Unexpected error {:?}", e),
|
2015-11-13 07:52:33 +00:00
|
|
|
};
|
2015-02-13 06:52:55 +00:00
|
|
|
}
|
2015-02-14 20:46:18 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_slice_range() {
|
2015-12-06 00:15:19 +00:00
|
|
|
let conn = Connection::connect("postgres://postgres@localhost", SslMode::None).unwrap();
|
2015-02-14 20:46:18 +00:00
|
|
|
|
|
|
|
let stmt = conn.prepare("SELECT $1::INT8RANGE").unwrap();
|
|
|
|
match stmt.query(&[&Slice(&[1i64])]) {
|
|
|
|
Ok(_) => panic!("Unexpected success"),
|
2015-12-27 05:05:23 +00:00
|
|
|
Err(Error::Conversion(ref e)) if e.is::<WrongType>() => {}
|
2015-02-15 23:11:15 +00:00
|
|
|
Err(e) => panic!("Unexpected error {:?}", e),
|
2015-11-13 07:52:33 +00:00
|
|
|
};
|
2015-02-14 20:46:18 +00:00
|
|
|
}
|
2016-01-23 04:00:42 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn domain() {
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
struct SessionId(Vec<u8>);
|
|
|
|
|
|
|
|
impl ToSql for SessionId {
|
|
|
|
fn to_sql<W: ?Sized>(&self, ty: &Type, out: &mut W, ctx: &SessionInfo) -> Result<IsNull>
|
|
|
|
where W: Write
|
|
|
|
{
|
|
|
|
let inner = match *ty.kind() {
|
|
|
|
Kind::Domain(ref inner) => inner,
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
self.0.to_sql(inner, out, ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn accepts(ty: &Type) -> bool {
|
|
|
|
ty.name() == "session_id" && match *ty.kind() { Kind::Domain(_) => true, _ => false }
|
|
|
|
}
|
|
|
|
|
|
|
|
to_sql_checked!();
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromSql for SessionId {
|
|
|
|
fn from_sql<R: Read>(ty: &Type, raw: &mut R, ctx: &SessionInfo) -> Result<Self> {
|
|
|
|
Vec::<u8>::from_sql(ty, raw, ctx).map(SessionId)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn accepts(ty: &Type) -> bool {
|
|
|
|
// This is super weird!
|
|
|
|
<Vec<u8> as FromSql>::accepts(ty)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let conn = Connection::connect("postgres://postgres@localhost", SslMode::None).unwrap();
|
|
|
|
conn.batch_execute("CREATE DOMAIN pg_temp.session_id AS bytea CHECK(octet_length(VALUE) = 16);
|
|
|
|
CREATE TABLE pg_temp.foo (id pg_temp.session_id);")
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let id = SessionId(b"0123456789abcdef".to_vec());
|
|
|
|
conn.execute("INSERT INTO pg_temp.foo (id) VALUES ($1)", &[&id]).unwrap();
|
|
|
|
let rows = conn.query("SELECT id FROM pg_temp.foo", &[]).unwrap();
|
|
|
|
assert_eq!(id, rows.get(0).get(0));
|
|
|
|
}
|