-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify the structure and codegen of
UpdateStatement
(#367)
The main goal of this refactoring was to remove the implementations of `SaveChangesDsl` from codegen and provide them generically instead. Since I want the code outside of codegen to be as approachable as possible, I made a few other changes to reduce the where clause of these implementations. I still would like to reduce the where clause further, but this is at a decent place. The following tree of changes were in support of this goal: - Identifiable needs to have the table. This is so we can generically do `table.find(id)` for `T: Identifiable`. - Structs can now be passed directly to `update` and `delete` - This caused us to change `UpdateTarget` to be a struct, and introduce an `AsUpdateTarget` trait so that we can abstract over whether the fields on that struct are a reference or not All of this will drastically simplify the non-procedural-macro form of `#[derive(AsChangeset)]`, and hopefully lead to more refactoring in the future.
- Loading branch information
Showing
21 changed files
with
241 additions
and
179 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,46 @@ | ||
use backend::Backend; | ||
use query_builder::*; | ||
use query_source::Table; | ||
use result::QueryResult; | ||
|
||
#[derive(Debug, Copy, Clone)] | ||
pub struct DeleteStatement<T>(T); | ||
#[derive(Debug)] | ||
pub struct DeleteStatement<T, U>(UpdateTarget<T, U>); | ||
|
||
impl<T> DeleteStatement<T> { | ||
impl<T, U> DeleteStatement<T, U> { | ||
#[doc(hidden)] | ||
pub fn new(t: T) -> Self { | ||
pub fn new(t: UpdateTarget<T, U>) -> Self { | ||
DeleteStatement(t) | ||
} | ||
} | ||
|
||
impl<T, DB> QueryFragment<DB> for DeleteStatement<T> where | ||
impl<T, U, DB> QueryFragment<DB> for DeleteStatement<T, U> where | ||
DB: Backend, | ||
T: UpdateTarget, | ||
T::WhereClause: QueryFragment<DB>, | ||
T: Table, | ||
T::FromClause: QueryFragment<DB>, | ||
U: QueryFragment<DB>, | ||
{ | ||
fn to_sql(&self, out: &mut DB::QueryBuilder) -> BuildQueryResult { | ||
out.push_sql("DELETE FROM "); | ||
try!(self.0.from_clause().to_sql(out)); | ||
if let Some(clause) = self.0.where_clause() { | ||
try!(self.0.table.from_clause().to_sql(out)); | ||
if let Some(ref clause) = self.0.where_clause { | ||
out.push_sql(" WHERE "); | ||
try!(clause.to_sql(out)); | ||
} | ||
Ok(()) | ||
} | ||
|
||
fn collect_binds(&self, out: &mut DB::BindCollector) -> QueryResult<()> { | ||
try!(self.0.from_clause().collect_binds(out)); | ||
if let Some(clause) = self.0.where_clause() { | ||
try!(self.0.table.from_clause().collect_binds(out)); | ||
if let Some(ref clause) = self.0.where_clause { | ||
try!(clause.collect_binds(out)); | ||
} | ||
Ok(()) | ||
} | ||
|
||
fn is_safe_to_cache_prepared(&self) -> bool { | ||
self.0.from_clause().is_safe_to_cache_prepared() && | ||
self.0.where_clause().map(|w| w.is_safe_to_cache_prepared()).unwrap_or(true) | ||
self.0.table.from_clause().is_safe_to_cache_prepared() && | ||
self.0.where_clause.as_ref().map(|w| w.is_safe_to_cache_prepared()).unwrap_or(true) | ||
} | ||
} | ||
|
||
impl_query_id!(noop: DeleteStatement<T>); | ||
impl_query_id!(noop: DeleteStatement<T, U>); |
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
Oops, something went wrong.