Skip to main content

aegis_client/
lib.rs

1//! Aegis Client - Database Client SDK
2//!
3//! Native Rust client library for connecting to Aegis database instances.
4//! Provides async/await API, connection pooling, and automatic failover.
5//!
6//! Key Features:
7//! - Async-first API with tokio integration
8//! - Connection pooling and management
9//! - Automatic retry and failover
10//! - Query builder with type safety
11//! - Cluster-aware routing
12//!
13//! @version 0.1.0
14//! @author AutomataNexus Development Team
15
16pub 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
32/// The main client for interacting with Aegis databases.
33pub struct AegisClient {
34    #[allow(dead_code)]
35    config: ClientConfig,
36    pool: ConnectionPool,
37}
38
39impl AegisClient {
40    /// Create a new client with the given configuration.
41    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    /// Connect to a database with default configuration.
49    pub async fn connect(url: &str) -> Result<Self, ClientError> {
50        let config = ClientConfig::from_url(url)?;
51        Self::new(config).await
52    }
53
54    /// Execute a query and return results.
55    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    /// Execute a query with parameters.
61    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    /// Execute a statement (INSERT, UPDATE, DELETE).
71    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    /// Execute a statement with parameters.
77    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    /// Start a new transaction.
87    pub async fn begin(&self) -> Result<Transaction, ClientError> {
88        let conn = self.pool.get().await?;
89        Transaction::begin(conn).await
90    }
91
92    /// Create a query builder.
93    pub fn query_builder(&self) -> QueryBuilder {
94        QueryBuilder::new()
95    }
96
97    /// Get connection pool statistics.
98    pub fn pool_stats(&self) -> pool::PoolStats {
99        self.pool.stats()
100    }
101
102    /// Check if the client is connected.
103    pub async fn is_connected(&self) -> bool {
104        self.pool.is_healthy().await
105    }
106
107    /// Close all connections.
108    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}