sqlx_firebird/options/
mod.rs

1//
2// Copyright © 2023, RedSoft
3// License: MIT
4//
5
6mod connect;
7mod parse;
8
9use std::env::var;
10use std::fmt::{Debug, Formatter};
11
12use rsfbclient::charset::{self, Charset};
13use rsfbclient::prelude::TransactionConfiguration;
14use rsfbclient::{builder_native, ConnRemote, Dialect, DynLink, NativeConnectionBuilder};
15use sqlx_core::connection::LogSettings;
16
17#[derive(Debug, Clone)]
18struct FbConnectOptionsInfo {
19    host: String,
20    port: u16,
21    username: String,
22    database: String,
23    role_name: Option<String>,
24    dialect: Dialect,
25    stmt_cache_size: usize,
26    charset: Charset,
27    page_size: Option<u32>,
28    transaction_conf: TransactionConfiguration,
29}
30
31impl Default for FbConnectOptionsInfo {
32    fn default() -> Self {
33        Self {
34            host: String::from("localhost"),
35            port: 3050,
36            username: String::from("SYSDBA"),
37            database: String::from("test.fdb"),
38            role_name: None,
39            dialect: Dialect::D3,
40            stmt_cache_size: 20,
41            page_size: None,
42            charset: charset::UTF_8,
43            transaction_conf: TransactionConfiguration::default(),
44        }
45    }
46}
47
48#[derive(Clone)]
49pub struct FbConnectOptions {
50    info: FbConnectOptionsInfo,
51    pub(crate) log_settings: LogSettings,
52    pub(crate) builder: NativeConnectionBuilder<DynLink, ConnRemote>,
53    pub(crate) command_channel_size: usize,
54    pub(crate) row_channel_size: usize,
55}
56
57impl Debug for FbConnectOptions {
58    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
59        write!(f, "{:?}", self.info)
60    }
61}
62
63impl FbConnectOptions {
64    pub fn new() -> Self {
65        let mut retval = Self {
66            info: FbConnectOptionsInfo::default(),
67            log_settings: LogSettings::default(),
68            builder: builder_native().with_dyn_link().with_remote(),
69            command_channel_size: 50,
70            row_channel_size: 50,
71        };
72
73        if let Some(port) = var("FBPORT").ok().and_then(|v| v.parse().ok()) {
74            retval = retval.port(port);
75        }
76
77        if let Some(host) = var("FBHOST").ok() {
78            retval = retval.host(host);
79        }
80
81        if let Some(db_name) = var("FBDATABASE").ok() {
82            retval = retval.database(db_name);
83        }
84
85        if let Some(user) = var("FBUSER").ok() {
86            retval = retval.username(user);
87        }
88
89        if let Some(pass) = var("FBPASSWORD").ok() {
90            retval = retval.password(pass);
91        }
92
93        retval
94    }
95
96    pub fn host<T>(mut self, val: T) -> Self
97    where
98        T: Into<String>,
99    {
100        let val = val.into();
101        self.info.host = val.clone();
102        self.builder.host(val);
103        self
104    }
105
106    pub fn port(mut self, val: u16) -> Self {
107        self.info.port = val;
108        self.builder.port(val);
109        self
110    }
111
112    pub fn database<T>(mut self, val: T) -> Self
113    where
114        T: Into<String>,
115    {
116        let val = val.into();
117        self.info.database = val.clone();
118        self.builder.db_name(val);
119        self
120    }
121
122    pub fn username<T>(mut self, val: T) -> Self
123    where
124        T: Into<String>,
125    {
126        let val = val.into();
127        self.info.username = val.clone();
128        self.builder.user(val);
129        self
130    }
131
132    pub fn password<T>(mut self, val: T) -> Self
133    where
134        T: Into<String>,
135    {
136        let val = val.into();
137        self.builder.pass(val);
138        self
139    }
140
141    pub fn role_name<T>(mut self, val: T) -> Self
142    where
143        T: Into<String>,
144    {
145        let val = val.into();
146        self.info.role_name = Some(val.clone());
147        self.builder.role(val);
148        self
149    }
150
151    pub fn charset(mut self, val: Charset) -> Self {
152        self.info.charset = val.clone();
153        self.builder.charset(val);
154        self
155    }
156
157    pub fn dialect(mut self, val: Dialect) -> Self {
158        self.info.dialect = val;
159        self.builder.dialect(val);
160        self
161    }
162
163    pub fn stmt_cache_size(mut self, val: usize) -> Self {
164        self.info.stmt_cache_size = val;
165        self.builder.stmt_cache_size(val);
166        self
167    }
168
169    pub fn page_size(mut self, val: u32) -> Self {
170        self.info.page_size = Some(val);
171        self.builder.page_size(val);
172        self
173    }
174
175    pub fn transaction(mut self, conf: TransactionConfiguration) -> Self {
176        self.info.transaction_conf = conf.clone();
177        self.builder.transaction(conf);
178        self
179    }
180
181    pub fn row_buffer_size(mut self, val: usize) -> Self {
182        self.row_channel_size = val;
183        self
184    }
185
186    pub fn command_buffer_size(mut self, val: usize) -> Self {
187        self.command_channel_size = val;
188        self
189    }
190}