Skip to main content

SyncConnection

Struct SyncConnection 

Source
pub struct SyncConnection { /* private fields */ }
Expand description

A synchronous PostgreSQL connection over a Unix domain socket.

This is the blocking counterpart to Connection. All I/O is synchronous using std::os::unix::net::UnixStream. No tokio runtime is required.

§Thread safety

SyncConnection is Send but not Sync — it must be used by one thread at a time. This matches the PostgreSQL wire protocol which is inherently sequential.

§Example

use bsql_driver_postgres::{SyncConnection, Config, Arena};

let config = Config::from_url("postgres://user@localhost/db?host=/tmp").unwrap();
let mut conn = SyncConnection::connect(&config).unwrap();
let mut arena = Arena::new();

let hash = bsql_driver_postgres::hash_sql("SELECT 1 AS n");
let result = conn.query("SELECT 1 AS n", hash, &[], &mut arena).unwrap();
assert_eq!(result.len(), 1);

Implementations§

Source§

impl SyncConnection

Source

pub fn connect(config: &Config) -> Result<Self, DriverError>

Connect to PostgreSQL via Unix domain socket and complete the startup/auth handshake. Fully synchronous — no tokio runtime needed.

config.host must start with / (UDS directory path).

§Errors

Returns an error if the host is not a UDS path, connection fails, or authentication fails.

Source

pub fn prepare_only( &mut self, sql: &str, sql_hash: u64, ) -> Result<(), DriverError>

Prepare a statement without executing it (Parse+Describe+Sync only).

If the statement is already cached, this is a no-op.

Source

pub fn query( &mut self, sql: &str, sql_hash: u64, params: &[&(dyn Encode + Sync)], arena: &mut Arena, ) -> Result<QueryResult, DriverError>

Execute a prepared query and return rows in arena-allocated storage.

Optimized path: after send_pipeline flushes, we parse BindComplete + DataRow* + CommandComplete + ReadyForQuery directly from stream_buf, avoiding per-message read_message_buffered overhead. DataRow payloads are parsed in-place from stream_buf into arena storage.

Source

pub fn execute_monolithic( &mut self, sql: &str, sql_hash: u64, params: &[&(dyn Encode + Sync)], ) -> Result<u64, DriverError>

Monolithic execute — everything in one function, no intermediate calls.

Inlines the entire send_pipeline + response parsing path for INSERT/UPDATE/DELETE. On cache hit: template copy + param patch + write_all + inline message parsing. No send_pipeline(), no flush_write(), no refill_stream_buf(). The compiler sees the entire path and can optimize globally.

On cache miss (first execution of a statement), falls through to the cold execute_with_prepare path.

Source

pub fn execute( &mut self, sql: &str, sql_hash: u64, params: &[&(dyn Encode + Sync)], ) -> Result<u64, DriverError>

Execute a query without result rows (INSERT/UPDATE/DELETE).

Delegates to execute_monolithic which inlines the entire send + receive path. Kept for API compatibility.

Source

pub fn execute_pipeline( &mut self, sql: &str, sql_hash: u64, param_sets: &[&[&(dyn Encode + Sync)]], ) -> Result<Vec<u64>, DriverError>

Execute the same prepared statement N times with different parameters in a single pipeline round-trip.

Sends all N Bind+Execute messages followed by one Sync. PostgreSQL processes them in order and returns N BindComplete+CommandComplete responses followed by one ReadyForQuery.

This is a real optimization for bulk operations: N inserts in a transaction become 1 round-trip instead of N round-trips.

Returns the number of affected rows for each parameter set.

Source

pub fn for_each<F>( &mut self, sql: &str, sql_hash: u64, params: &[&(dyn Encode + Sync)], f: F, ) -> Result<(), DriverError>
where F: FnMut(PgDataRow<'_>) -> Result<(), DriverError>,

Process each row via a closure with zero-copy PgDataRow.

Source

pub fn for_each_raw_monolithic<F>( &mut self, sql: &str, sql_hash: u64, params: &[&(dyn Encode + Sync)], f: F, ) -> Result<(), DriverError>
where F: FnMut(&[u8]) -> Result<(), DriverError>,

Monolithic for_each_raw — everything in one function, no intermediate calls.

Inlines the entire send_pipeline + response parsing path for SELECT queries processed via a raw byte closure. On cache hit: template copy + param patch + write_all + inline DataRow streaming + inline ReadyForQuery. No send_pipeline(), no flush_write(), no refill_stream_buf().

On cache miss (first execution of a statement), falls through to the cold for_each_raw_with_prepare path.

Source

pub fn for_each_raw<F>( &mut self, sql: &str, sql_hash: u64, params: &[&(dyn Encode + Sync)], f: F, ) -> Result<(), DriverError>
where F: FnMut(&[u8]) -> Result<(), DriverError>,

Process each DataRow as raw bytes — fastest path.

Delegates to for_each_raw_monolithic which inlines the entire send + receive path. Kept for API compatibility.

Source

pub fn simple_query(&mut self, sql: &str) -> Result<(), DriverError>

Simple query protocol — for non-prepared SQL (BEGIN, COMMIT, SET, etc.).

Source

pub fn simple_query_rows( &mut self, sql: &str, ) -> Result<Vec<SimpleRow>, DriverError>

Execute a simple (text protocol) query and return all result rows.

Source

pub fn close(self) -> Result<(), DriverError>

Send Terminate and close the connection.

Source

pub fn is_idle(&self) -> bool

Whether the connection is in an idle transaction state.

Source

pub fn is_in_transaction(&self) -> bool

Whether the connection is in a transaction.

Source

pub fn is_in_failed_transaction(&self) -> bool

Whether the connection is in a failed transaction.

Source

pub fn touch(&mut self)

Record that the connection was just used.

Source

pub fn idle_duration(&self) -> Duration

How long since this connection last completed a query.

Source

pub fn parameter(&self, name: &str) -> Option<&str>

Get a server parameter value.

Source

pub fn pid(&self) -> i32

Backend process ID.

Source

pub fn secret_key(&self) -> i32

Backend secret key.

Source

pub fn drain_notifications(&mut self) -> Vec<Notification>

Drain all buffered notifications.

Source

pub fn pending_notification_count(&self) -> usize

Number of pending notifications.

Source

pub fn set_max_stmt_cache_size(&mut self, size: usize)

Set the maximum number of cached prepared statements.

Source

pub fn stmt_cache_len(&self) -> usize

Number of currently cached prepared statements.

Source

pub fn created_at(&self) -> Instant

When this connection was created.

Trait Implementations§

Source§

impl Debug for SyncConnection

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.