1pub mod config;
17pub mod connection;
18pub mod error;
19pub mod pool;
20pub mod query;
21pub mod result;
22pub mod transaction;
23
24pub use config::{ClientConfig, ConnectionConfig};
25pub use connection::Connection;
26pub use error::ClientError;
27pub use pool::ConnectionPool;
28pub use query::{Query, QueryBuilder};
29pub use result::{QueryResult, Row, Value};
30pub use transaction::Transaction;
31
32pub struct AegisClient {
34 #[allow(dead_code)]
35 config: ClientConfig,
36 pool: ConnectionPool,
37}
38
39impl AegisClient {
40 pub async fn new(config: ClientConfig) -> Result<Self, ClientError> {
42 let pool =
43 ConnectionPool::with_connection_config(config.pool.clone(), config.connection.clone())
44 .await?;
45 Ok(Self { config, pool })
46 }
47
48 pub async fn connect(url: &str) -> Result<Self, ClientError> {
50 let config = ClientConfig::from_url(url)?;
51 Self::new(config).await
52 }
53
54 pub async fn query(&self, sql: &str) -> Result<QueryResult, ClientError> {
56 let conn = self.pool.get().await?;
57 conn.query(sql).await
58 }
59
60 pub async fn query_with_params(
62 &self,
63 sql: &str,
64 params: Vec<Value>,
65 ) -> Result<QueryResult, ClientError> {
66 let conn = self.pool.get().await?;
67 conn.query_with_params(sql, params).await
68 }
69
70 pub async fn execute(&self, sql: &str) -> Result<u64, ClientError> {
72 let conn = self.pool.get().await?;
73 conn.execute(sql).await
74 }
75
76 pub async fn execute_with_params(
78 &self,
79 sql: &str,
80 params: Vec<Value>,
81 ) -> Result<u64, ClientError> {
82 let conn = self.pool.get().await?;
83 conn.execute_with_params(sql, params).await
84 }
85
86 pub async fn begin(&self) -> Result<Transaction, ClientError> {
88 let conn = self.pool.get().await?;
89 Transaction::begin(conn).await
90 }
91
92 pub fn query_builder(&self) -> QueryBuilder {
94 QueryBuilder::new()
95 }
96
97 pub fn pool_stats(&self) -> pool::PoolStats {
99 self.pool.stats()
100 }
101
102 pub async fn is_connected(&self) -> bool {
104 self.pool.is_healthy().await
105 }
106
107 pub async fn close(&self) {
109 self.pool.close().await;
110 }
111}
112
113#[cfg(test)]
114mod tests {
115 use super::*;
116
117 #[test]
118 fn test_client_config_from_url() {
119 let config = ClientConfig::from_url("aegis://localhost:9090/testdb")
120 .expect("Should parse valid URL");
121 assert_eq!(config.connection.host, "localhost");
122 assert_eq!(config.connection.port, 9090);
123 assert_eq!(config.connection.database, "testdb");
124 }
125}