reinhardt-db 0.1.1

Django-style database layer for Reinhardt framework
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
//! # Database Engine
//!
//! SQLAlchemy-inspired database engine with connection pooling.
//!
//! This module provides a high-level database engine abstraction with built-in
//! connection pooling using SQLx's `AnyPool`. The pooling configuration allows
//! fine-grained control over connection lifecycle, timeouts, and pool sizes.
//!
//! ## Connection Pooling
//!
//! The engine uses SQLx's connection pooling with the following configurable parameters:
//! - `pool_min_size`: Minimum number of connections in the pool
//! - `pool_max_size`: Maximum number of connections in the pool
//! - `pool_timeout`: Timeout for acquiring a connection from the pool
//! - `pool_idle_timeout`: Maximum idle time before a connection is closed
//! - `pool_max_lifetime`: Maximum lifetime of a connection before it's closed
//!
//! ## Examples
//!
//! **Note:** `Engine` uses `sqlx::Any` which requires database-specific feature flags.
//! For simpler usage in tests and applications, use `DatabaseEngine` instead.
//!
//! ```rust,no_run
//! use reinhardt_db::orm::engine::{EngineConfig, create_engine_with_config};
//!
//! # tokio::runtime::Runtime::new().unwrap().block_on(async {
//! // Create engine with custom pool configuration
//! let config = EngineConfig::new("postgres://localhost/mydb")
//!     .with_pool_size(5, 20)  // Min: 5, Max: 20
//!     .with_timeout(30)       // 30 seconds connection timeout
//!     .with_idle_timeout(Some(600))    // 10 minutes idle timeout
//!     .with_max_lifetime(Some(1800));  // 30 minutes max lifetime
//!
//! let engine = create_engine_with_config(config).await.unwrap();
//! # });
//! ```
//!
//! For a simpler API that works in doctests, see `DatabaseEngine` below.
//!
//! This module is inspired by SQLAlchemy's engine implementation
//! Copyright 2005-2025 SQLAlchemy authors and contributors
//! Licensed under MIT License. See THIRD-PARTY-NOTICES for details.

use crate::backends::{DatabaseError, DatabaseType, Row as DbRow, connection::DatabaseConnection};
use sqlx::{Any, AnyPool, pool::PoolOptions};
use std::time::Duration;

/// Database engine configuration
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EngineConfig {
	/// Database URL (e.g., "sqlite::memory:", "postgres://...")
	pub url: String,

	/// Connection pool size (min)
	pub pool_min_size: u32,

	/// Connection pool size (max)
	pub pool_max_size: u32,

	/// Connection timeout in seconds
	pub pool_timeout: u64,

	/// Maximum idle time for a connection in seconds (None = no limit)
	pub pool_idle_timeout: Option<u64>,

	/// Maximum lifetime for a connection in seconds (None = no limit)
	pub pool_max_lifetime: Option<u64>,

	/// Enable echo (log all SQL)
	pub echo: bool,

	/// Enable query result caching
	pub query_cache_size: usize,
}

impl Default for EngineConfig {
	fn default() -> Self {
		Self {
			url: "sqlite::memory:".to_string(),
			pool_min_size: 1,
			pool_max_size: 10,
			pool_timeout: 30,
			pool_idle_timeout: Some(600),  // 10 minutes
			pool_max_lifetime: Some(1800), // 30 minutes
			echo: false,
			query_cache_size: 500,
		}
	}
}

impl EngineConfig {
	/// Create new config with URL
	pub fn new(url: impl Into<String>) -> Self {
		Self {
			url: url.into(),
			..Default::default()
		}
	}
	/// Set pool sizes
	pub fn with_pool_size(mut self, min: u32, max: u32) -> Self {
		self.pool_min_size = min;
		self.pool_max_size = max;
		self
	}

	/// Set connection timeout in seconds
	pub fn with_timeout(mut self, timeout: u64) -> Self {
		self.pool_timeout = timeout;
		self
	}

	/// Set idle timeout in seconds (None = no limit)
	pub fn with_idle_timeout(mut self, timeout: Option<u64>) -> Self {
		self.pool_idle_timeout = timeout;
		self
	}

	/// Set max lifetime in seconds (None = no limit)
	pub fn with_max_lifetime(mut self, lifetime: Option<u64>) -> Self {
		self.pool_max_lifetime = lifetime;
		self
	}

	/// Enable SQL echo
	pub fn with_echo(mut self, echo: bool) -> Self {
		self.echo = echo;
		self
	}
	/// Set query cache size
	pub fn with_cache_size(mut self, size: usize) -> Self {
		self.query_cache_size = size;
		self
	}
}

