1use super::auth_types::*;
5use super::builder::PgDriverBuilder;
6use super::connection::PgConnection;
7use super::pool;
8use super::rls::RlsContext;
9use super::types::*;
10
11pub struct PgDriver {
13 pub(super) connection: PgConnection,
14 pub(super) rls_context: Option<RlsContext>,
16}
17
18impl PgDriver {
19 pub fn new(connection: PgConnection) -> Self {
21 Self {
22 connection,
23 rls_context: None,
24 }
25 }
26
27 pub fn builder() -> PgDriverBuilder {
40 PgDriverBuilder::new()
41 }
42
43 pub async fn connect(host: &str, port: u16, user: &str, database: &str) -> PgResult<Self> {
52 let connection = PgConnection::connect(host, port, user, database).await?;
53 Ok(Self::new(connection))
54 }
55
56 pub async fn connect_with_password(
59 host: &str,
60 port: u16,
61 user: &str,
62 database: &str,
63 password: &str,
64 ) -> PgResult<Self> {
65 let connection =
66 PgConnection::connect_with_password(host, port, user, database, Some(password)).await?;
67 Ok(Self::new(connection))
68 }
69
70 pub async fn connect_with_options(
72 host: &str,
73 port: u16,
74 user: &str,
75 database: &str,
76 password: Option<&str>,
77 options: ConnectOptions,
78 ) -> PgResult<Self> {
79 let connection =
80 PgConnection::connect_with_options(host, port, user, database, password, options)
81 .await?;
82 Ok(Self::new(connection))
83 }
84
85 pub async fn connect_logical_replication(
90 host: &str,
91 port: u16,
92 user: &str,
93 database: &str,
94 password: Option<&str>,
95 ) -> PgResult<Self> {
96 let options = ConnectOptions::default().with_logical_replication();
97 Self::connect_with_options(host, port, user, database, password, options).await
98 }
99
100 pub async fn connect_logical_replication_with_options(
102 host: &str,
103 port: u16,
104 user: &str,
105 database: &str,
106 password: Option<&str>,
107 options: ConnectOptions,
108 ) -> PgResult<Self> {
109 Self::connect_with_options(
110 host,
111 port,
112 user,
113 database,
114 password,
115 options.with_logical_replication(),
116 )
117 .await
118 }
119
120 pub async fn connect_env() -> PgResult<Self> {
131 let url = std::env::var("DATABASE_URL").map_err(|_| {
132 PgError::Connection("DATABASE_URL environment variable not set".to_string())
133 })?;
134 Self::connect_url(&url).await
135 }
136
137 pub async fn connect_url(url: &str) -> PgResult<Self> {
150 let (host, port, user, database, password) = Self::parse_database_url(url)?;
151
152 let mut pool_cfg = pool::PoolConfig::new(&host, port, &user, &database);
154 if let Some(pw) = &password {
155 pool_cfg = pool_cfg.password(pw);
156 }
157 if let Some((_, query)) = url.split_once('?') {
158 pool::apply_url_query_params(&mut pool_cfg, query, &host)?;
159 }
160
161 let mut opts = ConnectOptions {
162 tls_mode: pool_cfg.tls_mode,
163 gss_enc_mode: pool_cfg.gss_enc_mode,
164 tls_ca_cert_pem: pool_cfg.tls_ca_cert_pem,
165 mtls: pool_cfg.mtls,
166 gss_token_provider: pool_cfg.gss_token_provider,
167 gss_token_provider_ex: pool_cfg.gss_token_provider_ex,
168 auth: pool_cfg.auth_settings,
169 startup_params: Vec::new(),
170 };
171
172 if let Some((_, query)) = url.split_once('?') {
174 for pair in query.split('&') {
175 let mut kv = pair.splitn(2, '=');
176 let key = kv.next().unwrap_or_default().trim();
177 let value = kv.next().unwrap_or_default().trim();
178 if key.eq_ignore_ascii_case("replication") {
179 let replication_mode = if value.eq_ignore_ascii_case("database") {
180 "database"
181 } else if value.eq_ignore_ascii_case("true")
182 || value.eq_ignore_ascii_case("on")
183 || value == "1"
184 {
185 "database"
188 } else {
189 return Err(PgError::Connection(format!(
190 "Invalid replication startup mode '{}': expected database|true|on|1",
191 value
192 )));
193 };
194 opts = opts.with_startup_param("replication", replication_mode);
195 }
196 }
197 }
198
199 Self::connect_with_options(&host, port, &user, &database, password.as_deref(), opts).await
200 }
201
202 pub(crate) fn parse_database_url(
209 url: &str,
210 ) -> PgResult<(String, u16, String, String, Option<String>)> {
211 let after_scheme = if let Some(rest) = url.strip_prefix("postgres://") {
212 rest
213 } else if let Some(rest) = url.strip_prefix("postgresql://") {
214 rest
215 } else {
216 return Err(PgError::Connection(
217 "Invalid DATABASE_URL: expected postgres:// or postgresql://".to_string(),
218 ));
219 };
220
221 let (auth_part, host_db_part) = if let Some(at_pos) = after_scheme.rfind('@') {
223 (Some(&after_scheme[..at_pos]), &after_scheme[at_pos + 1..])
224 } else {
225 (None, after_scheme)
226 };
227
228 let (user, password) = if let Some(auth) = auth_part {
230 if auth.is_empty() {
231 return Err(PgError::Connection(
232 "Invalid DATABASE_URL: missing user".to_string(),
233 ));
234 }
235 let parts: Vec<&str> = auth.splitn(2, ':').collect();
236 if parts.len() == 2 {
237 let user = Self::percent_decode(parts[0])?;
239 if user.is_empty() {
240 return Err(PgError::Connection(
241 "Invalid DATABASE_URL: missing user".to_string(),
242 ));
243 }
244 (user, Some(Self::percent_decode(parts[1])?))
245 } else {
246 let user = Self::percent_decode(parts[0])?;
247 if user.is_empty() {
248 return Err(PgError::Connection(
249 "Invalid DATABASE_URL: missing user".to_string(),
250 ));
251 }
252 (user, None)
253 }
254 } else {
255 ("postgres".to_string(), None)
256 };
257
258 let (host_port, database) = if let Some(slash_pos) = host_db_part.find('/') {
260 let raw_db = &host_db_part[slash_pos + 1..];
261 let db = Self::percent_decode(raw_db.split('?').next().unwrap_or(raw_db))?;
263 if db.is_empty() {
264 return Err(PgError::Connection(
265 "Invalid DATABASE_URL: missing database name".to_string(),
266 ));
267 }
268 (&host_db_part[..slash_pos], db)
269 } else {
270 return Err(PgError::Connection(
271 "Invalid DATABASE_URL: missing database name".to_string(),
272 ));
273 };
274
275 let (host, port) = if host_port.starts_with('[') {
277 let end = host_port.find(']').ok_or_else(|| {
278 PgError::Connection("Invalid DATABASE_URL: malformed IPv6 host".to_string())
279 })?;
280 let host = &host_port[..=end];
281 if host == "[]" {
282 return Err(PgError::Connection(
283 "Invalid DATABASE_URL: missing host".to_string(),
284 ));
285 }
286 let suffix = &host_port[end + 1..];
287 let port = if suffix.is_empty() {
288 5432
289 } else if let Some(port_str) = suffix.strip_prefix(':') {
290 Self::parse_database_url_port(port_str)?
291 } else {
292 return Err(PgError::Connection(
293 "Invalid DATABASE_URL: malformed IPv6 host".to_string(),
294 ));
295 };
296 (host.to_string(), port)
297 } else if let Some(colon_pos) = host_port.rfind(':') {
298 let port_str = &host_port[colon_pos + 1..];
299 let host = &host_port[..colon_pos];
300 if host.is_empty() {
301 return Err(PgError::Connection(
302 "Invalid DATABASE_URL: missing host".to_string(),
303 ));
304 }
305 let port = Self::parse_database_url_port(port_str)?;
306 (host.to_string(), port)
307 } else {
308 if host_port.is_empty() {
309 return Err(PgError::Connection(
310 "Invalid DATABASE_URL: missing host".to_string(),
311 ));
312 }
313 (host_port.to_string(), 5432) };
315
316 Ok((host, port, user, database, password))
317 }
318
319 fn parse_database_url_port(port_str: &str) -> PgResult<u16> {
320 if port_str.is_empty() {
321 return Err(PgError::Connection(
322 "Invalid DATABASE_URL: missing port after ':'".to_string(),
323 ));
324 }
325 let port = port_str
326 .parse::<u16>()
327 .map_err(|_| PgError::Connection(format!("Invalid port: {}", port_str)))?;
328 if port == 0 {
329 return Err(PgError::Connection(
330 "Invalid port: 0 (expected 1..=65535)".to_string(),
331 ));
332 }
333 Ok(port)
334 }
335
336 pub(crate) fn percent_decode(s: &str) -> PgResult<String> {
339 fn hex_value(byte: u8) -> Option<u8> {
340 match byte {
341 b'0'..=b'9' => Some(byte - b'0'),
342 b'a'..=b'f' => Some(byte - b'a' + 10),
343 b'A'..=b'F' => Some(byte - b'A' + 10),
344 _ => None,
345 }
346 }
347
348 let bytes = s.as_bytes();
349 let mut decoded = Vec::with_capacity(bytes.len());
350 let mut i = 0;
351
352 while i < bytes.len() {
353 if bytes[i] == b'%' {
354 if i + 2 >= bytes.len() {
355 return Err(PgError::Connection(
356 "Invalid DATABASE_URL percent-encoding: '%' must be followed by two hex digits"
357 .to_string(),
358 ));
359 }
360 let (Some(hi), Some(lo)) = (hex_value(bytes[i + 1]), hex_value(bytes[i + 2]))
361 else {
362 return Err(PgError::Connection(
363 "Invalid DATABASE_URL percent-encoding: '%' must be followed by two hex digits"
364 .to_string(),
365 ));
366 };
367 decoded.push((hi << 4) | lo);
368 i += 3;
369 } else {
370 decoded.push(bytes[i]);
371 i += 1;
372 }
373 }
374
375 String::from_utf8(decoded).map_err(|_| {
376 PgError::Connection(
377 "Invalid DATABASE_URL percent-encoding: decoded value is not UTF-8".to_string(),
378 )
379 })
380 }
381
382 pub async fn connect_with_timeout(
393 host: &str,
394 port: u16,
395 user: &str,
396 database: &str,
397 password: &str,
398 timeout: std::time::Duration,
399 ) -> PgResult<Self> {
400 tokio::time::timeout(
401 timeout,
402 Self::connect_with_password(host, port, user, database, password),
403 )
404 .await
405 .map_err(|_| PgError::Timeout(format!("connection after {:?}", timeout)))?
406 }
407 pub fn clear_cache(&mut self) {
411 self.connection.clear_prepared_statement_state();
412 }
413
414 pub fn cache_stats(&self) -> (usize, usize) {
417 (
418 self.connection.stmt_cache.len(),
419 self.connection.stmt_cache.cap().get(),
420 )
421 }
422}