From e5c2edaf97c3f7b132c1d701587c5c23d342ff31 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Mon, 17 Nov 2014 13:46:33 -0800 Subject: [PATCH] More enum namespacing preparation --- README.md | 8 +-- src/io.rs | 13 +++-- src/lib.rs | 129 +++++++++++++++++++++------------------------ src/macros.rs | 4 +- tests/test.rs | 114 ++++++++++++++++++++------------------- tests/types/mod.rs | 10 ++-- 6 files changed, 132 insertions(+), 146 deletions(-) diff --git a/README.md b/README.md index 81ecac0b..f1533613 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ extern crate time; use time::Timespec; -use postgres::{Connection, NoSsl}; +use postgres::{Connection, SslMode}; struct Person { id: i32, @@ -31,7 +31,7 @@ struct Person { } fn main() { - let conn = Connection::connect("postgres://postgres@localhost", &NoSsl) + let conn = Connection::connect("postgres://postgres@localhost", &SslMode::None) .unwrap(); conn.execute("CREATE TABLE person ( @@ -297,8 +297,8 @@ traits. ### UUID type [UUID](http://www.postgresql.org/docs/9.4/static/datatype-uuid.html) support is -provided optionally by the `uuid` feature. It is enabled by default. To -disable `UUID` support, add `default-features = false` to your Cargo manifest: +provided optionally by the `uuid` feature. It is enabled by default. To disable +`UUID` support, add `default-features = false` to your Cargo manifest: ```toml [dependencies.postgres] diff --git a/src/io.rs b/src/io.rs index 89d31cdd..9fe2471b 100644 --- a/src/io.rs +++ b/src/io.rs @@ -4,8 +4,7 @@ use std::io::net::tcp; use std::io::net::pipe; use std::io::{Stream, IoResult}; -use {ConnectParams, SslMode, NoSsl, PreferSsl, RequireSsl, ConnectTarget}; -use error::{ConnectError, NoSslSupport, SslError}; +use {ConnectParams, SslMode, ConnectTarget, ConnectError}; use message; use message::{SslRequest, WriteMessage}; @@ -90,9 +89,9 @@ pub fn initialize_stream(params: &ConnectParams, ssl: &SslMode) let mut socket = try!(open_socket(params)); let (ssl_required, ctx) = match *ssl { - NoSsl => return Ok(MaybeSslStream::Normal(socket)), - PreferSsl(ref ctx) => (false, ctx), - RequireSsl(ref ctx) => (true, ctx) + SslMode::None => return Ok(MaybeSslStream::Normal(socket)), + SslMode::Prefer(ref ctx) => (false, ctx), + SslMode::Require(ref ctx) => (true, ctx) }; try!(socket.write_message(&SslRequest { code: message::SSL_CODE })); @@ -100,7 +99,7 @@ pub fn initialize_stream(params: &ConnectParams, ssl: &SslMode) if try!(socket.read_u8()) == 'N' as u8 { if ssl_required { - return Err(NoSslSupport); + return Err(ConnectError::NoSslSupport); } else { return Ok(MaybeSslStream::Normal(socket)); } @@ -108,6 +107,6 @@ pub fn initialize_stream(params: &ConnectParams, ssl: &SslMode) match ssl::SslStream::new(ctx, socket) { Ok(stream) => Ok(MaybeSslStream::Ssl(stream)), - Err(err) => Err(SslError(err)) + Err(err) => Err(ConnectError::SslError(err)) } } diff --git a/src/lib.rs b/src/lib.rs index 91661255..d0c9012a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,7 +8,7 @@ //! //! use time::Timespec; //! -//! use postgres::{Connection, NoSsl}; +//! use postgres::{Connection, SslMode}; //! //! struct Person { //! id: i32, @@ -18,7 +18,7 @@ //! } //! //! fn main() { -//! let conn = Connection::connect("postgresql://postgres@localhost", &NoSsl) +//! let conn = Connection::connect("postgresql://postgres@localhost", &SslMode::None) //! .unwrap(); //! //! conn.execute("CREATE TABLE person ( @@ -75,17 +75,6 @@ use std::mem; use std::fmt; use std::result; -use error::{InvalidUrl, - MissingPassword, - MissingUser, - PgConnectBadResponse, - PgInvalidColumn, - PgStreamDesynchronized, - PgWrongParamCount, - UnsupportedAuthentication, - PgWrongConnection, - PgWrongTransaction, - PgBadResponse}; use io::{MaybeSslStream, InternalStream}; use message::{FrontendMessage, BackendMessage, RowDescriptionEntry}; use message::FrontendMessage::*; @@ -163,7 +152,7 @@ impl<'a> IntoConnectParams for &'a str { fn into_connect_params(self) -> result::Result { match Url::parse(self) { Ok(url) => url.into_connect_params(), - Err(err) => return Err(InvalidUrl(err)), + Err(err) => return Err(ConnectError::InvalidUrl(err)), } } } @@ -178,7 +167,7 @@ impl IntoConnectParams for Url { .. } = self; - let maybe_path = try!(url::decode_component(host[]).map_err(InvalidUrl)); + let maybe_path = try!(url::decode_component(host[]).map_err(ConnectError::InvalidUrl)); let target = if maybe_path[].starts_with("/") { ConnectTarget::Unix(Path::new(maybe_path)) } else { @@ -194,7 +183,7 @@ impl IntoConnectParams for Url { let (_, path) = path[].slice_shift_char(); Some(path.into_string()) } else { - None + Option::None }; Ok(ConnectParams { @@ -274,15 +263,15 @@ pub struct CancelData { /// ## Example /// /// ```rust,no_run -/// # use postgres::{Connection, NoSsl}; +/// # use postgres::{Connection, SslMode}; /// # let url = ""; -/// let conn = Connection::connect(url, &NoSsl).unwrap(); +/// let conn = Connection::connect(url, &SslMode::None).unwrap(); /// let cancel_data = conn.cancel_data(); /// spawn(proc() { /// conn.execute("SOME EXPENSIVE QUERY", []).unwrap(); /// }); /// # let _ = -/// postgres::cancel_query(url, &NoSsl, cancel_data); +/// postgres::cancel_query(url, &SslMode::None, cancel_data); /// ``` pub fn cancel_query(params: T, ssl: &SslMode, data: CancelData) -> result::Result<(), ConnectError> where T: IntoConnectParams { @@ -334,7 +323,7 @@ impl InnerConnection { .. } = params; - let user = try!(user.ok_or(MissingUser)); + let user = try!(user.ok_or(ConnectError::MissingUser)); let mut conn = InnerConnection { stream: BufferedStream::new(stream), @@ -357,7 +346,7 @@ impl InnerConnection { options.push(("user".into_string(), user.user.clone())); match database { Some(database) => options.push(("database".into_string(), database)), - None => {} + Option::None => {} } try!(conn.write_messages([StartupMessage { @@ -375,7 +364,7 @@ impl InnerConnection { } ReadyForQuery { .. } => break, ErrorResponse { fields } => return DbError::new_connect(fields), - _ => return Err(PgConnectBadResponse), + _ => return Err(ConnectError::PgConnectBadResponse), } } @@ -418,13 +407,13 @@ impl InnerConnection { match try!(self.read_message()) { AuthenticationOk => return Ok(()), AuthenticationCleartextPassword => { - let pass = try!(user.password.ok_or(MissingPassword)); + let pass = try!(user.password.ok_or(ConnectError::MissingPassword)); try!(self.write_messages([PasswordMessage { password: pass[], }])); } AuthenticationMD5Password { salt } => { - let pass = try!(user.password.ok_or(MissingPassword)); + let pass = try!(user.password.ok_or(ConnectError::MissingPassword)); let hasher = Hasher::new(MD5); hasher.update(pass.as_bytes()); hasher.update(user.user.as_bytes()); @@ -440,11 +429,11 @@ impl InnerConnection { AuthenticationKerberosV5 | AuthenticationSCMCredential | AuthenticationGSS - | AuthenticationSSPI => return Err(UnsupportedAuthentication), + | AuthenticationSSPI => return Err(ConnectError::UnsupportedAuthentication), ErrorResponse { fields } => return DbError::new_connect(fields), _ => { self.desynchronized = true; - return Err(PgConnectBadResponse); + return Err(ConnectError::PgConnectBadResponse); } } @@ -453,7 +442,7 @@ impl InnerConnection { ErrorResponse { fields } => return DbError::new_connect(fields), _ => { self.desynchronized = true; - return Err(PgConnectBadResponse); + return Err(ConnectError::PgConnectBadResponse); } } } @@ -679,23 +668,23 @@ impl Connection { /// ## Examples /// /// ```rust,no_run - /// # use postgres::{Connection, NoSsl}; + /// # use postgres::{Connection, SslMode}; /// # let _ = || { /// let url = "postgresql://postgres:hunter2@localhost:2994/foodb"; - /// let conn = try!(Connection::connect(url, &NoSsl)); + /// let conn = try!(Connection::connect(url, &SslMode::None)); /// # Ok(()) }; /// ``` /// /// ```rust,no_run - /// # use postgres::{Connection, NoSsl}; + /// # use postgres::{Connection, SslMode}; /// # let _ = || { /// let url = "postgresql://postgres@%2Frun%2Fpostgres"; - /// let conn = try!(Connection::connect(url, &NoSsl)); + /// let conn = try!(Connection::connect(url, &SslMode::None)); /// # Ok(()) }; /// ``` /// /// ```rust,no_run - /// # use postgres::{Connection, UserInfo, ConnectParams, NoSsl, ConnectTarget}; + /// # use postgres::{Connection, UserInfo, ConnectParams, SslMode, ConnectTarget}; /// # let _ = || { /// # let some_crazy_path = Path::new(""); /// let params = ConnectParams { @@ -708,7 +697,7 @@ impl Connection { /// database: None, /// options: vec![], /// }; - /// let conn = try!(Connection::connect(params, &NoSsl)); + /// let conn = try!(Connection::connect(params, &SslMode::None)); /// # Ok(()) }; /// ``` pub fn connect(params: T, ssl: &SslMode) -> result::Result @@ -742,8 +731,8 @@ impl Connection { /// ## Example /// /// ```rust,no_run - /// # use postgres::{Connection, NoSsl}; - /// # let conn = Connection::connect("", &NoSsl).unwrap(); + /// # use postgres::{Connection, SslMode}; + /// # let conn = Connection::connect("", &SslMode::None).unwrap(); /// let maybe_stmt = conn.prepare("SELECT foo FROM bar WHERE baz = $1"); /// let stmt = match maybe_stmt { /// Ok(stmt) => stmt, @@ -752,7 +741,7 @@ impl Connection { pub fn prepare<'a>(&'a self, query: &str) -> Result> { let mut conn = self.conn.borrow_mut(); if conn.trans_depth != 0 { - return Err(PgWrongTransaction); + return Err(Error::PgWrongTransaction); } conn.prepare(query, self) } @@ -765,10 +754,10 @@ impl Connection { /// ## Example /// /// ```rust,no_run - /// # use postgres::{Connection, NoSsl}; + /// # use postgres::{Connection, SslMode}; /// # use postgres::types::ToSql; /// # let _ = || { - /// # let conn = Connection::connect("", &NoSsl).unwrap(); + /// # let conn = Connection::connect("", &SslMode::None).unwrap(); /// try!(conn.execute("CREATE TABLE foo ( /// bar INT PRIMARY KEY, /// baz VARCHAR @@ -784,7 +773,7 @@ impl Connection { -> Result> { let mut conn = self.conn.borrow_mut(); if conn.trans_depth != 0 { - return Err(PgWrongTransaction); + return Err(Error::PgWrongTransaction); } conn.prepare_copy_in(table, rows, self) } @@ -802,9 +791,9 @@ impl Connection { /// ## Example /// /// ```rust,no_run - /// # use postgres::{Connection, NoSsl}; + /// # use postgres::{Connection, SslMode}; /// # fn foo() -> Result<(), postgres::Error> { - /// # let conn = Connection::connect("", &NoSsl).unwrap(); + /// # let conn = Connection::connect("", &SslMode::None).unwrap(); /// let trans = try!(conn.transaction()); /// try!(trans.execute("UPDATE foo SET bar = 10", [])); /// // ... @@ -817,7 +806,7 @@ impl Connection { let mut conn = self.conn.borrow_mut(); check_desync!(conn); if conn.trans_depth != 0 { - return Err(PgWrongTransaction); + return Err(Error::PgWrongTransaction); } try!(conn.quick_query("BEGIN")); conn.trans_depth += 1; @@ -877,7 +866,7 @@ impl Connection { pub fn batch_execute(&self, query: &str) -> Result<()> { let mut conn = self.conn.borrow_mut(); if conn.trans_depth != 0 { - return Err(PgWrongTransaction); + return Err(Error::PgWrongTransaction); } conn.quick_query(query).map(|_| ()) } @@ -921,11 +910,11 @@ impl Connection { /// Specifies the SSL support requested for a new connection pub enum SslMode { /// The connection will not use SSL - NoSsl, + None, /// The connection will use SSL if the backend supports it - PreferSsl(SslContext), + Prefer(SslContext), /// The connection must use SSL - RequireSsl(SslContext) + Require(SslContext) } /// Represents a transaction on a database connection. @@ -965,7 +954,7 @@ impl<'conn> Transaction<'conn> { pub fn prepare<'a>(&'a self, query: &str) -> Result> { let mut conn = self.conn.conn.borrow_mut(); if conn.trans_depth != self.depth { - return Err(PgWrongTransaction); + return Err(Error::PgWrongTransaction); } conn.prepare(query, self.conn) } @@ -975,7 +964,7 @@ impl<'conn> Transaction<'conn> { -> Result> { let mut conn = self.conn.conn.borrow_mut(); if conn.trans_depth != self.depth { - return Err(PgWrongTransaction); + return Err(Error::PgWrongTransaction); } conn.prepare_copy_in(table, cols, self.conn) } @@ -989,7 +978,7 @@ impl<'conn> Transaction<'conn> { pub fn batch_execute(&self, query: &str) -> Result<()> { let mut conn = self.conn.conn.borrow_mut(); if conn.trans_depth != self.depth { - return Err(PgWrongTransaction); + return Err(Error::PgWrongTransaction); } conn.quick_query(query).map(|_| ()) } @@ -999,7 +988,7 @@ impl<'conn> Transaction<'conn> { let mut conn = self.conn.conn.borrow_mut(); check_desync!(conn); if conn.trans_depth != self.depth { - return Err(PgWrongTransaction); + return Err(Error::PgWrongTransaction); } try!(conn.quick_query("SAVEPOINT sp")); conn.trans_depth += 1; @@ -1024,7 +1013,7 @@ impl<'conn> Transaction<'conn> { row_limit: i32) -> Result> { if self.conn as *const _ != stmt.conn as *const _ { - return Err(PgWrongConnection); + return Err(Error::PgWrongConnection); } check_desync!(self.conn); stmt.lazy_query(row_limit, params).map(|result| { @@ -1096,7 +1085,7 @@ impl<'conn> Statement<'conn> { -> Result<()> { let mut conn = self.conn.conn.borrow_mut(); if self.param_types.len() != params.len() { - return Err(PgWrongParamCount { + return Err(Error::PgWrongParamCount { expected: self.param_types.len(), actual: params.len(), }); @@ -1128,7 +1117,7 @@ impl<'conn> Statement<'conn> { } _ => { conn.desynchronized = true; - Err(PgBadResponse) + Err(Error::PgBadResponse) } } } @@ -1171,8 +1160,8 @@ impl<'conn> Statement<'conn> { /// ## Example /// /// ```rust,no_run - /// # use postgres::{Connection, NoSsl}; - /// # let conn = Connection::connect("", &NoSsl).unwrap(); + /// # use postgres::{Connection, SslMode}; + /// # let conn = Connection::connect("", &SslMode::None).unwrap(); /// # let bar = 1i32; /// # let baz = true; /// let stmt = conn.prepare("UPDATE foo SET bar = $1 WHERE baz = $2").unwrap(); @@ -1210,7 +1199,7 @@ impl<'conn> Statement<'conn> { } _ => { conn.desynchronized = true; - return Err(PgBadResponse); + return Err(Error::PgBadResponse); } } } @@ -1225,8 +1214,8 @@ impl<'conn> Statement<'conn> { /// ## Example /// /// ```rust,no_run - /// # use postgres::{Connection, NoSsl}; - /// # let conn = Connection::connect("", &NoSsl).unwrap(); + /// # use postgres::{Connection, SslMode}; + /// # let conn = Connection::connect("", &SslMode::None).unwrap(); /// let stmt = conn.prepare("SELECT foo FROM bar WHERE baz = $1").unwrap(); /// # let baz = true; /// let mut rows = match stmt.query(&[&baz]) { @@ -1332,7 +1321,7 @@ impl<'stmt> Rows<'stmt> { } _ => { conn.desynchronized = true; - return Err(PgBadResponse); + return Err(Error::PgBadResponse); } } } @@ -1380,7 +1369,7 @@ impl<'stmt> Iterator> for Rows<'stmt> { fn size_hint(&self) -> (uint, Option) { let lower = self.data.len(); let upper = if self.more_rows { - None + Option::None } else { Some(lower) }; @@ -1409,7 +1398,7 @@ impl<'stmt> Row<'stmt> { /// Returns an `Error` value if the index does not reference a column or /// the return type is not compatible with the Postgres type. pub fn get_opt(&self, idx: I) -> Result where I: RowIndex, T: FromSql { - let idx = try!(idx.idx(self.stmt).ok_or(PgInvalidColumn)); + let idx = try!(idx.idx(self.stmt).ok_or(Error::PgInvalidColumn)); FromSql::from_sql(&self.stmt.result_desc[idx].ty, &self.data[idx]) } @@ -1426,8 +1415,8 @@ impl<'stmt> Row<'stmt> { /// ## Example /// /// ```rust,no_run - /// # use postgres::{Connection, NoSsl}; - /// # let conn = Connection::connect("", &NoSsl).unwrap(); + /// # use postgres::{Connection, SslMode}; + /// # let conn = Connection::connect("", &SslMode::None).unwrap(); /// # let stmt = conn.prepare("").unwrap(); /// # let mut result = stmt.query([]).unwrap(); /// # let row = result.next().unwrap(); @@ -1453,7 +1442,7 @@ impl RowIndex for uint { #[inline] fn idx(&self, stmt: &Statement) -> Option { if *self > stmt.result_desc.len() { - None + Option::None } else { Some(*self) } @@ -1554,7 +1543,7 @@ impl<'a> CopyInStatement<'a> { } _ => { conn.desynchronized = true; - return Err(PgBadResponse); + return Err(Error::PgBadResponse); } } @@ -1562,7 +1551,7 @@ impl<'a> CopyInStatement<'a> { CopyInResponse { .. } => {} _ => { conn.desynchronized = true; - return Err(PgBadResponse); + return Err(Error::PgBadResponse); } } @@ -1579,7 +1568,7 @@ impl<'a> CopyInStatement<'a> { match (row.next(), types.next()) { (Some(val), Some(ty)) => { match val.to_sql(ty) { - Ok(None) => { + Ok(Option::None) => { let _ = buf.write_be_i32(-1); } Ok(Some(val)) => { @@ -1596,14 +1585,14 @@ impl<'a> CopyInStatement<'a> { } } } - (Some(_), None) | (None, Some(_)) => { + (Some(_), Option::None) | (Option::None, Some(_)) => { try_desync!(conn, conn.stream.write_message( &CopyFail { message: "Invalid column count", })); break 'l; } - (None, None) => break + (Option::None, Option::None) => break } } @@ -1632,7 +1621,7 @@ impl<'a> CopyInStatement<'a> { } _ => { conn.desynchronized = true; - return Err(PgBadResponse); + return Err(Error::PgBadResponse); } }; diff --git a/src/macros.rs b/src/macros.rs index edefed7e..bceea392 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -16,7 +16,7 @@ macro_rules! check_desync( panic!("Connection use after free. See mozilla/rust#13246."); } if $e.is_desynchronized() { - return Err(PgStreamDesynchronized); + return Err(::Error::PgStreamDesynchronized); } }) ) @@ -25,6 +25,6 @@ macro_rules! bad_response( ($s:expr) => ({ debug!("Unexpected response"); $s.desynchronized = true; - return Err(PgBadResponse); + return Err(::Error::PgBadResponse); }) ) diff --git a/tests/test.rs b/tests/test.rs index 16b1dee9..26b904cf 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -16,9 +16,7 @@ use postgres::{NoticeHandler, Connection, GenericConnection, ResultDescription, - RequireSsl, - PreferSsl, - NoSsl, + SslMode, Type, ToSql, DbError}; @@ -52,17 +50,17 @@ mod types; #[test] fn test_non_default_database() { - or_panic!(Connection::connect("postgres://postgres@localhost/postgres", &NoSsl)); + or_panic!(Connection::connect("postgres://postgres@localhost/postgres", &SslMode::None)); } #[test] fn test_url_terminating_slash() { - or_panic!(Connection::connect("postgres://postgres@localhost/", &NoSsl)); + or_panic!(Connection::connect("postgres://postgres@localhost/", &SslMode::None)); } #[test] fn test_prepare_err() { - let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &NoSsl)); + let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None)); match conn.prepare("invalid sql statment") { Err(PgDbError(DbError { code: SyntaxError, position: Some(Normal(1)), .. })) => (), Err(e) => panic!("Unexpected result {}", e), @@ -72,7 +70,7 @@ fn test_prepare_err() { #[test] fn test_unknown_database() { - match Connection::connect("postgres://postgres@localhost/asdf", &NoSsl) { + match Connection::connect("postgres://postgres@localhost/asdf", &SslMode::None) { Err(PgConnectDbError(DbError { code: InvalidCatalogName, .. })) => {} Err(resp) => panic!("Unexpected result {}", resp), _ => panic!("Unexpected result"), @@ -81,13 +79,13 @@ fn test_unknown_database() { #[test] fn test_connection_finish() { - let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &NoSsl)); + let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None)); assert!(conn.finish().is_ok()); } #[test] fn test_unix_connection() { - let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &NoSsl)); + let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None)); let stmt = or_panic!(conn.prepare("SHOW unix_socket_directories")); let result = or_panic!(stmt.query([])); let unix_socket_directories: String = result.map(|row| row.get(0)).next().unwrap(); @@ -100,13 +98,13 @@ fn test_unix_connection() { let path = url::utf8_percent_encode(unix_socket_directory, url::USERNAME_ENCODE_SET); let url = format!("postgres://postgres@{}", path); - let conn = or_panic!(Connection::connect(url[], &NoSsl)); + let conn = or_panic!(Connection::connect(url[], &SslMode::None)); assert!(conn.finish().is_ok()); } #[test] fn test_transaction_commit() { - let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &NoSsl)); + let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None)); or_panic!(conn.execute("CREATE TEMPORARY TABLE foo (id INT PRIMARY KEY)", [])); let trans = or_panic!(conn.transaction()); @@ -122,7 +120,7 @@ fn test_transaction_commit() { #[test] fn test_transaction_commit_finish() { - let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &NoSsl)); + let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None)); or_panic!(conn.execute("CREATE TEMPORARY TABLE foo (id INT PRIMARY KEY)", [])); let trans = or_panic!(conn.transaction()); @@ -138,7 +136,7 @@ fn test_transaction_commit_finish() { #[test] fn test_transaction_commit_method() { - let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &NoSsl)); + let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None)); or_panic!(conn.execute("CREATE TEMPORARY TABLE foo (id INT PRIMARY KEY)", [])); let trans = or_panic!(conn.transaction()); @@ -153,7 +151,7 @@ fn test_transaction_commit_method() { #[test] fn test_transaction_rollback() { - let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &NoSsl)); + let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None)); or_panic!(conn.execute("CREATE TEMPORARY TABLE foo (id INT PRIMARY KEY)", [])); or_panic!(conn.execute("INSERT INTO foo (id) VALUES ($1)", &[&1i32])); @@ -170,7 +168,7 @@ fn test_transaction_rollback() { #[test] fn test_transaction_rollback_finish() { - let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &NoSsl)); + let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None)); or_panic!(conn.execute("CREATE TEMPORARY TABLE foo (id INT PRIMARY KEY)", [])); or_panic!(conn.execute("INSERT INTO foo (id) VALUES ($1)", &[&1i32])); @@ -187,7 +185,7 @@ fn test_transaction_rollback_finish() { #[test] fn test_nested_transactions() { - let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &NoSsl)); + let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None)); or_panic!(conn.execute("CREATE TEMPORARY TABLE foo (id INT PRIMARY KEY)", [])); or_panic!(conn.execute("INSERT INTO foo (id) VALUES (1)", [])); @@ -233,7 +231,7 @@ fn test_nested_transactions() { #[test] fn test_nested_transactions_finish() { - let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &NoSsl)); + let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None)); or_panic!(conn.execute("CREATE TEMPORARY TABLE foo (id INT PRIMARY KEY)", [])); or_panic!(conn.execute("INSERT INTO foo (id) VALUES (1)", [])); @@ -288,7 +286,7 @@ fn test_nested_transactions_finish() { #[test] fn test_conn_prepare_with_trans() { - let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &NoSsl)); + let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None)); let _trans = or_panic!(conn.transaction()); match conn.prepare("") { Err(PgWrongTransaction) => {} @@ -304,7 +302,7 @@ fn test_conn_prepare_with_trans() { #[test] fn test_trans_prepare_with_nested_trans() { - let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &NoSsl)); + let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None)); let trans = or_panic!(conn.transaction()); let _trans2 = or_panic!(trans.transaction()); match trans.prepare("") { @@ -321,7 +319,7 @@ fn test_trans_prepare_with_nested_trans() { #[test] fn test_stmt_finish() { - let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &NoSsl)); + let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None)); or_panic!(conn.execute("CREATE TEMPORARY TABLE foo (id BIGINT PRIMARY KEY)", [])); let stmt = or_panic!(conn.prepare("SELECT * FROM foo")); assert!(stmt.finish().is_ok()); @@ -329,7 +327,7 @@ fn test_stmt_finish() { #[test] fn test_batch_execute() { - let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &NoSsl)); + let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None)); let query = "CREATE TEMPORARY TABLE foo (id BIGINT PRIMARY KEY); INSERT INTO foo (id) VALUES (10);"; or_panic!(conn.batch_execute(query)); @@ -342,7 +340,7 @@ fn test_batch_execute() { #[test] fn test_batch_execute_error() { - let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &NoSsl)); + let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None)); let query = "CREATE TEMPORARY TABLE foo (id BIGINT PRIMARY KEY); INSERT INTO foo (id) VALUES (10); asdfa; @@ -358,7 +356,7 @@ fn test_batch_execute_error() { #[test] fn test_transaction_batch_execute() { - let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &NoSsl)); + let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None)); let trans = or_panic!(conn.transaction()); let query = "CREATE TEMPORARY TABLE foo (id BIGINT PRIMARY KEY); INSERT INTO foo (id) VALUES (10);"; @@ -372,7 +370,7 @@ fn test_transaction_batch_execute() { #[test] fn test_query() { - let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &NoSsl)); + let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None)); or_panic!(conn.execute("CREATE TEMPORARY TABLE foo (id BIGINT PRIMARY KEY)", [])); or_panic!(conn.execute("INSERT INTO foo (id) VALUES ($1), ($2)", &[&1i64, &2i64])); @@ -384,7 +382,7 @@ fn test_query() { #[test] fn test_error_after_datarow() { - let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &NoSsl)); + let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None)); let stmt = or_panic!(conn.prepare(" SELECT (SELECT generate_series(1, ss.i)) @@ -401,7 +399,7 @@ FROM (SELECT gs.i #[test] fn test_result_finish() { - let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &NoSsl)); + let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None)); or_panic!(conn.execute("CREATE TEMPORARY TABLE foo (id BIGINT PRIMARY KEY)", [])); let stmt = or_panic!(conn.prepare("SELECT * FROM foo")); let result = or_panic!(stmt.query([])); @@ -410,7 +408,7 @@ fn test_result_finish() { #[test] fn test_lazy_query() { - let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &NoSsl)); + let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None)); let trans = or_panic!(conn.transaction()); or_panic!(trans.execute("CREATE TEMPORARY TABLE foo (id INT PRIMARY KEY)", [])); @@ -426,8 +424,8 @@ fn test_lazy_query() { #[test] fn test_lazy_query_wrong_conn() { - let conn1 = or_panic!(Connection::connect("postgres://postgres@localhost", &NoSsl)); - let conn2 = or_panic!(Connection::connect("postgres://postgres@localhost", &NoSsl)); + let conn1 = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None)); + let conn2 = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None)); let trans = or_panic!(conn1.transaction()); let stmt = or_panic!(conn2.prepare("SELECT 1::INT")); @@ -440,14 +438,14 @@ fn test_lazy_query_wrong_conn() { #[test] fn test_param_types() { - let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &NoSsl)); + let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None)); let stmt = or_panic!(conn.prepare("SELECT $1::INT, $2::VARCHAR")); assert_eq!(stmt.param_types(), [Type::Int4, Type::Varchar][]); } #[test] fn test_result_descriptions() { - let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &NoSsl)); + let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None)); let stmt = or_panic!(conn.prepare("SELECT 1::INT as a, 'hi'::VARCHAR as b")); assert!(stmt.result_descriptions() == [ResultDescription { name: "a".to_string(), ty: Type::Int4}, @@ -456,7 +454,7 @@ fn test_result_descriptions() { #[test] fn test_execute_counts() { - let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &NoSsl)); + let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None)); assert_eq!(0, or_panic!(conn.execute("CREATE TEMPORARY TABLE foo ( id SERIAL PRIMARY KEY, b INT @@ -469,7 +467,7 @@ fn test_execute_counts() { #[test] fn test_wrong_param_type() { - let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &NoSsl)); + let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None)); match conn.execute("SELECT $1::VARCHAR", &[&1i32]) { Err(PgWrongType(_)) => {} res => panic!("unexpected result {}", res) @@ -478,7 +476,7 @@ fn test_wrong_param_type() { #[test] fn test_too_few_params() { - let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &NoSsl)); + let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None)); match conn.execute("SELECT $1::INT, $2::INT", &[&1i32]) { Err(PgWrongParamCount { expected: 2, actual: 1 }) => {}, res => panic!("unexpected result {}", res) @@ -487,7 +485,7 @@ fn test_too_few_params() { #[test] fn test_too_many_params() { - let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &NoSsl)); + let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None)); match conn.execute("SELECT $1::INT, $2::INT", &[&1i32, &2i32, &3i32]) { Err(PgWrongParamCount { expected: 2, actual: 3 }) => {}, res => panic!("unexpected result {}", res) @@ -496,7 +494,7 @@ fn test_too_many_params() { #[test] fn test_index_named() { - let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &NoSsl)); + let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None)); let stmt = or_panic!(conn.prepare("SELECT 10::INT as val")); let result = or_panic!(stmt.query([])); @@ -506,7 +504,7 @@ fn test_index_named() { #[test] #[should_fail] fn test_index_named_fail() { - let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &NoSsl)); + let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None)); let stmt = or_panic!(conn.prepare("SELECT 10::INT as id")); let mut result = or_panic!(stmt.query([])); @@ -515,7 +513,7 @@ fn test_index_named_fail() { #[test] fn test_get_named_err() { - let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &NoSsl)); + let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None)); let stmt = or_panic!(conn.prepare("SELECT 10::INT as id")); let mut result = or_panic!(stmt.query([])); @@ -527,7 +525,7 @@ fn test_get_named_err() { #[test] fn test_get_was_null() { - let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &NoSsl)); + let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None)); let stmt = or_panic!(conn.prepare("SELECT NULL::INT as id")); let mut result = or_panic!(stmt.query([])); @@ -550,7 +548,7 @@ fn test_custom_notice_handler() { } let conn = or_panic!(Connection::connect( - "postgres://postgres@localhost?client_min_messages=NOTICE", &NoSsl)); + "postgres://postgres@localhost?client_min_messages=NOTICE", &SslMode::None)); conn.set_notice_handler(box Handler); or_panic!(conn.execute("CREATE FUNCTION pg_temp.note() RETURNS INT AS $$ BEGIN @@ -564,7 +562,7 @@ fn test_custom_notice_handler() { #[test] fn test_notification_iterator_none() { - let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &NoSsl)); + let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None)); assert!(conn.notifications().next().is_none()); } @@ -581,7 +579,7 @@ fn test_notification_iterator_some() { } } - let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &NoSsl)); + let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None)); let mut it = conn.notifications(); or_panic!(conn.execute("LISTEN test_notification_iterator_one_channel", [])); or_panic!(conn.execute("LISTEN test_notification_iterator_one_channel2", [])); @@ -612,12 +610,12 @@ fn test_notification_iterator_some() { #[test] // This test is pretty sad, but I don't think there's a better way :( fn test_cancel_query() { - let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &NoSsl)); + let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None)); let cancel_data = conn.cancel_data(); spawn(proc() { timer::sleep(Duration::milliseconds(500)); - assert!(postgres::cancel_query("postgres://postgres@localhost", &NoSsl, + assert!(postgres::cancel_query("postgres://postgres@localhost", &SslMode::None, cancel_data).is_ok()); }); @@ -632,7 +630,7 @@ fn test_cancel_query() { fn test_require_ssl_conn() { let ctx = SslContext::new(Sslv3).unwrap(); let conn = or_panic!(Connection::connect("postgres://postgres@localhost", - &RequireSsl(ctx))); + &SslMode::Require(ctx))); or_panic!(conn.execute("SELECT 1::VARCHAR", [])); } @@ -640,18 +638,18 @@ fn test_require_ssl_conn() { fn test_prefer_ssl_conn() { let ctx = SslContext::new(Sslv3).unwrap(); let conn = or_panic!(Connection::connect("postgres://postgres@localhost", - &PreferSsl(ctx))); + &SslMode::Prefer(ctx))); or_panic!(conn.execute("SELECT 1::VARCHAR", [])); } #[test] fn test_plaintext_pass() { - or_panic!(Connection::connect("postgres://pass_user:password@localhost/postgres", &NoSsl)); + or_panic!(Connection::connect("postgres://pass_user:password@localhost/postgres", &SslMode::None)); } #[test] fn test_plaintext_pass_no_pass() { - let ret = Connection::connect("postgres://pass_user@localhost/postgres", &NoSsl); + let ret = Connection::connect("postgres://pass_user@localhost/postgres", &SslMode::None); match ret { Err(MissingPassword) => (), Err(err) => panic!("Unexpected error {}", err), @@ -661,7 +659,7 @@ fn test_plaintext_pass_no_pass() { #[test] fn test_plaintext_pass_wrong_pass() { - let ret = Connection::connect("postgres://pass_user:asdf@localhost/postgres", &NoSsl); + let ret = Connection::connect("postgres://pass_user:asdf@localhost/postgres", &SslMode::None); match ret { Err(PgConnectDbError(DbError { code: InvalidPassword, .. })) => (), Err(err) => panic!("Unexpected error {}", err), @@ -671,12 +669,12 @@ fn test_plaintext_pass_wrong_pass() { #[test] fn test_md5_pass() { - or_panic!(Connection::connect("postgres://md5_user:password@localhost/postgres", &NoSsl)); + or_panic!(Connection::connect("postgres://md5_user:password@localhost/postgres", &SslMode::None)); } #[test] fn test_md5_pass_no_pass() { - let ret = Connection::connect("postgres://md5_user@localhost/postgres", &NoSsl); + let ret = Connection::connect("postgres://md5_user@localhost/postgres", &SslMode::None); match ret { Err(MissingPassword) => (), Err(err) => panic!("Unexpected error {}", err), @@ -686,7 +684,7 @@ fn test_md5_pass_no_pass() { #[test] fn test_md5_pass_wrong_pass() { - let ret = Connection::connect("postgres://md5_user:asdf@localhost/postgres", &NoSsl); + let ret = Connection::connect("postgres://md5_user:asdf@localhost/postgres", &SslMode::None); match ret { Err(PgConnectDbError(DbError { code: InvalidPassword, .. })) => (), Err(err) => panic!("Unexpected error {}", err), @@ -696,7 +694,7 @@ fn test_md5_pass_wrong_pass() { #[test] fn test_execute_copy_from_err() { - let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &NoSsl)); + let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None)); or_panic!(conn.execute("CREATE TEMPORARY TABLE foo (id INT)", [])); let stmt = or_panic!(conn.prepare("COPY foo (id) FROM STDIN")); match stmt.execute([]) { @@ -713,7 +711,7 @@ fn test_execute_copy_from_err() { #[test] fn test_copy_in() { - let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &NoSsl)); + let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None)); or_panic!(conn.execute("CREATE TEMPORARY TABLE foo (id INT, name VARCHAR)", [])); let stmt = or_panic!(conn.prepare_copy_in("foo", ["id", "name"])); @@ -728,7 +726,7 @@ fn test_copy_in() { #[test] fn test_copy_in_bad_column_count() { - let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &NoSsl)); + let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None)); or_panic!(conn.execute("CREATE TEMPORARY TABLE foo (id INT, name VARCHAR)", [])); let stmt = or_panic!(conn.prepare_copy_in("foo", ["id", "name"])); @@ -755,7 +753,7 @@ fn test_copy_in_bad_column_count() { #[test] fn test_copy_in_bad_type() { - let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &NoSsl)); + let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None)); or_panic!(conn.execute("CREATE TEMPORARY TABLE foo (id INT, name VARCHAR)", [])); let stmt = or_panic!(conn.prepare_copy_in("foo", ["id", "name"])); @@ -773,7 +771,7 @@ fn test_copy_in_bad_type() { #[test] fn test_batch_execute_copy_from_err() { - let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &NoSsl)); + let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None)); or_panic!(conn.execute("CREATE TEMPORARY TABLE foo (id INT)", [])); match conn.batch_execute("COPY foo (id) FROM STDIN") { Err(PgDbError(ref err)) if err.message[].contains("COPY") => {} @@ -789,7 +787,7 @@ fn test_generic_connection() { or_panic!(t.execute("SELECT 1", [])); } - let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &NoSsl)); + let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None)); f(&conn); let trans = or_panic!(conn.transaction()); f(&trans); diff --git a/tests/types/mod.rs b/tests/types/mod.rs index 4461c043..fa49a4d1 100644 --- a/tests/types/mod.rs +++ b/tests/types/mod.rs @@ -6,7 +6,7 @@ use time; use time::Timespec; use std::num::Float; -use postgres::{Connection, NoSsl}; +use postgres::{Connection, SslMode}; use postgres::types::array::ArrayBase; use postgres::types::{ToSql, FromSql}; @@ -31,7 +31,7 @@ mod range; mod uuid; fn test_type(sql_type: &str, checks: &[(T, S)]) { - let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &NoSsl)); + let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None)); for &(ref val, ref repr) in checks.iter() { let stmt = or_panic!(conn.prepare(format!("SELECT {:s}::{}", *repr, sql_type)[])); let result = or_panic!(stmt.query([])).next().unwrap().get(0u); @@ -111,7 +111,7 @@ fn test_text_params() { #[test] fn test_bpchar_params() { - let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &NoSsl)); + let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None)); or_panic!(conn.execute("CREATE TEMPORARY TABLE foo ( id SERIAL PRIMARY KEY, b CHAR(5) @@ -337,7 +337,7 @@ fn test_hstore_params() { } fn test_nan_param(sql_type: &str) { - let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &NoSsl)); + let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None)); let stmt = or_panic!(conn.prepare(format!("SELECT 'NaN'::{}", sql_type)[])); let mut result = or_panic!(stmt.query([])); let val: T = result.next().unwrap().get(0u); @@ -373,7 +373,7 @@ fn test_jsonarray_params() { #[test] fn test_pg_database_datname() { - let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &NoSsl)); + let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None)); let stmt = or_panic!(conn.prepare("SELECT datname FROM pg_database")); let mut result = or_panic!(stmt.query([]));