/// Database engine - manages connections and execution
pub struct Engine {
	pool: AnyPool,
	config: EngineConfig,
}

impl Engine {
	/// Create a new engine from config
	///
	pub async fn from_config(config: EngineConfig) -> Result<Self, sqlx::Error> {
		let mut pool_options = PoolOptions::<Any>::new()
			.min_connections(config.pool_min_size)
			.max_connections(config.pool_max_size)
			.acquire_timeout(Duration::from_secs(config.pool_timeout));

		// Set idle timeout if specified
		if let Some(idle_timeout) = config.pool_idle_timeout {
			pool_options = pool_options.idle_timeout(Duration::from_secs(idle_timeout));
		}

		// Set max lifetime if specified
		if let Some(max_lifetime) = config.pool_max_lifetime {
			pool_options = pool_options.max_lifetime(Duration::from_secs(max_lifetime));
		}

		let pool = pool_options.connect(&config.url).await?;

		Ok(Self { pool, config })
	}
	/// Create a new engine from URL
	///
	/// # Examples
	///
	/// ```ignore
	/// use reinhardt_db::orm::Engine;
	///
	/// # tokio::runtime::Runtime::new().unwrap().block_on(async {
	/// let engine = Engine::new("sqlite::memory:").await.unwrap();
	/// # });
	/// ```
	///
	/// **Note:** This requires the appropriate sqlx driver feature to be enabled.
	/// For simpler usage, see `DatabaseEngine::from_sqlite` or other database-specific constructors.
	pub async fn new(url: impl Into<String>) -> Result<Self, sqlx::Error> {
		Self::from_config(EngineConfig::new(url)).await
	}
	/// Get a connection from the pool
	///
	pub async fn connect(&self) -> Result<sqlx::pool::PoolConnection<Any>, sqlx::Error> {
		self.pool.acquire().await
	}
	/// Execute a SQL statement
	pub async fn execute(&self, sql: &str) -> Result<u64, sqlx::Error> {
		if self.config.echo {
			println!("SQL: {}", sql);
		}

		let result = sqlx::query(sql).execute(&self.pool).await?;
		Ok(result.rows_affected())
	}
	/// Execute a query and return results
	///
	pub async fn fetch_all(&self, sql: &str) -> Result<Vec<sqlx::any::AnyRow>, sqlx::Error> {
		if self.config.echo {
			println!("SQL: {}", sql);
		}

		sqlx::query(sql).fetch_all(&self.pool).await
	}
	/// Execute a query and return a single result
	///
	pub async fn fetch_one(&self, sql: &str) -> Result<sqlx::any::AnyRow, sqlx::Error> {
		if self.config.echo {
			println!("SQL: {}", sql);
		}

		sqlx::query(sql).fetch_one(&self.pool).await
	}
	/// Execute a query and return an optional result
	///
	pub async fn fetch_optional(
		&self,
		sql: &str,
	) -> Result<Option<sqlx::any::AnyRow>, sqlx::Error> {
		if self.config.echo {
			println!("SQL: {}", sql);
		}

		sqlx::query(sql).fetch_optional(&self.pool).await
	}
	/// Begin a transaction
	///
	pub async fn begin(&self) -> Result<sqlx::Transaction<'_, Any>, sqlx::Error> {
		self.pool.begin().await
	}
	/// Get the engine configuration
	///
	pub fn config(&self) -> &EngineConfig {
		&self.config
	}
	/// Get reference to the connection pool
	///
	pub fn pool(&self) -> &AnyPool {
		&self.pool
	}
	/// Clone this engine (shares the connection pool)
	///
	pub fn clone_ref(&self) -> Self {
		Self {
			pool: self.pool.clone(),
			config: self.config.clone(),
		}
	}
}
/// Create a new database engine
///
pub async fn create_engine(url: impl Into<String>) -> Result<Engine, sqlx::Error> {
	Engine::new(url).await
}
/// Create a new database engine with configuration
///
pub async fn create_engine_with_config(config: EngineConfig) -> Result<Engine, sqlx::Error> {
	Engine::from_config(config).await
}

/// Database engine using DatabaseConnection (multi-database support)
pub struct DatabaseEngine {
	connection: DatabaseConnection,
	db_type: DatabaseType,
	config: EngineConfig,
}

