Skip to main content

rullst_orm/
database.rs

1#[cfg(not(any(
2    feature = "strict-postgres",
3    feature = "strict-mysql",
4    feature = "strict-sqlite"
5)))]
6pub type RullstDatabase = sqlx::Any;
7
8#[cfg(feature = "strict-postgres")]
9pub type RullstDatabase = sqlx::Postgres;
10
11#[cfg(all(feature = "strict-mysql", not(feature = "strict-postgres")))]
12pub type RullstDatabase = sqlx::MySql;
13
14#[cfg(all(
15    feature = "strict-sqlite",
16    not(feature = "strict-postgres"),
17    not(feature = "strict-mysql")
18))]
19pub type RullstDatabase = sqlx::Sqlite;
20
21/// Whether the ORM runs on the SQLx `Any` pool (no strict driver feature),
22/// which carries only integer, float, text and blob values.
23pub(crate) const ANY_POOL: bool = cfg!(not(any(
24    feature = "strict-postgres",
25    feature = "strict-mysql",
26    feature = "strict-sqlite"
27)));
28
29pub trait QueryResultExt {
30    fn get_last_insert_id(&self) -> i64;
31}
32
33#[cfg(not(any(
34    feature = "strict-postgres",
35    feature = "strict-mysql",
36    feature = "strict-sqlite"
37)))]
38impl QueryResultExt for sqlx::any::AnyQueryResult {
39    #[cfg_attr(test, mutants::skip)]
40    fn get_last_insert_id(&self) -> i64 {
41        self.last_insert_id().unwrap_or(0)
42    }
43}
44
45#[cfg(feature = "strict-postgres")]
46impl QueryResultExt for sqlx::postgres::PgQueryResult {
47    #[cfg_attr(test, mutants::skip)]
48    fn get_last_insert_id(&self) -> i64 {
49        0
50    }
51}
52
53#[cfg(all(feature = "strict-mysql", not(feature = "strict-postgres")))]
54impl QueryResultExt for sqlx::mysql::MySqlQueryResult {
55    #[cfg_attr(test, mutants::skip)]
56    fn get_last_insert_id(&self) -> i64 {
57        self.last_insert_id() as i64
58    }
59}
60
61#[cfg(all(
62    feature = "strict-sqlite",
63    not(feature = "strict-postgres"),
64    not(feature = "strict-mysql")
65))]
66impl QueryResultExt for sqlx::sqlite::SqliteQueryResult {
67    #[cfg_attr(test, mutants::skip)]
68    fn get_last_insert_id(&self) -> i64 {
69        self.last_insert_rowid()
70    }
71}