elif-orm 0.7.1

Production-ready ORM with migrations, database services, connection pooling, and query builder
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
//! Core Database Backend Traits
//!
//! This module defines the core traits and types for database backend abstraction.
//! These traits abstract away database-specific implementations and provide a unified
//! interface for the ORM to work with different database systems.

use crate::error::{OrmError, OrmResult};
use async_trait::async_trait;
use serde_json::Value as JsonValue;
use std::collections::HashMap;
use std::sync::Arc;

/// Abstract database connection trait
#[async_trait]
pub trait DatabaseConnection: Send + Sync {
    /// Execute a query and return affected rows count
    async fn execute(&mut self, sql: &str, params: &[DatabaseValue]) -> OrmResult<u64>;

    /// Execute a query and return the result rows
    async fn fetch_all(
        &mut self,
        sql: &str,
        params: &[DatabaseValue],
    ) -> OrmResult<Vec<Box<dyn DatabaseRow>>>;

    /// Execute a query and return the first result row
    async fn fetch_optional(
        &mut self,
        sql: &str,
        params: &[DatabaseValue],
    ) -> OrmResult<Option<Box<dyn DatabaseRow>>>;

    /// Begin a transaction
    async fn begin_transaction(&mut self) -> OrmResult<Box<dyn DatabaseTransaction>>;

    /// Close the connection
    async fn close(&mut self) -> OrmResult<()>;
}

/// Abstract database transaction trait
#[async_trait]
pub trait DatabaseTransaction: Send + Sync {
    /// Execute a query within the transaction
    async fn execute(&mut self, sql: &str, params: &[DatabaseValue]) -> OrmResult<u64>;

    /// Execute a query and return result rows within the transaction
    async fn fetch_all(
        &mut self,
        sql: &str,
        params: &[DatabaseValue],
    ) -> OrmResult<Vec<Box<dyn DatabaseRow>>>;

    /// Execute a query and return the first result row within the transaction
    async fn fetch_optional(
        &mut self,
        sql: &str,
        params: &[DatabaseValue],
    ) -> OrmResult<Option<Box<dyn DatabaseRow>>>;

    /// Commit the transaction
    async fn commit(self: Box<Self>) -> OrmResult<()>;

    /// Rollback the transaction
    async fn rollback(self: Box<Self>) -> OrmResult<()>;
}

/// Abstract database connection pool trait
#[async_trait]
pub trait DatabasePool: Send + Sync {
    /// Acquire a connection from the pool
    async fn acquire(&self) -> OrmResult<Box<dyn DatabaseConnection>>;

    /// Begin a transaction from the pool
    async fn begin_transaction(&self) -> OrmResult<Box<dyn DatabaseTransaction>>;

    /// Execute a query directly on the pool
    async fn execute(&self, sql: &str, params: &[DatabaseValue]) -> OrmResult<u64>;

    /// Execute a query and return result rows directly on the pool
    async fn fetch_all(
        &self,
        sql: &str,
        params: &[DatabaseValue],
    ) -> OrmResult<Vec<Box<dyn DatabaseRow>>>;

    /// Execute a query and return the first result row directly on the pool
    async fn fetch_optional(
        &self,
        sql: &str,
        params: &[DatabaseValue],
    ) -> OrmResult<Option<Box<dyn DatabaseRow>>>;

    /// Close the pool
    async fn close(&self) -> OrmResult<()>;

    /// Get pool statistics
    fn stats(&self) -> DatabasePoolStats;

    /// Perform a health check on the pool
    async fn health_check(&self) -> OrmResult<std::time::Duration>;
}

/// Database pool statistics
#[derive(Debug, Clone)]
pub struct DatabasePoolStats {
    pub total_connections: u32,
    pub idle_connections: u32,
    pub active_connections: u32,
}

/// Abstract database row trait
pub trait DatabaseRow: Send + Sync {
    /// Get a column value by index
    fn get_by_index(&self, index: usize) -> OrmResult<DatabaseValue>;

    /// Get a column value by name
    fn get_by_name(&self, name: &str) -> OrmResult<DatabaseValue>;

    /// Get column count
    fn column_count(&self) -> usize;

    /// Get column names
    fn column_names(&self) -> Vec<String>;

    /// Convert row to JSON value
    fn to_json(&self) -> OrmResult<JsonValue>;

    /// Convert row to HashMap
    fn to_map(&self) -> OrmResult<HashMap<String, DatabaseValue>>;
}

