[−][src]Struct roa_pg::Client
An asynchronous PostgreSQL client.
The client is one half of what is returned when a connection is established. Users interact with the database through this client object.
Methods
impl Client
[src]
pub async fn prepare(&'_ self, query: &'_ str) -> Result<Statement, Error>
[src]
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.
pub async fn prepare_typed(
&'_ self,
query: &'_ str,
parameter_types: &'_ [Type]
) -> Result<Statement, Error>
[src]
&'_ self,
query: &'_ str,
parameter_types: &'_ [Type]
) -> Result<Statement, Error>
Like prepare
, but allows the types of query parameters to be explicitly specified.
The list of types may be smaller than the number of parameters - the types of the remaining parameters will be
inferred. For example, client.prepare_typed(query, &[])
is equivalent to client.prepare(query)
.
pub async fn query<T>(
&'_ self,
statement: &'_ T,
params: &'_ [&'_ (dyn ToSql + '_ + Sync)]
) -> Result<Vec<Row>, Error> where
T: ToStatement + ?Sized,
[src]
&'_ self,
statement: &'_ T,
params: &'_ [&'_ (dyn ToSql + '_ + Sync)]
) -> Result<Vec<Row>, Error> where
T: ToStatement + ?Sized,
Executes a statement, returning a vector 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.
The statement
argument can either be a Statement
, or a raw query string. If the same statement will be
repeatedly executed (perhaps with different query parameters), consider preparing the statement up front
with the prepare
method.
Panics
Panics if the number of parameters provided does not match the number expected.
pub async fn query_one<T>(
&'_ self,
statement: &'_ T,
params: &'_ [&'_ (dyn ToSql + '_ + Sync)]
) -> Result<Row, Error> where
T: ToStatement + ?Sized,
[src]
&'_ self,
statement: &'_ T,
params: &'_ [&'_ (dyn ToSql + '_ + Sync)]
) -> Result<Row, Error> where
T: ToStatement + ?Sized,
Executes a statement which returns a single row, returning it.
Returns an error if the query does not return exactly one row.
A statement may contain parameters, specified by $n
, where n
is the index of the parameter of the list
provided, 1-indexed.
The statement
argument can either be a Statement
, or a raw query string. If the same statement will be
repeatedly executed (perhaps with different query parameters), consider preparing the statement up front
with the prepare
method.
Panics
Panics if the number of parameters provided does not match the number expected.
pub async fn query_opt<T>(
&'_ self,
statement: &'_ T,
params: &'_ [&'_ (dyn ToSql + '_ + Sync)]
) -> Result<Option<Row>, Error> where
T: ToStatement + ?Sized,
[src]
&'_ self,
statement: &'_ T,
params: &'_ [&'_ (dyn ToSql + '_ + Sync)]
) -> Result<Option<Row>, Error> where
T: ToStatement + ?Sized,
Executes a statements which returns zero or one rows, returning it.
Returns an error if the query returns more than one row.
A statement may contain parameters, specified by $n
, where n
is the index of the parameter of the list
provided, 1-indexed.
The statement
argument can either be a Statement
, or a raw query string. If the same statement will be
repeatedly executed (perhaps with different query parameters), consider preparing the statement up front
with the prepare
method.
Panics
Panics if the number of parameters provided does not match the number expected.
pub async fn query_raw<'a, T, I>(
&'_ self,
statement: &'_ T,
params: I
) -> Result<RowStream, Error> where
I: IntoIterator<Item = &'a (dyn ToSql + 'a)>,
T: ToStatement + ?Sized,
<I as IntoIterator>::IntoIter: ExactSizeIterator,
[src]
&'_ self,
statement: &'_ T,
params: I
) -> Result<RowStream, Error> where
I: IntoIterator<Item = &'a (dyn ToSql + 'a)>,
T: ToStatement + ?Sized,
<I as IntoIterator>::IntoIter: ExactSizeIterator,
The maximally flexible version of query
.
A statement may contain parameters, specified by $n
, where n
is the index of the parameter of the list
provided, 1-indexed.
The statement
argument can either be a Statement
, or a raw query string. If the same statement will be
repeatedly executed (perhaps with different query parameters), consider preparing the statement up front
with the prepare
method.
Panics
Panics if the number of parameters provided does not match the number expected.
Examples
If you have a type like Vec<T>
where T: ToSql
Rust will not know how to use it as params. To get around
this the type must explicitly be converted to &dyn ToSql
.
use tokio_postgres::types::ToSql; use futures::{pin_mut, TryStreamExt}; let params: Vec<String> = vec![ "first param".into(), "second param".into(), ]; let mut it = client.query_raw( "SELECT foo FROM bar WHERE biz = $1 AND baz = $2", params.iter().map(|p| p as &dyn ToSql), ).await?; pin_mut!(it); while let Some(row) = it.try_next().await? { let foo: i32 = row.get("foo"); println!("foo: {}", foo); }
pub async fn execute<T>(
&'_ self,
statement: &'_ T,
params: &'_ [&'_ (dyn ToSql + '_ + Sync)]
) -> Result<u64, Error> where
T: ToStatement + ?Sized,
[src]
&'_ self,
statement: &'_ T,
params: &'_ [&'_ (dyn ToSql + '_ + Sync)]
) -> Result<u64, Error> where
T: ToStatement + ?Sized,
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.
The statement
argument can either be a Statement
, or a raw query string. If the same statement will be
repeatedly executed (perhaps with different query parameters), consider preparing the statement up front
with the prepare
method.
If the statement does not modify any rows (e.g. SELECT
), 0 is returned.
Panics
Panics if the number of parameters provided does not match the number expected.
pub async fn execute_raw<'a, T, I>(
&'_ self,
statement: &'_ T,
params: I
) -> Result<u64, Error> where
I: IntoIterator<Item = &'a (dyn ToSql + 'a)>,
T: ToStatement + ?Sized,
<I as IntoIterator>::IntoIter: ExactSizeIterator,
[src]
&'_ self,
statement: &'_ T,
params: I
) -> Result<u64, Error> where
I: IntoIterator<Item = &'a (dyn ToSql + 'a)>,
T: ToStatement + ?Sized,
<I as IntoIterator>::IntoIter: ExactSizeIterator,
The maximally flexible version of execute
.
A statement may contain parameters, specified by $n
, where n
is the index of the parameter of the list
provided, 1-indexed.
The statement
argument can either be a Statement
, or a raw query string. If the same statement will be
repeatedly executed (perhaps with different query parameters), consider preparing the statement up front
with the prepare
method.
Panics
Panics if the number of parameters provided does not match the number expected.
pub async fn copy_in<T, U>(
&'_ self,
statement: &'_ T
) -> Result<CopyInSink<U>, Error> where
T: ToStatement + ?Sized,
U: Buf + 'static + Send,
[src]
&'_ self,
statement: &'_ T
) -> Result<CopyInSink<U>, Error> where
T: ToStatement + ?Sized,
U: Buf + 'static + 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 the Sink::close
or finish
methods. If it is not, the copy will be aborted.
Panics
Panics if the statement contains parameters.
pub async fn copy_out<T>(
&'_ self,
statement: &'_ T
) -> Result<CopyOutStream, Error> where
T: ToStatement + ?Sized,
[src]
&'_ self,
statement: &'_ T
) -> Result<CopyOutStream, Error> where
T: ToStatement + ?Sized,
Executes a COPY TO STDOUT
statement, returning a stream of the resulting data.
PostgreSQL does not support parameters in COPY
statements, so this method does not take any.
Panics
Panics if the statement contains parameters.
pub async fn simple_query(
&'_ self,
query: &'_ str
) -> Result<Vec<SimpleQueryMessage>, Error>
[src]
&'_ self,
query: &'_ str
) -> Result<Vec<SimpleQueryMessage>, Error>
Executes a sequence of SQL statements using the simple query protocol, returning the resulting 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!
pub async fn batch_execute(&'_ self, query: &'_ str) -> Result<(), Error>
[src]
Executes a sequence of SQL statements using the simple query protocol.
Statements should be separated by semicolons. If an error occurs, execution of the sequence will stop at that point. This is intended for use when, for example, initializing a database schema.
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!
pub async fn transaction(&'_ mut self) -> Result<Transaction<'_>, Error>
[src]
Begins a new database transaction.
The transaction will roll back by default - use the commit
method to commit it.
pub fn build_transaction(&mut self) -> TransactionBuilder
[src]
Returns a builder for a transaction with custom settings.
Unlike the transaction
method, the builder can be used to control the transaction's isolation level and other
attributes.
pub fn cancel_token(&self) -> CancelToken
[src]
Constructs a cancellation token that can later be used to request cancellation of a query running on the connection associated with this client.
pub async fn cancel_query_raw<S, T>(
&'_ self,
stream: S,
tls: T
) -> Result<(), Error> where
S: AsyncRead + AsyncWrite + Unpin,
T: TlsConnect<S>,
[src]
&'_ self,
stream: S,
tls: T
) -> Result<(), Error> where
S: AsyncRead + AsyncWrite + Unpin,
T: TlsConnect<S>,
use Client::cancel_token() instead
Like cancel_query
, but uses a stream which is already connected to the server rather than opening a new
connection itself.
pub fn is_closed(&self) -> bool
[src]
Determines if the connection to the server has already closed.
In that case, all future queries will fail.
Trait Implementations
impl Debug for Client
[src]
impl GenericClient for Client
[src]
fn execute<'life0, 'life1, 'life2, 'life3, 'async_trait, T>(
&'life0 self,
query: &'life1 T,
params: &'life2 [&'life3 (dyn ToSql + 'life3 + Sync)]
) -> Pin<Box<dyn Future<Output = Result<u64, Error>> + 'async_trait + Send>> where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
T: ToStatement + Sync + Send + 'async_trait + ?Sized,
Client: 'async_trait,
[src]
&'life0 self,
query: &'life1 T,
params: &'life2 [&'life3 (dyn ToSql + 'life3 + Sync)]
) -> Pin<Box<dyn Future<Output = Result<u64, Error>> + 'async_trait + Send>> where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
T: ToStatement + Sync + Send + 'async_trait + ?Sized,
Client: 'async_trait,
fn execute_raw<'b, 'life0, 'life1, 'async_trait, I, T>(
&'life0 self,
statement: &'life1 T,
params: I
) -> Pin<Box<dyn Future<Output = Result<u64, Error>> + 'async_trait + Send>> where
'b: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
I: IntoIterator<Item = &'b (dyn ToSql + 'b)> + Sync + Send + 'async_trait,
T: ToStatement + Sync + Send + 'async_trait + ?Sized,
<I as IntoIterator>::IntoIter: ExactSizeIterator,
Client: 'async_trait,
[src]
&'life0 self,
statement: &'life1 T,
params: I
) -> Pin<Box<dyn Future<Output = Result<u64, Error>> + 'async_trait + Send>> where
'b: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
I: IntoIterator<Item = &'b (dyn ToSql + 'b)> + Sync + Send + 'async_trait,
T: ToStatement + Sync + Send + 'async_trait + ?Sized,
<I as IntoIterator>::IntoIter: ExactSizeIterator,
Client: 'async_trait,
fn query<'life0, 'life1, 'life2, 'life3, 'async_trait, T>(
&'life0 self,
query: &'life1 T,
params: &'life2 [&'life3 (dyn ToSql + 'life3 + Sync)]
) -> Pin<Box<dyn Future<Output = Result<Vec<Row>, Error>> + 'async_trait + Send>> where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
T: ToStatement + Sync + Send + 'async_trait + ?Sized,
Client: 'async_trait,
[src]
&'life0 self,
query: &'life1 T,
params: &'life2 [&'life3 (dyn ToSql + 'life3 + Sync)]
) -> Pin<Box<dyn Future<Output = Result<Vec<Row>, Error>> + 'async_trait + Send>> where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
T: ToStatement + Sync + Send + 'async_trait + ?Sized,
Client: 'async_trait,
fn query_one<'life0, 'life1, 'life2, 'life3, 'async_trait, T>(
&'life0 self,
statement: &'life1 T,
params: &'life2 [&'life3 (dyn ToSql + 'life3 + Sync)]
) -> Pin<Box<dyn Future<Output = Result<Row, Error>> + 'async_trait + Send>> where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
T: ToStatement + Sync + Send + 'async_trait + ?Sized,
Client: 'async_trait,
[src]
&'life0 self,
statement: &'life1 T,
params: &'life2 [&'life3 (dyn ToSql + 'life3 + Sync)]
) -> Pin<Box<dyn Future<Output = Result<Row, Error>> + 'async_trait + Send>> where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
T: ToStatement + Sync + Send + 'async_trait + ?Sized,
Client: 'async_trait,
fn query_opt<'life0, 'life1, 'life2, 'life3, 'async_trait, T>(
&'life0 self,
statement: &'life1 T,
params: &'life2 [&'life3 (dyn ToSql + 'life3 + Sync)]
) -> Pin<Box<dyn Future<Output = Result<Option<Row>, Error>> + 'async_trait + Send>> where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
T: ToStatement + Sync + Send + 'async_trait + ?Sized,
Client: 'async_trait,
[src]
&'life0 self,
statement: &'life1 T,
params: &'life2 [&'life3 (dyn ToSql + 'life3 + Sync)]
) -> Pin<Box<dyn Future<Output = Result<Option<Row>, Error>> + 'async_trait + Send>> where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
T: ToStatement + Sync + Send + 'async_trait + ?Sized,
Client: 'async_trait,
fn query_raw<'b, 'life0, 'life1, 'async_trait, T, I>(
&'life0 self,
statement: &'life1 T,
params: I
) -> Pin<Box<dyn Future<Output = Result<RowStream, Error>> + 'async_trait + Send>> where
'b: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
I: IntoIterator<Item = &'b (dyn ToSql + 'b)> + Sync + Send + 'async_trait,
T: ToStatement + Sync + Send + 'async_trait + ?Sized,
<I as IntoIterator>::IntoIter: ExactSizeIterator,
Client: 'async_trait,
[src]
&'life0 self,
statement: &'life1 T,
params: I
) -> Pin<Box<dyn Future<Output = Result<RowStream, Error>> + 'async_trait + Send>> where
'b: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
I: IntoIterator<Item = &'b (dyn ToSql + 'b)> + Sync + Send + 'async_trait,
T: ToStatement + Sync + Send + 'async_trait + ?Sized,
<I as IntoIterator>::IntoIter: ExactSizeIterator,
Client: 'async_trait,
fn prepare<'life0, 'life1, 'async_trait>(
&'life0 self,
query: &'life1 str
) -> Pin<Box<dyn Future<Output = Result<Statement, Error>> + 'async_trait + Send>> where
'life0: 'async_trait,
'life1: 'async_trait,
Client: 'async_trait,
[src]
&'life0 self,
query: &'life1 str
) -> Pin<Box<dyn Future<Output = Result<Statement, Error>> + 'async_trait + Send>> where
'life0: 'async_trait,
'life1: 'async_trait,
Client: 'async_trait,
fn prepare_typed<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
query: &'life1 str,
parameter_types: &'life2 [Type]
) -> Pin<Box<dyn Future<Output = Result<Statement, Error>> + 'async_trait + Send>> where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Client: 'async_trait,
[src]
&'life0 self,
query: &'life1 str,
parameter_types: &'life2 [Type]
) -> Pin<Box<dyn Future<Output = Result<Statement, Error>> + 'async_trait + Send>> where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Client: 'async_trait,
fn transaction<'life0, 'async_trait>(
&'life0 mut self
) -> Pin<Box<dyn Future<Output = Result<Transaction<'life0>, Error>> + 'async_trait + Send>> where
'life0: 'async_trait,
Client: 'async_trait,
[src]
&'life0 mut self
) -> Pin<Box<dyn Future<Output = Result<Transaction<'life0>, Error>> + 'async_trait + Send>> where
'life0: 'async_trait,
Client: 'async_trait,
Auto Trait Implementations
impl !RefUnwindSafe for Client
impl Send for Client
impl Sync for Client
impl Unpin for Client
impl !UnwindSafe for Client
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> Same<T> for T
type Output = T
Should always be Self
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
V: MultiLane<T>,