#
mcaptcha/
db.rs

    1// Copyright (C) 2022  Aravinth Manivannan <realaravinth@batsense.net>
    2// SPDX-FileCopyrightText: 2023 Aravinth Manivannan <realaravinth@batsense.net>
    3//
    4// SPDX-License-Identifier: AGPL-3.0-or-later
    5
    6use crate::settings::Settings;
    7use db_core::prelude::*;
    8
    9pub type BoxDB = Box<dyn MCDatabase>;
    10
    11pub mod pg {
    12    use super::*;
    13    use db_sqlx_postgres::{ConnectionOptions, Fresh};
    14    use sqlx::postgres::PgPoolOptions;
    15
    16    pub async fn get_data(settings: Option<Settings>) -> BoxDB {
    17        let settings = settings.unwrap_or_else(|| Settings::new().unwrap());
    18        let pool = settings.database.pool;
    19        let pool_options = PgPoolOptions::new().max_connections(pool);
    20        let connection_options = ConnectionOptions::Fresh(Fresh {
    21            pool_options,
    22            url: settings.database.url.clone(),
    23            disable_logging: !settings.debug,
    24        });
    25        let db = connection_options.connect().await.unwrap();
    26        db.migrate().await.unwrap();
    27        Box::new(db)
    28    }
    29}
    30
    31pub mod maria {
    32    use super::*;
    33    use db_sqlx_maria::{ConnectionOptions, Fresh};
    34    use sqlx::mysql::MySqlPoolOptions;
    35
    36    pub async fn get_data(settings: Option<Settings>) -> BoxDB {
    37        let settings = settings.unwrap_or_else(|| Settings::new().unwrap());
    38        let pool = settings.database.pool;
    39        let pool_options = MySqlPoolOptions::new().max_connections(pool);
    40        let connection_options = ConnectionOptions::Fresh(Fresh {
    41            pool_options,
    42            url: settings.database.url.clone(),
    43            disable_logging: !settings.debug,
    44        });
    45        let db = connection_options.connect().await.unwrap();
    46        db.migrate().await.unwrap();
    47        Box::new(db)
    48    }
    49}