Skip to content

Commit 7c6d388

Browse files
committed
Replace Connection#execute_returning_count with command.execute
Using `update` and `delete` was previously quite annoying to write. This makes the API feel much more fluid, and reads better if you're not actually using the count. Since `insert` is coming in this release as well, this will make things more useable.
1 parent 4d871f4 commit 7c6d388

8 files changed

Lines changed: 39 additions & 28 deletions

File tree

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ for Rust libraries in [RFC #1105](https://github.com/rust-lang/rfcs/blob/master/
66

77
## Unreleased
88

9+
### Added
10+
11+
* Added an `execute` method to `QueryFragment`, which is intended to replace
12+
`Connection#execute_returning_count`. The old method still exists for use
13+
under the hood, but has been hidden from docs and is not considered public
14+
API.
15+
916
### Changed
1017

1118
* Added a hidden `__Nonexhaustive` variant to `result::Error`. This is not

README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,7 @@ fn change_users_name(connection: &Connection, target: i32, new_name: &str) -> Qu
164164

165165
As with [`insert`][insert], we can return any type which implements
166166
[`Queriable`][queriable] for the right types. If you do not want to use the
167-
returned record(s), you should call
168-
[`execute_returning_count`][execute_returning_count] instead of
167+
returned record(s), you should call [`execute`][execute] instead of
169168
[`query_one`][query_one] or [`query_all`][query_all].
170169

171170
You can also use a struct to represent the changes, if it implements
@@ -217,8 +216,7 @@ fn delete_user(connection: &Connection, user: User) -> QueryResult<()> {
217216
use diesel::query_builder::delete;
218217
use users::dsl::*;
219218

220-
let command = delete(users.filter(id.eq(user.id)));
221-
let deleted_rows = try!(connection.execute_returning_count(&command));
219+
delete(users.filter(id.eq(user.id))).execute(&connection).unwrap();
222220
debug_assert!(deleted_rows == 1);
223221
Ok(())
224222
}
@@ -237,7 +235,7 @@ you can go about getting the data structures set up.
237235
[insertable]: http://sgrif.github.io/diesel/diesel/trait.Insertable.html
238236
[insert]: http://sgrif.github.io/diesel/diesel/struct.Connection.html#method.insert
239237
[insert_returning_count]: http://sgrif.github.io/diesel/diesel/struct.Connection.html#method.insert_returning_count
240-
[execute_returning_count]: http://sgrif.github.io/diesel/diesel/struct.Connection.html#method.execute_returning_count
238+
[execute]: http://sgrif.github.io/diesel/diesel/trait.ExecuteDsl.html#method.execute
241239
[query_one]: http://sgrif.github.io/diesel/diesel/struct.Connection.html#method.query_one
242240
[query_all]: http://sgrif.github.io/diesel/diesel/struct.Connection.html#method.query_all
243241
[update]: http://sgrif.github.io/diesel/diesel/query_builder/fn.update.html

diesel/src/connection/mod.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,10 +255,7 @@ impl Connection {
255255
self.exec_sql_params(&sql, &params, &Some(param_types)).map(|r| r.rows_affected())
256256
}
257257

258-
/// Executes the given command, returning the number of rows affected. Used
259-
/// in conjunction with
260-
/// [`update`](../query_builder/fn.update.html) and
261-
/// [`delete`](../query_builder/fn.delete.html)
258+
#[doc(hidden)]
262259
pub fn execute_returning_count<T>(&self, source: &T) -> QueryResult<usize> where
263260
T: QueryFragment,
264261
{

diesel/src/query_builder/functions.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ pub fn update<T: UpdateTarget>(source: T) -> IncompleteUpdateStatement<T> {
6262
/// # let connection = establish_connection();
6363
/// # let get_count = || users.count().first::<i64>(&connection).unwrap();
6464
/// let old_count = get_count();
65-
/// let command = delete(users.filter(id.eq(1)));
66-
/// connection.execute_returning_count(&command).unwrap();
65+
/// delete(users.filter(id.eq(1))).execute(&connection).unwrap();
6766
/// assert_eq!(old_count - 1, get_count());
6867
/// # }
6968
/// ```
@@ -86,7 +85,7 @@ pub fn update<T: UpdateTarget>(source: T) -> IncompleteUpdateStatement<T> {
8685
/// # use diesel::query_builder::delete;
8786
/// # let connection = establish_connection();
8887
/// # let get_count = || users.count().first::<i64>(&connection).unwrap();
89-
/// connection.execute_returning_count(&delete(users)).unwrap();
88+
/// delete(users).execute(&connection).unwrap();
9089
/// assert_eq!(0, get_count());
9190
/// # }
9291
/// ```

diesel/src/query_dsl/load_dsl.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use connection::{Connection, Cursor};
2-
use query_builder::{Query, AsQuery};
2+
use query_builder::{Query, QueryFragment, AsQuery};
33
use query_source::Queriable;
44
use result::QueryResult;
55
use super::LimitDsl;
@@ -26,3 +26,16 @@ pub trait LoadDsl: AsQuery + LimitDsl + Sized {
2626

2727
impl<T: AsQuery + LimitDsl> LoadDsl for T {
2828
}
29+
30+
pub trait ExecuteDsl: QueryFragment + Sized {
31+
/// Executes the given command, returning the number of rows affected. Used
32+
/// in conjunction with
33+
/// [`update`](../query_builder/fn.update.html) and
34+
/// [`delete`](../query_builder/fn.delete.html)
35+
fn execute(&self, conn: &Connection) -> QueryResult<usize> {
36+
conn.execute_returning_count(self)
37+
}
38+
}
39+
40+
impl<T: QueryFragment> ExecuteDsl for T {
41+
}

diesel/src/query_dsl/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub use self::belonging_to_dsl::BelongingToDsl;
1414
pub use self::count_dsl::CountDsl;
1515
pub use self::filter_dsl::FilterDsl;
1616
pub use self::limit_dsl::LimitDsl;
17-
pub use self::load_dsl::LoadDsl;
17+
pub use self::load_dsl::{LoadDsl, ExecuteDsl};
1818
pub use self::offset_dsl::OffsetDsl;
1919
pub use self::order_dsl::OrderDsl;
2020
pub use self::select_dsl::{SelectDsl, SelectSqlDsl};

diesel_tests/tests/insert.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,9 @@ fn delete() {
130130
use diesel::query_builder::delete;
131131
let connection = connection_with_sean_and_tess_in_users_table();
132132

133-
let command = delete(users.filter(name.eq("Sean")));
134-
let deleted_rows = connection.execute_returning_count(&command).unwrap();
133+
let deleted_rows = delete(users.filter(name.eq("Sean"))).execute(&connection);
135134

136-
assert_eq!(1, deleted_rows);
135+
assert_eq!(Ok(1), deleted_rows);
137136

138137
let num_users = users.count().first(&connection);
139138

diesel_tests/tests/update.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ fn test_updating_single_column() {
1212
let data: Vec<String> = users.select(name).load(&connection).unwrap().collect();
1313
assert_eq!(expected_data, data);
1414

15-
let command = update(users).set(name.eq("Jim"));
16-
connection.execute_returning_count(&command).unwrap();
15+
update(users).set(name.eq("Jim")).execute(&connection).unwrap();
1716

1817
let expected_data = vec!["Jim".to_string(); 2];
1918
let data: Vec<String> = users.select(name).load(&connection).unwrap().collect();
@@ -26,8 +25,8 @@ fn test_updating_single_column_of_single_row() {
2625

2726
let connection = connection_with_sean_and_tess_in_users_table();
2827

29-
let command = update(users.filter(id.eq(1))).set(name.eq("Jim"));
30-
connection.execute_returning_count(&command).unwrap();
28+
update(users.filter(id.eq(1))).set(name.eq("Jim"))
29+
.execute(&connection).unwrap();
3130

3231
let expected_data = vec!["Tess".to_string(), "Jim".to_string()];
3332
let data: Vec<String> = users.select(name).load(&connection).unwrap().collect();
@@ -40,17 +39,17 @@ fn test_updating_nullable_column() {
4039

4140
let connection = connection_with_sean_and_tess_in_users_table();
4241

43-
let command = update(users.filter(id.eq(1))).set(hair_color.eq(Some("black")));
44-
connection.execute_returning_count(&command).unwrap();
42+
update(users.filter(id.eq(1))).set(hair_color.eq(Some("black")))
43+
.execute(&connection).unwrap();
4544

4645
let data: Option<String> = users.select(hair_color)
4746
.filter(id.eq(1))
4847
.first(&connection)
4948
.unwrap();
5049
assert_eq!(Some("black".to_string()), data);
5150

52-
let command = update(users.filter(id.eq(1))).set(hair_color.eq(None::<String>));
53-
connection.execute_returning_count(&command).unwrap();
51+
update(users.filter(id.eq(1))).set(hair_color.eq(None::<String>))
52+
.execute(&connection).unwrap();
5453

5554
let data: QueryResult<Option<String>> = users.select(hair_color)
5655
.filter(id.eq(1))
@@ -64,11 +63,10 @@ fn test_updating_multiple_columns() {
6463

6564
let connection = connection_with_sean_and_tess_in_users_table();
6665

67-
let command = update(users.filter(id.eq(1))).set((
66+
update(users.filter(id.eq(1))).set((
6867
name.eq("Jim"),
6968
hair_color.eq(Some("black")),
70-
));
71-
connection.execute_returning_count(&command).unwrap();
69+
)).execute(&connection).unwrap();
7270

7371
let expected_user = User::with_hair_color(1, "Jim", "black");
7472
let user = connection.find(users, 1);

0 commit comments

Comments
 (0)