Skip to content

Commit 5157a00

Browse files
committed
Miscellaneous cleanup
1 parent 99e759b commit 5157a00

File tree

2 files changed

+27
-33
lines changed

2 files changed

+27
-33
lines changed

src/lib.rs

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -562,12 +562,11 @@ impl InnerPostgresConnection {
562562
let stmt_name = format!("statement_{}", self.next_stmt_id);
563563
self.next_stmt_id += 1;
564564

565-
let types = [];
566565
try_pg!(self.write_messages([
567566
Parse {
568567
name: stmt_name,
569568
query: query,
570-
param_types: types
569+
param_types: []
571570
},
572571
Describe {
573572
variant: 'S' as u8,
@@ -606,27 +605,8 @@ impl InnerPostgresConnection {
606605
try!(self.wait_for_ready());
607606

608607
// now that the connection is ready again, get unknown type names
609-
for param in param_types.mut_iter() {
610-
match *param {
611-
PgUnknownType { oid, .. } =>
612-
*param = PgUnknownType {
613-
name: try!(self.get_type_name(oid)),
614-
oid: oid
615-
},
616-
_ => {}
617-
}
618-
}
619-
620-
for desc in result_desc.mut_iter() {
621-
match desc.ty {
622-
PgUnknownType { oid, .. } =>
623-
desc.ty = PgUnknownType {
624-
name: try!(self.get_type_name(oid)),
625-
oid: oid
626-
},
627-
_ => {}
628-
}
629-
}
608+
try!(self.set_type_names(param_types.mut_iter()));
609+
try!(self.set_type_names(result_desc.mut_iter().map(|d| &mut d.ty)));
630610

631611
Ok(PostgresStatement {
632612
conn: conn,
@@ -638,8 +618,16 @@ impl InnerPostgresConnection {
638618
})
639619
}
640620

641-
fn is_desynchronized(&self) -> bool {
642-
self.desynchronized
621+
fn set_type_names<'a, I: Iterator<&'a mut PostgresType>>(&mut self, mut it: I)
622+
-> Result<(), PostgresError> {
623+
for ty in it {
624+
match *ty {
625+
PgUnknownType { oid, ref mut name } =>
626+
*name = try!(self.get_type_name(oid)),
627+
_ => {}
628+
}
629+
}
630+
Ok(())
643631
}
644632

645633
fn get_type_name(&mut self, oid: Oid) -> Result<~str, PostgresError> {
@@ -654,6 +642,10 @@ impl InnerPostgresConnection {
654642
Ok(name)
655643
}
656644

645+
fn is_desynchronized(&self) -> bool {
646+
self.desynchronized
647+
}
648+
657649
fn wait_for_ready(&mut self) -> Result<(), PostgresError> {
658650
match try_pg!(self.read_message()) {
659651
ReadyForQuery { .. } => Ok(()),
@@ -1045,14 +1037,14 @@ impl<'conn> PostgresStatement<'conn> {
10451037

10461038
fn inner_execute(&self, portal_name: &str, row_limit: uint, params: &[&ToSql])
10471039
-> Result<(), PostgresError> {
1048-
let mut formats = Vec::new();
1049-
let mut values = Vec::new();
10501040
if self.param_types.len() != params.len() {
10511041
return Err(PgWrongParamCount {
10521042
expected: self.param_types.len(),
10531043
actual: params.len(),
10541044
});
10551045
}
1046+
let mut formats = Vec::new();
1047+
let mut values = Vec::new();
10561048
for (&param, ty) in params.iter().zip(self.param_types.iter()) {
10571049
let (format, value) = try!(param.to_sql(ty));
10581050
formats.push(format as i16);
@@ -1404,11 +1396,10 @@ pub trait RowIndex: Clone {
14041396
impl RowIndex for uint {
14051397
#[inline]
14061398
fn idx(&self, stmt: &PostgresStatement) -> Option<uint> {
1407-
let idx = *self - 1;
1408-
if idx >= stmt.result_desc.len() {
1399+
if *self == 0 || *self > stmt.result_desc.len() {
14091400
None
14101401
} else {
1411-
Some(idx)
1402+
Some(*self - 1)
14121403
}
14131404
}
14141405
}

src/message.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub enum BackendMessage {
1313
AuthenticationKerberosV5,
1414
AuthenticationCleartextPassword,
1515
AuthenticationMD5Password {
16-
salt: ~[u8]
16+
salt: [u8, ..4]
1717
},
1818
AuthenticationSCMCredential,
1919
AuthenticationGSS,
@@ -229,7 +229,6 @@ impl<W: Writer> WriteMessage for W {
229229
}
230230

231231
let buf = buf.unwrap();
232-
233232
// add size of length value
234233
try!(self.write_be_i32((buf.len() + mem::size_of::<i32>()) as i32));
235234
try!(self.write(buf));
@@ -334,7 +333,11 @@ fn read_auth_message(buf: &mut MemReader) -> IoResult<BackendMessage> {
334333
0 => AuthenticationOk,
335334
2 => AuthenticationKerberosV5,
336335
3 => AuthenticationCleartextPassword,
337-
5 => AuthenticationMD5Password { salt: try!(buf.read_exact(4)) },
336+
5 => {
337+
let mut salt = [0, 0, 0, 0];
338+
try!(buf.fill(salt));
339+
AuthenticationMD5Password { salt: salt }
340+
},
338341
6 => AuthenticationSCMCredential,
339342
7 => AuthenticationGSS,
340343
9 => AuthenticationSSPI,

0 commit comments

Comments
 (0)