1use super::{
3 prelude::*,
4 sql::{create_db, Database, DatabaseOpts},
5};
6
7use serde::{Deserialize, Serialize};
8use sqlx::{Column, Row};
9use std::collections::BTreeMap;
10
11#[allow(dead_code)]
13#[derive(Debug, Serialize, Deserialize, Clone)]
14pub struct DefaultReturn<T> {
15 pub success: bool,
16 pub message: String,
17 pub payload: T,
18}
19
20#[derive(Debug, Serialize, Deserialize, Clone)]
22pub struct DatabaseReturn(pub BTreeMap<String, String>);
23
24#[derive(Clone)]
26#[cfg(feature = "postgres")]
27pub struct StarterDatabase {
28 pub db: Database<sqlx::PgPool>,
29 pub options: DatabaseOpts,
30 #[cfg(feature = "redis")]
31 pub cache: RedisCache,
32 #[cfg(feature = "moka")]
33 pub cache: MokaCache,
34 #[cfg(feature = "oysters")]
35 pub cache: OystersCache,
36}
37
38#[derive(Clone)]
40#[cfg(feature = "mysql")]
41pub struct StarterDatabase {
42 pub db: Database<sqlx::MySqlPool>,
43 pub options: DatabaseOpts,
44 #[cfg(feature = "redis")]
45 pub cache: RedisCache,
46 #[cfg(feature = "moka")]
47 pub cache: MokaCache,
48 #[cfg(feature = "oysters")]
49 pub cache: OystersCache,
50}
51
52#[derive(Clone)]
54#[cfg(feature = "sqlite")]
55pub struct StarterDatabase {
56 pub db: Database<sqlx::SqlitePool>,
57 pub options: DatabaseOpts,
58 #[cfg(feature = "redis")]
59 pub cache: RedisCache,
60 #[cfg(feature = "moka")]
61 pub cache: MokaCache,
62 #[cfg(feature = "oysters")]
63 pub cache: OystersCache,
64}
65
66impl StarterDatabase {
67 pub async fn new(options: DatabaseOpts) -> StarterDatabase {
68 StarterDatabase {
69 db: create_db(options.clone()).await,
70 options,
71 #[cfg(feature = "redis")]
72 cache: RedisCache::new().await,
73 #[cfg(feature = "moka")]
74 cache: MokaCache::new().await,
75 #[cfg(feature = "oysters")]
76 cache: OystersCache::new().await,
77 }
78 }
79
80 #[cfg(feature = "sqlite")]
86 pub fn textify_row(&self, row: sqlx::sqlite::SqliteRow) -> DatabaseReturn {
87 let columns = row.columns();
89
90 let mut out: BTreeMap<String, String> = BTreeMap::new();
92
93 for column in columns {
94 let name = column.name().to_string();
95 let value = row.get(&name.as_str());
96 out.insert(name, value);
97 }
98
99 DatabaseReturn(out)
101 }
102
103 #[cfg(feature = "postgres")]
109 pub fn textify_row(&self, row: sqlx::postgres::PgRow) -> DatabaseReturn {
110 let columns = row.columns();
112
113 let mut out: BTreeMap<String, String> = BTreeMap::new();
115
116 for column in columns {
117 let name = column.name().to_string();
118 let value = row.get(&name.as_str());
119 out.insert(name, value);
120 }
121
122 DatabaseReturn(out)
124 }
125
126 #[cfg(feature = "mysql")]
132 pub fn textify_row(&self, row: sqlx::mysql::MySqlRow) -> DatabaseReturn {
133 let columns = row.columns();
135
136 let mut out: BTreeMap<String, String> = BTreeMap::new();
138
139 for column in columns {
140 let name = column.name().to_string();
141
142 match row.try_get::<Vec<u8>, _>(&name.as_str()) {
143 Ok(value) => {
144 out.insert(
146 column.name().to_string(),
147 std::str::from_utf8(value.as_slice()).unwrap().to_string(),
148 );
149 }
150 Err(_) => {
151 let value = row.get(&name.as_str());
153 out.insert(name, value);
154 }
155 };
156 }
157
158 DatabaseReturn(out)
160 }
161}