Struct xitca_postgres::Client

source ·
pub struct Client { /* private fields */ }
Expand description

Client is a handler type for Driver. it interacts with latter using channel and message for IO operation and de/encoding of postgres protocol in byte format.

Client expose a set of high level API to make the interaction represented in Rust function and types.

§Lifetime

Client and Driver have a dependent lifetime where either side can trigger the other part to shutdown. From Client side it’s in the form of dropping ownership.

§Examples

// connect to a database and spawn driver as async task
let (cli, drv) = Postgres::new(cfg).connect().await?;
let handle = tokio::spawn(drv.into_future());

// drop client after finished usage
drop(cli);

// client would notify driver to shutdown when it's dropped.
// await on the handle would return a Result of the shutdown outcome from driver side.  
let _ = handle.await.unwrap();

Implementations§

source§

impl Client

source

pub async fn prepare( &self, query: &str, types: &[Type], ) -> Result<StatementGuarded<'_, Self>, Error>

Creates a new prepared statement.

Prepared statements can be executed repeatedly, and may contain query parameters (indicated by $1, $2, etc), which are set when executed. Prepared statements can only be used with the connection that created them.

source

pub fn query<'a, S>( &self, stmt: S, params: &[&(dyn ToSql + Sync)], ) -> Result<S::RowStream<'a>, Error>
where S: Encode + IntoStream + 'a,

Executes a statement, returning an async stream of the resulting rows.

A statement may contain parameters, specified by $n, where n is the index of the parameter of the list provided, 1-indexed.

If the same statement will be repeatedly executed (perhaps with different query parameters), consider preparing the statement up front with Client::prepare.

source

pub fn query_raw<'a, S, I>( &self, stmt: S, params: I, ) -> Result<S::RowStream<'a>, Error>
where S: Encode + IntoStream + 'a, I: AsParams,

The maximally flexible version of Client::query.

A statement may contain parameters, specified by $n, where n is the index of the parameter of the list provided, 1-indexed.

If the same statement will be repeatedly executed (perhaps with different query parameters), consider preparing the statement up front with Client::prepare.

source

pub fn execute<S>( &self, stmt: S, params: &[&(dyn ToSql + Sync)], ) -> ExecuteFuture
where S: Encode,

Executes a statement, returning the number of rows modified.

A statement may contain parameters, specified by $n, where n is the index of the parameter of the list provided, 1-indexed.

If the same statement will be repeatedly executed (perhaps with different query parameters), consider preparing the statement up front with Client::prepare.

If the statement does not modify any rows (e.g. SELECT), 0 is returned.

source

pub fn execute_raw<S, I>(&self, stmt: S, params: I) -> ExecuteFuture
where S: Encode, I: AsParams,

The maximally flexible version of Client::execute.

A statement may contain parameters, specified by $n, where n is the index of the parameter of the list provided, 1-indexed.

If the same statement will be repeatedly executed (perhaps with different query parameters), consider preparing the statement up front with Client::prepare.

source

pub fn query_simple( &self, stmt: &str, ) -> Result<<&str as IntoStream>::RowStream<'_>, Error>

Executes a sequence of SQL statements using the simple query protocol, returning async stream of rows.

Statements should be separated by semicolons. If an error occurs, execution of the sequence will stop at that point. The simple query protocol returns the values in rows as strings rather than in their binary encodings, so the associated row type doesn’t work with the FromSql trait. Rather than simply returning a list of the rows, this method returns a list of an enum which indicates either the completion of one of the commands, or a row of data. This preserves the framing between the separate statements in the request.

§Warning

Prepared statements should be use for any query which contains user-specified data, as they provided the functionality to safely embed that data in the request. Do not form statements via string concatenation and pass them to this method!

source

pub fn execute_simple(&self, stmt: &str) -> ExecuteFuture

source

pub fn query_unnamed<'a>( &'a self, stmt: &'a str, types: &'a [Type], params: &[&(dyn ToSql + Sync)], ) -> Result<<StatementUnnamed<'a, Self> as IntoStream>::RowStream<'a>, Error>

Embed prepare statement to the query request itself. Meaning query would finish in one round trip to database. However it should also be noted that the client type must be referenced during the whole progress and associated client must be kept around util streaming is finished.

