-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a Selectable
trait
#2709
Closed
Closed
Add a Selectable
trait
#2709
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e5a1795
Add a `Selectable` trait
weiznich 9d1a8f1
Make `Selectable` generic over the backend
weiznich 411b18d
Allow to embed options in `Selectable` impls
weiznich 452e1b9
Add some compile tests for the new `Selectable` trait
weiznich 1bc262a
Fix test error
weiznich File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -47,6 +47,7 @@ mod single_value_dsl; | |||||||||||||
pub use self::belonging_to_dsl::BelongingToDsl; | ||||||||||||||
pub use self::combine_dsl::CombineDsl; | ||||||||||||||
pub use self::join_dsl::{InternalJoinDsl, JoinOnDsl, JoinWithImplicitOnClause}; | ||||||||||||||
use self::load_dsl::LoadIntoDsl; | ||||||||||||||
#[doc(hidden)] | ||||||||||||||
pub use self::load_dsl::LoadQuery; | ||||||||||||||
pub use self::save_changes_dsl::{SaveChangesDsl, UpdateAndFetchResults}; | ||||||||||||||
|
@@ -1303,8 +1304,63 @@ pub trait RunQueryDsl<Conn>: Sized { | |||||||||||||
self.internal_load(conn) | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
/// Executes the given query, returning a `Vec` with the returned rows. | ||||||||||||||
/// | ||||||||||||||
/// This method is similar to [load](self::RunQueryDsl::load), but sets | ||||||||||||||
/// a corresponding select/returning clause based on the | ||||||||||||||
/// [Selectable](crate::query_builder::Selectable) impl for `U`. | ||||||||||||||
/// | ||||||||||||||
/// | ||||||||||||||
/// # Examples | ||||||||||||||
/// | ||||||||||||||
/// ## Returning a struct | ||||||||||||||
/// | ||||||||||||||
/// ```rust | ||||||||||||||
/// # include!("../doctest_setup.rs"); | ||||||||||||||
/// # use schema::users; | ||||||||||||||
/// # | ||||||||||||||
/// #[derive(Selectable, Queryable, PartialEq, Debug)] | ||||||||||||||
/// #[table_name = "users"] | ||||||||||||||
/// struct User { | ||||||||||||||
/// id: i32, | ||||||||||||||
/// name: String, | ||||||||||||||
/// } | ||||||||||||||
/// | ||||||||||||||
/// # fn main() { | ||||||||||||||
/// # run_test(); | ||||||||||||||
/// # } | ||||||||||||||
/// # | ||||||||||||||
/// # fn run_test() -> QueryResult<()> { | ||||||||||||||
/// # use diesel::insert_into; | ||||||||||||||
/// # use schema::users::dsl::*; | ||||||||||||||
/// # let connection = establish_connection(); | ||||||||||||||
/// let data = users | ||||||||||||||
/// .load_into::<User>(&connection)?; | ||||||||||||||
/// | ||||||||||||||
/// // This query is equivalent to | ||||||||||||||
/// // users.select((users::id, users::name)).load::<User>(&connection)?; | ||||||||||||||
/// | ||||||||||||||
/// let expected_data = vec![ | ||||||||||||||
/// User { id: 1, name: String::from("Sean") }, | ||||||||||||||
/// User { id: 2, name: String::from("Tess") }, | ||||||||||||||
/// ]; | ||||||||||||||
/// assert_eq!(expected_data, data); | ||||||||||||||
/// # Ok(()) | ||||||||||||||
/// # } | ||||||||||||||
/// ``` | ||||||||||||||
fn load_into<U>(self, conn: &Conn) -> QueryResult<Vec<U>> | ||||||||||||||
where | ||||||||||||||
Self: LoadIntoDsl<Conn, U>, | ||||||||||||||
{ | ||||||||||||||
LoadIntoDsl::load_into(self, conn) | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
/// Runs the command, and returns the affected row. | ||||||||||||||
/// | ||||||||||||||
/// This method behaves similar to [get_result](self::RunQueryDsl::get_result) | ||||||||||||||
/// than [load_into](self::RunQueryDsl::load_into) behaves to | ||||||||||||||
/// [load](self::RunQueryDsl::load) | ||||||||||||||
Comment on lines
+1363
to
+1365
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
/// | ||||||||||||||
/// `Err(NotFound)` will be returned if the query affected 0 rows. You can | ||||||||||||||
/// call `.optional()` on the result of this if the command was optional to | ||||||||||||||
/// get back a `Result<Option<U>>` | ||||||||||||||
|
@@ -1355,6 +1411,67 @@ pub trait RunQueryDsl<Conn>: Sized { | |||||||||||||
first_or_not_found(self.load(conn)) | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
/// Runs the command, and returns the affected row. | ||||||||||||||
/// | ||||||||||||||
/// `Err(NotFound)` will be returned if the query affected 0 rows. You can | ||||||||||||||
/// call `.optional()` on the result of this if the command was optional to | ||||||||||||||
/// get back a `Result<Option<U>>` | ||||||||||||||
/// | ||||||||||||||
/// When this method is called on an insert, update, or delete statement, | ||||||||||||||
/// it will implicitly add a `RETURNING *` to the query, | ||||||||||||||
/// unless a returning clause was already specified. | ||||||||||||||
/// | ||||||||||||||
/// This method only returns the first row that was affected, even if more | ||||||||||||||
/// rows are affected. | ||||||||||||||
/// | ||||||||||||||
/// # Example | ||||||||||||||
/// | ||||||||||||||
/// ```rust | ||||||||||||||
/// # include!("../doctest_setup.rs"); | ||||||||||||||
/// # | ||||||||||||||
/// # | ||||||||||||||
/// # fn main() { | ||||||||||||||
/// # run_test(); | ||||||||||||||
/// # } | ||||||||||||||
/// # | ||||||||||||||
/// # use schema::users; | ||||||||||||||
/// # | ||||||||||||||
/// #[derive(Selectable, Queryable, PartialEq, Debug)] | ||||||||||||||
/// #[table_name = "users"] | ||||||||||||||
/// pub struct UserName { | ||||||||||||||
/// name: String | ||||||||||||||
/// } | ||||||||||||||
/// # | ||||||||||||||
/// # #[cfg(feature = "postgres")] | ||||||||||||||
/// # fn run_test() -> QueryResult<()> { | ||||||||||||||
/// # use diesel::{insert_into, update}; | ||||||||||||||
/// # use schema::users::dsl::*; | ||||||||||||||
/// # let connection = establish_connection(); | ||||||||||||||
/// let inserted_row = insert_into(users) | ||||||||||||||
/// .values(name.eq("Ruby")) | ||||||||||||||
/// .load_into::<UserName>(&connection)?; | ||||||||||||||
/// assert_eq!(UserName { name: "Ruby".into_string() }, inserted_row); | ||||||||||||||
/// | ||||||||||||||
/// // This will return `NotFound`, as there is no user with ID 4 | ||||||||||||||
/// let update_result = update(users.find(4)) | ||||||||||||||
/// .set(name.eq("Jim")) | ||||||||||||||
/// .load_into::<UserName>(&connection); | ||||||||||||||
/// assert_eq!(Err(diesel::NotFound), update_result); | ||||||||||||||
/// # Ok(()) | ||||||||||||||
/// # } | ||||||||||||||
/// # | ||||||||||||||
/// # #[cfg(not(feature = "postgres"))] | ||||||||||||||
/// # fn run_test() -> QueryResult<()> { | ||||||||||||||
/// # Ok(()) | ||||||||||||||
/// # } | ||||||||||||||
/// ``` | ||||||||||||||
fn load_into_single<U>(self, conn: &Conn) -> QueryResult<U> | ||||||||||||||
where | ||||||||||||||
Self: LoadIntoDsl<Conn, U>, | ||||||||||||||
{ | ||||||||||||||
first_or_not_found(<_ as RunQueryDsl<_>>::load_into(self, conn)) | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
/// Runs the command, returning an `Vec` with the affected rows. | ||||||||||||||
/// | ||||||||||||||
/// This method is an alias for [`load`], but with a name that makes more | ||||||||||||||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you should also explain what the difference between
Queryable
andSelectable
and when you may want to use each of them.Also you can mention what bind method will be used: by name or by index