prax_query/connection/mod.rs
1//! Connection string parsing and multi-database configuration.
2//!
3//! This module provides utilities for parsing database connection URLs and
4//! managing configurations for multiple database backends.
5//!
6//! # Supported URL Formats
7//!
8//! ## PostgreSQL
9//! ```text
10//! postgres://user:password@host:port/database?options
11//! postgresql://user:password@host:port/database?options
12//! ```
13//!
14//! ## MySQL
15//! ```text
16//! mysql://user:password@host:port/database?options
17//! mariadb://user:password@host:port/database?options
18//! ```
19//!
20//! ## SQLite
21//! ```text
22//! sqlite://path/to/database.db?options
23//! sqlite::memory:
24//! file:path/to/database.db?options
25//! ```
26//!
27//! # Parsing Connection URLs
28//!
29//! ```rust
30//! use prax_query::ConnectionString;
31//!
32//! // PostgreSQL URL
33//! let conn = ConnectionString::parse("postgres://user:pass@localhost:5432/mydb").unwrap();
34//! assert_eq!(conn.host(), Some("localhost"));
35//! assert_eq!(conn.port(), Some(5432));
36//! assert_eq!(conn.database(), Some("mydb"));
37//!
38//! // MySQL URL
39//! let conn = ConnectionString::parse("mysql://user:pass@localhost:3306/mydb").unwrap();
40//!
41//! // SQLite URL (note: uses :// prefix)
42//! let conn = ConnectionString::parse("sqlite://./data.db").unwrap();
43//! ```
44//!
45//! # Driver Types
46//!
47//! ```rust
48//! use prax_query::Driver;
49//!
50//! // Default ports
51//! assert_eq!(Driver::Postgres.default_port(), Some(5432));
52//! assert_eq!(Driver::MySql.default_port(), Some(3306));
53//! assert_eq!(Driver::Sqlite.default_port(), None);
54//!
55//! // Parse from scheme
56//! assert_eq!(Driver::from_scheme("postgres").unwrap(), Driver::Postgres);
57//! assert_eq!(Driver::from_scheme("postgresql").unwrap(), Driver::Postgres);
58//! assert_eq!(Driver::from_scheme("mysql").unwrap(), Driver::MySql);
59//! assert_eq!(Driver::from_scheme("mariadb").unwrap(), Driver::MySql);
60//! assert_eq!(Driver::from_scheme("sqlite").unwrap(), Driver::Sqlite);
61//! ```
62//!
63//! # SSL Modes
64//!
65//! ```rust
66//! use prax_query::connection::SslMode;
67//!
68//! // Available SSL modes
69//! let mode = SslMode::Disable; // No SSL
70//! let mode = SslMode::Prefer; // Use SSL if available
71//! let mode = SslMode::Require; // Require SSL
72//! let mode = SslMode::VerifyCa; // Verify CA certificate
73//! let mode = SslMode::VerifyFull; // Verify CA and hostname
74//! ```
75
76mod config;
77mod env;
78mod options;
79mod parser;
80mod pool;
81
82pub use config::{DatabaseConfig, DatabaseConfigBuilder, MultiDatabaseConfig};
83pub use env::{EnvExpander, EnvSource};
84pub use options::{
85 ConnectionOptions, MySqlOptions, PoolOptions, PostgresOptions, SqliteOptions, SslConfig,
86 SslMode,
87};
88pub use parser::{ConnectionString, Driver, ParsedUrl};
89pub use pool::PoolConfig;
90
91use thiserror::Error;
92
93/// Errors that can occur during connection string parsing.
94#[derive(Error, Debug)]
95pub enum ConnectionError {
96 /// Invalid URL format.
97 #[error("Invalid connection URL: {0}")]
98 InvalidUrl(String),
99
100 /// Unknown database driver.
101 #[error("Unknown database driver: {0}")]
102 UnknownDriver(String),
103
104 /// Missing required field.
105 #[error("Missing required field: {0}")]
106 MissingField(String),
107
108 /// Invalid option value.
109 #[error("Invalid option '{key}': {message}")]
110 InvalidOption { key: String, message: String },
111
112 /// Environment variable not found.
113 #[error("Environment variable not found: {0}")]
114 EnvNotFound(String),
115
116 /// Invalid environment variable value.
117 #[error("Invalid environment variable '{name}': {message}")]
118 InvalidEnvValue { name: String, message: String },
119
120 /// Configuration error.
121 #[error("Configuration error: {0}")]
122 Config(String),
123}
124
125/// Result type for connection operations.
126pub type ConnectionResult<T> = Result<T, ConnectionError>;