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
//! # wasi-pg-client
//!
//! A production-grade PostgreSQL client library for WASI Preview 2.
//!
//! ## Quick Start
//!
//! ```rust,no_run
//! use wasi_pg_client::{Connection, Config};
//!
//! #[wstd::main]
//! async fn main() -> Result<(), wasi_pg_client::PgError> {
//! let config = Config::from_uri("postgresql://user:pass@localhost/mydb")?;
//! let mut conn = Connection::connect(&config).await?;
//!
//! let result = conn.query("SELECT id, name FROM users").await?;
//! for row in result.iter() {
//! let id: i32 = row.get(0)?;
//! let name: String = row.get(1)?;
//! println!("{}: {}", id, name);
//! }
//!
//! conn.close().await?;
//! Ok(())
//! }
//! ```
//!
//! ## Key Features
//!
//! - **Full PostgreSQL wire protocol v3** — simple and extended query protocols
//! - **Parameterized queries** — SQL injection prevention with `$1`, `$2` syntax
//! - **Prepared statements** — with automatic LRU caching
//! - **Streaming results** — O(1) memory for large queries via `RowStream`
//! - **Parameterized streaming** — `query_params_stream()` for streaming with parameters
//! - **Transactions** — RAII guards with automatic rollback on drop
//! - **Savepoints** — nested transaction scopes
//! - **COPY protocol** — bulk import/export with CSV and binary support
//! - **LISTEN/NOTIFY** — asynchronous pub/sub with timeout support
//! - **TLS** — via rustls (pure Rust, WASI-compatible)
//! - **SCRAM-SHA-256** and MD5 authentication
//! - **Connection pooling** — via `wasi_pg_client::pool` module with `Mutex`-based thread safety
//! - **Automatic reconnection** — with exponential backoff and session state rebuild
//! - **Retry policies** — for transient errors (serialization failures, deadlocks)
//! - **Query cancellation** — out-of-band via `CancelToken` (with TLS support)
//! - **Runtime parameter setting** — `set_param()` with automatic re-application on reconnect
//! - **Structured logging** — via `tracing`
//! - **Compiles to `wasm32-wasip2`** and native targets
//!
//! ## Feature Flags
//!
//! | Feature | Default | Description |
//! |---------|---------|-------------|
//! | `tls` | ✅ | TLS support via rustls |
//! | `scram` | ✅ | SCRAM-SHA-256 authentication |
//! | `md5-auth` | ❌ | MD5 authentication (legacy) |
//! | `pool` | ❌ | Connection pooling |
//! | `tracing` | ✅ | Structured logging via tracing |
//! | `uuid` | ❌ | UUID type support via uuid crate |
//! | `serde-json` | ❌ | JSON type support via serde_json |
//! | `chrono` | ❌ | chrono integration for date/time |
//! | `test-native` | ❌ | Native transport for testing |
//! | `tokio-transport` | ❌ | Tokio async TCP transport for native builds |
//!
//! ## WASI P2 Requirements
//!
//! This library targets `wasm32-wasip2`. When running in wasmtime, use:
//! ```bash
//! wasmtime run --wasi inherit-network --wasi inherit-env component.wasm
//! ```
//!
//! The `getrandom` crate must be configured with `features = ["wasi"]` for
//! cryptographic randomness (required for SCRAM auth and TLS).
//!
//! ## Tracing
//!
//! When the `tracing` feature is enabled, the library emits structured events
//! at the following levels:
//!
//! | Level | What gets logged |
//! |-------|-----------------|
//! | ERROR | Fatal errors: auth failed, TLS handshake failed, reconnection failed |
//! | WARN | Recoverable problems: connection broken, transaction rolled back, dirty connection discarded |
//! | INFO | Normal operations: connection established/closed, query completed |
//! | DEBUG | Detailed info: TCP connect, auth method, pool acquire/release, connection state |
//! | TRACE | Wire-level detail: every protocol message, full SQL |
//!
//! ⚠️ TRACE may expose sensitive data. Use only in development, never in production.
// Internal modules.
// Transport scaffolding — directory module for TCP, TLS, and test transports.
// Reconnection and retry support.
// Internal tracing helpers (target constants, redaction).
// ── Public API re-exports ──
// Core types
pub use CancelToken;
pub use ;
pub use ;
pub use ;
pub use Notification;
pub use ;
pub use ;
pub use RowStream;
pub use ;
pub use ;
pub use ;
// Error types
pub use sqlstate;
pub use ;
// Type system
pub use JsonB;
pub use ;
// Transport (for custom transports / testing)
pub use ;
// TLS (behind feature flag)
pub use ;
// Reconnection
pub use ;
pub use ;
// Connection pooling (behind the `pool` feature flag).
// Protocol types (for advanced use)
pub use ;
pub use BackendMessage;
pub use FrontendMessage;
/// Builder type alias for [`Config`].
///
/// The `Config` type already uses the builder pattern via its `new()` method
/// and chainable setters. This alias is provided for discoverability.
pub type ConfigBuilder = Config;
// Prelude for convenient imports.
/// Runtime sanity check that `getrandom` is properly configured for WASI P2.
///
/// Call this early (e.g. during `Connection::connect`) to get a clear panic
/// message instead of a cryptic runtime failure deep inside crypto code.