impl DatabaseEngine {
	/// Create a new database engine from DatabaseConnection
	///
	/// # Examples
	///
	/// ```ignore
	/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
	/// use reinhardt_db::orm::engine::DatabaseEngine;
	/// use reinhardt_db::reinhardt_db::backends::drivers::{DatabaseConnection, DatabaseType};
	///
	/// let connection = DatabaseConnection::connect("postgres://localhost/mydb").await?;
	/// let engine = DatabaseEngine::new(connection, DatabaseType::Postgres);
	/// // Engine is ready to execute queries
	/// # Ok(())
	/// # }
	/// ```
	pub fn new(connection: DatabaseConnection, db_type: DatabaseType) -> Self {
		Self {
			connection,
			db_type,
			config: EngineConfig::default(),
		}
	}

	/// Create a new database engine with configuration
	pub fn with_config(
		connection: DatabaseConnection,
		db_type: DatabaseType,
		config: EngineConfig,
	) -> Self {
		Self {
			connection,
			db_type,
			config,
		}
	}

	/// Create a new PostgreSQL engine
	///
	/// # Examples
	///
	/// ```
	/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
	/// use reinhardt_db::orm::connection::DatabaseConnection;
	///
	/// // For doctest purposes, using mock connection (feature-gated methods not available)
	/// // In production with 'postgres' feature: DatabaseEngine::from_postgres(url).await
	/// let connection = DatabaseConnection::connect("postgres://localhost/mydb").await?;
	/// assert_eq!(connection.backend(), reinhardt_db::orm::connection::DatabaseBackend::Postgres);
	/// # Ok(())
	/// # }
	/// # tokio::runtime::Runtime::new().unwrap().block_on(example());
	/// ```
	#[cfg(feature = "postgres")]
	pub async fn from_postgres(url: &str) -> Result<Self, DatabaseError> {
		let connection = DatabaseConnection::connect_postgres(url).await?;
		Ok(Self::new(connection, DatabaseType::Postgres))
	}

	/// Create a new SQLite engine
	///
	/// # Examples
	///
	/// ```
	/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
	/// use reinhardt_db::orm::connection::DatabaseConnection;
	///
	/// // For doctest purposes, using mock connection (feature-gated methods not available)
	/// // In production with 'sqlite' feature: DatabaseEngine::from_sqlite(":memory:").await
	/// let connection = DatabaseConnection::connect(":memory:").await?;
	/// assert_eq!(connection.backend(), reinhardt_db::orm::connection::DatabaseBackend::Postgres);
	/// # Ok(())
	/// # }
	/// # tokio::runtime::Runtime::new().unwrap().block_on(example());
	/// ```
	#[cfg(feature = "sqlite")]
	pub async fn from_sqlite(url: &str) -> Result<Self, DatabaseError> {
		let connection = DatabaseConnection::connect_sqlite(url).await?;
		Ok(Self::new(connection, DatabaseType::Sqlite))
	}

	/// Create a new MySQL engine
	#[cfg(feature = "mysql")]
	pub async fn from_mysql(url: &str) -> Result<Self, DatabaseError> {
		let connection = DatabaseConnection::connect_mysql(url).await?;
		Ok(Self::new(connection, DatabaseType::Mysql))
	}

	/// Get reference to the database connection
	pub fn connection(&self) -> &DatabaseConnection {
		&self.connection
	}

	/// Get the database type
	pub fn database_type(&self) -> DatabaseType {
		self.db_type
	}

	/// Get the engine configuration
	pub fn config(&self) -> &EngineConfig {
		&self.config
	}

	/// Execute a SQL statement
	///
	/// # Examples
	///
	/// ```no_run
	/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
	/// use reinhardt_db::orm::connection::DatabaseConnection;
	///
	/// // Create mock connection (URL is ignored in current mock implementation)
	/// let connection = DatabaseConnection::connect("sqlite::memory:").await?;
	///
	/// // Execute SQL statements (mock always returns 0)
	/// let rows_affected = connection.execute("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)", vec![]).await?;
	/// assert_eq!(rows_affected, 0);
	///
	/// let rows_affected = connection.execute("INSERT INTO users (id, name) VALUES (1, 'Alice')", vec![]).await?;
	/// assert_eq!(rows_affected, 0);
	///
	/// // Query returns empty vec in mock
	/// let rows = connection.query("SELECT * FROM users", vec![]).await?;
	/// assert_eq!(rows.len(), 0);
	/// # Ok(())
	/// # }
	/// # tokio::runtime::Runtime::new().unwrap().block_on(example());
	/// ```
	pub async fn execute(&self, sql: &str) -> Result<u64, DatabaseError> {
		if self.config.echo {
			println!("SQL: {}", sql);
		}

		let result = self.connection.execute(sql, vec![]).await?;
		Ok(result.rows_affected)
	}

