Struct sea_orm::SelectorRaw

source ·
pub struct SelectorRaw<S>where
    S: SelectorTrait,
{ /* private fields */ }
Expand description

Performs a raw SELECT operation on a model

Implementations

Select a custom Model from a raw SQL Statement.

Create SelectorRaw from Statement and columns. Executing this SelectorRaw will return a type T which implement TryGetableMany.

use sea_orm::{entity::*, query::*, tests_cfg::cake, FromQueryResult};

#[derive(Debug, PartialEq, FromQueryResult)]
struct SelectResult {
    name: String,
    num_of_cakes: i32,
}

let res: Vec<SelectResult> = cake::Entity::find()
    .from_raw_sql(Statement::from_sql_and_values(
        DbBackend::Postgres,
        r#"SELECT "cake"."name", count("cake"."id") AS "num_of_cakes" FROM "cake""#,
        vec![],
    ))
    .into_model::<SelectResult>()
    .all(&db)
    .await?;

assert_eq!(
    res,
    vec![
        SelectResult {
            name: "Chocolate Forest".to_owned(),
            num_of_cakes: 1,
        },
        SelectResult {
            name: "New York Cheese".to_owned(),
            num_of_cakes: 1,
        },
    ]
);

assert_eq!(
    db.into_transaction_log(),
    vec![Transaction::from_sql_and_values(
        DbBackend::Postgres,
        r#"SELECT "cake"."name", count("cake"."id") AS "num_of_cakes" FROM "cake""#,
        vec![]
    ),]
);
use sea_orm::{entity::*, query::*, tests_cfg::cake};

let res: Vec<serde_json::Value> = cake::Entity::find().from_raw_sql(
    Statement::from_sql_and_values(
        DbBackend::Postgres, r#"SELECT "cake"."id", "cake"."name" FROM "cake""#, vec![]
    )
)
.into_json()
.all(&db)
.await?;

assert_eq!(
    res,
    vec![
        serde_json::json!({
            "name": "Chocolate Forest",
            "num_of_cakes": 1,
        }),
        serde_json::json!({
            "name": "New York Cheese",
            "num_of_cakes": 1,
        }),
    ]
);

assert_eq!(
    db.into_transaction_log(),
    vec![
    Transaction::from_sql_and_values(
            DbBackend::Postgres, r#"SELECT "cake"."id", "cake"."name" FROM "cake""#, vec![]
    ),
]);

Get an item from the Select query

use sea_orm::{entity::*, query::*, tests_cfg::cake};

let _: Option<cake::Model> = cake::Entity::find()
    .from_raw_sql(Statement::from_sql_and_values(
        DbBackend::Postgres,
        r#"SELECT "cake"."id", "cake"."name" FROM "cake" WHERE "id" = $1"#,
        vec![1.into()],
    ))
    .one(&db)
    .await?;

assert_eq!(
    db.into_transaction_log(),
    vec![Transaction::from_sql_and_values(
        DbBackend::Postgres,
        r#"SELECT "cake"."id", "cake"."name" FROM "cake" WHERE "id" = $1"#,
        vec![1.into()]
    ),]
);

Get all items from the Select query

use sea_orm::{entity::*, query::*, tests_cfg::cake};

let _: Vec<cake::Model> = cake::Entity::find()
    .from_raw_sql(Statement::from_sql_and_values(
        DbBackend::Postgres,
        r#"SELECT "cake"."id", "cake"."name" FROM "cake""#,
        vec![],
    ))
    .all(&db)
    .await?;

assert_eq!(
    db.into_transaction_log(),
    vec![Transaction::from_sql_and_values(
        DbBackend::Postgres,
        r#"SELECT "cake"."id", "cake"."name" FROM "cake""#,
        vec![]
    ),]
);

Stream the results of the Select operation

Trait Implementations

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Select operation
Paginate the result of a select operation.
Perform a count on the paginated results

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more