source

pub fn transaction( &mut self, ) -> impl Future<Output = Result<Transaction<'_, Self>, Error>> + Send

start a transaction

source

pub fn copy_in( &mut self, stmt: &Statement, ) -> impl Future<Output = Result<CopyIn<'_, Self>, Error>> + Send

Executes a COPY FROM STDIN statement, returning a sink used to write the copy data.

PostgreSQL does not support parameters in COPY statements, so this method does not take any. The copy must be explicitly completed via CopyIn::finish. If it is not, the copy will be aborted.

source

pub async fn copy_out(&self, stmt: &Statement) -> Result<CopyOut, Error>

Executes a COPY TO STDOUT statement, returning async stream of the resulting data.

PostgreSQL does not support parameters in COPY statements, so this method does not take any.

source

pub fn cancel_token(&self) -> Session

Constructs a cancellation token that can later be used to request cancellation of a query running on the connection associated with this client.

source

pub fn closed(&self) -> bool

a lossy hint of running state of io driver. an io driver shutdown can happen at the same time this api is called.

source

pub fn typeinfo(&self) -> Option<Statement>

source

pub fn set_typeinfo(&self, statement: &Statement)

source

pub fn typeinfo_composite(&self) -> Option<Statement>

source

pub fn set_typeinfo_composite(&self, statement: &Statement)

source

pub fn typeinfo_enum(&self) -> Option<Statement>

source

pub fn set_typeinfo_enum(&self, statement: &Statement)

source

pub fn type_(&self, oid: Oid) -> Option<Type>

source

pub fn set_type(&self, oid: Oid, type_: &Type)

source

pub fn clear_type_cache(&self)

Clears the client’s type information cache.

When user-defined types are used in a query, the client loads their definitions from the database and caches them for the lifetime of the client. If those definitions are changed in the database, this method can be used to flush the local cache and allow the new, updated definitions to be loaded.

source§

impl Client

source

pub fn pipeline<'a, B, const SYNC_MODE: bool>( &self, pipe: Pipeline<'a, B, SYNC_MODE>, ) -> Result<PipelineStream<'a>, Error>
where B: DerefMut<Target = BytesMut>,

execute the pipeline.

Trait Implementations§

source§

impl ClientBorrowMut for Client

source§

fn _borrow_mut(&mut self) -> &mut Client

source§

impl Copy for Client

source§

fn send_one_way<F>(&self, func: F) -> Result<(), Error>
where F: FnOnce(&mut BytesMut) -> Result<(), Error>,

source§

impl Drop for Client

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl Prepare for Client

source§

fn _get_type( &self, oid: Oid, ) -> Pin<Box<dyn Future<Output = Result<Type, Error>> + Send + '_>>

source§

fn _prepare( &self, query: &str, types: &[Type], ) -> impl Future<Output = Result<Statement, Error>> + Send

source§

impl Query for Client

source§

fn _send_encode_query<S, I>( &self, stmt: S, params: I, ) -> Result<Response, Error>
where S: Encode, I: AsParams,

encode statement and params and send it to client driver
source§

fn _query<'a, S>( &self, stmt: S, params: &[&(dyn ToSql + Sync)], ) -> Result<S::RowStream<'a>, Error>
where S: Encode + IntoStream + 'a,

query with statement and dynamic typed params. Read more
source§

fn _query_raw<'a, S, I>( &self, stmt: S, params: I, ) -> Result<S::RowStream<'a>, Error>
where S: Encode + IntoStream + 'a, I: AsParams,

flexible version of Query::_query
source§

fn _execute<S>(&self, stmt: S, params: &[&(dyn ToSql + Sync)]) -> ExecuteFuture
where S: Encode,

query that don’t return any row but number of rows affected by it
source§

fn _execute_raw<S, I>(&self, stmt: S, params: I) -> ExecuteFuture
where S: Encode, I: AsParams,

flexible version of Query::_execute

Auto Trait Implementations§

§

impl Freeze for Client

§

impl !RefUnwindSafe for Client

§

impl Send for Client

§

impl Sync for Client

§

impl Unpin for Client

§

impl !UnwindSafe for Client

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> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more