	/// Execute a query and return all results
	pub async fn fetch_all(&self, sql: &str) -> Result<Vec<DbRow>, DatabaseError> {
		if self.config.echo {
			println!("SQL: {}", sql);
		}

		self.connection.fetch_all(sql, vec![]).await
	}

	/// Execute a query and return a single result
	pub async fn fetch_one(&self, sql: &str) -> Result<DbRow, DatabaseError> {
		if self.config.echo {
			println!("SQL: {}", sql);
		}

		self.connection.fetch_one(sql, vec![]).await
	}

	/// Execute a query and return an optional result
	pub async fn fetch_optional(&self, sql: &str) -> Result<Option<DbRow>, DatabaseError> {
		if self.config.echo {
			println!("SQL: {}", sql);
		}

		let rows = self.connection.fetch_all(sql, vec![]).await?;
		Ok(rows.into_iter().next())
	}

	/// Clone this engine (shares the database connection)
	pub fn clone_ref(&self) -> Self {
		Self {
			connection: self.connection.clone(),
			db_type: self.db_type,
			config: self.config.clone(),
		}
	}
}

/// Create a new database engine from PostgreSQL URL
#[cfg(feature = "postgres")]
pub async fn create_database_engine_postgres(url: &str) -> Result<DatabaseEngine, DatabaseError> {
	DatabaseEngine::from_postgres(url).await
}

/// Create a new database engine from SQLite URL
#[cfg(feature = "sqlite")]
pub async fn create_database_engine_sqlite(url: &str) -> Result<DatabaseEngine, DatabaseError> {
	DatabaseEngine::from_sqlite(url).await
}

/// Create a new database engine from MySQL URL
#[cfg(feature = "mysql")]
pub async fn create_database_engine_mysql(url: &str) -> Result<DatabaseEngine, DatabaseError> {
	DatabaseEngine::from_mysql(url).await
}

#[cfg(test)]
mod tests {
	use sqlx::sqlite::{SqlitePool, SqlitePoolOptions};

	// Helper to create SQLite pool for tests
	async fn create_test_pool() -> SqlitePool {
		SqlitePoolOptions::new()
			.min_connections(0)
			.max_connections(5)
			.connect("sqlite::memory:")
			.await
			.expect("Failed to create SQLite pool")
	}

	#[tokio::test]
	async fn test_engine_creation() {
		let pool = create_test_pool().await;
		// Verify pool was created successfully by checking it's not closed
		assert!(!pool.is_closed());
		pool.close().await;
	}

	#[tokio::test]
	async fn test_engine_execute() {
		let pool = create_test_pool().await;

		let result = sqlx::query("CREATE TABLE test (id INTEGER PRIMARY KEY, name TEXT)")
			.execute(&pool)
			.await;

		assert!(result.is_ok());
		pool.close().await;
	}

	#[tokio::test]
	async fn test_engine_query() {
		let pool = create_test_pool().await;

		sqlx::query("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)")
			.execute(&pool)
			.await
			.unwrap();

		sqlx::query("INSERT INTO users (id, name) VALUES (1, 'Alice')")
			.execute(&pool)
			.await
			.unwrap();

		let rows = sqlx::query("SELECT * FROM users")
			.fetch_all(&pool)
			.await
			.expect("Query failed");

		assert_eq!(rows.len(), 1);
		pool.close().await;
	}

	#[tokio::test]
	async fn test_engine_with_config() {
		let pool = SqlitePoolOptions::new()
			.min_connections(2)
			.max_connections(5)
			.connect("sqlite::memory:")
			.await
			.expect("Failed to create engine with config");

		// Verify pool was created with correct config
		assert!(!pool.is_closed());
		pool.close().await;
	}

	#[tokio::test]
	async fn test_transaction() {
		let pool = create_test_pool().await;

		sqlx::query("CREATE TABLE accounts (id INTEGER PRIMARY KEY, balance INTEGER)")
			.execute(&pool)
			.await
			.unwrap();

		let mut tx = pool.begin().await.expect("Failed to begin transaction");

		sqlx::query("INSERT INTO accounts (id, balance) VALUES (1, 100)")
			.execute(&mut *tx)
			.await
			.unwrap();

		tx.commit().await.expect("Failed to commit");

		let rows = sqlx::query("SELECT * FROM accounts")
			.fetch_all(&pool)
			.await
			.unwrap();
		assert_eq!(rows.len(), 1);
		pool.close().await;
	}
}