1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
use expression::Expression; use super::{IntoUpdateTarget, IncompleteUpdateStatement, IncompleteInsertStatement, SelectStatement}; use super::delete_statement::DeleteStatement; use super::insert_statement::Insert; /// Creates an update statement. Helpers for updating a single row can be /// generated by deriving [`AsChangeset`](query_builder/trait.AsChangeset.html) /// /// # Examples /// /// ### Updating a single record: /// /// ```rust /// # #[macro_use] extern crate diesel; /// # include!("src/doctest_setup.rs"); /// # /// # table! { /// # users { /// # id -> Integer, /// # name -> VarChar, /// # } /// # } /// # /// # #[cfg(feature = "postgres")] /// # fn main() { /// # use self::users::dsl::*; /// # let connection = establish_connection(); /// let updated_row = diesel::update(users.filter(id.eq(1))) /// .set(name.eq("James")) /// .get_result(&connection); /// // On backends that support it, you can call `get_result` instead of `execute` /// // to have `RETURNING *` automatically appended to the query. Alternatively, you /// // can explicitly return an expression by using the `returning` method before /// // getting the result. /// assert_eq!(Ok((1, "James".to_string())), updated_row); /// # } /// # #[cfg(not(feature = "postgres"))] /// # fn main() {} /// ``` pub fn update<T: IntoUpdateTarget>(source: T) -> IncompleteUpdateStatement<T::Table, T::WhereClause> { IncompleteUpdateStatement::new(source.into_update_target()) } /// Creates a delete statement. Will delete the records in the given set. /// Because this function has a very generic name, it is not exported by /// default. /// /// # Examples /// /// ### Deleting a single record: /// /// ```rust /// # #[macro_use] extern crate diesel; /// # include!("src/doctest_setup.rs"); /// # /// # table! { /// # users { /// # id -> Integer, /// # name -> VarChar, /// # } /// # } /// # /// # fn main() { /// # delete(); /// # } /// # /// # fn delete() -> QueryResult<()> { /// # use self::users::dsl::*; /// # let connection = establish_connection(); /// # let get_count = || users.count().first::<i64>(&connection); /// let old_count = get_count(); /// try!(diesel::delete(users.filter(id.eq(1))).execute(&connection)); /// assert_eq!(old_count.map(|count| count - 1), get_count()); /// # Ok(()) /// # } /// ``` /// /// ### Deleting a whole table: /// /// ```rust /// # #[macro_use] extern crate diesel; /// # include!("src/doctest_setup.rs"); /// # /// # table! { /// # users { /// # id -> Integer, /// # name -> VarChar, /// # } /// # } /// # /// # fn main() { /// # delete(); /// # } /// # /// # fn delete() -> QueryResult<()> { /// # use self::users::dsl::*; /// # let connection = establish_connection(); /// # let get_count = || users.count().first::<i64>(&connection); /// try!(diesel::delete(users).execute(&connection)); /// assert_eq!(Ok(0), get_count()); /// # Ok(()) /// # } /// ``` pub fn delete<T: IntoUpdateTarget>(source: T) -> DeleteStatement<T::Table, T::WhereClause> { DeleteStatement::new(source.into_update_target()) } /// Creates an insert statement. Will add the given data to a table. This /// function is not exported by default. As with other commands, the resulting /// query can return the inserted rows if you choose. pub fn insert<T: ?Sized>(records: &T) -> IncompleteInsertStatement<&T, Insert> { IncompleteInsertStatement::new(records, Insert) } /// Creates a bare select statement, with no from clause. Primarily used for /// testing diesel itself, but likely useful for third party crates as well. The /// given expressions must be selectable from anywhere. pub fn select<T>(expression: T) -> SelectStatement<T, ()> where T: Expression, { SelectStatement::simple(expression, ()) }