sqlx-firebird 0.1.0-beta.1

sqlx firebird driver
Documentation
//
// Copyright © 2023, RedSoft
// License: MIT
//

mod connect;
mod parse;

use std::env::var;
use std::fmt::{Debug, Formatter};

use rsfbclient::charset::{self, Charset};
use rsfbclient::prelude::TransactionConfiguration;
use rsfbclient::{builder_native, ConnRemote, Dialect, DynLink, NativeConnectionBuilder};
use sqlx_core::connection::LogSettings;

#[derive(Debug, Clone)]
struct FbConnectOptionsInfo {
    host: String,
    port: u16,
    username: String,
    database: String,
    role_name: Option<String>,
    dialect: Dialect,
    stmt_cache_size: usize,
    charset: Charset,
    page_size: Option<u32>,
    transaction_conf: TransactionConfiguration,
}

impl Default for FbConnectOptionsInfo {
    fn default() -> Self {
        Self {
            host: String::from("localhost"),
            port: 3050,
            username: String::from("SYSDBA"),
            database: String::from("test.fdb"),
            role_name: None,
            dialect: Dialect::D3,
            stmt_cache_size: 20,
            page_size: None,
            charset: charset::UTF_8,
            transaction_conf: TransactionConfiguration::default(),
        }
    }
}

#[derive(Clone)]
pub struct FbConnectOptions {
    info: FbConnectOptionsInfo,
    pub(crate) log_settings: LogSettings,
    pub(crate) builder: NativeConnectionBuilder<DynLink, ConnRemote>,
    pub(crate) command_channel_size: usize,
    pub(crate) row_channel_size: usize,
}

impl Debug for FbConnectOptions {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:?}", self.info)
    }
}

impl FbConnectOptions {
    pub fn new() -> Self {
        let mut retval = Self {
            info: FbConnectOptionsInfo::default(),
            log_settings: LogSettings::default(),
            builder: builder_native().with_dyn_link().with_remote(),
            command_channel_size: 50,
            row_channel_size: 50,
        };

        if let Some(port) = var("FBPORT").ok().and_then(|v| v.parse().ok()) {
            retval = retval.port(port);
        }

        if let Some(host) = var("FBHOST").ok() {
            retval = retval.host(host);
        }

        if let Some(db_name) = var("FBDATABASE").ok() {
            retval = retval.database(db_name);
        }

        if let Some(user) = var("FBUSER").ok() {
            retval = retval.username(user);
        }

        if let Some(pass) = var("FBPASSWORD").ok() {
            retval = retval.password(pass);
        }

        retval
    }

    pub fn host<T>(mut self, val: T) -> Self
    where
        T: Into<String>,
    {
        let val = val.into();
        self.info.host = val.clone();
        self.builder.host(val);
        self
    }

    pub fn port(mut self, val: u16) -> Self {
        self.info.port = val;
        self.builder.port(val);
        self
    }

    pub fn database<T>(mut self, val: T) -> Self
    where
        T: Into<String>,
    {
        let val = val.into();
        self.info.database = val.clone();
        self.builder.db_name(val);
        self
    }

    pub fn username<T>(mut self, val: T) -> Self
    where
        T: Into<String>,
    {
        let val = val.into();
        self.info.username = val.clone();
        self.builder.user(val);
        self
    }

    pub fn password<T>(mut self, val: T) -> Self
    where
        T: Into<String>,
    {
        let val = val.into();
        self.builder.pass(val);
        self
    }

    pub fn role_name<T>(mut self, val: T) -> Self
    where
        T: Into<String>,
    {
        let val = val.into();
        self.info.role_name = Some(val.clone());
        self.builder.role(val);
        self
    }

    pub fn charset(mut self, val: Charset) -> Self {
        self.info.charset = val.clone();
        self.builder.charset(val);
        self
    }

    pub fn dialect(mut self, val: Dialect) -> Self {
        self.info.dialect = val;
        self.builder.dialect(val);
        self
    }

    pub fn stmt_cache_size(mut self, val: usize) -> Self {
        self.info.stmt_cache_size = val;
        self.builder.stmt_cache_size(val);
        self
    }

    pub fn page_size(mut self, val: u32) -> Self {
        self.info.page_size = Some(val);
        self.builder.page_size(val);
        self
    }

    pub fn transaction(mut self, conf: TransactionConfiguration) -> Self {
        self.info.transaction_conf = conf.clone();
        self.builder.transaction(conf);
        self
    }

    pub fn row_buffer_size(mut self, val: usize) -> Self {
        self.row_channel_size = val;
        self
    }

    pub fn command_buffer_size(mut self, val: usize) -> Self {
        self.command_channel_size = val;
        self
    }
}