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).
§Arguments
host— PostgreSQL server hostname or IP.port— TCP port (typically 5432).user— PostgreSQL role name.database— Target database name.
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. Supports server-requested auth flow: cleartext, MD5, or SCRAM-SHA-256.
Sourcepub async fn connect_with_options(
host: &str,
port: u16,
user: &str,
database: &str,
password: Option<&str>,
options: ConnectOptions,
) -> PgResult<Self>
pub async fn connect_with_options( host: &str, port: u16, user: &str, database: &str, password: Option<&str>, options: ConnectOptions, ) -> PgResult<Self>
Connect with explicit security options.
Sourcepub async fn connect_logical_replication(
host: &str,
port: u16,
user: &str,
database: &str,
password: Option<&str>,
) -> PgResult<Self>
pub async fn connect_logical_replication( host: &str, port: u16, user: &str, database: &str, password: Option<&str>, ) -> PgResult<Self>
Connect in logical replication mode (replication=database).
This enables replication commands such as IDENTIFY_SYSTEM and
CREATE_REPLICATION_SLOT.
Sourcepub async fn connect_logical_replication_with_options(
host: &str,
port: u16,
user: &str,
database: &str,
password: Option<&str>,
options: ConnectOptions,
) -> PgResult<Self>
pub async fn connect_logical_replication_with_options( host: &str, port: u16, user: &str, database: &str, password: Option<&str>, options: ConnectOptions, ) -> PgResult<Self>
Connect with explicit options and force logical replication mode.
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>
Connect using a PostgreSQL connection URL.
Parses the URL format: postgresql://user:password@host:port/database?params
or postgres://user:password@host:port/database?params
Supports all enterprise query params (sslmode, auth_mode, gss_provider,
channel_binding, etc.) — same set as PoolConfig::from_qail_config.
§Example
let driver = PgDriver::connect_url("postgresql://user:pass@localhost:5432/mydb?sslmode=require").await?;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).
Source§impl PgDriver
impl PgDriver
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_all_with_format(
&mut self,
cmd: &Qail,
result_format: ResultFormat,
) -> PgResult<Vec<PgRow>>
pub async fn fetch_all_with_format( &mut self, cmd: &Qail, result_format: ResultFormat, ) -> PgResult<Vec<PgRow>>
Execute a QAIL command and fetch all rows using a specific result format.
result_format controls server result-column encoding:
ResultFormat::Textfor standard text decoding.ResultFormat::Binaryfor binary wire values.
Sourcepub async fn fetch_typed_with_format<T: QailRow>(
&mut self,
cmd: &Qail,
result_format: ResultFormat,
) -> PgResult<Vec<T>>
pub async fn fetch_typed_with_format<T: QailRow>( &mut self, cmd: &Qail, result_format: ResultFormat, ) -> PgResult<Vec<T>>
Execute a QAIL command and fetch all rows as a typed struct with explicit result format.
Use ResultFormat::Binary to get binary wire values; row decoding should use
metadata-aware accessors such as PgRow::try_get() / try_get_by_name().
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 (text format). Returns None if no rows are returned.
Sourcepub async fn fetch_one_typed_with_format<T: QailRow>(
&mut self,
cmd: &Qail,
result_format: ResultFormat,
) -> PgResult<Option<T>>
pub async fn fetch_one_typed_with_format<T: QailRow>( &mut self, cmd: &Qail, result_format: ResultFormat, ) -> PgResult<Option<T>>
Execute a QAIL command and fetch a single row as a typed struct with explicit result format.
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.
Optimized: encodes wire bytes into reusable write_buf (zero-alloc).
Sourcepub async fn fetch_all_uncached_with_format(
&mut self,
cmd: &Qail,
result_format: ResultFormat,
) -> PgResult<Vec<PgRow>>
pub async fn fetch_all_uncached_with_format( &mut self, cmd: &Qail, result_format: ResultFormat, ) -> PgResult<Vec<PgRow>>
Execute a QAIL command and fetch all rows (UNCACHED) with explicit result format.
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_all_fast_with_format(
&mut self,
cmd: &Qail,
result_format: ResultFormat,
) -> PgResult<Vec<PgRow>>
pub async fn fetch_all_fast_with_format( &mut self, cmd: &Qail, result_format: ResultFormat, ) -> PgResult<Vec<PgRow>>
Execute a QAIL command and fetch all rows (FAST VERSION) with explicit result format.
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. Like fetch_all(), but caches the prepared statement on the server. On first call: sends Parse + Describe + Bind + Execute + Sync On subsequent calls: sends only Bind + Execute + Sync (SKIPS Parse!) Column metadata (RowDescription) is cached alongside the statement so that by-name column access works on every call.
Optimized: all wire messages are batched into a single write_all syscall.
Sourcepub async fn fetch_all_cached_with_format(
&mut self,
cmd: &Qail,
result_format: ResultFormat,
) -> PgResult<Vec<PgRow>>
pub async fn fetch_all_cached_with_format( &mut self, cmd: &Qail, result_format: ResultFormat, ) -> PgResult<Vec<PgRow>>
Execute a QAIL command with prepared statement caching and explicit result format.
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 query_ast(&mut self, cmd: &Qail) -> PgResult<QueryResult>
pub async fn query_ast(&mut self, cmd: &Qail) -> PgResult<QueryResult>
Query a QAIL command and return rows (for SELECT/GET queries).
Like execute() but collects RowDescription + DataRow messages
instead of discarding them.
Sourcepub async fn query_ast_with_format(
&mut self,
cmd: &Qail,
result_format: ResultFormat,
) -> PgResult<QueryResult>
pub async fn query_ast_with_format( &mut self, cmd: &Qail, result_format: ResultFormat, ) -> PgResult<QueryResult>
Query a QAIL command and return rows using an explicit result format.
Source§impl PgDriver
impl PgDriver
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 set_rls_context(&mut self, ctx: RlsContext) -> PgResult<()>
pub async fn set_rls_context(&mut self, ctx: RlsContext) -> PgResult<()>
Set the RLS context for multi-tenant data isolation.
Configures PostgreSQL session variables (app.current_tenant_id, etc.)
so that RLS policies automatically filter data by tenant.
Since PgDriver takes &mut self, the borrow checker guarantees
that set_config and all subsequent queries execute on the same
connection — no pool race conditions possible.
§Example
driver.set_rls_context(RlsContext::tenant("tenant-123")).await?;
let orders = driver.fetch_all(&Qail::get("orders")).await?;
// orders only contains rows for tenant-123Sourcepub async fn clear_rls_context(&mut self) -> PgResult<()>
pub async fn clear_rls_context(&mut self) -> PgResult<()>
Clear the RLS context, resetting session variables to safe defaults.
After clearing, all RLS-protected queries will return zero rows (empty tenant scope matches nothing).
Sourcepub fn rls_context(&self) -> Option<&RlsContext>
pub fn rls_context(&self) -> Option<&RlsContext>
Get the current RLS context, if any.
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 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 copy_export_table_stream<F, Fut>(
&mut self,
table: &str,
columns: &[String],
on_chunk: F,
) -> PgResult<()>
pub async fn copy_export_table_stream<F, Fut>( &mut self, table: &str, columns: &[String], on_chunk: F, ) -> PgResult<()>
Stream table export using COPY TO STDOUT with bounded memory usage.
Chunks are forwarded directly from PostgreSQL to on_chunk.
Sourcepub async fn copy_export_cmd_stream<F, Fut>(
&mut self,
cmd: &Qail,
on_chunk: F,
) -> PgResult<()>
pub async fn copy_export_cmd_stream<F, Fut>( &mut self, cmd: &Qail, on_chunk: F, ) -> PgResult<()>
Stream an AST-native Qail::Export command as raw COPY chunks.
Sourcepub async fn copy_export_cmd_stream_rows<F>(
&mut self,
cmd: &Qail,
on_row: F,
) -> PgResult<()>
pub async fn copy_export_cmd_stream_rows<F>( &mut self, cmd: &Qail, on_row: F, ) -> PgResult<()>
Stream an AST-native Qail::Export command as parsed text rows.
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
}
}Source§impl PgDriver
impl PgDriver
Sourcepub async fn identify_system(&mut self) -> PgResult<IdentifySystem>
pub async fn identify_system(&mut self) -> PgResult<IdentifySystem>
Driver wrapper for PgConnection::identify_system.
Sourcepub async fn create_logical_replication_slot(
&mut self,
slot_name: &str,
output_plugin: &str,
temporary: bool,
two_phase: bool,
) -> PgResult<ReplicationSlotInfo>
pub async fn create_logical_replication_slot( &mut self, slot_name: &str, output_plugin: &str, temporary: bool, two_phase: bool, ) -> PgResult<ReplicationSlotInfo>
Driver wrapper for PgConnection::create_logical_replication_slot.
Sourcepub async fn drop_replication_slot(
&mut self,
slot_name: &str,
wait: bool,
) -> PgResult<()>
pub async fn drop_replication_slot( &mut self, slot_name: &str, wait: bool, ) -> PgResult<()>
Driver wrapper for PgConnection::drop_replication_slot.
Sourcepub async fn start_logical_replication(
&mut self,
slot_name: &str,
start_lsn: &str,
options: &[ReplicationOption],
) -> PgResult<ReplicationStreamStart>
pub async fn start_logical_replication( &mut self, slot_name: &str, start_lsn: &str, options: &[ReplicationOption], ) -> PgResult<ReplicationStreamStart>
Driver wrapper for PgConnection::start_logical_replication.
Sourcepub async fn recv_replication_message(
&mut self,
) -> PgResult<ReplicationStreamMessage>
pub async fn recv_replication_message( &mut self, ) -> PgResult<ReplicationStreamMessage>
Driver wrapper for PgConnection::recv_replication_message.
Sourcepub async fn send_standby_status_update(
&mut self,
write_lsn: u64,
flush_lsn: u64,
apply_lsn: u64,
reply_requested: bool,
) -> PgResult<()>
pub async fn send_standby_status_update( &mut self, write_lsn: u64, flush_lsn: u64, apply_lsn: u64, reply_requested: bool, ) -> PgResult<()>
Driver wrapper for PgConnection::send_standby_status_update.