use std::fmt::{self, Debug, Formatter}; #[cfg(feature = "async")] use futures_util::future::{BoxFuture, FutureExt, TryFutureExt}; use sqlx_core::net::Stream as NetStream; use sqlx_core::{Close, Connect, Connection, Runtime}; use crate::protocol::backend::TransactionStatus; use crate::stream::PgStream; use crate::{PgConnectOptions, Postgres}; #[macro_use] mod flush; mod connect; mod executor; /// A single connection (also known as a session) to a /// PostgreSQL database server. pub struct PgConnection { stream: PgStream, // number of commands that have been executed // and have yet to see their completion acknowledged // in other words, the number of messages // we expect before the stream is clear pending_ready_for_query_count: usize, // current transaction status transaction_status: TransactionStatus, // process id of this backend // can be used to send cancel requests #[allow(dead_code)] process_id: u32, // secret key of this backend // can be used to send cancel requests #[allow(dead_code)] secret_key: u32, } impl Debug for PgConnection where Rt: Runtime, { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { f.debug_struct("PgConnection").finish() } } impl PgConnection { pub(crate) fn new(stream: NetStream) -> Self { Self { stream: PgStream::new(stream), process_id: 0, secret_key: 0, transaction_status: TransactionStatus::Idle, pending_ready_for_query_count: 0, } } } impl Connection for PgConnection { type Database = Postgres; #[cfg(feature = "async")] fn ping(&mut self) -> BoxFuture<'_, sqlx_core::Result<()>> where Rt: sqlx_core::Async, { todo!() } #[cfg(feature = "async")] fn describe<'x, 'e, 'q>( &'e mut self, query: &'q str, ) -> BoxFuture<'x, sqlx_core::Result>> where Rt: sqlx_core::Async, 'e: 'x, 'q: 'x, { todo!() } } impl Connect for PgConnection { type Options = PgConnectOptions; #[cfg(feature = "async")] fn connect_with(options: &PgConnectOptions) -> BoxFuture<'_, sqlx_core::Result> where Self: Sized, Rt: sqlx_core::Async, { PgConnection::connect_async(options).boxed() } } impl Close for PgConnection { #[cfg(feature = "async")] fn close(mut self) -> BoxFuture<'static, sqlx_core::Result<()>> where Rt: sqlx_core::Async, { Box::pin(async move { self.stream.close_async().await?; Ok(()) }) } } #[cfg(feature = "blocking")] mod blocking { use sqlx_core::blocking::{Close, Connect, Connection, Runtime}; use super::{PgConnectOptions, PgConnection, Postgres}; impl Connection for PgConnection { #[inline] fn ping(&mut self) -> sqlx_core::Result<()> { todo!() } fn describe<'x, 'e, 'q>( &'e mut self, query: &'q str, ) -> sqlx_core::Result> where 'e: 'x, 'q: 'x, { todo!() } } impl Connect for PgConnection { #[inline] fn connect_with(options: &PgConnectOptions) -> sqlx_core::Result where Self: Sized, { Self::connect_blocking(options) } } impl Close for PgConnection { #[inline] fn close(mut self) -> sqlx_core::Result<()> { self.stream.close_blocking() } } }