use crate::client::async_::GenericClient;
use futures::{self, StreamExt, TryStreamExt};
pub struct I32Query<'a, C: GenericClient, T, const N: usize> {
client: &'a C,
params: [&'a (dyn postgres_types::ToSql + Sync); N],
stmt: &'a mut crate::client::async_::Stmt,
extractor: fn(&tokio_postgres::Row) -> i32,
mapper: fn(i32) -> T,
}
impl<'a, C, T: 'a, const N: usize> I32Query<'a, C, T, N>
where
C: GenericClient,
{
pub fn map<R>(self, mapper: fn(i32) -> R) -> I32Query<'a, C, R, N> {
I32Query {
client: self.client,
params: self.params,
stmt: self.stmt,
extractor: self.extractor,
mapper,
}
}
pub async fn one(self) -> Result<T, tokio_postgres::Error> {
let stmt = self.stmt.prepare(self.client).await?;
let row = self.client.query_one(stmt, &self.params).await?;
Ok((self.mapper)((self.extractor)(&row)))
}
pub async fn all(self) -> Result<Vec<T>, tokio_postgres::Error> {
self.iter().await?.try_collect().await
}
pub async fn opt(self) -> Result<Option<T>, tokio_postgres::Error> {
let stmt = self.stmt.prepare(self.client).await?;
Ok(self
.client
.query_opt(stmt, &self.params)
.await?
.map(|row| (self.mapper)((self.extractor)(&row))))
}
pub async fn iter(
self,
) -> Result<
impl futures::Stream<Item = Result<T, tokio_postgres::Error>> + 'a,
tokio_postgres::Error,
> {
let stmt = self.stmt.prepare(self.client).await?;
let it = self
.client
.query_raw(stmt, crate::slice_iter(&self.params))
.await?
.map(move |res| res.map(|row| (self.mapper)((self.extractor)(&row))))
.into_stream();
Ok(it)
}
}
pub fn migrate_revision() -> MigrateRevisionStmt {
MigrateRevisionStmt(crate::client::async_::Stmt::new(
"SELECT migrations.revision
FROM
migrations",
))
}
pub struct MigrateRevisionStmt(crate::client::async_::Stmt);
impl MigrateRevisionStmt {
pub fn bind<'a, C: GenericClient>(&'a mut self, client: &'a C) -> I32Query<'a, C, i32, 0> {
I32Query {
client,
params: [],
stmt: &mut self.0,
extractor: |row| row.get(0),
mapper: |it| it,
}
}
}
pub fn set_migrate_revision() -> SetMigrateRevisionStmt {
SetMigrateRevisionStmt(crate::client::async_::Stmt::new(
"SELECT set_migrate_revision($1)",
))
}
pub struct SetMigrateRevisionStmt(crate::client::async_::Stmt);
impl SetMigrateRevisionStmt {
pub fn bind<'a, C: GenericClient>(
&'a mut self,
client: &'a C,
revision: &'a i32,
) -> I32Query<'a, C, i32, 1> {
I32Query {
client,
params: [revision],
stmt: &mut self.0,
extractor: |row| row.get(0),
mapper: |it| it,
}
}
}