Make constants associated
This commit is contained in:
parent
165a3342b9
commit
2777703ef3
@ -1,8 +1,8 @@
|
||||
use std::fs::File;
|
||||
use std::io::{Write, BufWriter};
|
||||
use std::path::Path;
|
||||
use phf_codegen;
|
||||
use linked_hash_map::LinkedHashMap;
|
||||
use phf_codegen;
|
||||
use std::fs::File;
|
||||
use std::io::{BufWriter, Write};
|
||||
use std::path::Path;
|
||||
|
||||
const ERRCODES_TXT: &'static str = include_str!("errcodes.txt");
|
||||
|
||||
@ -59,7 +59,6 @@ impl SqlState {{
|
||||
pub fn code(&self) -> &str {{
|
||||
&self.0
|
||||
}}
|
||||
}}
|
||||
"
|
||||
).unwrap();
|
||||
}
|
||||
@ -70,14 +69,16 @@ fn make_consts(codes: &LinkedHashMap<String, Vec<String>>, file: &mut BufWriter<
|
||||
write!(
|
||||
file,
|
||||
r#"
|
||||
/// {code}
|
||||
pub const {name}: SqlState = SqlState(Cow::Borrowed("{code}"));
|
||||
/// {code}
|
||||
pub const {name}: SqlState = SqlState(Cow::Borrowed("{code}"));
|
||||
"#,
|
||||
name = name,
|
||||
code = code,
|
||||
).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
write!(file, "}}").unwrap();
|
||||
}
|
||||
|
||||
fn make_map(codes: &LinkedHashMap<String, Vec<String>>, file: &mut BufWriter<File>) {
|
||||
@ -89,7 +90,7 @@ static SQLSTATE_MAP: phf::Map<&'static str, SqlState> = "
|
||||
).unwrap();
|
||||
let mut builder = phf_codegen::Map::new();
|
||||
for (code, names) in codes {
|
||||
builder.entry(&**code, &names[0]);
|
||||
builder.entry(&**code, &format!("SqlState::{}", &names[0]));
|
||||
}
|
||||
builder.build(file).unwrap();
|
||||
write!(file, ";\n").unwrap();
|
||||
|
@ -261,14 +261,7 @@ fn make_impl(w: &mut BufWriter<File>, types: &BTreeMap<u32, Type>) {
|
||||
}
|
||||
|
||||
fn make_consts(w: &mut BufWriter<File>, types: &BTreeMap<u32, Type>) {
|
||||
write!(
|
||||
w,
|
||||
"pub mod consts {{
|
||||
use types::Type;
|
||||
use types::type_gen::Inner;
|
||||
",
|
||||
).unwrap();
|
||||
|
||||
write!(w, "impl Type {{").unwrap();
|
||||
for type_ in types.values() {
|
||||
write!(
|
||||
w,
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -4,7 +4,7 @@ use self::bit_vec::BitVec;
|
||||
use postgres_protocol::types;
|
||||
use std::error::Error;
|
||||
|
||||
use types::{FromSql, IsNull, ToSql, Type, BIT, VARBIT};
|
||||
use types::{FromSql, IsNull, ToSql, Type};
|
||||
|
||||
impl<'a> FromSql<'a> for BitVec {
|
||||
fn from_sql(_: &Type, raw: &[u8]) -> Result<BitVec, Box<Error + Sync + Send>> {
|
||||
|
@ -5,7 +5,7 @@ use self::chrono::{DateTime, Duration, FixedOffset, Local, NaiveDate, NaiveDateT
|
||||
use postgres_protocol::types;
|
||||
use std::error::Error;
|
||||
|
||||
use types::{FromSql, IsNull, ToSql, Type, DATE, TIME, TIMESTAMP, TIMESTAMPTZ};
|
||||
use types::{FromSql, IsNull, ToSql, Type};
|
||||
|
||||
fn base() -> NaiveDateTime {
|
||||
NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 0)
|
||||
|
@ -4,7 +4,7 @@ use self::eui48::MacAddress;
|
||||
use postgres_protocol::types;
|
||||
use std::error::Error;
|
||||
|
||||
use types::{FromSql, IsNull, ToSql, Type, MACADDR};
|
||||
use types::{FromSql, IsNull, ToSql, Type};
|
||||
|
||||
impl<'a> FromSql<'a> for MacAddress {
|
||||
fn from_sql(_: &Type, raw: &[u8]) -> Result<MacAddress, Box<Error + Sync + Send>> {
|
||||
|
@ -5,7 +5,7 @@ use fallible_iterator::FallibleIterator;
|
||||
use postgres_protocol::types;
|
||||
use std::error::Error;
|
||||
|
||||
use types::{FromSql, IsNull, ToSql, Type, BOX, PATH, POINT};
|
||||
use types::{FromSql, IsNull, ToSql, Type};
|
||||
|
||||
impl<'a> FromSql<'a> for Point<f64> {
|
||||
fn from_sql(_: &Type, raw: &[u8]) -> Result<Self, Box<Error + Sync + Send>> {
|
||||
|
@ -15,16 +15,15 @@ use types::type_gen::{Inner, Other};
|
||||
pub use postgres_protocol::Oid;
|
||||
|
||||
pub use types::special::{Date, Timestamp};
|
||||
pub use types::type_gen::consts::*;
|
||||
|
||||
/// Generates a simple implementation of `ToSql::accepts` which accepts the
|
||||
/// types passed to it.
|
||||
#[macro_export]
|
||||
macro_rules! accepts {
|
||||
($($expected:pat),+) => (
|
||||
($($expected:ident),+) => (
|
||||
fn accepts(ty: &$crate::types::Type) -> bool {
|
||||
match *ty {
|
||||
$($expected)|+ => true,
|
||||
$($crate::types::Type::$expected)|+ => true,
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
@ -411,7 +410,7 @@ impl<'a> FromSql<'a> for &'a str {
|
||||
|
||||
fn accepts(ty: &Type) -> bool {
|
||||
match *ty {
|
||||
VARCHAR | TEXT | BPCHAR | NAME | UNKNOWN => true,
|
||||
Type::VARCHAR | Type::TEXT | Type::BPCHAR | Type::NAME | Type::UNKNOWN => true,
|
||||
ref ty if ty.name() == "citext" => true,
|
||||
_ => false,
|
||||
}
|
||||
@ -419,7 +418,7 @@ impl<'a> FromSql<'a> for &'a str {
|
||||
}
|
||||
|
||||
macro_rules! simple_from {
|
||||
($t:ty, $f:ident, $($expected:pat),+) => {
|
||||
($t:ty, $f:ident, $($expected:ident),+) => {
|
||||
impl<'a> FromSql<'a> for $t {
|
||||
fn from_sql(_: &Type, raw: &'a [u8]) -> Result<$t, Box<Error + Sync + Send>> {
|
||||
types::$f(raw)
|
||||
@ -513,8 +512,7 @@ pub enum IsNull {
|
||||
/// # Arrays
|
||||
///
|
||||
/// `ToSql` is implemented for `Vec<T>` and `&[T]` where `T` implements `ToSql`,
|
||||
/// and corresponds to one-dimentional Postgres arrays with an index offset of
|
||||
/// 1.
|
||||
/// and corresponds to one-dimentional Postgres arrays with an index offset of 1.
|
||||
pub trait ToSql: fmt::Debug {
|
||||
/// Converts the value of `self` into the binary format of the specified
|
||||
/// Postgres `Type`, appending it to `out`.
|
||||
@ -655,7 +653,7 @@ impl<'a> ToSql for &'a str {
|
||||
|
||||
fn accepts(ty: &Type) -> bool {
|
||||
match *ty {
|
||||
VARCHAR | TEXT | BPCHAR | NAME | UNKNOWN => true,
|
||||
Type::VARCHAR | Type::TEXT | Type::BPCHAR | Type::NAME | Type::UNKNOWN => true,
|
||||
ref ty if ty.name() == "citext" => true,
|
||||
_ => false,
|
||||
}
|
||||
@ -689,7 +687,7 @@ impl ToSql for String {
|
||||
}
|
||||
|
||||
macro_rules! simple_to {
|
||||
($t:ty, $f:ident, $($expected:pat),+) => {
|
||||
($t:ty, $f:ident, $($expected:ident),+) => {
|
||||
impl ToSql for $t {
|
||||
fn to_sql(&self,
|
||||
_: &Type,
|
||||
|
@ -4,11 +4,11 @@ use self::rustc_serialize::json;
|
||||
use std::error::Error;
|
||||
use std::io::{Read, Write};
|
||||
|
||||
use types::{FromSql, IsNull, ToSql, Type, JSON, JSONB};
|
||||
use types::{FromSql, IsNull, ToSql, Type};
|
||||
|
||||
impl<'a> FromSql<'a> for json::Json {
|
||||
fn from_sql(ty: &Type, mut raw: &[u8]) -> Result<json::Json, Box<Error + Sync + Send>> {
|
||||
if *ty == JSONB {
|
||||
if *ty == Type::JSONB {
|
||||
let mut b = [0; 1];
|
||||
raw.read_exact(&mut b)?;
|
||||
// We only support version 1 of the jsonb binary format
|
||||
@ -24,7 +24,7 @@ impl<'a> FromSql<'a> for json::Json {
|
||||
|
||||
impl ToSql for json::Json {
|
||||
fn to_sql(&self, ty: &Type, out: &mut Vec<u8>) -> Result<IsNull, Box<Error + Sync + Send>> {
|
||||
if *ty == JSONB {
|
||||
if *ty == Type::JSONB {
|
||||
out.push(1);
|
||||
}
|
||||
write!(out, "{}", self)?;
|
||||
|
@ -4,11 +4,11 @@ use self::serde_json::Value;
|
||||
use std::error::Error;
|
||||
use std::io::{Read, Write};
|
||||
|
||||
use types::{FromSql, IsNull, ToSql, Type, JSON, JSONB};
|
||||
use types::{FromSql, IsNull, ToSql, Type};
|
||||
|
||||
impl<'a> FromSql<'a> for Value {
|
||||
fn from_sql(ty: &Type, mut raw: &[u8]) -> Result<Value, Box<Error + Sync + Send>> {
|
||||
if *ty == JSONB {
|
||||
if *ty == Type::JSONB {
|
||||
let mut b = [0; 1];
|
||||
raw.read_exact(&mut b)?;
|
||||
// We only support version 1 of the jsonb binary format
|
||||
@ -24,7 +24,7 @@ impl<'a> FromSql<'a> for Value {
|
||||
|
||||
impl ToSql for Value {
|
||||
fn to_sql(&self, ty: &Type, out: &mut Vec<u8>) -> Result<IsNull, Box<Error + Sync + Send>> {
|
||||
if *ty == JSONB {
|
||||
if *ty == Type::JSONB {
|
||||
out.push(1);
|
||||
}
|
||||
write!(out, "{}", self)?;
|
||||
|
@ -2,7 +2,7 @@ use postgres_protocol::types;
|
||||
use std::error::Error;
|
||||
use std::{i32, i64};
|
||||
|
||||
use types::{FromSql, IsNull, ToSql, Type, DATE, TIMESTAMP, TIMESTAMPTZ};
|
||||
use types::{FromSql, IsNull, ToSql, Type};
|
||||
|
||||
/// A wrapper that can be used to represent infinity with `Type::Date` types.
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
@ -25,7 +25,7 @@ impl<'a, T: FromSql<'a>> FromSql<'a> for Date<T> {
|
||||
}
|
||||
|
||||
fn accepts(ty: &Type) -> bool {
|
||||
*ty == DATE && T::accepts(ty)
|
||||
*ty == Type::DATE && T::accepts(ty)
|
||||
}
|
||||
}
|
||||
|
||||
@ -42,7 +42,7 @@ impl<T: ToSql> ToSql for Date<T> {
|
||||
}
|
||||
|
||||
fn accepts(ty: &Type) -> bool {
|
||||
*ty == DATE && T::accepts(ty)
|
||||
*ty == Type::DATE && T::accepts(ty)
|
||||
}
|
||||
|
||||
to_sql_checked!();
|
||||
@ -71,7 +71,7 @@ impl<'a, T: FromSql<'a>> FromSql<'a> for Timestamp<T> {
|
||||
|
||||
fn accepts(ty: &Type) -> bool {
|
||||
match *ty {
|
||||
TIMESTAMP | TIMESTAMPTZ if T::accepts(ty) => true,
|
||||
Type::TIMESTAMP | Type::TIMESTAMPTZ if T::accepts(ty) => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
@ -91,7 +91,7 @@ impl<T: ToSql> ToSql for Timestamp<T> {
|
||||
|
||||
fn accepts(ty: &Type) -> bool {
|
||||
match *ty {
|
||||
TIMESTAMP | TIMESTAMPTZ if T::accepts(ty) => true,
|
||||
Type::TIMESTAMP | Type::TIMESTAMPTZ if T::accepts(ty) => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
@ -4,7 +4,7 @@ use self::time::Timespec;
|
||||
use postgres_protocol::types;
|
||||
use std::error::Error;
|
||||
|
||||
use types::{FromSql, IsNull, ToSql, Type, TIMESTAMP, TIMESTAMPTZ};
|
||||
use types::{FromSql, IsNull, ToSql, Type};
|
||||
|
||||
const USEC_PER_SEC: i64 = 1_000_000;
|
||||
const NSEC_PER_USEC: i64 = 1_000;
|
||||
|
@ -1345,10 +1345,7 @@ impl Inner {
|
||||
}
|
||||
}
|
||||
}
|
||||
pub mod consts {
|
||||
use types::Type;
|
||||
use types::type_gen::Inner;
|
||||
|
||||
impl Type {
|
||||
/// BOOL - boolean, 'true'/'false'
|
||||
pub const BOOL: Type = Type(Inner::Bool);
|
||||
|
||||
|
@ -4,7 +4,7 @@ use self::uuid::Uuid;
|
||||
use postgres_protocol::types;
|
||||
use std::error::Error;
|
||||
|
||||
use types::{FromSql, IsNull, ToSql, Type, UUID};
|
||||
use types::{FromSql, IsNull, ToSql, Type};
|
||||
|
||||
impl<'a> FromSql<'a> for Uuid {
|
||||
fn from_sql(_: &Type, raw: &[u8]) -> Result<Uuid, Box<Error + Sync + Send>> {
|
||||
|
@ -94,7 +94,7 @@ use std::result;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
use error::{DbError, UNDEFINED_COLUMN, UNDEFINED_TABLE};
|
||||
use error::{DbError, SqlState};
|
||||
use notification::{Notification, Notifications};
|
||||
use params::{IntoConnectParams, User};
|
||||
use priv_io::MessageStream;
|
||||
@ -102,7 +102,7 @@ use rows::Rows;
|
||||
use stmt::{Column, Statement};
|
||||
use tls::TlsHandshake;
|
||||
use transaction::{IsolationLevel, Transaction};
|
||||
use types::{Field, FromSql, IsNull, Kind, Oid, ToSql, Type, CHAR, NAME, OID};
|
||||
use types::{Field, FromSql, IsNull, Kind, Oid, ToSql, Type};
|
||||
|
||||
#[doc(inline)]
|
||||
pub use error::Error;
|
||||
@ -737,7 +737,7 @@ impl InnerConnection {
|
||||
) {
|
||||
Ok(..) => {}
|
||||
// Range types weren't added until Postgres 9.2, so pg_range may not exist
|
||||
Err(ref e) if e.code() == Some(&UNDEFINED_TABLE) => {
|
||||
Err(ref e) if e.code() == Some(&SqlState::UNDEFINED_TABLE) => {
|
||||
self.raw_prepare(
|
||||
TYPEINFO_QUERY,
|
||||
"SELECT t.typname, t.typtype, t.typelem, NULL::OID, \
|
||||
@ -758,21 +758,25 @@ impl InnerConnection {
|
||||
#[allow(if_not_else)]
|
||||
fn read_type(&mut self, oid: Oid) -> Result<Type> {
|
||||
self.setup_typeinfo_query()?;
|
||||
self.raw_execute(TYPEINFO_QUERY, "", 0, &[OID], &[&oid])?;
|
||||
self.raw_execute(TYPEINFO_QUERY, "", 0, &[Type::OID], &[&oid])?;
|
||||
let mut row = None;
|
||||
self.read_rows(|r| row = Some(r))?;
|
||||
|
||||
let get_raw = |i: usize| row.as_ref().and_then(|r| r.get(i));
|
||||
|
||||
let (name, type_, elem_oid, rngsubtype, basetype, schema, relid) = {
|
||||
let name = String::from_sql_nullable(&NAME, get_raw(0)).map_err(error::conversion)?;
|
||||
let type_ = i8::from_sql_nullable(&CHAR, get_raw(1)).map_err(error::conversion)?;
|
||||
let elem_oid = Oid::from_sql_nullable(&OID, get_raw(2)).map_err(error::conversion)?;
|
||||
let rngsubtype =
|
||||
Option::<Oid>::from_sql_nullable(&OID, get_raw(3)).map_err(error::conversion)?;
|
||||
let basetype = Oid::from_sql_nullable(&OID, get_raw(4)).map_err(error::conversion)?;
|
||||
let schema = String::from_sql_nullable(&NAME, get_raw(5)).map_err(error::conversion)?;
|
||||
let relid = Oid::from_sql_nullable(&OID, get_raw(6)).map_err(error::conversion)?;
|
||||
let name =
|
||||
String::from_sql_nullable(&Type::NAME, get_raw(0)).map_err(error::conversion)?;
|
||||
let type_ = i8::from_sql_nullable(&Type::CHAR, get_raw(1)).map_err(error::conversion)?;
|
||||
let elem_oid =
|
||||
Oid::from_sql_nullable(&Type::OID, get_raw(2)).map_err(error::conversion)?;
|
||||
let rngsubtype = Option::<Oid>::from_sql_nullable(&Type::OID, get_raw(3))
|
||||
.map_err(error::conversion)?;
|
||||
let basetype =
|
||||
Oid::from_sql_nullable(&Type::OID, get_raw(4)).map_err(error::conversion)?;
|
||||
let schema =
|
||||
String::from_sql_nullable(&Type::NAME, get_raw(5)).map_err(error::conversion)?;
|
||||
let relid = Oid::from_sql_nullable(&Type::OID, get_raw(6)).map_err(error::conversion)?;
|
||||
(name, type_, elem_oid, rngsubtype, basetype, schema, relid)
|
||||
};
|
||||
|
||||
@ -810,7 +814,7 @@ impl InnerConnection {
|
||||
) {
|
||||
Ok(..) => {}
|
||||
// Postgres 9.0 doesn't have enumsortorder
|
||||
Err(ref e) if e.code() == Some(&UNDEFINED_COLUMN) => {
|
||||
Err(ref e) if e.code() == Some(&SqlState::UNDEFINED_COLUMN) => {
|
||||
self.raw_prepare(
|
||||
TYPEINFO_ENUM_QUERY,
|
||||
"SELECT enumlabel \
|
||||
@ -828,13 +832,14 @@ impl InnerConnection {
|
||||
|
||||
fn read_enum_variants(&mut self, oid: Oid) -> Result<Vec<String>> {
|
||||
self.setup_typeinfo_enum_query()?;
|
||||
self.raw_execute(TYPEINFO_ENUM_QUERY, "", 0, &[OID], &[&oid])?;
|
||||
self.raw_execute(TYPEINFO_ENUM_QUERY, "", 0, &[Type::OID], &[&oid])?;
|
||||
let mut rows = vec![];
|
||||
self.read_rows(|row| rows.push(row))?;
|
||||
|
||||
let mut variants = vec![];
|
||||
for row in rows {
|
||||
variants.push(String::from_sql_nullable(&NAME, row.get(0)).map_err(error::conversion)?);
|
||||
variants
|
||||
.push(String::from_sql_nullable(&Type::NAME, row.get(0)).map_err(error::conversion)?);
|
||||
}
|
||||
|
||||
Ok(variants)
|
||||
@ -861,15 +866,17 @@ impl InnerConnection {
|
||||
|
||||
fn read_composite_fields(&mut self, relid: Oid) -> Result<Vec<Field>> {
|
||||
self.setup_typeinfo_composite_query()?;
|
||||
self.raw_execute(TYPEINFO_COMPOSITE_QUERY, "", 0, &[OID], &[&relid])?;
|
||||
self.raw_execute(TYPEINFO_COMPOSITE_QUERY, "", 0, &[Type::OID], &[&relid])?;
|
||||
let mut rows = vec![];
|
||||
self.read_rows(|row| rows.push(row))?;
|
||||
|
||||
let mut fields = vec![];
|
||||
for row in rows {
|
||||
let (name, type_) = {
|
||||
let name = String::from_sql_nullable(&NAME, row.get(0)).map_err(error::conversion)?;
|
||||
let type_ = Oid::from_sql_nullable(&OID, row.get(1)).map_err(error::conversion)?;
|
||||
let name =
|
||||
String::from_sql_nullable(&Type::NAME, row.get(0)).map_err(error::conversion)?;
|
||||
let type_ =
|
||||
Oid::from_sql_nullable(&Type::OID, row.get(1)).map_err(error::conversion)?;
|
||||
(name, type_)
|
||||
};
|
||||
let type_ = self.get_type(type_)?;
|
||||
|
@ -12,12 +12,11 @@ extern crate url;
|
||||
|
||||
use fallible_iterator::FallibleIterator;
|
||||
use postgres::error::ErrorPosition::Normal;
|
||||
use postgres::error::{DbError, CARDINALITY_VIOLATION, INVALID_CATALOG_NAME, INVALID_PASSWORD,
|
||||
QUERY_CANCELED, SYNTAX_ERROR, UNDEFINED_TABLE};
|
||||
use postgres::error::{DbError, SqlState};
|
||||
use postgres::notification::Notification;
|
||||
use postgres::params::IntoConnectParams;
|
||||
use postgres::transaction::{self, IsolationLevel};
|
||||
use postgres::types::{FLOAT8, INT4, Kind, Oid, Type, WrongType, VARCHAR};
|
||||
use postgres::types::{Kind, Oid, Type, WrongType};
|
||||
use postgres::{Connection, GenericConnection, HandleNotice, TlsMode};
|
||||
use std::io;
|
||||
use std::thread;
|
||||
@ -58,7 +57,7 @@ fn test_prepare_err() {
|
||||
));
|
||||
let err = conn.prepare("invalid sql database").unwrap_err();
|
||||
match err.as_db() {
|
||||
Some(e) if e.code == SYNTAX_ERROR && e.position == Some(Normal(1)) => {}
|
||||
Some(e) if e.code == SqlState::SYNTAX_ERROR && e.position == Some(Normal(1)) => {}
|
||||
_ => panic!("Unexpected result {:?}", err),
|
||||
}
|
||||
}
|
||||
@ -66,7 +65,7 @@ fn test_prepare_err() {
|
||||
#[test]
|
||||
fn test_unknown_database() {
|
||||
match Connection::connect("postgres://postgres@localhost:5433/asdf", TlsMode::None) {
|
||||
Err(ref e) if e.code() == Some(&INVALID_CATALOG_NAME) => {}
|
||||
Err(ref e) if e.code() == Some(&SqlState::INVALID_CATALOG_NAME) => {}
|
||||
Err(resp) => panic!("Unexpected result {:?}", resp),
|
||||
_ => panic!("Unexpected result"),
|
||||
}
|
||||
@ -429,7 +428,7 @@ fn test_batch_execute_error() {
|
||||
|
||||
let stmt = conn.prepare("SELECT * FROM foo ORDER BY id");
|
||||
match stmt {
|
||||
Err(ref e) if e.code() == Some(&UNDEFINED_TABLE) => {}
|
||||
Err(ref e) if e.code() == Some(&SqlState::UNDEFINED_TABLE) => {}
|
||||
Err(e) => panic!("unexpected error {:?}", e),
|
||||
_ => panic!("unexpected success"),
|
||||
}
|
||||
@ -488,7 +487,7 @@ FROM (SELECT gs.i
|
||||
LIMIT 2) ss",
|
||||
));
|
||||
match stmt.query(&[]) {
|
||||
Err(ref e) if e.code() == Some(&CARDINALITY_VIOLATION) => {}
|
||||
Err(ref e) if e.code() == Some(&SqlState::CARDINALITY_VIOLATION) => {}
|
||||
Err(err) => panic!("Unexpected error {:?}", err),
|
||||
Ok(_) => panic!("Expected failure"),
|
||||
};
|
||||
@ -540,7 +539,7 @@ fn test_param_types() {
|
||||
TlsMode::None,
|
||||
));
|
||||
let stmt = or_panic!(conn.prepare("SELECT $1::INT, $2::VARCHAR"));
|
||||
assert_eq!(stmt.param_types(), &[INT4, VARCHAR][..]);
|
||||
assert_eq!(stmt.param_types(), &[Type::INT4, Type::VARCHAR][..]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -553,9 +552,9 @@ fn test_columns() {
|
||||
let cols = stmt.columns();
|
||||
assert_eq!(2, cols.len());
|
||||
assert_eq!(cols[0].name(), "a");
|
||||
assert_eq!(cols[0].type_(), &INT4);
|
||||
assert_eq!(cols[0].type_(), &Type::INT4);
|
||||
assert_eq!(cols[1].name(), "b");
|
||||
assert_eq!(cols[1].type_(), &VARCHAR);
|
||||
assert_eq!(cols[1].type_(), &Type::VARCHAR);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -568,9 +567,9 @@ fn test_execute_counts() {
|
||||
0,
|
||||
or_panic!(conn.execute(
|
||||
"CREATE TEMPORARY TABLE foo (
|
||||
id SERIAL PRIMARY KEY,
|
||||
b INT
|
||||
)",
|
||||
id SERIAL PRIMARY KEY,
|
||||
b INT
|
||||
)",
|
||||
&[],
|
||||
))
|
||||
);
|
||||
@ -898,7 +897,7 @@ fn test_cancel_query() {
|
||||
});
|
||||
|
||||
match conn.execute("SELECT pg_sleep(10)", &[]) {
|
||||
Err(ref e) if e.code() == Some(&QUERY_CANCELED) => {}
|
||||
Err(ref e) if e.code() == Some(&SqlState::QUERY_CANCELED) => {}
|
||||
Err(res) => panic!("Unexpected result {:?}", res),
|
||||
_ => panic!("Unexpected result"),
|
||||
}
|
||||
@ -998,7 +997,7 @@ fn test_plaintext_pass_wrong_pass() {
|
||||
TlsMode::None,
|
||||
);
|
||||
match ret {
|
||||
Err(ref e) if e.code() == Some(&INVALID_PASSWORD) => {}
|
||||
Err(ref e) if e.code() == Some(&SqlState::INVALID_PASSWORD) => {}
|
||||
Err(err) => panic!("Unexpected error {:?}", err),
|
||||
_ => panic!("Expected error"),
|
||||
}
|
||||
@ -1029,7 +1028,7 @@ fn test_md5_pass_wrong_pass() {
|
||||
TlsMode::None,
|
||||
);
|
||||
match ret {
|
||||
Err(ref e) if e.code() == Some(&INVALID_PASSWORD) => {}
|
||||
Err(ref e) if e.code() == Some(&SqlState::INVALID_PASSWORD) => {}
|
||||
Err(err) => panic!("Unexpected error {:?}", err),
|
||||
_ => panic!("Expected error"),
|
||||
}
|
||||
@ -1063,7 +1062,7 @@ fn test_scram_pass_wrong_pass() {
|
||||
TlsMode::None,
|
||||
);
|
||||
match ret {
|
||||
Err(ref e) if e.code() == Some(&INVALID_PASSWORD) => {}
|
||||
Err(ref e) if e.code() == Some(&SqlState::INVALID_PASSWORD) => {}
|
||||
Err(err) => panic!("Unexpected error {:?}", err),
|
||||
_ => panic!("Expected error"),
|
||||
}
|
||||
@ -1244,7 +1243,7 @@ fn test_custom_range_element_type() {
|
||||
let stmt = or_panic!(conn.prepare("SELECT $1::floatrange"));
|
||||
let ty = &stmt.param_types()[0];
|
||||
assert_eq!("floatrange", ty.name());
|
||||
assert_eq!(&Kind::Range(FLOAT8), ty.kind());
|
||||
assert_eq!(&Kind::Range(Type::FLOAT8), ty.kind());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -5,8 +5,7 @@ use std::f64;
|
||||
use std::fmt;
|
||||
use std::result;
|
||||
|
||||
use postgres::types::{FromSql, FromSqlOwned, INT4, IsNull, Kind, ToSql, Type, WrongType, NUMERIC,
|
||||
TEXT};
|
||||
use postgres::types::{FromSql, FromSqlOwned, IsNull, Kind, ToSql, Type, WrongType};
|
||||
use postgres::{Connection, TlsMode};
|
||||
|
||||
#[cfg(feature = "with-bit-vec")]
|
||||
@ -479,11 +478,11 @@ fn composite() {
|
||||
match *type_.kind() {
|
||||
Kind::Composite(ref fields) => {
|
||||
assert_eq!(fields[0].name(), "name");
|
||||
assert_eq!(fields[0].type_(), &TEXT);
|
||||
assert_eq!(fields[0].type_(), &Type::TEXT);
|
||||
assert_eq!(fields[1].name(), "supplier");
|
||||
assert_eq!(fields[1].type_(), &INT4);
|
||||
assert_eq!(fields[1].type_(), &Type::INT4);
|
||||
assert_eq!(fields[2].name(), "price");
|
||||
assert_eq!(fields[2].type_(), &NUMERIC);
|
||||
assert_eq!(fields[2].type_(), &Type::NUMERIC);
|
||||
}
|
||||
ref t => panic!("bad type {:?}", t),
|
||||
}
|
||||
|
@ -89,7 +89,7 @@ pub use error::Error;
|
||||
#[doc(inline)]
|
||||
pub use postgres_shared::{error, params, types, CancelData, Notification};
|
||||
|
||||
use error::{DbError, UNDEFINED_COLUMN, UNDEFINED_TABLE};
|
||||
use error::{DbError, SqlState};
|
||||
use params::{ConnectParams, IntoConnectParams};
|
||||
use rows::Row;
|
||||
use sink::SinkExt;
|
||||
@ -97,7 +97,7 @@ use stmt::{Column, Statement};
|
||||
use stream::PostgresStream;
|
||||
use tls::Handshake;
|
||||
use transaction::Transaction;
|
||||
use types::{Field, FromSql, IsNull, Kind, Oid, ToSql, Type, CHAR, NAME, OID};
|
||||
use types::{Field, FromSql, IsNull, Kind, Oid, ToSql, Type};
|
||||
|
||||
#[macro_use]
|
||||
mod macros;
|
||||
@ -717,36 +717,36 @@ impl Connection {
|
||||
oid: Oid,
|
||||
) -> Box<Future<Item = (Type, Connection), Error = (Error, Connection)> + Send> {
|
||||
self.setup_typeinfo_query()
|
||||
.and_then(move |c| c.raw_execute(TYPEINFO_QUERY, "", &[OID], &[&oid]))
|
||||
.and_then(move |c| c.raw_execute(TYPEINFO_QUERY, "", &[Type::OID], &[&oid]))
|
||||
.and_then(|c| c.read_rows().collect())
|
||||
.and_then(move |(r, c)| {
|
||||
let get = |idx| r.get(0).and_then(|r| r.get(idx));
|
||||
|
||||
let name = match String::from_sql_nullable(&NAME, get(0)) {
|
||||
let name = match String::from_sql_nullable(&Type::NAME, get(0)) {
|
||||
Ok(v) => v,
|
||||
Err(e) => return Either::A(Err((error::conversion(e), c)).into_future()),
|
||||
};
|
||||
let type_ = match i8::from_sql_nullable(&CHAR, get(1)) {
|
||||
let type_ = match i8::from_sql_nullable(&Type::CHAR, get(1)) {
|
||||
Ok(v) => v,
|
||||
Err(e) => return Either::A(Err((error::conversion(e), c)).into_future()),
|
||||
};
|
||||
let elem_oid = match Oid::from_sql_nullable(&OID, get(2)) {
|
||||
let elem_oid = match Oid::from_sql_nullable(&Type::OID, get(2)) {
|
||||
Ok(v) => v,
|
||||
Err(e) => return Either::A(Err((error::conversion(e), c)).into_future()),
|
||||
};
|
||||
let rngsubtype = match Option::<Oid>::from_sql_nullable(&OID, get(3)) {
|
||||
let rngsubtype = match Option::<Oid>::from_sql_nullable(&Type::OID, get(3)) {
|
||||
Ok(v) => v,
|
||||
Err(e) => return Either::A(Err((error::conversion(e), c)).into_future()),
|
||||
};
|
||||
let basetype = match Oid::from_sql_nullable(&OID, get(4)) {
|
||||
let basetype = match Oid::from_sql_nullable(&Type::OID, get(4)) {
|
||||
Ok(v) => v,
|
||||
Err(e) => return Either::A(Err((error::conversion(e), c)).into_future()),
|
||||
};
|
||||
let schema = match String::from_sql_nullable(&NAME, get(5)) {
|
||||
let schema = match String::from_sql_nullable(&Type::NAME, get(5)) {
|
||||
Ok(v) => v,
|
||||
Err(e) => return Either::A(Err((error::conversion(e), c)).into_future()),
|
||||
};
|
||||
let relid = match Oid::from_sql_nullable(&OID, get(6)) {
|
||||
let relid = match Oid::from_sql_nullable(&Type::OID, get(6)) {
|
||||
Ok(v) => v,
|
||||
Err(e) => return Either::A(Err((error::conversion(e), c)).into_future()),
|
||||
};
|
||||
@ -811,7 +811,7 @@ impl Connection {
|
||||
WHERE t.oid = $1",
|
||||
).or_else(|(e, c)| {
|
||||
// Range types weren't added until Postgres 9.2, so pg_range may not exist
|
||||
if e.code() == Some(&UNDEFINED_TABLE) {
|
||||
if e.code() == Some(&SqlState::UNDEFINED_TABLE) {
|
||||
Either::A(c.raw_prepare(
|
||||
TYPEINFO_QUERY,
|
||||
"SELECT t.typname, t.typtype, t.typelem, \
|
||||
@ -838,12 +838,12 @@ impl Connection {
|
||||
oid: Oid,
|
||||
) -> Box<Future<Item = (Vec<String>, Connection), Error = (Error, Connection)> + Send> {
|
||||
self.setup_typeinfo_enum_query()
|
||||
.and_then(move |c| c.raw_execute(TYPEINFO_ENUM_QUERY, "", &[OID], &[&oid]))
|
||||
.and_then(move |c| c.raw_execute(TYPEINFO_ENUM_QUERY, "", &[Type::OID], &[&oid]))
|
||||
.and_then(|c| c.read_rows().collect())
|
||||
.and_then(|(r, c)| {
|
||||
let mut variants = vec![];
|
||||
for row in r {
|
||||
let variant = match String::from_sql_nullable(&NAME, row.get(0)) {
|
||||
let variant = match String::from_sql_nullable(&Type::NAME, row.get(0)) {
|
||||
Ok(v) => v,
|
||||
Err(e) => return Err((error::conversion(e), c)),
|
||||
};
|
||||
@ -868,7 +868,7 @@ impl Connection {
|
||||
WHERE enumtypid = $1 \
|
||||
ORDER BY enumsortorder",
|
||||
).or_else(|(e, c)| {
|
||||
if e.code() == Some(&UNDEFINED_COLUMN) {
|
||||
if e.code() == Some(&SqlState::UNDEFINED_COLUMN) {
|
||||
Either::A(c.raw_prepare(
|
||||
TYPEINFO_ENUM_QUERY,
|
||||
"SELECT enumlabel FROM pg_catalog.pg_enum WHERE \
|
||||
@ -890,15 +890,15 @@ impl Connection {
|
||||
oid: Oid,
|
||||
) -> Box<Future<Item = (Vec<Field>, Connection), Error = (Error, Connection)> + Send> {
|
||||
self.setup_typeinfo_composite_query()
|
||||
.and_then(move |c| c.raw_execute(TYPEINFO_COMPOSITE_QUERY, "", &[OID], &[&oid]))
|
||||
.and_then(move |c| c.raw_execute(TYPEINFO_COMPOSITE_QUERY, "", &[Type::OID], &[&oid]))
|
||||
.and_then(|c| c.read_rows().collect())
|
||||
.and_then(|(r, c)| {
|
||||
futures::stream::iter_ok(r).fold((vec![], c), |(mut fields, c), row| {
|
||||
let name = match String::from_sql_nullable(&NAME, row.get(0)) {
|
||||
let name = match String::from_sql_nullable(&Type::NAME, row.get(0)) {
|
||||
Ok(name) => name,
|
||||
Err(e) => return Either::A(Err((error::conversion(e), c)).into_future()),
|
||||
};
|
||||
let oid = match Oid::from_sql_nullable(&OID, row.get(1)) {
|
||||
let oid = match Oid::from_sql_nullable(&Type::OID, row.get(1)) {
|
||||
Ok(oid) => oid,
|
||||
Err(e) => return Either::A(Err((error::conversion(e), c)).into_future()),
|
||||
};
|
||||
|
@ -6,9 +6,9 @@ use std::time::Duration;
|
||||
use tokio_core::reactor::{Core, Interval};
|
||||
|
||||
use super::*;
|
||||
use error::{INVALID_AUTHORIZATION_SPECIFICATION, INVALID_PASSWORD, QUERY_CANCELED};
|
||||
use error::SqlState;
|
||||
use params::{ConnectParams, Host};
|
||||
use types::{FromSql, INT4, IsNull, Kind, ToSql, Type, BYTEA, NUMERIC, TEXT};
|
||||
use types::{FromSql, IsNull, Kind, ToSql, Type};
|
||||
|
||||
#[test]
|
||||
fn md5_user() {
|
||||
@ -48,7 +48,7 @@ fn md5_user_wrong_pass() {
|
||||
&handle,
|
||||
);
|
||||
match l.run(done) {
|
||||
Err(ref e) if e.code() == Some(&INVALID_PASSWORD) => {}
|
||||
Err(ref e) if e.code() == Some(&SqlState::INVALID_PASSWORD) => {}
|
||||
Err(e) => panic!("unexpected error {}", e),
|
||||
Ok(_) => panic!("unexpected success"),
|
||||
}
|
||||
@ -92,7 +92,7 @@ fn pass_user_wrong_pass() {
|
||||
&handle,
|
||||
);
|
||||
match l.run(done) {
|
||||
Err(ref e) if e.code() == Some(&INVALID_PASSWORD) => {}
|
||||
Err(ref e) if e.code() == Some(&SqlState::INVALID_PASSWORD) => {}
|
||||
Err(e) => panic!("unexpected error {}", e),
|
||||
Ok(_) => panic!("unexpected success"),
|
||||
}
|
||||
@ -128,7 +128,7 @@ fn batch_execute_err() {
|
||||
.and_then(|c| c.batch_execute("SELECT * FROM bogo"))
|
||||
.then(|r| match r {
|
||||
Err((e, s)) => {
|
||||
assert_eq!(e.code(), Some(&UNDEFINED_TABLE));
|
||||
assert_eq!(e.code(), Some(&SqlState::UNDEFINED_TABLE));
|
||||
s.batch_execute("SELECT * FROM foo")
|
||||
}
|
||||
Ok(_) => panic!("unexpected success"),
|
||||
@ -259,7 +259,10 @@ fn ssl_user_ssl_required() {
|
||||
);
|
||||
|
||||
match l.run(done) {
|
||||
Err(ref e) => assert_eq!(e.code(), Some(&INVALID_AUTHORIZATION_SPECIFICATION)),
|
||||
Err(ref e) => assert_eq!(
|
||||
e.code(),
|
||||
Some(&SqlState::INVALID_AUTHORIZATION_SPECIFICATION)
|
||||
),
|
||||
Ok(_) => panic!("unexpected success"),
|
||||
}
|
||||
}
|
||||
@ -305,7 +308,7 @@ fn domain() {
|
||||
|
||||
fn accepts(ty: &Type) -> bool {
|
||||
match *ty.kind() {
|
||||
Kind::Domain(BYTEA) => ty.name() == "session_id",
|
||||
Kind::Domain(Type::BYTEA) => ty.name() == "session_id",
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
@ -372,11 +375,11 @@ fn composite() {
|
||||
match *type_.kind() {
|
||||
Kind::Composite(ref fields) => {
|
||||
assert_eq!(fields[0].name(), "name");
|
||||
assert_eq!(fields[0].type_(), &TEXT);
|
||||
assert_eq!(fields[0].type_(), &Type::TEXT);
|
||||
assert_eq!(fields[1].name(), "supplier");
|
||||
assert_eq!(fields[1].type_(), &INT4);
|
||||
assert_eq!(fields[1].type_(), &Type::INT4);
|
||||
assert_eq!(fields[2].name(), "price");
|
||||
assert_eq!(fields[2].type_(), &NUMERIC);
|
||||
assert_eq!(fields[2].type_(), &Type::NUMERIC);
|
||||
}
|
||||
ref t => panic!("bad type {:?}", t),
|
||||
}
|
||||
@ -442,7 +445,7 @@ fn cancel() {
|
||||
let (select, cancel) = l.run(done).unwrap();
|
||||
cancel.unwrap();
|
||||
match select {
|
||||
Err((e, _)) => assert_eq!(e.code(), Some(&QUERY_CANCELED)),
|
||||
Err((e, _)) => assert_eq!(e.code(), Some(&SqlState::QUERY_CANCELED)),
|
||||
Ok(_) => panic!("unexpected success"),
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user