/// Extension trait for DatabaseRow to support typed column access for models
pub trait DatabaseRowExt {
    /// Get a typed value from a column (for model deserialization)
    fn get<T>(&self, column: &str) -> Result<T, crate::error::ModelError>
    where
        T: for<'de> serde::Deserialize<'de>;

    /// Try to get an optional typed value from a column
    fn try_get<T>(&self, column: &str) -> Result<Option<T>, crate::error::ModelError>
    where
        T: for<'de> serde::Deserialize<'de>;
}

impl<R: DatabaseRow + ?Sized> DatabaseRowExt for R {
    fn get<T>(&self, column: &str) -> Result<T, crate::error::ModelError>
    where
        T: for<'de> serde::Deserialize<'de>,
    {
        let db_value = self.get_by_name(column)?;

        let json_value = db_value.to_json();
        serde_json::from_value(json_value).map_err(|e| {
            crate::error::ModelError::Serialization(format!(
                "Failed to deserialize column '{}': {}",
                column, e
            ))
        })
    }

    fn try_get<T>(&self, column: &str) -> Result<Option<T>, crate::error::ModelError>
    where
        T: for<'de> serde::Deserialize<'de>,
    {
        match self.get_by_name(column) {
            Ok(db_value) => {
                if db_value.is_null() {
                    Ok(None)
                } else {
                    let json_value = db_value.to_json();
                    let parsed: T = serde_json::from_value(json_value).map_err(|e| {
                        crate::error::ModelError::Serialization(format!(
                            "Failed to deserialize column '{}': {}",
                            column, e
                        ))
                    })?;
                    Ok(Some(parsed))
                }
            }
            Err(crate::error::ModelError::ColumnNotFound(_)) => Ok(None),
            Err(e) => Err(e), // Preserve the original error type and information
        }
    }
}

/// Database value enumeration for type-safe parameter binding
#[derive(Debug, Clone, PartialEq)]
pub enum DatabaseValue {
    Null,
    Bool(bool),
    Int32(i32),
    Int64(i64),
    Float32(f32),
    Float64(f64),
    String(String),
    Bytes(Vec<u8>),
    Uuid(uuid::Uuid),
    DateTime(chrono::DateTime<chrono::Utc>),
    Date(chrono::NaiveDate),
    Time(chrono::NaiveTime),
    Json(JsonValue),
    Array(Vec<DatabaseValue>),
}

impl DatabaseValue {
    /// Check if the value is null
    pub fn is_null(&self) -> bool {
        matches!(self, DatabaseValue::Null)
    }

    /// Convert to JSON value
    pub fn to_json(&self) -> JsonValue {
        match self {
            DatabaseValue::Null => JsonValue::Null,
            DatabaseValue::Bool(b) => JsonValue::Bool(*b),
            DatabaseValue::Int32(i) => JsonValue::Number(serde_json::Number::from(*i)),
            DatabaseValue::Int64(i) => JsonValue::Number(serde_json::Number::from(*i)),
            DatabaseValue::Float32(f) => JsonValue::Number(
                serde_json::Number::from_f64(*f as f64)
                    .unwrap_or_else(|| serde_json::Number::from(0)),
            ),
            DatabaseValue::Float64(f) => serde_json::Number::from_f64(*f)
                .map(JsonValue::Number)
                .unwrap_or(JsonValue::Null),
            DatabaseValue::String(s) => JsonValue::String(s.clone()),
            DatabaseValue::Bytes(b) => JsonValue::Array(
                b.iter()
                    .map(|&x| JsonValue::Number(serde_json::Number::from(x)))
                    .collect(),
            ),
            DatabaseValue::Uuid(u) => JsonValue::String(u.to_string()),
            DatabaseValue::DateTime(dt) => JsonValue::String(dt.to_rfc3339()),
            DatabaseValue::Date(d) => JsonValue::String(d.to_string()),
            DatabaseValue::Time(t) => JsonValue::String(t.to_string()),
            DatabaseValue::Json(j) => j.clone(),
            DatabaseValue::Array(arr) => {
                JsonValue::Array(arr.iter().map(|v| v.to_json()).collect())
            }
        }
    }

