Skip to content

Commit be080e7

Browse files
committed
Stop storing num_params in statement
Postgres will check the count for us.
1 parent 30d1a43 commit be080e7

2 files changed

Lines changed: 32 additions & 25 deletions

File tree

src/lib.rs

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -254,24 +254,22 @@ impl PostgresConnection {
254254
query: query,
255255
param_types: types
256256
});
257+
self.write_message(&Describe { variant: 'S' as u8, name: stmt_name });
257258
self.write_message(&Sync);
258259
259-
match self.read_message() {
260+
match_read_message!(self, {
260261
ParseComplete => (),
261-
ErrorResponse { fields } =>
262-
return Err(PostgresDbError::new(fields)),
262+
ErrorResponse { fields } => {
263+
self.wait_for_ready();
264+
return Err(PostgresDbError::new(fields));
265+
},
263266
resp => fail!("Bad response: %?", resp.to_str())
264-
}
265-
266-
self.wait_for_ready();
267-
268-
self.write_message(&Describe { variant: 'S' as u8, name: stmt_name });
269-
self.write_message(&Sync);
267+
})
270268
271-
let num_params = match self.read_message() {
272-
ParameterDescription { types } => types.len(),
269+
match_read_message!(self, {
270+
ParameterDescription {_} => (),
273271
resp => fail!("Bad response: %?", resp.to_str())
274-
};
272+
})
275273
276274
match_read_message!(self, {
277275
RowDescription {_} | NoData => (),
@@ -283,7 +281,6 @@ impl PostgresConnection {
283281
Ok(PostgresStatement {
284282
conn: self,
285283
name: stmt_name,
286-
num_params: num_params,
287284
next_portal_id: Cell::new(0)
288285
})
289286
}
@@ -366,7 +363,6 @@ impl<'self> PostgresTransaction<'self> {
366363
pub struct PostgresStatement<'self> {
367364
priv conn: &'self PostgresConnection,
368365
priv name: ~str,
369-
priv num_params: uint,
370366
priv next_portal_id: Cell<uint>
371367
}
372368
@@ -390,17 +386,8 @@ impl<'self> Drop for PostgresStatement<'self> {
390386
}
391387
392388
impl<'self> PostgresStatement<'self> {
393-
pub fn num_params(&self) -> uint {
394-
self.num_params
395-
}
396-
397389
fn execute(&self, portal_name: &str, params: &[&ToSql])
398390
-> Option<PostgresDbError> {
399-
if self.num_params != params.len() {
400-
fail!("Expected %u params but got %u", self.num_params,
401-
params.len());
402-
}
403-
404391
let formats = [];
405392
let values: ~[Option<~[u8]>] = params.iter().map(|val| val.to_sql())
406393
.collect();

src/test.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,26 @@ fn test_nulls() {
6868
};
6969
}
7070
71+
#[test]
72+
fn test_wrong_num_params() {
73+
let conn = PostgresConnection::connect("postgres://postgres@127.0.0.1:5432");
74+
75+
do conn.in_transaction |trans| {
76+
trans.prepare("CREATE TABLE foo (
77+
id BIGINT PRIMARY KEY,
78+
val VARCHAR
79+
)").update([]);
80+
let res = trans.prepare("INSERT INTO foo (id, val) VALUES ($1, $2), ($3, $4)")
81+
.try_update([&1 as &ToSql, & &"foobar" as &ToSql]);
82+
match res {
83+
Err(PostgresDbError { code: ~"08P01", _ }) => (),
84+
resp => fail!("Unexpected response: %?", resp)
85+
}
86+
87+
trans.set_rollback();
88+
}
89+
}
90+
7191
#[test]
7292
fn test_plaintext_pass() {
7393
PostgresConnection::connect("postgres://pass_user:password@127.0.0.1:5432");
@@ -86,7 +106,7 @@ fn test_plaintext_pass_no_pass() {
86106
fn test_plaintext_pass_wrong_pass() {
87107
let ret = PostgresConnection::try_connect("postgres://pass_user:asdf@127.0.0.1:5432");
88108
match ret {
89-
Err(DbError(PostgresDbError { code, _ })) => assert_eq!(code, ~"28P01"),
109+
Err(DbError(PostgresDbError { code: ~"28P01", _ })) => (),
90110
ret => fail!("Unexpected result %?", ret)
91111
}
92112
}
@@ -109,7 +129,7 @@ fn test_md5_pass_no_pass() {
109129
fn test_md5_pass_wrong_pass() {
110130
let ret = PostgresConnection::try_connect("postgres://md5_user:asdf@127.0.0.1:5432");
111131
match ret {
112-
Err(DbError(PostgresDbError { code, _ })) => assert_eq!(code, ~"28P01"),
132+
Err(DbError(PostgresDbError { code: ~"28P01", _ })) => (),
113133
ret => fail!("Unexpected result %?", ret)
114134
}
115135
}

0 commit comments

Comments
 (0)