Skip to content

Commit 8b787cf

Browse files
committed
Convenience update methods
cc rust-postgres#11
1 parent e93df44 commit 8b787cf

2 files changed

Lines changed: 43 additions & 20 deletions

File tree

src/lib.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,20 @@ impl PostgresConnection {
322322
ret
323323
}
324324
325+
pub fn update(&self, query: &str, params: &[&ToSql]) -> uint {
326+
match self.try_update(query, params) {
327+
Ok(res) => res,
328+
Err(err) => fail!("Error running update: %s", err.to_str())
329+
}
330+
}
331+
332+
pub fn try_update(&self, query: &str, params: &[&ToSql])
333+
-> Result<uint, PostgresDbError> {
334+
do self.try_prepare(query).chain |stmt| {
335+
stmt.try_update(params)
336+
}
337+
}
338+
325339
fn quick_query(&self, query: &str) {
326340
self.write_message(&Query { query: query });
327341
@@ -360,6 +374,15 @@ impl<'self> PostgresTransaction<'self> {
360374
self.conn.try_prepare(query)
361375
}
362376
377+
pub fn update(&self, query: &str, params: &[&ToSql]) -> uint {
378+
self.conn.update(query, params)
379+
}
380+
381+
pub fn try_update(&self, query: &str, params: &[&ToSql])
382+
-> Result<uint, PostgresDbError> {
383+
self.conn.try_update(query, params)
384+
}
385+
363386
pub fn will_commit(&self) -> bool {
364387
let commit = self.commit.take();
365388
self.commit.put_back(commit);
@@ -529,6 +552,8 @@ impl<'self> PostgresStatement<'self> {
529552
more_rows: true,
530553
max_rows: row_limit
531554
};
555+
// We have to make sure to execute the result at least once since it
556+
// may have side effects (e.g. INSERT ... RETURNING ...)
532557
result.execute();
533558
534559
Ok(result)

src/test.rs

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ fn test_in_transaction(blk: &fn(&PostgresTransaction)) {
4242
#[test]
4343
fn test_query() {
4444
do test_in_transaction |trans| {
45-
trans.prepare("CREATE TABLE foo (id BIGINT PRIMARY KEY)").update([]);
46-
trans.prepare("INSERT INTO foo (id) VALUES ($1), ($2)")
47-
.update([&1 as &ToSql, &2 as &ToSql]);
45+
trans.update("CREATE TABLE foo (id BIGINT PRIMARY KEY)", []);
46+
trans.update("INSERT INTO foo (id) VALUES ($1), ($2)",
47+
[&1 as &ToSql, &2 as &ToSql]);
4848
let stmt = trans.prepare("SELECT * from foo ORDER BY id");
4949
let result = stmt.query([]);
5050

@@ -55,13 +55,12 @@ fn test_query() {
5555
#[test]
5656
fn test_nulls() {
5757
do test_in_transaction |trans| {
58-
trans.prepare("CREATE TABLE foo (
58+
trans.update("CREATE TABLE foo (
5959
id SERIAL PRIMARY KEY,
6060
val VARCHAR
61-
)").update([]);
62-
trans.prepare("INSERT INTO foo (val) VALUES ($1), ($2)")
63-
.update([& &"foobar" as &ToSql,
64-
&None::<~str> as &ToSql]);
61+
)", []);
62+
trans.update("INSERT INTO foo (val) VALUES ($1), ($2)",
63+
[& &"foobar" as &ToSql, &None::<~str> as &ToSql]);
6564
let stmt = trans.prepare("SELECT val FROM foo ORDER BY id");
6665
let result = stmt.query([]);
6766

@@ -75,10 +74,10 @@ fn test_nulls() {
7574
#[test]
7675
fn test_lazy_query() {
7776
do test_in_transaction |trans| {
78-
trans.prepare("CREATE TABLE foo (
77+
trans.update("CREATE TABLE foo (
7978
id SERIAL PRIMARY KEY,
8079
val BIGINT
81-
)").update([]);
80+
)", []);
8281
let stmt = trans.prepare("INSERT INTO foo (val) VALUES ($1)");
8382
let data = ~[1i64, 2, 3, 4, 5, 6];
8483
for datum in data.iter() {
@@ -95,10 +94,10 @@ fn test_lazy_query() {
9594
9695
fn test_param_type<T: Eq+ToSql+FromSql>(sql_type: &str, values: &[T]) {
9796
do test_in_transaction |trans| {
98-
trans.prepare("CREATE TABLE foo (
97+
trans.update("CREATE TABLE foo (
9998
id SERIAL PRIMARY KEY,
10099
b " + sql_type +
101-
")").update([]);
100+
")", []);
102101
let stmt = trans.prepare("INSERT INTO foo (b) VALUES ($1)");
103102
for value in values.iter() {
104103
stmt.update([value as &ToSql]);
@@ -148,13 +147,12 @@ fn test_binary_f64_params() {
148147
149148
fn test_nan_param<T: Float+ToSql+FromSql>(sql_type: &str) {
150149
do test_in_transaction |trans| {
151-
trans.prepare("CREATE TABLE foo (
150+
trans.update("CREATE TABLE foo (
152151
id SERIAL PRIMARY KEY,
153152
b " + sql_type +
154-
")").update([]);
153+
")", []);
155154
let nan: T = Float::NaN();
156-
trans.prepare("INSERT INTO foo (b) VALUES ($1)")
157-
.update([&nan as &ToSql]);
155+
trans.update("INSERT INTO foo (b) VALUES ($1)", [&nan as &ToSql]);
158156
159157
let stmt = trans.prepare("SELECT b FROM foo");
160158
let mut result = stmt.query([]);
@@ -176,12 +174,12 @@ fn test_f64_nan_param() {
176174
#[test]
177175
fn test_wrong_num_params() {
178176
do test_in_transaction |trans| {
179-
trans.prepare("CREATE TABLE foo (
177+
trans.update("CREATE TABLE foo (
180178
id SERIAL PRIMARY KEY,
181179
val VARCHAR
182-
)").update([]);
183-
let res = trans.prepare("INSERT INTO foo (val) VALUES ($1), ($2)")
184-
.try_update([& &"foobar" as &ToSql]);
180+
)", []);
181+
let res = trans.try_update("INSERT INTO foo (val) VALUES ($1), ($2)",
182+
[& &"foobar" as &ToSql]);
185183
match res {
186184
Err(PostgresDbError { code: ~"08P01", _ }) => (),
187185
resp => fail!("Unexpected response: %?", resp)

0 commit comments

Comments
 (0)