    /// Create DatabaseValue from JSON value
    pub fn from_json(json: JsonValue) -> Self {
        match json {
            JsonValue::Null => DatabaseValue::Null,
            JsonValue::Bool(b) => DatabaseValue::Bool(b),
            JsonValue::Number(n) => {
                if let Some(i) = n.as_i64() {
                    if i >= i32::MIN as i64 && i <= i32::MAX as i64 {
                        DatabaseValue::Int32(i as i32)
                    } else {
                        DatabaseValue::Int64(i)
                    }
                } else if let Some(f) = n.as_f64() {
                    DatabaseValue::Float64(f)
                } else {
                    DatabaseValue::Null
                }
            }
            JsonValue::String(s) => {
                // Try to parse as UUID first
                if let Ok(uuid) = uuid::Uuid::parse_str(&s) {
                    DatabaseValue::Uuid(uuid)
                } else if let Ok(dt) = chrono::DateTime::parse_from_rfc3339(&s) {
                    DatabaseValue::DateTime(dt.with_timezone(&chrono::Utc))
                } else {
                    DatabaseValue::String(s)
                }
            }
            JsonValue::Array(arr) => {
                let db_values: Vec<DatabaseValue> =
                    arr.into_iter().map(DatabaseValue::from_json).collect();
                DatabaseValue::Array(db_values)
            }
            JsonValue::Object(_) => DatabaseValue::Json(json),
        }
    }
}

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

impl From<i32> for DatabaseValue {
    fn from(value: i32) -> Self {
        DatabaseValue::Int32(value)
    }
}

impl From<i64> for DatabaseValue {
    fn from(value: i64) -> Self {
        DatabaseValue::Int64(value)
    }
}

impl From<f32> for DatabaseValue {
    fn from(value: f32) -> Self {
        DatabaseValue::Float32(value)
    }
}

impl From<f64> for DatabaseValue {
    fn from(value: f64) -> Self {
        DatabaseValue::Float64(value)
    }
}

impl From<String> for DatabaseValue {
    fn from(value: String) -> Self {
        DatabaseValue::String(value)
    }
}

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

impl From<Vec<u8>> for DatabaseValue {
    fn from(value: Vec<u8>) -> Self {
        DatabaseValue::Bytes(value)
    }
}

impl From<uuid::Uuid> for DatabaseValue {
    fn from(value: uuid::Uuid) -> Self {
        DatabaseValue::Uuid(value)
    }
}

impl From<chrono::DateTime<chrono::Utc>> for DatabaseValue {
    fn from(value: chrono::DateTime<chrono::Utc>) -> Self {
        DatabaseValue::DateTime(value)
    }
}

impl From<chrono::NaiveDate> for DatabaseValue {
    fn from(value: chrono::NaiveDate) -> Self {
        DatabaseValue::Date(value)
    }
}

impl From<chrono::NaiveTime> for DatabaseValue {
    fn from(value: chrono::NaiveTime) -> Self {
        DatabaseValue::Time(value)
    }
}

impl From<JsonValue> for DatabaseValue {
    fn from(value: JsonValue) -> Self {
        DatabaseValue::Json(value)
    }
}

impl<T> From<Option<T>> for DatabaseValue
where
    T: Into<DatabaseValue>,
{
    fn from(value: Option<T>) -> Self {
        match value {
            Some(v) => v.into(),
            None => DatabaseValue::Null,
        }
    }
}

/// SQL dialect enumeration for generating database-specific SQL
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SqlDialect {
    PostgreSQL,
    MySQL,
    SQLite,
}

impl SqlDialect {
    /// Get the parameter placeholder style for this dialect
    pub fn parameter_placeholder(&self, index: usize) -> String {
        match self {
            SqlDialect::PostgreSQL => format!("${}", index + 1),
            SqlDialect::MySQL | SqlDialect::SQLite => "?".to_string(),
        }
    }

    /// Get the quote character for identifiers in this dialect
    pub fn identifier_quote(&self) -> char {
        match self {
            SqlDialect::PostgreSQL => '"',
            SqlDialect::MySQL => '`',
            SqlDialect::SQLite => '"',
        }
    }

    /// Check if this dialect supports boolean types
    pub fn supports_boolean(&self) -> bool {
        match self {
            SqlDialect::PostgreSQL | SqlDialect::SQLite => true,
            SqlDialect::MySQL => false,
        }
    }

    /// Check if this dialect supports JSON types
    pub fn supports_json(&self) -> bool {
        match self {
            SqlDialect::PostgreSQL | SqlDialect::MySQL => true,
            SqlDialect::SQLite => false,
        }
    }

    /// Get the current timestamp function for this dialect
    pub fn current_timestamp(&self) -> &'static str {
        match self {
            SqlDialect::PostgreSQL => "NOW()",
            SqlDialect::MySQL => "CURRENT_TIMESTAMP",
            SqlDialect::SQLite => "datetime('now')",
        }
    }

    /// Get the auto-increment column definition for this dialect
    pub fn auto_increment(&self) -> &'static str {
        match self {
            SqlDialect::PostgreSQL => "SERIAL",
            SqlDialect::MySQL => "AUTO_INCREMENT",
            SqlDialect::SQLite => "AUTOINCREMENT",
        }
    }
}

