[][src]Trait diesel::query_dsl::RunQueryDsl

pub trait RunQueryDsl<Conn>: Sized {
    fn execute(self, conn: &Conn) -> QueryResult<usize>
    where
        Conn: Connection,
        Self: ExecuteDsl<Conn>
, { ... }
fn load<U>(self, conn: &Conn) -> QueryResult<Vec<U>>
    where
        Self: LoadQuery<Conn, U>
, { ... }
fn get_result<U>(self, conn: &Conn) -> QueryResult<U>
    where
        Self: LoadQuery<Conn, U>
, { ... }
fn get_results<U>(self, conn: &Conn) -> QueryResult<Vec<U>>
    where
        Self: LoadQuery<Conn, U>
, { ... }
fn first<U>(self, conn: &Conn) -> QueryResult<U>
    where
        Self: LimitDsl,
        Limit<Self>: LoadQuery<Conn, U>
, { ... } }

Methods used to execute queries.

Provided methods

fn execute(self, conn: &Conn) -> QueryResult<usize> where
    Conn: Connection,
    Self: ExecuteDsl<Conn>, 

Executes the given command, returning the number of rows affected.

execute is usually used in conjunction with insert_into, update and delete where the number of affected rows is often enough information.

When asking the database to return data from a query, load should probably be used instead.

Example

let inserted_rows = insert_into(users)
    .values(name.eq("Ruby"))
    .execute(&connection)?;
assert_eq!(1, inserted_rows);

let inserted_rows = insert_into(users)
    .values(&vec![name.eq("Jim"), name.eq("James")])
    .execute(&connection)?;
assert_eq!(2, inserted_rows);

fn load<U>(self, conn: &Conn) -> QueryResult<Vec<U>> where
    Self: LoadQuery<Conn, U>, 

Executes the given query, returning a Vec with the returned rows.

When using the query builder, the return type can be a tuple of the values, or a struct which implements Queryable.

When this method is called on sql_query, the return type can only be a struct which implements QueryableByName

For insert, update, and delete operations where only a count of affected is needed, execute should be used instead.

Examples

Returning a single field

let data = users.select(name)
    .load::<String>(&connection)?;
assert_eq!(vec!["Sean", "Tess"], data);

Returning a tuple

let data = users
    .load::<(i32, String)>(&connection)?;
let expected_data = vec![
    (1, String::from("Sean")),
    (2, String::from("Tess")),
];
assert_eq!(expected_data, data);

Returning a struct

#[derive(Queryable, PartialEq, Debug)]
struct User {
    id: i32,
    name: String,
}

let data = users
    .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);

fn get_result<U>(self, conn: &Conn) -> QueryResult<U> where
    Self: LoadQuery<Conn, U>, 

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.

Example

let inserted_row = insert_into(users)
    .values(name.eq("Ruby"))
    .get_result(&connection)?;
assert_eq!((3, String::from("Ruby")), 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"))
    .get_result::<(i32, String)>(&connection);
assert_eq!(Err(diesel::NotFound), update_result);

fn get_results<U>(self, conn: &Conn) -> QueryResult<Vec<U>> where
    Self: LoadQuery<Conn, U>, 

Runs the command, returning an Vec with the affected rows.

This method is an alias for load, but with a name that makes more sense for insert, update, and delete statements.

fn first<U>(self, conn: &Conn) -> QueryResult<U> where
    Self: LimitDsl,
    Limit<Self>: LoadQuery<Conn, U>, 

Attempts to load a single record.

This method is equivalent to .limit(1).get_result()

Returns Ok(record) if found, and Err(NotFound) if no results are returned. If the query truly is optional, you can call .optional() on the result of this to get a Result<Option<U>>.

Example:

diesel::insert_into(users)
    .values(&vec![name.eq("Sean"), name.eq("Pascal")])
    .execute(&connection)?;

let first_name = users.order(id).select(name).first(&connection);
assert_eq!(Ok(String::from("Sean")), first_name);

let not_found = users
    .filter(name.eq("Foo"))
    .first::<(i32, String)>(&connection);
assert_eq!(Err(diesel::NotFound), not_found);
Loading content...

Implementors

impl<'_, Conn: Connection, Query> RunQueryDsl<Conn> for BoxedSqlQuery<'_, Conn::Backend, Query>[src]

impl<Inner, Conn> RunQueryDsl<Conn> for SqlQuery<Inner>[src]

impl<Query, Value, Conn> RunQueryDsl<Conn> for UncheckedBind<Query, Value>[src]

impl<ST, T, Conn> RunQueryDsl<Conn> for SqlLiteral<ST, T>[src]

impl<T, Conn> RunQueryDsl<Conn> for T where
    T: Table
[src]

impl<T, U, Op, Ret, Conn> RunQueryDsl<Conn> for InsertStatement<T, U, Op, Ret>[src]

impl<T, U, Ret, Conn> RunQueryDsl<Conn> for DeleteStatement<T, U, Ret>[src]

impl<T, U, V, Ret, Conn> RunQueryDsl<Conn> for UpdateStatement<T, U, V, Ret>[src]

Loading content...