encore/storage/postgres/
row.rs1use std::fmt;
4use std::future::poll_fn;
5use std::pin::Pin;
6use std::sync::atomic::{AtomicU64, Ordering};
7use std::sync::Arc;
8use std::task::{Context, Poll};
9
10use futures_core::Stream;
11
12use crate::error::{ErrCode, Error};
13
14use super::db_err;
15use super::types::{Decoded, FromSql, Type};
16
17pub use tokio_postgres::Column;
18
19pub struct Row(tokio_postgres::Row);
21
22impl Row {
23 pub(super) fn new(row: tokio_postgres::Row) -> Self {
24 Self(row)
25 }
26
27 pub fn columns(&self) -> &[Column] {
29 self.0.columns()
30 }
31
32 pub fn is_empty(&self) -> bool {
34 self.0.is_empty()
35 }
36
37 pub fn len(&self) -> usize {
39 self.0.len()
40 }
41
42 pub fn get<'a, I, T>(&'a self, idx: I) -> T
55 where
56 I: RowIndex + fmt::Display,
57 T: FromSql<'a>,
58 {
59 match self.try_get(&idx) {
60 Ok(value) => value,
61 Err(err) => panic!("error retrieving column {idx}: {}", err.message),
62 }
63 }
64
65 pub fn try_get<'a, I, T>(&'a self, idx: I) -> Result<T, Error>
67 where
68 I: RowIndex + fmt::Display,
69 T: FromSql<'a>,
70 {
71 self.0
72 .try_get::<I, Decoded<T>>(idx)
73 .map(|value| value.0)
74 .map_err(|err| Error::wrap(ErrCode::Internal, err))
75 }
76}
77
78impl fmt::Debug for Row {
79 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
80 self.0.fmt(f)
81 }
82}
83
84pub trait RowIndex: tokio_postgres::row::RowIndex {}
87
88impl<I: tokio_postgres::row::RowIndex + ?Sized> RowIndex for I {}
89
90pub struct RowStream {
100 inner: Pin<Box<tokio_postgres::RowStream>>,
101}
102
103impl RowStream {
104 pub(super) fn new(inner: tokio_postgres::RowStream) -> Self {
105 Self {
106 inner: Box::pin(inner),
107 }
108 }
109
110 pub async fn next(&mut self) -> Option<Result<Row, Error>> {
112 poll_fn(|cx| Pin::new(&mut *self).poll_next(cx)).await
113 }
114
115 pub fn rows_affected(&self) -> Option<u64> {
118 self.inner.rows_affected()
119 }
120}
121
122impl Stream for RowStream {
123 type Item = Result<Row, Error>;
124
125 fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
126 self.get_mut()
127 .inner
128 .as_mut()
129 .poll_next(cx)
130 .map(|row| row.map(|row| row.map(Row::new).map_err(db_err)))
131 }
132}
133
134#[derive(Debug, Clone, Copy, PartialEq, Eq)]
136pub(super) struct ConnId(u64);
137
138impl ConnId {
139 pub(super) fn new() -> Self {
140 static NEXT: AtomicU64 = AtomicU64::new(0);
141 Self(NEXT.fetch_add(1, Ordering::Relaxed))
142 }
143}
144
145#[derive(Clone)]
151pub struct Statement {
152 pub(super) inner: tokio_postgres::Statement,
153 pub(super) conn: ConnId,
154 pub(super) query: Arc<str>,
155}
156
157impl Statement {
158 pub fn params(&self) -> &[Type] {
160 self.inner.params()
161 }
162
163 pub fn columns(&self) -> &[Column] {
165 self.inner.columns()
166 }
167}
168
169mod sealed {
170 pub trait Sealed {}
171}
172
173pub trait ToStatement: sealed::Sealed + Sync {
176 #[doc(hidden)]
177 fn __convert(&self) -> StatementRef<'_>;
178}
179
180#[doc(hidden)]
181pub enum StatementRef<'a> {
182 Statement(&'a Statement),
183 Query(&'a str),
184}
185
186impl StatementRef<'_> {
187 pub(super) fn text(&self) -> &str {
189 match self {
190 StatementRef::Statement(statement) => &statement.query,
191 StatementRef::Query(query) => query,
192 }
193 }
194}
195
196impl sealed::Sealed for Statement {}
197
198impl ToStatement for Statement {
199 fn __convert(&self) -> StatementRef<'_> {
200 StatementRef::Statement(self)
201 }
202}
203
204impl sealed::Sealed for str {}
205
206impl ToStatement for str {
207 fn __convert(&self) -> StatementRef<'_> {
208 StatementRef::Query(self)
209 }
210}
211
212impl sealed::Sealed for String {}
213
214impl ToStatement for String {
215 fn __convert(&self) -> StatementRef<'_> {
216 StatementRef::Query(self)
217 }
218}