grorm 0.1.1

Goroutine-native ORM for Rust with multi-database support
Documentation
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
pub mod default;
pub mod mysql;
pub mod postgres;
pub mod sqlite;

pub use default::{DefaultDriver, DefaultDriverFactory};
pub use mysql::{MysqlDriver, MysqlDriverFactory};
pub use postgres::{PostgresDriver, PostgresDriverFactory};
pub use sqlite::{SqliteDriver, SqliteDriverFactory};

use crate::error::Error;

/// 数据库连接配置
#[derive(Debug, Clone)]
pub struct ConnectionConfig {
    pub db_type: String,
    pub host: String,
    pub port: u16,
    pub username: String,
    pub password: String,
    pub database: String,
    pub ssl_mode: SslMode,
    pub max_connections: usize,
}

impl Default for ConnectionConfig {
    fn default() -> Self {
        ConnectionConfig {
            db_type: "".to_string(),
            host: "".to_string(),
            port: 0,
            username: "".to_string(),
            password: "".to_string(),
            database: "".to_string(),
            ssl_mode: SslMode::Disable,
            max_connections: 10,
        }
    }
}

impl ConnectionConfig {
    pub fn new(host: &str, port: u16, username: &str, password: &str, database: &str) -> Self {
        ConnectionConfig {
            db_type: String::new(),
            host: host.to_string(),
            port,
            username: username.to_string(),
            password: password.to_string(),
            database: database.to_string(),
            ssl_mode: SslMode::Disable,
            max_connections: 10,
        }
    }

    pub fn sqlite(db_path: &str) -> Self {
        ConnectionConfig {
            db_type: "sqlite".to_string(),
            host: db_path.to_string(),
            port: 0,
            username: String::new(),
            password: String::new(),
            database: db_path.to_string(),
            ssl_mode: SslMode::Disable,
            max_connections: 10,
        }
    }

    pub fn postgres(host: &str, port: u16, database: &str, username: &str, password: &str) -> Self {
        ConnectionConfig {
            db_type: "postgres".to_string(),
            host: host.to_string(),
            port,
            username: username.to_string(),
            password: password.to_string(),
            database: database.to_string(),
            ssl_mode: SslMode::Disable,
            max_connections: 10,
        }
    }

    pub fn mysql(host: &str, port: u16, database: &str, username: &str, password: &str) -> Self {
        ConnectionConfig {
            db_type: "mysql".to_string(),
            host: host.to_string(),
            port,
            username: username.to_string(),
            password: password.to_string(),
            database: database.to_string(),
            ssl_mode: SslMode::Disable,
            max_connections: 10,
        }
    }

    pub fn with_ssl(mut self, mode: SslMode) -> Self {
        self.ssl_mode = mode;
        self
    }

    pub fn with_max_connections(mut self, max: usize) -> Self {
        self.max_connections = max;
        self
    }
}

#[derive(Debug, Clone, PartialEq)]
pub enum SslMode {
    Disable,
    Prefer,
    Require,
    VerifyCa,
    VerifyFull,
}

/// 数据库类型标识
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum DatabaseType {
    Postgresql,
    Mysql,
    Sqlite,
    None,
}

/// 查询结果
pub struct QueryResult {
    pub rows: Vec<Row>,
    pub affected_rows: u64,
    pub last_insert_id: Option<i64>,
}

/// 单行数据
pub struct Row {
    columns: Vec<Column>,
    values: Vec<Option<String>>,
}

impl Row {
    pub fn new() -> Self {
        Row {
            columns: Vec::new(),
            values: Vec::new(),
        }
    }

    pub fn with_capacity(capacity: usize) -> Self {
        Row {
            columns: Vec::with_capacity(capacity),
            values: Vec::with_capacity(capacity),
        }
    }

    pub fn push(&mut self, column: Column, value: Option<String>) {
        self.columns.push(column);
        self.values.push(value);
    }

    pub fn get(&self, idx: usize) -> Option<&str> {
        self.values.get(idx).and_then(|v| v.as_deref())
    }

    pub fn get_by_name(&self, name: &str) -> Option<&str> {
        self.columns
            .iter()
            .position(|c| c.name == name)
            .and_then(|idx| self.get(idx))
    }

    pub fn column_count(&self) -> usize {
        self.columns.len()
    }

    pub fn iter(&self) -> RowIter<'_> {
        RowIter { row: self, idx: 0 }
    }
}

pub struct RowIter<'a> {
    row: &'a Row,
    idx: usize,
}

impl<'a> Iterator for RowIter<'a> {
    type Item = (&'a Column, Option<&'a str>);

    fn next(&mut self) -> Option<Self::Item> {
        if self.idx >= self.row.columns.len() {
            None
        } else {
            let col = &self.row.columns[self.idx];
            let val = self.row.values[self.idx].as_deref();
            self.idx += 1;
            Some((col, val))
        }
    }
}

/// 列信息
#[derive(Debug, Clone)]
pub struct Column {
    pub name: String,
    pub data_type: DataType,
    pub nullable: bool,
}

impl Column {
    pub fn new(name: &str, data_type: DataType) -> Self {
        Column {
            name: name.to_string(),
            data_type,
            nullable: true,
        }
    }