/// Database backend trait that provides database-specific implementations
#[async_trait]
pub trait DatabaseBackend: Send + Sync {
    /// Create a connection pool from a database URL
    async fn create_pool(
        &self,
        database_url: &str,
        config: DatabasePoolConfig,
    ) -> OrmResult<Arc<dyn DatabasePool>>;

    /// Get the SQL dialect used by this backend
    fn sql_dialect(&self) -> SqlDialect;

    /// Get the backend type
    fn backend_type(&self) -> crate::backends::DatabaseBackendType;

    /// Validate a database URL for this backend
    fn validate_database_url(&self, url: &str) -> OrmResult<()>;

    /// Parse connection parameters from a database URL
    fn parse_database_url(&self, url: &str) -> OrmResult<DatabaseConnectionConfig>;
}

/// Database pool configuration
#[derive(Debug, Clone)]
pub struct DatabasePoolConfig {
    pub max_connections: u32,
    pub min_connections: u32,
    pub acquire_timeout_seconds: u64,
    pub idle_timeout_seconds: Option<u64>,
    pub max_lifetime_seconds: Option<u64>,
    pub test_before_acquire: bool,
}

impl Default for DatabasePoolConfig {
    fn default() -> Self {
        Self {
            max_connections: 10,
            min_connections: 1,
            acquire_timeout_seconds: 30,
            idle_timeout_seconds: Some(600),  // 10 minutes
            max_lifetime_seconds: Some(1800), // 30 minutes
            test_before_acquire: true,
        }
    }
}

/// Database connection configuration parsed from URL
#[derive(Debug, Clone)]
pub struct DatabaseConnectionConfig {
    pub host: String,
    pub port: u16,
    pub database: String,
    pub username: Option<String>,
    pub password: Option<String>,
    pub ssl_mode: Option<String>,
    pub additional_params: HashMap<String, String>,
}

/// Database backend registry for managing multiple backend implementations
pub struct DatabaseBackendRegistry {
    backends: HashMap<crate::backends::DatabaseBackendType, Arc<dyn DatabaseBackend>>,
}

impl DatabaseBackendRegistry {
    /// Create a new backend registry
    pub fn new() -> Self {
        Self {
            backends: HashMap::new(),
        }
    }

    /// Register a database backend
    pub fn register(
        &mut self,
        backend_type: crate::backends::DatabaseBackendType,
        backend: Arc<dyn DatabaseBackend>,
    ) {
        self.backends.insert(backend_type, backend);
    }

    /// Get a database backend by type
    pub fn get(
        &self,
        backend_type: &crate::backends::DatabaseBackendType,
    ) -> Option<Arc<dyn DatabaseBackend>> {
        self.backends.get(backend_type).cloned()
    }

    /// Create a connection pool using the appropriate backend for the given URL
    pub async fn create_pool(
        &self,
        database_url: &str,
        config: DatabasePoolConfig,
    ) -> OrmResult<Arc<dyn DatabasePool>> {
        let backend_type = self.detect_backend_from_url(database_url)?;
        let backend = self.get(&backend_type).ok_or_else(|| {
            OrmError::Connection(format!("No backend registered for {}", backend_type))
        })?;

        backend.create_pool(database_url, config).await
    }

    /// Detect database backend type from URL
    fn detect_backend_from_url(
        &self,
        url: &str,
    ) -> OrmResult<crate::backends::DatabaseBackendType> {
        if url.starts_with("postgresql://") || url.starts_with("postgres://") {
            Ok(crate::backends::DatabaseBackendType::PostgreSQL)
        } else if url.starts_with("mysql://") {
            Ok(crate::backends::DatabaseBackendType::MySQL)
        } else if url.starts_with("sqlite://") || url.starts_with("file:") {
            Ok(crate::backends::DatabaseBackendType::SQLite)
        } else {
            Err(OrmError::Connection(format!(
                "Unable to detect database backend from URL: {}",
                url
            )))
        }
    }

    /// List all registered backend types
    pub fn registered_backends(&self) -> Vec<crate::backends::DatabaseBackendType> {
        self.backends.keys().cloned().collect()
    }
}

impl Default for DatabaseBackendRegistry {
    fn default() -> Self {
        Self::new()
    }
}