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
use sqlx::sqlite::SqliteRow;

pub const SQLITE_CONSTRAINT_PRIMARYKEY: &str = "1555";
pub const SQLITE_CONSTRAINT_FOREIGNKEY: &str = "787";

#[macro_export]
macro_rules! db_error_code {
    ($result:expr, $error:expr, $code:expr) => {
        if let Err(sqlx::Error::Database(ref error)) = $result {
            if let Some(code) = error.code() {
                if code == $code {
                    return Err($error);
                }
            }
        }
    };
}

#[macro_export]
macro_rules! db_error_message {
    ($result:expr, $error:expr, $message:expr) => {
        if let Err(sqlx::Error::Database(ref error)) = $result {
            if error.message() == $message {
                return Err($error);
            }
        }
    };
}

pub(crate) trait FromRow
where
    Self: Sized,
{
    type Error;

    fn try_from_row(row: SqliteRow) -> Result<Self, Self::Error>;
}