    pub fn not_null(mut self) -> Self {
        self.nullable = false;
        self
    }
}

/// 数据类型(数据库无关的抽象)
#[derive(Debug, Clone, PartialEq)]
pub enum DataType {
    Boolean,
    Int2,
    Int4,
    Int8,
    Float4,
    Float8,
    Text,
    Varchar(usize),
    Char(usize),
    Date,
    Time,
    Timestamp,
    Json,
    Jsonb,
    Uuid,
    Bytea,
    Array(Box<DataType>),
    Custom(String),
}

/// 参数化查询的参数
#[derive(Debug, Clone)]
pub enum Parameter {
    Null,
    Int(i64),
    Float(f64),
    String(String),
    Bool(bool),
    Bytes(Vec<u8>),
}

impl Parameter {
    pub fn as_sql_string(&self, _db_type: DatabaseType) -> String {
        match self {
            Parameter::Null => "NULL".to_string(),
            Parameter::Int(v) => v.to_string(),
            Parameter::Float(v) => v.to_string(),
            Parameter::Bool(v) => v.to_string(),
            Parameter::String(v) => {
                let escaped = v.replace('\'', "''");
                format!("'{}'", escaped)
            }
            Parameter::Bytes(v) => {
                format!("'\\x{}'", hex::encode(v))
            }
        }
    }
}

// 方便的类型转换
impl From<i32> for Parameter {
    fn from(v: i32) -> Self {
        Parameter::Int(v as i64)
    }
}

impl From<i64> for Parameter {
    fn from(v: i64) -> Self {
        Parameter::Int(v)
    }
}

impl From<&str> for Parameter {
    fn from(v: &str) -> Self {
        Parameter::String(v.to_string())
    }
}

impl From<String> for Parameter {
    fn from(v: String) -> Self {
        Parameter::String(v)
    }
}

impl From<bool> for Parameter {
    fn from(v: bool) -> Self {
        Parameter::Bool(v)
    }
}

impl From<f64> for Parameter {
    fn from(v: f64) -> Self {
        Parameter::Float(v)
    }
}

/// 数据库驱动核心 trait
pub trait DatabaseDriver: Send + Sync {
    /// 驱动的数据库类型
    fn db_type(&self) -> DatabaseType;

    /// 建立连接
    fn connect(&mut self, config: &ConnectionConfig) -> Result<(), Error>;

    /// 关闭连接
    fn close(&mut self) -> Result<(), Error>;

    /// 执行查询(返回结果集)
    fn query(&mut self, sql: &str, params: &[Parameter]) -> Result<QueryResult, Error>;

    /// 执行命令(不返回结果集)
    fn execute(&mut self, sql: &str, params: &[Parameter]) -> Result<u64, Error>;

    /// 准备语句
    fn prepare(&mut self, name: &str, sql: &str) -> Result<(), Error>;

    /// 执行已准备的语句
    fn execute_prepared(&mut self, name: &str, params: &[Parameter]) -> Result<QueryResult, Error>;

    /// 开始事务
    fn begin(&mut self) -> Result<(), Error>;

    /// 提交事务
    fn commit(&mut self) -> Result<(), Error>;

    /// 回滚事务
    fn rollback(&mut self) -> Result<(), Error>;

    /// 转义标识符(表名、列名)
    fn escape_identifier(&self, ident: &str) -> String;

    /// 获取最后插入的 ID
    fn last_insert_id(&mut self) -> Result<Option<i64>, Error>;

    /// 连接是否有效
    fn is_connected(&self) -> bool;

    /// 获取当前连接的版本信息
    fn version(&mut self) -> Result<String, Error>;

    /// 分页查询的 LIMIT/OFFSET 语法
    fn limit_offset_clause(&self, limit: Option<usize>, offset: Option<usize>) -> String;

    /// 占位符风格($1, ?, :name, 等)
    fn placeholder_style(&self) -> PlaceholderStyle;
}

/// 占位符风格
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum PlaceholderStyle {
    /// PostgreSQL: $1, $2, $3
    DollarNumbered,
    /// MySQL: ?, ?, ?
    Positional,
    /// SQLite: ?, ?, ?
    PositionalSqlite,
    /// 命名占位符: :name, :name2
    Named,
}

/// 驱动工厂 trait
pub trait DriverFactory: Send + Sync {
    fn create(&self) -> Box<dyn DatabaseDriver>;
    fn db_type(&self) -> DatabaseType;
}

// 注册驱动的方式
pub struct DriverRegistry {
    drivers: std::collections::HashMap<DatabaseType, Box<dyn DriverFactory>>,
}

impl DriverRegistry {
    pub fn new() -> Self {
        DriverRegistry {
            drivers: std::collections::HashMap::new(),
        }
    }

    pub fn register<F>(&mut self, factory: F)
    where
        F: DriverFactory + 'static,
    {
        let db_type = factory.db_type();
        self.drivers.insert(db_type, Box::new(factory));
    }

    pub fn get(&self, db_type: DatabaseType) -> Option<&dyn DriverFactory> {
        self.drivers.get(&db_type).map(|f| f.as_ref())
    }

    pub fn create_driver(&self, db_type: DatabaseType) -> Option<Box<dyn DatabaseDriver>> {
        self.get(db_type).map(|f| f.create())
    }
}