databeam/
database.rs

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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//! Database handler
use super::{
    cachedb::CacheDB,
    sql::{create_db, Database, DatabaseOpts},
};

use serde::{Deserialize, Serialize};
use sqlx::{Column, Row};
use std::collections::BTreeMap;

/// Default API return value
#[allow(dead_code)]
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct DefaultReturn<T> {
    pub success: bool,
    pub message: String,
    pub payload: T,
}

/// Basic return type for database output
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct DatabaseReturn(pub BTreeMap<String, String>);

/// Basic database
#[derive(Clone)]
#[cfg(feature = "postgres")]
pub struct StarterDatabase {
    pub db: Database<sqlx::PgPool>,
    pub options: DatabaseOpts,
    pub cachedb: CacheDB,
}

/// Basic database
#[derive(Clone)]
#[cfg(feature = "mysql")]
pub struct StarterDatabase {
    pub db: Database<sqlx::MySqlPool>,
    pub options: DatabaseOpts,
    pub cachedb: CacheDB,
}

/// Basic database
#[derive(Clone)]
#[cfg(feature = "sqlite")]
pub struct StarterDatabase {
    pub db: Database<sqlx::SqlitePool>,
    pub options: DatabaseOpts,
    pub cachedb: CacheDB,
}

impl StarterDatabase {
    pub async fn new(options: DatabaseOpts) -> StarterDatabase {
        StarterDatabase {
            db: create_db(options.clone()).await,
            options,
            cachedb: CacheDB::new().await,
        }
    }

    /// Convert all columns into a [`HashMap`].
    ///
    /// # Arguments
    /// * `row`
    /// * `as_bytes` - a [`Vec`] containing all the columns that we want to read in their original `Vec<u8>` form
    #[cfg(feature = "sqlite")]
    pub fn textify_row(&self, row: sqlx::sqlite::SqliteRow) -> DatabaseReturn {
        // get all columns
        let columns = row.columns();

        // create output
        let mut out: BTreeMap<String, String> = BTreeMap::new();

        for column in columns {
            let name = column.name().to_string();
            let value = row.get(&name.as_str());
            out.insert(name, value);
        }

        // return
        DatabaseReturn(out)
    }

    /// Convert all columns into a [`HashMap`].
    ///
    /// # Arguments
    /// * `row`
    /// * `as_bytes` - a [`Vec`] containing all the columns that we want to read in their original `Vec<u8>` form
    #[cfg(feature = "postgres")]
    pub fn textify_row(&self, row: sqlx::postgres::PgRow) -> DatabaseReturn {
        // get all columns
        let columns = row.columns();

        // create output
        let mut out: BTreeMap<String, String> = BTreeMap::new();

        for column in columns {
            let name = column.name().to_string();
            let value = row.get(&name.as_str());
            out.insert(name, value);
        }

        // return
        DatabaseReturn(out)
    }

    /// Convert all columns into a [`HashMap`].
    ///
    /// # Arguments
    /// * `row`
    /// * `as_bytes` - a [`Vec`] containing all the columns that we want to read in their original `Vec<u8>` form
    #[cfg(feature = "mysql")]
    pub fn textify_row(&self, row: sqlx::mysql::MySqlRow) -> DatabaseReturn {
        // get all columns
        let columns = row.columns();

        // create output
        let mut out: BTreeMap<String, String> = BTreeMap::new();

        for column in columns {
            let name = column.name().to_string();

            match row.try_get::<Vec<u8>, _>(&name.as_str()) {
                Ok(value) => {
                    // we're going to convert this to a string and then add it to the output!
                    out.insert(
                        column.name().to_string(),
                        std::str::from_utf8(value.as_slice()).unwrap().to_string(),
                    );
                }
                Err(_) => {
                    // already text
                    let value = row.get(&name.as_str());
                    out.insert(name, value);
                }
            };
        }

        // return
        DatabaseReturn(out)
    }
}