Formatting cleanup

This commit is contained in:
Steven Fackler 2014-08-15 19:50:11 -07:00
parent c577e460f6
commit f7c0038fa3
2 changed files with 44 additions and 60 deletions

View File

@ -192,20 +192,17 @@ pub struct PostgresConnectParams {
/// `PostgresConnectParams`. /// `PostgresConnectParams`.
pub trait IntoConnectParams { pub trait IntoConnectParams {
/// Converts the value of `self` into a `PostgresConnectParams`. /// Converts the value of `self` into a `PostgresConnectParams`.
fn into_connect_params(self) -> Result<PostgresConnectParams, fn into_connect_params(self) -> Result<PostgresConnectParams, PostgresConnectError>;
PostgresConnectError>;
} }
impl IntoConnectParams for PostgresConnectParams { impl IntoConnectParams for PostgresConnectParams {
fn into_connect_params(self) -> Result<PostgresConnectParams, fn into_connect_params(self) -> Result<PostgresConnectParams, PostgresConnectError> {
PostgresConnectError> {
Ok(self) Ok(self)
} }
} }
impl<'a> IntoConnectParams for &'a str { impl<'a> IntoConnectParams for &'a str {
fn into_connect_params(self) -> Result<PostgresConnectParams, fn into_connect_params(self) -> Result<PostgresConnectParams, PostgresConnectError> {
PostgresConnectError> {
match Url::parse(self) { match Url::parse(self) {
Ok(url) => url.into_connect_params(), Ok(url) => url.into_connect_params(),
Err(err) => return Err(InvalidUrl(err)), Err(err) => return Err(InvalidUrl(err)),
@ -214,8 +211,7 @@ impl<'a> IntoConnectParams for &'a str {
} }
impl IntoConnectParams for Url { impl IntoConnectParams for Url {
fn into_connect_params(self) -> Result<PostgresConnectParams, fn into_connect_params(self) -> Result<PostgresConnectParams, PostgresConnectError> {
PostgresConnectError> {
let Url { let Url {
host, host,
port, port,
@ -336,8 +332,7 @@ pub struct PostgresCancelData {
/// # let _ = /// # let _ =
/// postgres::cancel_query(url, &NoSsl, cancel_data); /// postgres::cancel_query(url, &NoSsl, cancel_data);
/// ``` /// ```
pub fn cancel_query<T: IntoConnectParams>(params: T, ssl: &SslMode, pub fn cancel_query<T: IntoConnectParams>(params: T, ssl: &SslMode, data: PostgresCancelData)
data: PostgresCancelData)
-> Result<(), PostgresConnectError> { -> Result<(), PostgresConnectError> {
let params = try!(params.into_connect_params()); let params = try!(params.into_connect_params());
@ -379,8 +374,7 @@ impl Drop for InnerPostgresConnection {
impl InnerPostgresConnection { impl InnerPostgresConnection {
fn connect<T: IntoConnectParams>(params: T, ssl: &SslMode) fn connect<T: IntoConnectParams>(params: T, ssl: &SslMode)
-> Result<InnerPostgresConnection, -> Result<InnerPostgresConnection, PostgresConnectError> {
PostgresConnectError> {
let params = try!(params.into_connect_params()); let params = try!(params.into_connect_params());
let stream = try!(io::initialize_stream(&params, ssl)); let stream = try!(io::initialize_stream(&params, ssl));
@ -456,23 +450,25 @@ impl InnerPostgresConnection {
debug_assert!(!self.desynchronized); debug_assert!(!self.desynchronized);
loop { loop {
match try_desync!(self, self.stream.read_message()) { match try_desync!(self, self.stream.read_message()) {
NoticeResponse { fields } => NoticeResponse { fields } => {
self.notice_handler.handle(PostgresDbError::new(fields)), self.notice_handler.handle(PostgresDbError::new(fields))
NotificationResponse { pid, channel, payload } => }
NotificationResponse { pid, channel, payload } => {
self.notifications.push(PostgresNotification { self.notifications.push(PostgresNotification {
pid: pid, pid: pid,
channel: channel, channel: channel,
payload: payload payload: payload
}), })
ParameterStatus { parameter, value } => }
debug!("Parameter {} = {}", parameter, value), ParameterStatus { parameter, value } => {
debug!("Parameter {} = {}", parameter, value)
}
val => return Ok(val) val => return Ok(val)
} }
} }
} }
fn handle_auth(&mut self, user: PostgresUserInfo) fn handle_auth(&mut self, user: PostgresUserInfo) -> Result<(), PostgresConnectError> {
-> Result<(), PostgresConnectError> {
match try_pg_conn!(self.read_message()) { match try_pg_conn!(self.read_message()) {
AuthenticationOk => return Ok(()), AuthenticationOk => return Ok(()),
AuthenticationCleartextPassword => { AuthenticationCleartextPassword => {
@ -506,8 +502,7 @@ impl InnerPostgresConnection {
| AuthenticationSCMCredential | AuthenticationSCMCredential
| AuthenticationGSS | AuthenticationGSS
| AuthenticationSSPI => return Err(UnsupportedAuthentication), | AuthenticationSSPI => return Err(UnsupportedAuthentication),
ErrorResponse { fields } => ErrorResponse { fields } => return Err(PgConnectDbError(PostgresDbError::new(fields))),
return Err(PgConnectDbError(PostgresDbError::new(fields))),
_ => { _ => {
self.desynchronized = true; self.desynchronized = true;
return Err(PgConnectBadResponse); return Err(PgConnectBadResponse);
@ -516,8 +511,7 @@ impl InnerPostgresConnection {
match try_pg_conn!(self.read_message()) { match try_pg_conn!(self.read_message()) {
AuthenticationOk => Ok(()), AuthenticationOk => Ok(()),
ErrorResponse { fields } => ErrorResponse { fields } => Err(PgConnectDbError(PostgresDbError::new(fields))),
Err(PgConnectDbError(PostgresDbError::new(fields))),
_ => { _ => {
self.desynchronized = true; self.desynchronized = true;
return Err(PgConnectBadResponse); return Err(PgConnectBadResponse);
@ -526,12 +520,12 @@ impl InnerPostgresConnection {
} }
fn set_notice_handler(&mut self, handler: Box<PostgresNoticeHandler+Send>) fn set_notice_handler(&mut self, handler: Box<PostgresNoticeHandler+Send>)
-> Box<PostgresNoticeHandler+Send> { -> Box<PostgresNoticeHandler+Send> {
mem::replace(&mut self.notice_handler, handler) mem::replace(&mut self.notice_handler, handler)
} }
fn prepare<'a>(&mut self, query: &str, conn: &'a PostgresConnection) fn prepare<'a>(&mut self, query: &str, conn: &'a PostgresConnection)
-> PostgresResult<PostgresStatement<'a>> { -> PostgresResult<PostgresStatement<'a>> {
let stmt_name = format!("s{}", self.next_stmt_id); let stmt_name = format!("s{}", self.next_stmt_id);
self.next_stmt_id += 1; self.next_stmt_id += 1;
@ -557,20 +551,22 @@ impl InnerPostgresConnection {
} }
let mut param_types: Vec<PostgresType> = match try_pg!(self.read_message()) { let mut param_types: Vec<PostgresType> = match try_pg!(self.read_message()) {
ParameterDescription { types } => ParameterDescription { types } => {
types.iter().map(|ty| PostgresType::from_oid(*ty)).collect(), types.iter().map(|ty| PostgresType::from_oid(*ty)).collect()
}
_ => bad_response!(self), _ => bad_response!(self),
}; };
let mut result_desc: Vec<ResultDescription> = match try_pg!(self.read_message()) { let mut result_desc: Vec<ResultDescription> = match try_pg!(self.read_message()) {
RowDescription { descriptions } => RowDescription { descriptions } => {
descriptions.move_iter().map(|desc| { descriptions.move_iter().map(|desc| {
let RowDescriptionEntry { name, type_oid, .. } = desc; let RowDescriptionEntry { name, type_oid, .. } = desc;
ResultDescription { ResultDescription {
name: name, name: name,
ty: PostgresType::from_oid(type_oid) ty: PostgresType::from_oid(type_oid)
} }
}).collect(), }).collect()
}
NoData => vec![], NoData => vec![],
_ => bad_response!(self) _ => bad_response!(self)
}; };
@ -592,14 +588,14 @@ impl InnerPostgresConnection {
} }
fn set_type_names<'a, I: Iterator<&'a mut PostgresType>>(&mut self, mut it: I) fn set_type_names<'a, I: Iterator<&'a mut PostgresType>>(&mut self, mut it: I)
-> PostgresResult<()> { -> PostgresResult<()> {
for ty in it { for ty in it {
match *ty { match *ty {
PgUnknownType { oid, ref mut name } => PgUnknownType { oid, ref mut name } => *name = try!(self.get_type_name(oid)),
*name = try!(self.get_type_name(oid)),
_ => {} _ => {}
} }
} }
Ok(()) Ok(())
} }
@ -721,8 +717,7 @@ impl PostgresConnection {
/// let maybe_conn = PostgresConnection::connect(params, &NoSsl); /// let maybe_conn = PostgresConnection::connect(params, &NoSsl);
/// ``` /// ```
pub fn connect<T: IntoConnectParams>(params: T, ssl: &SslMode) pub fn connect<T: IntoConnectParams>(params: T, ssl: &SslMode)
-> Result<PostgresConnection, -> Result<PostgresConnection, PostgresConnectError> {
PostgresConnectError> {
InnerPostgresConnection::connect(params, ssl).map(|conn| { InnerPostgresConnection::connect(params, ssl).map(|conn| {
PostgresConnection { conn: RefCell::new(conn) } PostgresConnection { conn: RefCell::new(conn) }
}) })
@ -730,7 +725,7 @@ impl PostgresConnection {
/// Sets the notice handler for the connection, returning the old handler. /// Sets the notice handler for the connection, returning the old handler.
pub fn set_notice_handler(&self, handler: Box<PostgresNoticeHandler+Send>) pub fn set_notice_handler(&self, handler: Box<PostgresNoticeHandler+Send>)
-> Box<PostgresNoticeHandler+Send> { -> Box<PostgresNoticeHandler+Send> {
self.conn.borrow_mut().set_notice_handler(handler) self.conn.borrow_mut().set_notice_handler(handler)
} }
@ -762,8 +757,7 @@ impl PostgresConnection {
/// Ok(stmt) => stmt, /// Ok(stmt) => stmt,
/// Err(err) => fail!("Error preparing statement: {}", err) /// Err(err) => fail!("Error preparing statement: {}", err)
/// }; /// };
pub fn prepare<'a>(&'a self, query: &str) pub fn prepare<'a>(&'a self, query: &str) -> PostgresResult<PostgresStatement<'a>> {
-> PostgresResult<PostgresStatement<'a>> {
let mut conn = self.conn.borrow_mut(); let mut conn = self.conn.borrow_mut();
if conn.trans_depth != 0 { if conn.trans_depth != 0 {
return Err(PgWrongTransaction); return Err(PgWrongTransaction);
@ -796,8 +790,7 @@ impl PostgresConnection {
/// # Ok(()) /// # Ok(())
/// # } /// # }
/// ``` /// ```
pub fn transaction<'a>(&'a self) pub fn transaction<'a>(&'a self) -> PostgresResult<PostgresTransaction<'a>> {
-> PostgresResult<PostgresTransaction<'a>> {
check_desync!(self); check_desync!(self);
if self.conn.borrow().trans_depth != 0 { if self.conn.borrow().trans_depth != 0 {
return Err(PgWrongTransaction); return Err(PgWrongTransaction);
@ -818,8 +811,7 @@ impl PostgresConnection {
/// or execution of the statement. /// or execution of the statement.
/// ///
/// On success, returns the number of rows modified or 0 if not applicable. /// On success, returns the number of rows modified or 0 if not applicable.
pub fn execute(&self, query: &str, params: &[&ToSql]) pub fn execute(&self, query: &str, params: &[&ToSql]) -> PostgresResult<uint> {
-> PostgresResult<uint> {
self.prepare(query).and_then(|stmt| stmt.execute(params)) self.prepare(query).and_then(|stmt| stmt.execute(params))
} }
@ -898,8 +890,7 @@ impl PostgresConnection {
self.conn.borrow().canary() self.conn.borrow().canary()
} }
fn quick_query(&self, query: &str) fn quick_query(&self, query: &str) -> PostgresResult<Vec<Vec<Option<String>>>> {
-> PostgresResult<Vec<Vec<Option<String>>>> {
self.conn.borrow_mut().quick_query(query) self.conn.borrow_mut().quick_query(query)
} }
@ -959,8 +950,7 @@ impl<'conn> PostgresTransaction<'conn> {
} }
/// Like `PostgresConnection::prepare`. /// Like `PostgresConnection::prepare`.
pub fn prepare<'a>(&'a self, query: &str) pub fn prepare<'a>(&'a self, query: &str) -> PostgresResult<PostgresStatement<'a>> {
-> PostgresResult<PostgresStatement<'a>> {
if self.conn.conn.borrow().trans_depth != self.depth { if self.conn.conn.borrow().trans_depth != self.depth {
return Err(PgWrongTransaction); return Err(PgWrongTransaction);
} }
@ -968,8 +958,7 @@ impl<'conn> PostgresTransaction<'conn> {
} }
/// Like `PostgresConnection::execute`. /// Like `PostgresConnection::execute`.
pub fn execute(&self, query: &str, params: &[&ToSql]) pub fn execute(&self, query: &str, params: &[&ToSql]) -> PostgresResult<uint> {
-> PostgresResult<uint> {
self.prepare(query).and_then(|s| s.execute(params)) self.prepare(query).and_then(|s| s.execute(params))
} }
@ -983,8 +972,7 @@ impl<'conn> PostgresTransaction<'conn> {
} }
/// Like `PostgresConnection::transaction`. /// Like `PostgresConnection::transaction`.
pub fn transaction<'a>(&'a self) pub fn transaction<'a>(&'a self) -> PostgresResult<PostgresTransaction<'a>> {
-> PostgresResult<PostgresTransaction<'a>> {
check_desync!(self.conn); check_desync!(self.conn);
if self.conn.conn.borrow().trans_depth != self.depth { if self.conn.conn.borrow().trans_depth != self.depth {
return Err(PgWrongTransaction); return Err(PgWrongTransaction);
@ -1010,8 +998,7 @@ impl<'conn> PostgresTransaction<'conn> {
stmt: &'stmt PostgresStatement, stmt: &'stmt PostgresStatement,
params: &[&ToSql], params: &[&ToSql],
row_limit: i32) row_limit: i32)
-> PostgresResult<PostgresLazyRows -> PostgresResult<PostgresLazyRows <'trans, 'stmt>> {
<'trans, 'stmt>> {
if self.conn as *const _ != stmt.conn as *const _ { if self.conn as *const _ != stmt.conn as *const _ {
return Err(PgWrongConnection); return Err(PgWrongConnection);
} }
@ -1097,7 +1084,7 @@ impl<'conn> PostgresStatement<'conn> {
} }
fn inner_execute(&self, portal_name: &str, row_limit: i32, params: &[&ToSql]) fn inner_execute(&self, portal_name: &str, row_limit: i32, params: &[&ToSql])
-> PostgresResult<()> { -> PostgresResult<()> {
if self.param_types.len() != params.len() { if self.param_types.len() != params.len() {
return Err(PgWrongParamCount { return Err(PgWrongParamCount {
expected: self.param_types.len(), expected: self.param_types.len(),
@ -1142,7 +1129,7 @@ impl<'conn> PostgresStatement<'conn> {
} }
fn lazy_query<'a>(&'a self, row_limit: i32, params: &[&ToSql]) fn lazy_query<'a>(&'a self, row_limit: i32, params: &[&ToSql])
-> PostgresResult<PostgresRows<'a>> { -> PostgresResult<PostgresRows<'a>> {
let id = self.next_portal_id.get(); let id = self.next_portal_id.get();
self.next_portal_id.set(id + 1); self.next_portal_id.set(id + 1);
let portal_name = format!("{}p{}", self.name, id); let portal_name = format!("{}p{}", self.name, id);
@ -1239,8 +1226,7 @@ impl<'conn> PostgresStatement<'conn> {
/// println!("foo: {}", foo); /// println!("foo: {}", foo);
/// } /// }
/// ``` /// ```
pub fn query<'a>(&'a self, params: &[&ToSql]) pub fn query<'a>(&'a self, params: &[&ToSql]) -> PostgresResult<PostgresRows<'a>> {
-> PostgresResult<PostgresRows<'a>> {
check_desync!(self.conn); check_desync!(self.conn);
self.lazy_query(0, params) self.lazy_query(0, params)
} }
@ -1308,8 +1294,7 @@ impl<'stmt> PostgresRows<'stmt> {
fn read_rows(&mut self) -> PostgresResult<()> { fn read_rows(&mut self) -> PostgresResult<()> {
loop { loop {
match try_pg!(self.stmt.conn.read_message()) { match try_pg!(self.stmt.conn.read_message()) {
EmptyQueryResponse | EmptyQueryResponse | CommandComplete { .. } => {
CommandComplete { .. } => {
self.more_rows = false; self.more_rows = false;
break; break;
}, },

View File

@ -95,8 +95,7 @@ fn test_unix_connection() {
fail!("can't test connect_unix; unix_socket_directories is empty"); fail!("can't test connect_unix; unix_socket_directories is empty");
} }
let unix_socket_directory = unix_socket_directories.as_slice() let unix_socket_directory = unix_socket_directories.as_slice() .split(',').next().unwrap();
.split(',').next().unwrap();
let url = format!("postgres://postgres@{}", url::encode_component(unix_socket_directory)); let url = format!("postgres://postgres@{}", url::encode_component(unix_socket_directory));
let conn = or_fail!(PostgresConnection::connect(url.as_slice(), &NoSsl)); let conn = or_fail!(PostgresConnection::connect(url.as_slice(), &NoSsl));