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
use crate::{error::*, ActiveModelTrait, DatabaseConnection, Insert, Statement};
use sea_query::InsertStatement;
use std::future::Future;

#[derive(Clone, Debug)]
pub struct Inserter {
    query: InsertStatement,
}

#[derive(Clone, Debug)]
pub struct InsertResult {
    pub last_insert_id: u64,
}

impl<A> Insert<A>
where
    A: ActiveModelTrait,
{
    #[allow(unused_mut)]
    pub fn exec(
        self,
        db: &DatabaseConnection,
    ) -> impl Future<Output = Result<InsertResult, DbErr>> + '_ {
        // so that self is dropped before entering await
        let mut query = self.query;
        #[cfg(feature = "sqlx-postgres")]
        if let DatabaseConnection::SqlxPostgresPoolConnection(_) = db {
            use crate::{EntityTrait, Iterable};
            use sea_query::{Alias, Expr, Query};
            for key in <A::Entity as EntityTrait>::PrimaryKey::iter() {
                query.returning(
                    Query::select()
                        .expr_as(Expr::col(key), Alias::new("last_insert_id"))
                        .to_owned(),
                );
            }
        }
        Inserter::new(query).exec(db)
    }
}

impl Inserter {
    pub fn new(query: InsertStatement) -> Self {
        Self { query }
    }

    pub fn exec(
        self,
        db: &DatabaseConnection,
    ) -> impl Future<Output = Result<InsertResult, DbErr>> + '_ {
        let builder = db.get_database_backend();
        exec_insert(builder.build(&self.query), db)
    }
}

// Only Statement impl Send
async fn exec_insert(statement: Statement, db: &DatabaseConnection) -> Result<InsertResult, DbErr> {
    // TODO: Postgres instead use query_one + returning clause
    let result = match db {
        #[cfg(feature = "sqlx-postgres")]
        DatabaseConnection::SqlxPostgresPoolConnection(conn) => {
            let res = conn.query_one(statement).await?.unwrap();
            crate::query_result_into_exec_result(res)?
        }
        _ => db.execute(statement).await?,
    };
    Ok(InsertResult {
        last_insert_id: result.last_insert_id(),
    })
}