use std::fmt;
use std::future::poll_fn;
use std::pin::Pin;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use std::task::{Context, Poll};
use futures_core::Stream;
use super::types::{Decoded, FromSql, Type};
use super::{db_err, Error};
pub use tokio_postgres::Column;
pub struct Row(tokio_postgres::Row);
impl Row {
pub(super) fn new(row: tokio_postgres::Row) -> Self {
Self(row)
}
pub fn columns(&self) -> &[Column] {
self.0.columns()
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn len(&self) -> usize {
self.0.len()
}
pub fn get<'a, I, T>(&'a self, idx: I) -> T
where
I: RowIndex + fmt::Display,
T: FromSql<'a>,
{
match self.try_get(&idx) {
Ok(value) => value,
Err(err) => panic!("error retrieving column {idx}: {err}"),
}
}
pub fn try_get<'a, I, T>(&'a self, idx: I) -> Result<T, Error>
where
I: RowIndex + fmt::Display,
T: FromSql<'a>,
{
self.0
.try_get::<I, Decoded<T>>(idx)
.map(|value| value.0)
.map_err(|err| Error::Other(Box::new(err)))
}
}
impl fmt::Debug for Row {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
pub trait RowIndex: tokio_postgres::row::RowIndex {}
impl<I: tokio_postgres::row::RowIndex + ?Sized> RowIndex for I {}
pub struct RowStream {
inner: Pin<Box<tokio_postgres::RowStream>>,
}
impl RowStream {
pub(super) fn new(inner: tokio_postgres::RowStream) -> Self {
Self {
inner: Box::pin(inner),
}
}
pub async fn next(&mut self) -> Option<Result<Row, Error>> {
poll_fn(|cx| Pin::new(&mut *self).poll_next(cx)).await
}
pub fn rows_affected(&self) -> Option<u64> {
self.inner.rows_affected()
}
}
impl Stream for RowStream {
type Item = Result<Row, Error>;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
self.get_mut()
.inner
.as_mut()
.poll_next(cx)
.map(|row| row.map(|row| row.map(Row::new).map_err(db_err)))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) struct ConnId(u64);
impl ConnId {
pub(super) fn new() -> Self {
static NEXT: AtomicU64 = AtomicU64::new(0);
Self(NEXT.fetch_add(1, Ordering::Relaxed))
}
}
#[derive(Clone)]
pub struct Statement {
pub(super) inner: tokio_postgres::Statement,
pub(super) conn: ConnId,
pub(super) query: Arc<str>,
}
impl Statement {
pub fn params(&self) -> &[Type] {
self.inner.params()
}
pub fn columns(&self) -> &[Column] {
self.inner.columns()
}
}
mod sealed {
pub trait Sealed {}
}
pub trait ToStatement: sealed::Sealed + Sync {
#[doc(hidden)]
fn __convert(&self) -> StatementRef<'_>;
}
#[doc(hidden)]
pub enum StatementRef<'a> {
Statement(&'a Statement),
Query(&'a str),
}
impl StatementRef<'_> {
pub(super) fn text(&self) -> &str {
match self {
StatementRef::Statement(statement) => &statement.query,
StatementRef::Query(query) => query,
}
}
}
impl sealed::Sealed for Statement {}
impl ToStatement for Statement {
fn __convert(&self) -> StatementRef<'_> {
StatementRef::Statement(self)
}
}
impl sealed::Sealed for str {}
impl ToStatement for str {
fn __convert(&self) -> StatementRef<'_> {
StatementRef::Query(self)
}
}
impl sealed::Sealed for String {}
impl ToStatement for String {
fn __convert(&self) -> StatementRef<'_> {
StatementRef::Query(self)
}
}