pub struct PgDriver { /* private fields */ }Expand description
Combines the pure encoder (Layer 2) with async I/O (Layer 3).
Implementations§
Source§impl PgDriver
impl PgDriver
Sourcepub fn new(connection: PgConnection) -> Self
pub fn new(connection: PgConnection) -> Self
Create a new driver with an existing connection.
Sourcepub fn builder() -> PgDriverBuilder
pub fn builder() -> PgDriverBuilder
Sourcepub async fn connect(
host: &str,
port: u16,
user: &str,
database: &str,
) -> PgResult<Self>
pub async fn connect( host: &str, port: u16, user: &str, database: &str, ) -> PgResult<Self>
Connect to PostgreSQL and create a driver (trust mode, no password).
Sourcepub async fn connect_with_password(
host: &str,
port: u16,
user: &str,
database: &str,
password: &str,
) -> PgResult<Self>
pub async fn connect_with_password( host: &str, port: u16, user: &str, database: &str, password: &str, ) -> PgResult<Self>
Connect to PostgreSQL with password authentication (SCRAM-SHA-256).
Sourcepub async fn connect_env() -> PgResult<Self>
pub async fn connect_env() -> PgResult<Self>
Sourcepub async fn connect_url(url: &str) -> PgResult<Self>
pub async fn connect_url(url: &str) -> PgResult<Self>
Sourcepub async fn connect_with_timeout(
host: &str,
port: u16,
user: &str,
database: &str,
password: &str,
timeout: Duration,
) -> PgResult<Self>
pub async fn connect_with_timeout( host: &str, port: u16, user: &str, database: &str, password: &str, timeout: Duration, ) -> PgResult<Self>
Sourcepub fn clear_cache(&mut self)
pub fn clear_cache(&mut self)
Clear the prepared statement cache. Frees memory by removing all cached statements. Note: Statements remain on the PostgreSQL server until connection closes.
Sourcepub fn cache_stats(&self) -> (usize, usize)
pub fn cache_stats(&self) -> (usize, usize)
Get cache statistics. Returns (current_size, max_capacity).
Sourcepub async fn fetch_all(&mut self, cmd: &Qail) -> PgResult<Vec<PgRow>>
pub async fn fetch_all(&mut self, cmd: &Qail) -> PgResult<Vec<PgRow>>
Execute a QAIL command and fetch all rows (CACHED + ZERO-ALLOC). Default method - uses prepared statement caching for best performance. On first call: sends Parse + Bind + Execute + Sync On subsequent calls with same SQL: sends only Bind + Execute (SKIPS Parse!) Uses LRU cache with max 1000 statements (auto-evicts oldest).
Sourcepub async fn fetch_one_typed<T: QailRow>(
&mut self,
cmd: &Qail,
) -> PgResult<Option<T>>
pub async fn fetch_one_typed<T: QailRow>( &mut self, cmd: &Qail, ) -> PgResult<Option<T>>
Execute a QAIL command and fetch a single row as a typed struct. Returns None if no rows are returned.
Sourcepub async fn fetch_all_uncached(&mut self, cmd: &Qail) -> PgResult<Vec<PgRow>>
pub async fn fetch_all_uncached(&mut self, cmd: &Qail) -> PgResult<Vec<PgRow>>
Execute a QAIL command and fetch all rows (UNCACHED). Sends Parse + Bind + Execute on every call. Use for one-off queries or when caching is not desired.
Sourcepub async fn fetch_all_fast(&mut self, cmd: &Qail) -> PgResult<Vec<PgRow>>
pub async fn fetch_all_fast(&mut self, cmd: &Qail) -> PgResult<Vec<PgRow>>
Execute a QAIL command and fetch all rows (FAST VERSION). Uses optimized recv_with_data_fast for faster response parsing. Skips column metadata collection for maximum speed.
Sourcepub async fn fetch_one(&mut self, cmd: &Qail) -> PgResult<PgRow>
pub async fn fetch_one(&mut self, cmd: &Qail) -> PgResult<PgRow>
Execute a QAIL command and fetch one row.
Sourcepub async fn fetch_all_cached(&mut self, cmd: &Qail) -> PgResult<Vec<PgRow>>
pub async fn fetch_all_cached(&mut self, cmd: &Qail) -> PgResult<Vec<PgRow>>
Execute a QAIL command with PREPARED STATEMENT CACHING (ZERO-ALLOC). Like fetch_all(), but caches the prepared statement on the server. On first call: sends Parse + Bind + Execute + Sync On subsequent calls: sends only Bind + Execute + Sync (SKIPS Parse!)
Sourcepub async fn execute(&mut self, cmd: &Qail) -> PgResult<u64>
pub async fn execute(&mut self, cmd: &Qail) -> PgResult<u64>
Execute a QAIL command (for mutations) - ZERO-ALLOC.
Sourcepub async fn rollback(&mut self) -> PgResult<()>
pub async fn rollback(&mut self) -> PgResult<()>
Rollback the current transaction (AST-native).
Sourcepub async fn savepoint(&mut self, name: &str) -> PgResult<()>
pub async fn savepoint(&mut self, name: &str) -> PgResult<()>
Create a named savepoint within the current transaction.
Savepoints allow partial rollback within a transaction.
Use rollback_to() to return to this savepoint.
§Example
driver.begin().await?;
driver.execute(&insert1).await?;
driver.savepoint("sp1").await?;
driver.execute(&insert2).await?;
driver.rollback_to("sp1").await?; // Undo insert2, keep insert1
driver.commit().await?;Sourcepub async fn rollback_to(&mut self, name: &str) -> PgResult<()>
pub async fn rollback_to(&mut self, name: &str) -> PgResult<()>
Rollback to a previously created savepoint. Discards all changes since the named savepoint was created, but keeps the transaction open.
Sourcepub async fn release_savepoint(&mut self, name: &str) -> PgResult<()>
pub async fn release_savepoint(&mut self, name: &str) -> PgResult<()>
Release a savepoint (free resources, if no longer needed). After release, the savepoint cannot be rolled back to.
Sourcepub async fn execute_batch(&mut self, cmds: &[Qail]) -> PgResult<Vec<u64>>
pub async fn execute_batch(&mut self, cmds: &[Qail]) -> PgResult<Vec<u64>>
Execute multiple commands in a single atomic transaction. All commands succeed or all are rolled back.
§Example
let cmds = vec![
Qail::add("users").columns(["name"]).values(["Alice"]),
Qail::add("users").columns(["name"]).values(["Bob"]),
];
let results = driver.execute_batch(&cmds).await?;
// results = [1, 1] (rows affected)Sourcepub async fn set_statement_timeout(&mut self, ms: u32) -> PgResult<()>
pub async fn set_statement_timeout(&mut self, ms: u32) -> PgResult<()>
Sourcepub async fn reset_statement_timeout(&mut self) -> PgResult<()>
pub async fn reset_statement_timeout(&mut self) -> PgResult<()>
Reset statement timeout to default (no limit).
Sourcepub async fn pipeline_batch(&mut self, cmds: &[Qail]) -> PgResult<usize>
pub async fn pipeline_batch(&mut self, cmds: &[Qail]) -> PgResult<usize>
Sourcepub async fn pipeline_fetch(
&mut self,
cmds: &[Qail],
) -> PgResult<Vec<Vec<PgRow>>>
pub async fn pipeline_fetch( &mut self, cmds: &[Qail], ) -> PgResult<Vec<Vec<PgRow>>>
Execute multiple Qail ASTs and return full row data.
Sourcepub async fn prepare(&mut self, sql: &str) -> PgResult<PreparedStatement>
pub async fn prepare(&mut self, sql: &str) -> PgResult<PreparedStatement>
Prepare a SQL statement for repeated execution.
Sourcepub async fn pipeline_prepared_fast(
&mut self,
stmt: &PreparedStatement,
params_batch: &[Vec<Option<Vec<u8>>>],
) -> PgResult<usize>
pub async fn pipeline_prepared_fast( &mut self, stmt: &PreparedStatement, params_batch: &[Vec<Option<Vec<u8>>>], ) -> PgResult<usize>
Execute a prepared statement pipeline in FAST mode (count only).
Sourcepub async fn execute_raw(&mut self, sql: &str) -> PgResult<()>
pub async fn execute_raw(&mut self, sql: &str) -> PgResult<()>
Execute a raw SQL string.
⚠️ Discouraged: Violates AST-native philosophy.
Use for bootstrap DDL only (e.g., migration table creation).
For transactions, use begin(), commit(), rollback().
Sourcepub async fn fetch_raw(&mut self, sql: &str) -> PgResult<Vec<PgRow>>
pub async fn fetch_raw(&mut self, sql: &str) -> PgResult<Vec<PgRow>>
Execute a raw SQL query and return rows. ⚠️ Discouraged: Violates AST-native philosophy. Use for bootstrap/admin queries only.
Sourcepub async fn copy_bulk(
&mut self,
cmd: &Qail,
rows: &[Vec<Value>],
) -> PgResult<u64>
pub async fn copy_bulk( &mut self, cmd: &Qail, rows: &[Vec<Value>], ) -> PgResult<u64>
Bulk insert data using PostgreSQL COPY protocol (AST-native). Uses a Qail::Add to get validated table and column names from the AST, not user-provided strings. This is the sound, AST-native approach.
§Example
// Create a Qail::Add to define table and columns
let cmd = Qail::add("users")
.columns(["id", "name", "email"]);
// Bulk insert rows
let rows: Vec<Vec<Value>> = vec![
vec![Value::Int(1), Value::String("Alice"), Value::String("alice@ex.com")],
vec![Value::Int(2), Value::String("Bob"), Value::String("bob@ex.com")],
];
driver.copy_bulk(&cmd, &rows).await?;Sourcepub async fn copy_bulk_bytes(
&mut self,
cmd: &Qail,
data: &[u8],
) -> PgResult<u64>
pub async fn copy_bulk_bytes( &mut self, cmd: &Qail, data: &[u8], ) -> PgResult<u64>
Fastest bulk insert using pre-encoded COPY data. Accepts raw COPY text format bytes. Use when caller has already encoded rows to avoid any encoding overhead.
§Format
Data should be tab-separated rows with newlines (COPY text format):
1\thello\t3.14\n2\tworld\t2.71\n
§Example
let cmd = Qail::add("users").columns(["id", "name"]);
let data = b"1\tAlice\n2\tBob\n";
driver.copy_bulk_bytes(&cmd, data).await?;Sourcepub async fn copy_export_table(
&mut self,
table: &str,
columns: &[String],
) -> PgResult<Vec<u8>>
pub async fn copy_export_table( &mut self, table: &str, columns: &[String], ) -> PgResult<Vec<u8>>
Sourcepub async fn stream_cmd(
&mut self,
cmd: &Qail,
batch_size: usize,
) -> PgResult<Vec<Vec<PgRow>>>
pub async fn stream_cmd( &mut self, cmd: &Qail, batch_size: usize, ) -> PgResult<Vec<Vec<PgRow>>>
Stream large result sets using PostgreSQL cursors. This method uses DECLARE CURSOR internally to stream rows in batches, avoiding loading the entire result set into memory.
§Example
let cmd = Qail::get("large_table");
let batches = driver.stream_cmd(&cmd, 100).await?;
for batch in batches {
for row in batch {
// process row
}
}