From 75de360e9fa9cf74b2b78b8da8782ed3e9d1d765 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Fri, 21 Feb 2014 23:18:39 -0800 Subject: [PATCH] Update for API changes --- src/lib.rs | 25 ++++---- src/message.rs | 140 ++++++++++++++++++++++---------------------- src/stmt.rs | 18 +++--- src/test.rs | 4 +- src/types/mod.rs | 2 +- src/types/range.rs | 2 +- submodules/rust-phf | 2 +- 7 files changed, 97 insertions(+), 96 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 7b82fe12..4923e973 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -69,6 +69,7 @@ extern crate extra; extern crate openssl; extern crate serialize; extern crate sync; +extern crate time; #[phase(syntax)] extern crate phf_mac; extern crate phf; @@ -568,7 +569,7 @@ impl InnerPostgresConnection { match if_ok_pg!(self.read_message()) { ParseComplete => {} ErrorResponse { fields } => { - if_ok!(self.wait_for_ready()); + try!(self.wait_for_ready()); return Err(PgDbError(PostgresDbError::new(fields))); } _ => unreachable!() @@ -589,14 +590,14 @@ impl InnerPostgresConnection { _ => unreachable!() }; - if_ok!(self.wait_for_ready()); + try!(self.wait_for_ready()); // now that the connection is ready again, get unknown type names for param in param_types.mut_iter() { match *param { PgUnknownType { oid, .. } => *param = PgUnknownType { - name: if_ok!(self.get_type_name(oid)), + name: try!(self.get_type_name(oid)), oid: oid }, _ => {} @@ -607,7 +608,7 @@ impl InnerPostgresConnection { match desc.ty { PgUnknownType { oid, .. } => desc.ty = PgUnknownType { - name: if_ok!(self.get_type_name(oid)), + name: try!(self.get_type_name(oid)), oid: oid }, _ => {} @@ -629,7 +630,7 @@ impl InnerPostgresConnection { Some(name) => return Ok(name.clone()), None => {} } - let name = if_ok!(self.quick_query( + let name = try!(self.quick_query( format!("SELECT typname FROM pg_type WHERE oid={}", oid)))[0][0] .unwrap(); self.unknown_types.insert(oid, name.clone()); @@ -657,7 +658,7 @@ impl InnerPostgresConnection { opt.map(|b| str::from_utf8_owned(b).unwrap())) .collect()), ErrorResponse { fields } => { - if_ok!(self.wait_for_ready()); + try!(self.wait_for_ready()); return Err(PgDbError(PostgresDbError::new(fields))); } _ => {} @@ -762,7 +763,7 @@ impl PostgresConnection { pub fn try_transaction<'a>(&'a self) -> Result, PostgresError> { check_desync!(self); - if_ok!(self.quick_query("BEGIN")); + try!(self.quick_query("BEGIN")); Ok(PostgresTransaction { conn: self, commit: Cell::new(true), @@ -889,15 +890,15 @@ impl<'conn> PostgresTransaction<'conn> { fn finish_inner(&mut self) -> Result<(), PostgresError> { if task::failing() || !self.commit.get() { if self.nested { - if_ok!(self.conn.quick_query("ROLLBACK TO sp")); + try!(self.conn.quick_query("ROLLBACK TO sp")); } else { - if_ok!(self.conn.quick_query("ROLLBACK")); + try!(self.conn.quick_query("ROLLBACK")); } } else { if self.nested { - if_ok!(self.conn.quick_query("RELEASE sp")); + try!(self.conn.quick_query("RELEASE sp")); } else { - if_ok!(self.conn.quick_query("COMMIT")); + try!(self.conn.quick_query("COMMIT")); } } Ok(()) @@ -934,7 +935,7 @@ impl<'conn> PostgresTransaction<'conn> { pub fn try_transaction<'a>(&'a self) -> Result, PostgresError> { check_desync!(self.conn); - if_ok!(self.conn.quick_query("SAVEPOINT sp")); + try!(self.conn.quick_query("SAVEPOINT sp")); Ok(PostgresTransaction { conn: self.conn, commit: Cell::new(true), diff --git a/src/message.rs b/src/message.rs index f60ddeda..082b415e 100644 --- a/src/message.rs +++ b/src/message.rs @@ -125,7 +125,7 @@ trait WriteCStr { impl WriteCStr for W { fn write_cstr(&mut self, s: &str) -> IoResult<()> { - if_ok!(self.write(s.as_bytes())); + try!(self.write(s.as_bytes())); self.write_u8(0) } } @@ -144,78 +144,78 @@ impl WriteMessage for W { match *message { Bind { portal, statement, formats, values, result_formats } => { ident = Some('B'); - if_ok!(buf.write_cstr(portal)); - if_ok!(buf.write_cstr(statement)); + try!(buf.write_cstr(portal)); + try!(buf.write_cstr(statement)); - if_ok!(buf.write_be_i16(formats.len() as i16)); + try!(buf.write_be_i16(formats.len() as i16)); for format in formats.iter() { - if_ok!(buf.write_be_i16(*format)); + try!(buf.write_be_i16(*format)); } - if_ok!(buf.write_be_i16(values.len() as i16)); + try!(buf.write_be_i16(values.len() as i16)); for value in values.iter() { match *value { None => { - if_ok!(buf.write_be_i32(-1)); + try!(buf.write_be_i32(-1)); } Some(ref value) => { - if_ok!(buf.write_be_i32(value.len() as i32)); - if_ok!(buf.write(*value)); + try!(buf.write_be_i32(value.len() as i32)); + try!(buf.write(*value)); } } } - if_ok!(buf.write_be_i16(result_formats.len() as i16)); + try!(buf.write_be_i16(result_formats.len() as i16)); for format in result_formats.iter() { - if_ok!(buf.write_be_i16(*format)); + try!(buf.write_be_i16(*format)); } } CancelRequest { code, process_id, secret_key } => { - if_ok!(buf.write_be_i32(code)); - if_ok!(buf.write_be_i32(process_id)); - if_ok!(buf.write_be_i32(secret_key)); + try!(buf.write_be_i32(code)); + try!(buf.write_be_i32(process_id)); + try!(buf.write_be_i32(secret_key)); } Close { variant, name } => { ident = Some('C'); - if_ok!(buf.write_u8(variant)); - if_ok!(buf.write_cstr(name)); + try!(buf.write_u8(variant)); + try!(buf.write_cstr(name)); } Describe { variant, name } => { ident = Some('D'); - if_ok!(buf.write_u8(variant)); - if_ok!(buf.write_cstr(name)); + try!(buf.write_u8(variant)); + try!(buf.write_cstr(name)); } Execute { portal, max_rows } => { ident = Some('E'); - if_ok!(buf.write_cstr(portal)); - if_ok!(buf.write_be_i32(max_rows)); + try!(buf.write_cstr(portal)); + try!(buf.write_be_i32(max_rows)); } Parse { name, query, param_types } => { ident = Some('P'); - if_ok!(buf.write_cstr(name)); - if_ok!(buf.write_cstr(query)); - if_ok!(buf.write_be_i16(param_types.len() as i16)); + try!(buf.write_cstr(name)); + try!(buf.write_cstr(query)); + try!(buf.write_be_i16(param_types.len() as i16)); for ty in param_types.iter() { - if_ok!(buf.write_be_i32(*ty)); + try!(buf.write_be_i32(*ty)); } } PasswordMessage { password } => { ident = Some('p'); - if_ok!(buf.write_cstr(password)); + try!(buf.write_cstr(password)); } Query { query } => { ident = Some('Q'); - if_ok!(buf.write_cstr(query)); + try!(buf.write_cstr(query)); } StartupMessage { version, parameters } => { - if_ok!(buf.write_be_i32(version)); + try!(buf.write_be_i32(version)); for &(ref k, ref v) in parameters.iter() { - if_ok!(buf.write_cstr(k.as_slice())); - if_ok!(buf.write_cstr(v.as_slice())); + try!(buf.write_cstr(k.as_slice())); + try!(buf.write_cstr(v.as_slice())); } - if_ok!(buf.write_u8(0)); + try!(buf.write_u8(0)); } - SslRequest { code } => if_ok!(buf.write_be_i32(code)), + SslRequest { code } => try!(buf.write_be_i32(code)), Sync => { ident = Some('S'); } @@ -225,15 +225,15 @@ impl WriteMessage for W { } match ident { - Some(ident) => if_ok!(self.write_u8(ident as u8)), + Some(ident) => try!(self.write_u8(ident as u8)), None => () } let buf = buf.unwrap(); // add size of length value - if_ok!(self.write_be_i32((buf.len() + mem::size_of::()) as i32)); - if_ok!(self.write(buf)); + try!(self.write_be_i32((buf.len() + mem::size_of::()) as i32)); + try!(self.write(buf)); Ok(()) } @@ -246,7 +246,7 @@ trait ReadCStr { impl ReadCStr for R { fn read_cstr(&mut self) -> IoResult<~str> { - let mut buf = if_ok!(self.read_until(0)); + let mut buf = try!(self.read_until(0)); buf.pop(); Ok(str::from_utf8_owned(buf).unwrap()) } @@ -261,39 +261,39 @@ impl ReadMessage for R { fn read_message(&mut self) -> IoResult { debug!("Reading message"); - let ident = if_ok!(self.read_u8()); + let ident = try!(self.read_u8()); // subtract size of length value - let len = if_ok!(self.read_be_i32()) as uint - mem::size_of::(); - let mut buf = MemReader::new(if_ok!(self.read_bytes(len))); + let len = try!(self.read_be_i32()) as uint - mem::size_of::(); + let mut buf = MemReader::new(try!(self.read_bytes(len))); let ret = match ident as char { '1' => ParseComplete, '2' => BindComplete, '3' => CloseComplete, 'A' => NotificationResponse { - pid: if_ok!(buf.read_be_i32()), - channel: if_ok!(buf.read_cstr()), - payload: if_ok!(buf.read_cstr()) + pid: try!(buf.read_be_i32()), + channel: try!(buf.read_cstr()), + payload: try!(buf.read_cstr()) }, - 'C' => CommandComplete { tag: if_ok!(buf.read_cstr()) }, - 'D' => if_ok!(read_data_row(&mut buf)), - 'E' => ErrorResponse { fields: if_ok!(read_fields(&mut buf)) }, + 'C' => CommandComplete { tag: try!(buf.read_cstr()) }, + 'D' => try!(read_data_row(&mut buf)), + 'E' => ErrorResponse { fields: try!(read_fields(&mut buf)) }, 'I' => EmptyQueryResponse, 'K' => BackendKeyData { - process_id: if_ok!(buf.read_be_i32()), - secret_key: if_ok!(buf.read_be_i32()) + process_id: try!(buf.read_be_i32()), + secret_key: try!(buf.read_be_i32()) }, 'n' => NoData, - 'N' => NoticeResponse { fields: if_ok!(read_fields(&mut buf)) }, - 'R' => if_ok!(read_auth_message(&mut buf)), + 'N' => NoticeResponse { fields: try!(read_fields(&mut buf)) }, + 'R' => try!(read_auth_message(&mut buf)), 's' => PortalSuspended, 'S' => ParameterStatus { - parameter: if_ok!(buf.read_cstr()), - value: if_ok!(buf.read_cstr()) + parameter: try!(buf.read_cstr()), + value: try!(buf.read_cstr()) }, - 't' => if_ok!(read_parameter_description(&mut buf)), - 'T' => if_ok!(read_row_description(&mut buf)), - 'Z' => ReadyForQuery { state: if_ok!(buf.read_u8()) }, + 't' => try!(read_parameter_description(&mut buf)), + 'T' => try!(read_row_description(&mut buf)), + 'Z' => ReadyForQuery { state: try!(buf.read_u8()) }, ident => fail!("Unknown message identifier `{}`", ident) }; debug!("Read message {:?}", ret); @@ -304,25 +304,25 @@ impl ReadMessage for R { fn read_fields(buf: &mut MemReader) -> IoResult<~[(u8, ~str)]> { let mut fields = ~[]; loop { - let ty = if_ok!(buf.read_u8()); + let ty = try!(buf.read_u8()); if ty == 0 { break; } - fields.push((ty, if_ok!(buf.read_cstr()))); + fields.push((ty, try!(buf.read_cstr()))); } Ok(fields) } fn read_data_row(buf: &mut MemReader) -> IoResult { - let len = if_ok!(buf.read_be_i16()) as uint; + let len = try!(buf.read_be_i16()) as uint; let mut values = vec::with_capacity(len); for _ in range(0, len) { - let val = match if_ok!(buf.read_be_i32()) { + let val = match try!(buf.read_be_i32()) { -1 => None, - len => Some(if_ok!(buf.read_bytes(len as uint))) + len => Some(try!(buf.read_bytes(len as uint))) }; values.push(val); } @@ -331,11 +331,11 @@ fn read_data_row(buf: &mut MemReader) -> IoResult { } fn read_auth_message(buf: &mut MemReader) -> IoResult { - Ok(match if_ok!(buf.read_be_i32()) { + Ok(match try!(buf.read_be_i32()) { 0 => AuthenticationOk, 2 => AuthenticationKerberosV5, 3 => AuthenticationCleartextPassword, - 5 => AuthenticationMD5Password { salt: if_ok!(buf.read_bytes(4)) }, + 5 => AuthenticationMD5Password { salt: try!(buf.read_bytes(4)) }, 6 => AuthenticationSCMCredential, 7 => AuthenticationGSS, 9 => AuthenticationSSPI, @@ -344,29 +344,29 @@ fn read_auth_message(buf: &mut MemReader) -> IoResult { } fn read_parameter_description(buf: &mut MemReader) -> IoResult { - let len = if_ok!(buf.read_be_i16()) as uint; + let len = try!(buf.read_be_i16()) as uint; let mut types = vec::with_capacity(len); for _ in range(0, len) { - types.push(if_ok!(buf.read_be_u32())); + types.push(try!(buf.read_be_u32())); } Ok(ParameterDescription { types: types }) } fn read_row_description(buf: &mut MemReader) -> IoResult { - let len = if_ok!(buf.read_be_i16()) as uint; + let len = try!(buf.read_be_i16()) as uint; let mut types = vec::with_capacity(len); for _ in range(0, len) { types.push(RowDescriptionEntry { - name: if_ok!(buf.read_cstr()), - table_oid: if_ok!(buf.read_be_u32()), - column_id: if_ok!(buf.read_be_i16()), - type_oid: if_ok!(buf.read_be_u32()), - type_size: if_ok!(buf.read_be_i16()), - type_modifier: if_ok!(buf.read_be_i32()), - format: if_ok!(buf.read_be_i16()) + name: try!(buf.read_cstr()), + table_oid: try!(buf.read_be_u32()), + column_id: try!(buf.read_be_i16()), + type_oid: try!(buf.read_be_u32()), + type_size: try!(buf.read_be_i16()), + type_modifier: try!(buf.read_be_i32()), + format: try!(buf.read_be_i16()) }); } diff --git a/src/stmt.rs b/src/stmt.rs index 09d858e8..23540f90 100644 --- a/src/stmt.rs +++ b/src/stmt.rs @@ -130,7 +130,7 @@ impl<'conn> NormalPostgresStatement<'conn> { match if_ok_pg!(self.conn.read_message()) { ReadyForQuery { .. } => break, ErrorResponse { fields } => { - if_ok!(self.conn.wait_for_ready()); + try!(self.conn.wait_for_ready()); return Err(PgDbError(PostgresDbError::new(fields))); } _ => {} @@ -173,7 +173,7 @@ impl<'conn> NormalPostgresStatement<'conn> { match if_ok_pg!(self.conn.read_message()) { BindComplete => Ok(()), ErrorResponse { fields } => { - if_ok!(self.conn.wait_for_ready()); + try!(self.conn.wait_for_ready()); Err(PgDbError(PostgresDbError::new(fields))) } _ => unreachable!() @@ -186,7 +186,7 @@ impl<'conn> NormalPostgresStatement<'conn> { self.next_portal_id.set(id + 1); let portal_name = format!("{}_portal_{}", self.name, id); - if_ok!(self.execute(portal_name, row_limit, params)); + try!(self.execute(portal_name, row_limit, params)); let mut result = PostgresResult { stmt: self, @@ -196,7 +196,7 @@ impl<'conn> NormalPostgresStatement<'conn> { more_rows: true, finished: false, }; - if_ok!(result.read_rows()) + try!(result.read_rows()) Ok(result) } @@ -214,14 +214,14 @@ impl<'conn> PostgresStatement for NormalPostgresStatement<'conn> { fn try_execute(&self, params: &[&ToSql]) -> Result { check_desync!(self.conn); - if_ok!(self.execute("", 0, params)); + try!(self.execute("", 0, params)); let num; loop { match if_ok_pg!(self.conn.read_message()) { DataRow { .. } => {} ErrorResponse { fields } => { - if_ok!(self.conn.wait_for_ready()); + try!(self.conn.wait_for_ready()); return Err(PgDbError(PostgresDbError::new(fields))); } CommandComplete { tag } => { @@ -239,7 +239,7 @@ impl<'conn> PostgresStatement for NormalPostgresStatement<'conn> { _ => unreachable!() } } - if_ok!(self.conn.wait_for_ready()); + try!(self.conn.wait_for_ready()); Ok(num) } @@ -379,7 +379,7 @@ impl<'stmt> PostgresResult<'stmt> { match if_ok_pg!(self.stmt.conn.read_message()) { ReadyForQuery { .. } => break, ErrorResponse { fields } => { - if_ok!(self.stmt.conn.wait_for_ready()); + try!(self.stmt.conn.wait_for_ready()); return Err(PgDbError(PostgresDbError::new(fields))); } _ => {} @@ -433,7 +433,7 @@ impl<'stmt> PostgresResult<'stmt> { pub fn try_next(&mut self) -> Result>, PostgresError> { if self.data.is_empty() && self.more_rows { - if_ok!(self.execute()); + try!(self.execute()); } let row = self.data.pop_front().map(|row| { diff --git a/src/test.rs b/src/test.rs index 37fbc6e3..19907746 100644 --- a/src/test.rs +++ b/src/test.rs @@ -1,7 +1,7 @@ use sync::DuplexStream; use sync::Future; -use extra::time; -use extra::time::Timespec; +use time; +use time::Timespec; use extra::json; use uuid::Uuid; use openssl::ssl::{SslContext, Sslv3}; diff --git a/src/types/mod.rs b/src/types/mod.rs index 263fbde2..25a02719 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -3,7 +3,7 @@ extern crate extra; -use extra::time::Timespec; +use time::Timespec; use extra::json; use extra::json::Json; use uuid::Uuid; diff --git a/src/types/range.rs b/src/types/range.rs index 07d97105..b7c86327 100644 --- a/src/types/range.rs +++ b/src/types/range.rs @@ -6,7 +6,7 @@ extern crate extra; use std::cmp; use std::i32; use std::i64; -use extra::time::Timespec; +use time::Timespec; /// The `quote!` macro can make it easier to create ranges. It roughly mirrors /// traditional mathematic range syntax. diff --git a/submodules/rust-phf b/submodules/rust-phf index 46e784f8..dba9fa31 160000 --- a/submodules/rust-phf +++ b/submodules/rust-phf @@ -1 +1 @@ -Subproject commit 46e784f82b9a40aa11f0f7e9db5439a481a08e46 +Subproject commit dba9fa31e751545706ffe0eecb76d85e41c93a61