From abe423e8acfa595befd0a7d7343d28c799406e05 Mon Sep 17 00:00:00 2001 From: Virgiel <> Date: Sat, 13 May 2023 21:46:09 +0200 Subject: [PATCH] Cleaning --- .../cornucopia_benches/generated/Cargo.toml | 43 - .../generated/src/client/array_iterator.rs | 66 -- .../generated/src/client/async_/deadpool.rs | 132 --- .../src/client/async_/generic_client.rs | 171 --- .../generated/src/client/async_/mod.rs | 14 - .../generated/src/client/async_/private.rs | 32 - .../generated/src/client/domain.rs | 105 -- .../generated/src/client/mod.rs | 16 - .../generated/src/client/sync/mod.rs | 8 - .../generated/src/client/sync/private.rs | 31 - .../generated/src/client/type_traits.rs | 149 --- .../generated/src/client/utils.rs | 14 - .../cornucopia_benches/generated/src/lib.rs | 981 ------------------ benches/temp/Cargo.toml | 38 - benches/temp/src/client.rs | 11 - benches/temp/src/client/array_iterator.rs | 57 - benches/temp/src/client/async_.rs | 34 - benches/temp/src/client/async_/deadpool.rs | 119 --- .../temp/src/client/async_/generic_client.rs | 157 --- benches/temp/src/client/domain.rs | 90 -- benches/temp/src/client/sync.rs | 30 - benches/temp/src/client/type_traits.rs | 120 --- benches/temp/src/client/utils.rs | 12 - benches/temp/src/lib.rs | 957 ----------------- crates/cornucopia/src/codegen/cargo.rs | 1 - examples/auto_build/_build.rs | 8 +- 26 files changed, 3 insertions(+), 3393 deletions(-) delete mode 100644 benches/execution/cornucopia_benches/generated/Cargo.toml delete mode 100644 benches/execution/cornucopia_benches/generated/src/client/array_iterator.rs delete mode 100644 benches/execution/cornucopia_benches/generated/src/client/async_/deadpool.rs delete mode 100644 benches/execution/cornucopia_benches/generated/src/client/async_/generic_client.rs delete mode 100644 benches/execution/cornucopia_benches/generated/src/client/async_/mod.rs delete mode 100644 benches/execution/cornucopia_benches/generated/src/client/async_/private.rs delete mode 100644 benches/execution/cornucopia_benches/generated/src/client/domain.rs delete mode 100644 benches/execution/cornucopia_benches/generated/src/client/mod.rs delete mode 100644 benches/execution/cornucopia_benches/generated/src/client/sync/mod.rs delete mode 100644 benches/execution/cornucopia_benches/generated/src/client/sync/private.rs delete mode 100644 benches/execution/cornucopia_benches/generated/src/client/type_traits.rs delete mode 100644 benches/execution/cornucopia_benches/generated/src/client/utils.rs delete mode 100644 benches/execution/cornucopia_benches/generated/src/lib.rs delete mode 100644 benches/temp/Cargo.toml delete mode 100644 benches/temp/src/client.rs delete mode 100644 benches/temp/src/client/array_iterator.rs delete mode 100644 benches/temp/src/client/async_.rs delete mode 100644 benches/temp/src/client/async_/deadpool.rs delete mode 100644 benches/temp/src/client/async_/generic_client.rs delete mode 100644 benches/temp/src/client/domain.rs delete mode 100644 benches/temp/src/client/sync.rs delete mode 100644 benches/temp/src/client/type_traits.rs delete mode 100644 benches/temp/src/client/utils.rs delete mode 100644 benches/temp/src/lib.rs diff --git a/benches/execution/cornucopia_benches/generated/Cargo.toml b/benches/execution/cornucopia_benches/generated/Cargo.toml deleted file mode 100644 index 6ac2c650..00000000 --- a/benches/execution/cornucopia_benches/generated/Cargo.toml +++ /dev/null @@ -1,43 +0,0 @@ - - # This file was generated with `cornucopia`. Do not modify - [package] - name = "generated" - version = "0.9.0" - edition = "2021" - - [dependencies] - #TODO - # Required - postgres = "*" - postgres-types = { version = "*", features = ["derive"] } - # Postgres interaction -postgres-protocol = "0.6.4" -## Iterator utils required for working with `postgres_protocol::types::ArrayValues` -fallible-iterator = "0.2.0" - # Async - tokio = { version = "*", features = ["full"] } - tokio-postgres = { version = "*", features = [ - "with-serde_json-1", - "with-time-0_3", - "with-uuid-1", - "with-eui48-1", - ] } - futures = "*" - # Async connection pooling - deadpool-postgres = { version = "*" } - - # Row serialization - serde = { version = "*", features = ["derive"] } - - # Extra types - serde_json = "*" - time = "*" - uuid = "*" - eui48 = "*" - rust_decimal = { version = "*", features = ["db-postgres"] } - - # async -async-trait = "0.1.63" - - - diff --git a/benches/execution/cornucopia_benches/generated/src/client/array_iterator.rs b/benches/execution/cornucopia_benches/generated/src/client/array_iterator.rs deleted file mode 100644 index c27fba8e..00000000 --- a/benches/execution/cornucopia_benches/generated/src/client/array_iterator.rs +++ /dev/null @@ -1,66 +0,0 @@ -use fallible_iterator::FallibleIterator; -use postgres_protocol::types::{array_from_sql, ArrayValues}; -use postgres_types::{FromSql, Kind, Type}; -use std::fmt::Debug; -use std::marker::PhantomData; - -use super::utils::escape_domain; - -/// Iterator over the items in a PostgreSQL array. You only need this if you are -/// working with custom zero-cost type mapping of rows containing PostgreSQL arrays. -pub struct ArrayIterator<'a, T: FromSql<'a>> { - values: ArrayValues<'a>, - ty: Type, - _type: PhantomData, -} - -impl<'a, T: FromSql<'a>> Debug for ArrayIterator<'a, T> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("ArrayIterator") - .field("values", &"[T]") - .field("ty", &self.ty) - .field("_type", &self._type) - .finish() - } -} - -impl<'a, T: FromSql<'a>> Iterator for ArrayIterator<'a, T> { - type Item = T; - - fn next(&mut self) -> Option { - self.values - .next() - .unwrap() - .map(|raw| T::from_sql_nullable(&self.ty, raw).unwrap()) - } -} - -impl<'a, T: FromSql<'a>> FromSql<'a> for ArrayIterator<'a, T> { - fn from_sql( - ty: &Type, - raw: &'a [u8], - ) -> Result, Box> { - let member_type = match *escape_domain(ty).kind() { - Kind::Array(ref member) => escape_domain(member), - _ => panic!("expected array type got {ty}"), - }; - - let array = array_from_sql(raw)?; - if array.dimensions().count()? > 1 { - return Err("array contains too many dimensions".into()); - } - - Ok(ArrayIterator { - ty: member_type.clone(), - values: array.values(), - _type: PhantomData::default(), - }) - } - - fn accepts(ty: &Type) -> bool { - match *ty.kind() { - Kind::Array(ref inner) => T::accepts(escape_domain(inner)), - _ => false, - } - } -} diff --git a/benches/execution/cornucopia_benches/generated/src/client/async_/deadpool.rs b/benches/execution/cornucopia_benches/generated/src/client/async_/deadpool.rs deleted file mode 100644 index 076731f1..00000000 --- a/benches/execution/cornucopia_benches/generated/src/client/async_/deadpool.rs +++ /dev/null @@ -1,132 +0,0 @@ -use async_trait::async_trait; -use deadpool_postgres::{ - Client as DeadpoolClient, ClientWrapper, Transaction as DeadpoolTransaction, -}; -use tokio_postgres::{ - types::BorrowToSql, Client as PgClient, Error, RowStream, Statement, ToStatement, - Transaction as PgTransaction, -}; - -use super::generic_client::GenericClient; - -#[async_trait] -impl GenericClient for DeadpoolClient { - async fn prepare(&self, query: &str) -> Result { - ClientWrapper::prepare_cached(self, query).await - } - - async fn execute( - &self, - query: &T, - params: &[&(dyn tokio_postgres::types::ToSql + Sync)], - ) -> Result - where - T: ?Sized + tokio_postgres::ToStatement + Sync + Send, - { - PgClient::execute(self, query, params).await - } - - async fn query_one( - &self, - statement: &T, - params: &[&(dyn tokio_postgres::types::ToSql + Sync)], - ) -> Result - where - T: ?Sized + tokio_postgres::ToStatement + Sync + Send, - { - PgClient::query_one(self, statement, params).await - } - - async fn query_opt( - &self, - statement: &T, - params: &[&(dyn tokio_postgres::types::ToSql + Sync)], - ) -> Result, Error> - where - T: ?Sized + tokio_postgres::ToStatement + Sync + Send, - { - PgClient::query_opt(self, statement, params).await - } - - async fn query( - &self, - query: &T, - params: &[&(dyn tokio_postgres::types::ToSql + Sync)], - ) -> Result, Error> - where - T: ?Sized + tokio_postgres::ToStatement + Sync + Send, - { - PgClient::query(self, query, params).await - } - - async fn query_raw(&self, statement: &T, params: I) -> Result - where - T: ?Sized + ToStatement + Sync + Send, - P: BorrowToSql, - I: IntoIterator + Sync + Send, - I::IntoIter: ExactSizeIterator, - { - PgClient::query_raw(self, statement, params).await - } -} - -#[async_trait] -impl GenericClient for DeadpoolTransaction<'_> { - async fn prepare(&self, query: &str) -> Result { - DeadpoolTransaction::prepare_cached(self, query).await - } - - async fn execute( - &self, - query: &T, - params: &[&(dyn tokio_postgres::types::ToSql + Sync)], - ) -> Result - where - T: ?Sized + tokio_postgres::ToStatement + Sync + Send, - { - PgTransaction::execute(self, query, params).await - } - - async fn query_one( - &self, - statement: &T, - params: &[&(dyn tokio_postgres::types::ToSql + Sync)], - ) -> Result - where - T: ?Sized + tokio_postgres::ToStatement + Sync + Send, - { - PgTransaction::query_one(self, statement, params).await - } - - async fn query_opt( - &self, - statement: &T, - params: &[&(dyn tokio_postgres::types::ToSql + Sync)], - ) -> Result, Error> - where - T: ?Sized + tokio_postgres::ToStatement + Sync + Send, - { - PgTransaction::query_opt(self, statement, params).await - } - - async fn query( - &self, - query: &T, - params: &[&(dyn tokio_postgres::types::ToSql + Sync)], - ) -> Result, Error> - where - T: ?Sized + tokio_postgres::ToStatement + Sync + Send, - { - PgTransaction::query(self, query, params).await - } - - async fn query_raw(&self, statement: &T, params: I) -> Result - where - T: ?Sized + ToStatement + Sync + Send, - P: BorrowToSql, - I: IntoIterator + Sync + Send, - I::IntoIter: ExactSizeIterator, - { - PgTransaction::query_raw(self, statement, params).await - } -} diff --git a/benches/execution/cornucopia_benches/generated/src/client/async_/generic_client.rs b/benches/execution/cornucopia_benches/generated/src/client/async_/generic_client.rs deleted file mode 100644 index e0042c9b..00000000 --- a/benches/execution/cornucopia_benches/generated/src/client/async_/generic_client.rs +++ /dev/null @@ -1,171 +0,0 @@ -use async_trait::async_trait; -use tokio_postgres::{ - types::BorrowToSql, Client, Error, RowStream, Statement, ToStatement, Transaction, -}; - -/// Abstraction over multiple types of asynchronous clients. -/// This allows you to use tokio_postgres clients and transactions interchangeably. -/// -/// In addition, when the `deadpool` feature is enabled (default), this trait also -/// abstracts over deadpool clients and transactions -#[async_trait] -pub trait GenericClient: Send + Sync { - async fn prepare(&self, query: &str) -> Result; - async fn execute( - &self, - query: &T, - params: &[&(dyn tokio_postgres::types::ToSql + Sync)], - ) -> Result - where - T: ?Sized + tokio_postgres::ToStatement + Sync + Send; - async fn query_one( - &self, - statement: &T, - params: &[&(dyn tokio_postgres::types::ToSql + Sync)], - ) -> Result - where - T: ?Sized + tokio_postgres::ToStatement + Sync + Send; - async fn query_opt( - &self, - statement: &T, - params: &[&(dyn tokio_postgres::types::ToSql + Sync)], - ) -> Result, Error> - where - T: ?Sized + tokio_postgres::ToStatement + Sync + Send; - async fn query( - &self, - query: &T, - params: &[&(dyn tokio_postgres::types::ToSql + Sync)], - ) -> Result, Error> - where - T: ?Sized + tokio_postgres::ToStatement + Sync + Send; - - async fn query_raw(&self, statement: &T, params: I) -> Result - where - T: ?Sized + ToStatement + Sync + Send, - P: BorrowToSql, - I: IntoIterator + Sync + Send, - I::IntoIter: ExactSizeIterator; -} - -#[async_trait] -impl GenericClient for Transaction<'_> { - async fn prepare(&self, query: &str) -> Result { - Transaction::prepare(self, query).await - } - - async fn execute( - &self, - query: &T, - params: &[&(dyn tokio_postgres::types::ToSql + Sync)], - ) -> Result - where - T: ?Sized + tokio_postgres::ToStatement + Sync + Send, - { - Transaction::execute(self, query, params).await - } - - async fn query_one( - &self, - statement: &T, - params: &[&(dyn tokio_postgres::types::ToSql + Sync)], - ) -> Result - where - T: ?Sized + tokio_postgres::ToStatement + Sync + Send, - { - Transaction::query_one(self, statement, params).await - } - - async fn query_opt( - &self, - statement: &T, - params: &[&(dyn tokio_postgres::types::ToSql + Sync)], - ) -> Result, Error> - where - T: ?Sized + tokio_postgres::ToStatement + Sync + Send, - { - Transaction::query_opt(self, statement, params).await - } - - async fn query( - &self, - query: &T, - params: &[&(dyn tokio_postgres::types::ToSql + Sync)], - ) -> Result, Error> - where - T: ?Sized + tokio_postgres::ToStatement + Sync + Send, - { - Transaction::query(self, query, params).await - } - - async fn query_raw(&self, statement: &T, params: I) -> Result - where - T: ?Sized + ToStatement + Sync + Send, - P: BorrowToSql, - I: IntoIterator + Sync + Send, - I::IntoIter: ExactSizeIterator, - { - Transaction::query_raw(self, statement, params).await - } -} - -#[async_trait] -impl GenericClient for Client { - async fn prepare(&self, query: &str) -> Result { - Client::prepare(self, query).await - } - - async fn execute( - &self, - query: &T, - params: &[&(dyn tokio_postgres::types::ToSql + Sync)], - ) -> Result - where - T: ?Sized + tokio_postgres::ToStatement + Sync + Send, - { - Client::execute(self, query, params).await - } - - async fn query_one( - &self, - statement: &T, - params: &[&(dyn tokio_postgres::types::ToSql + Sync)], - ) -> Result - where - T: ?Sized + tokio_postgres::ToStatement + Sync + Send, - { - Client::query_one(self, statement, params).await - } - - async fn query_opt( - &self, - statement: &T, - params: &[&(dyn tokio_postgres::types::ToSql + Sync)], - ) -> Result, Error> - where - T: ?Sized + tokio_postgres::ToStatement + Sync + Send, - { - Client::query_opt(self, statement, params).await - } - - async fn query( - &self, - query: &T, - params: &[&(dyn tokio_postgres::types::ToSql + Sync)], - ) -> Result, Error> - where - T: ?Sized + tokio_postgres::ToStatement + Sync + Send, - { - Client::query(self, query, params).await - } - - async fn query_raw(&self, statement: &T, params: I) -> Result - where - T: ?Sized + ToStatement + Sync + Send, - P: BorrowToSql, - I: IntoIterator + Sync + Send, - I::IntoIter: ExactSizeIterator, - { - Client::query_raw(self, statement, params).await - } -} diff --git a/benches/execution/cornucopia_benches/generated/src/client/async_/mod.rs b/benches/execution/cornucopia_benches/generated/src/client/async_/mod.rs deleted file mode 100644 index 17a241ae..00000000 --- a/benches/execution/cornucopia_benches/generated/src/client/async_/mod.rs +++ /dev/null @@ -1,14 +0,0 @@ -#[doc(hidden)] -pub mod private; - -pub use generic_client::GenericClient; - -//#[cfg(feature = "deadpool")] -mod deadpool; -mod generic_client; - -/// This trait allows you to bind parameters to a query using a single -/// struct, rather than passing each bind parameter as a function parameter. -pub trait Params<'a, P, O, C> { - fn params(&'a mut self, client: &'a C, params: &'a P) -> O; -} diff --git a/benches/execution/cornucopia_benches/generated/src/client/async_/private.rs b/benches/execution/cornucopia_benches/generated/src/client/async_/private.rs deleted file mode 100644 index 534b159b..00000000 --- a/benches/execution/cornucopia_benches/generated/src/client/async_/private.rs +++ /dev/null @@ -1,32 +0,0 @@ -pub use super::super::{slice_iter, Domain, DomainArray}; - -use super::generic_client::GenericClient; -use tokio_postgres::{Error, Statement}; - -/// Cached statement -pub struct Stmt { - query: &'static str, - cached: Option, -} - -impl Stmt { - #[must_use] - pub fn new(query: &'static str) -> Self { - Self { - query, - cached: None, - } - } - - pub async fn prepare<'a, C: GenericClient>( - &'a mut self, - client: &C, - ) -> Result<&'a Statement, Error> { - if self.cached.is_none() { - let stmt = client.prepare(self.query).await?; - self.cached = Some(stmt); - } - // the statement is always prepared at this point - Ok(unsafe { self.cached.as_ref().unwrap_unchecked() }) - } -} diff --git a/benches/execution/cornucopia_benches/generated/src/client/domain.rs b/benches/execution/cornucopia_benches/generated/src/client/domain.rs deleted file mode 100644 index 458862f0..00000000 --- a/benches/execution/cornucopia_benches/generated/src/client/domain.rs +++ /dev/null @@ -1,105 +0,0 @@ -use postgres_protocol::types::{array_to_sql, ArrayDimension}; -use postgres_types::{private::BytesMut, IsNull, Kind, ToSql, Type}; -use std::{ - error::Error, - fmt::{Debug, Formatter}, -}; - -use super::{type_traits::ArraySql, utils::escape_domain}; - -pub struct Domain(pub T); - -impl Debug for Domain { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - f.debug_tuple("DomainWrapper").field(&self.0).finish() - } -} - -impl ToSql for Domain { - fn to_sql(&self, ty: &Type, out: &mut BytesMut) -> Result> - where - Self: Sized, - { - postgres_types::ToSql::to_sql(&self.0, escape_domain(ty), out) - } - - fn accepts(ty: &Type) -> bool - where - Self: Sized, - { - return T::accepts(escape_domain(ty)); - } - - fn to_sql_checked( - &self, - ty: &Type, - out: &mut BytesMut, - ) -> Result> { - postgres_types::__to_sql_checked(self, ty, out) - } -} - -pub struct DomainArray<'a, T: ToSql + Sync, A: ArraySql>(pub &'a A); - -impl<'a, T: ToSql + Sync, A: ArraySql> Debug for DomainArray<'a, T, A> { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - f.debug_tuple("ArrayDomain").field(&self.0).finish() - } -} - -impl<'a, T: ToSql + Sync + 'a, A: ArraySql> ToSql for DomainArray<'a, T, A> { - fn to_sql(&self, ty: &Type, w: &mut BytesMut) -> Result> { - self.0.escape_domain_to_sql(ty, w) - } - - fn accepts(ty: &Type) -> bool { - match *ty.kind() { - Kind::Array(ref member) => T::accepts(escape_domain(member)), - _ => false, - } - } - - fn to_sql_checked( - &self, - ty: &Type, - out: &mut BytesMut, - ) -> Result> { - postgres_types::__to_sql_checked(self, ty, out) - } -} - -pub fn escape_domain_to_sql( - ty: &Type, - w: &mut BytesMut, - iter: impl Iterator + ExactSizeIterator, -) -> Result> { - let member_type = match *ty.kind() { - Kind::Array(ref member) => escape_domain(member), - _ => panic!("expected array type got {ty}"), - }; - - let dimension = ArrayDimension { - len: downcast(iter.len())?, - lower_bound: 1, - }; - - array_to_sql( - Some(dimension), - member_type.oid(), - iter, - |e, w| match Domain(e).to_sql(member_type, w)? { - IsNull::No => Ok(postgres_protocol::IsNull::No), - IsNull::Yes => Ok(postgres_protocol::IsNull::Yes), - }, - w, - )?; - Ok(IsNull::No) -} - -fn downcast(len: usize) -> Result> { - if len > i32::max_value() as usize { - Err("value too large to transmit".into()) - } else { - Ok(len as i32) - } -} diff --git a/benches/execution/cornucopia_benches/generated/src/client/mod.rs b/benches/execution/cornucopia_benches/generated/src/client/mod.rs deleted file mode 100644 index 467be92c..00000000 --- a/benches/execution/cornucopia_benches/generated/src/client/mod.rs +++ /dev/null @@ -1,16 +0,0 @@ -mod array_iterator; -mod domain; -mod type_traits; -mod utils; - -pub mod async_; -pub mod sync; - -pub use array_iterator::ArrayIterator; -pub use domain::{Domain, DomainArray}; -pub use type_traits::{ArraySql, BytesSql, IterSql, StringSql}; - -//#[cfg(feature = "with-serde_json-1")] -pub use type_traits::JsonSql; - -pub use utils::slice_iter; diff --git a/benches/execution/cornucopia_benches/generated/src/client/sync/mod.rs b/benches/execution/cornucopia_benches/generated/src/client/sync/mod.rs deleted file mode 100644 index e86c3ba2..00000000 --- a/benches/execution/cornucopia_benches/generated/src/client/sync/mod.rs +++ /dev/null @@ -1,8 +0,0 @@ -#[doc(hidden)] -pub mod private; - -/// This trait allows you to bind parameters to a query using a single -/// struct, rather than passing each bind parameter as a function parameter. -pub trait Params<'a, P, O, C> { - fn params(&'a mut self, client: &'a mut C, params: &'a P) -> O; -} diff --git a/benches/execution/cornucopia_benches/generated/src/client/sync/private.rs b/benches/execution/cornucopia_benches/generated/src/client/sync/private.rs deleted file mode 100644 index e09d262c..00000000 --- a/benches/execution/cornucopia_benches/generated/src/client/sync/private.rs +++ /dev/null @@ -1,31 +0,0 @@ -pub use super::super::{slice_iter, Domain, DomainArray}; - -use postgres::Statement; - -/// Cached statement -pub struct Stmt { - query: &'static str, - cached: Option, -} - -impl Stmt { - #[must_use] - pub fn new(query: &'static str) -> Self { - Self { - query, - cached: None, - } - } - - pub fn prepare<'a, C: postgres::GenericClient>( - &'a mut self, - client: &mut C, - ) -> Result<&'a Statement, postgres::Error> { - if self.cached.is_none() { - let stmt = client.prepare(self.query)?; - self.cached = Some(stmt); - } - // the statement is always prepared at this point - Ok(unsafe { self.cached.as_ref().unwrap_unchecked() }) - } -} diff --git a/benches/execution/cornucopia_benches/generated/src/client/type_traits.rs b/benches/execution/cornucopia_benches/generated/src/client/type_traits.rs deleted file mode 100644 index 7c7990d2..00000000 --- a/benches/execution/cornucopia_benches/generated/src/client/type_traits.rs +++ /dev/null @@ -1,149 +0,0 @@ -use std::borrow::Cow; - -use super::domain::escape_domain_to_sql; -use postgres_protocol::types::{self, ArrayDimension}; -use postgres_types::{private::BytesMut, to_sql_checked, IsNull, Kind, ToSql, Type}; - -pub trait StringSql: std::fmt::Debug + ToSql + Sync {} -impl StringSql for &T {} -impl StringSql for String {} -impl StringSql for &str {} -impl StringSql for Cow<'_, str> {} -impl StringSql for Box {} - -pub trait BytesSql: std::fmt::Debug + ToSql + Send + Sync {} -impl BytesSql for &T {} -impl BytesSql for Vec {} -impl BytesSql for &[u8] {} - -//#[cfg(feature = "with-serde_json-1")] -pub trait JsonSql: std::fmt::Debug + ToSql + Sync + Send {} -//#[cfg(feature = "with-serde_json-1")] -impl JsonSql for &T {} -//#[cfg(feature = "with-serde_json-1")] -impl JsonSql for serde_json::value::Value {} -//#[cfg(feature = "with-serde_json-1")] -impl JsonSql for postgres_types::Json {} - -pub trait ArraySql: std::fmt::Debug + ToSql + Send + Sync { - type Item; - fn escape_domain_to_sql( - &self, - ty: &Type, - w: &mut BytesMut, - ) -> Result>; -} -impl> ArraySql for &A { - type Item = T; - - fn escape_domain_to_sql( - &self, - ty: &Type, - w: &mut BytesMut, - ) -> Result> { - A::escape_domain_to_sql(self, ty, w) - } -} -impl ArraySql for Vec { - type Item = T; - - fn escape_domain_to_sql( - &self, - ty: &Type, - w: &mut BytesMut, - ) -> Result> { - escape_domain_to_sql(ty, w, self.iter()) - } -} - -impl ArraySql for &[T] { - type Item = T; - - fn escape_domain_to_sql( - &self, - ty: &Type, - w: &mut BytesMut, - ) -> Result> { - escape_domain_to_sql(ty, w, self.iter()) - } -} - -impl< - T: std::fmt::Debug + ToSql + Send + Sync, - I: Iterator + ExactSizeIterator, - F: Fn() -> I + Send + Sync, - > ArraySql for IterSql -{ - type Item = T; - - fn escape_domain_to_sql( - &self, - ty: &Type, - w: &mut BytesMut, - ) -> Result> { - escape_domain_to_sql(ty, w, (self.0)()) - } -} - -pub struct IterSql + ExactSizeIterator, F: Fn() -> I + Sync>(pub F); - -impl + ExactSizeIterator, F: Fn() -> I + Sync> std::fmt::Debug - for IterSql -{ - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_tuple("ArrayFn").finish() - } -} - -// Taken from `postgres` -impl + ExactSizeIterator, F: Fn() -> I + Sync> ToSql - for IterSql -{ - fn to_sql( - &self, - ty: &Type, - w: &mut BytesMut, - ) -> Result> { - let member_type = match *ty.kind() { - Kind::Array(ref member) => member, - _ => panic!("expected array type"), - }; - - let iter = (self.0)(); - - let dimension = ArrayDimension { - len: downcast(iter.len())?, - lower_bound: 1, - }; - - types::array_to_sql( - Some(dimension), - member_type.oid(), - iter, - |e, w| match e.to_sql(member_type, w)? { - IsNull::No => Ok(postgres_protocol::IsNull::No), - IsNull::Yes => Ok(postgres_protocol::IsNull::Yes), - }, - w, - )?; - Ok(IsNull::No) - } - - fn accepts(ty: &Type) -> bool { - match *ty.kind() { - Kind::Array(ref member) => T::accepts(member), - _ => false, - } - } - - to_sql_checked!(); -} - -// https://github.com/sfackler/rust-postgres/blob/765395f288861209a644c621bf72172acd482515/postgres-types/src/lib.rs -fn downcast(len: usize) -> Result> { - if len > i32::max_value() as usize { - Err("value too large to transmit".into()) - } else { - Ok(len as i32) - } -} diff --git a/benches/execution/cornucopia_benches/generated/src/client/utils.rs b/benches/execution/cornucopia_benches/generated/src/client/utils.rs deleted file mode 100644 index d046783d..00000000 --- a/benches/execution/cornucopia_benches/generated/src/client/utils.rs +++ /dev/null @@ -1,14 +0,0 @@ -use postgres_types::{Kind, ToSql, Type}; - -pub fn escape_domain(ty: &Type) -> &Type { - match ty.kind() { - Kind::Domain(ty) => ty, - _ => ty, - } -} - -pub fn slice_iter<'a>( - s: &'a [&'a (dyn ToSql + Sync)], -) -> impl ExactSizeIterator + 'a { - s.iter().map(|s| *s as _) -} diff --git a/benches/execution/cornucopia_benches/generated/src/lib.rs b/benches/execution/cornucopia_benches/generated/src/lib.rs deleted file mode 100644 index 17ee8cd9..00000000 --- a/benches/execution/cornucopia_benches/generated/src/lib.rs +++ /dev/null @@ -1,981 +0,0 @@ -// This file was generated with `cornucopia`. Do not modify. - -#[allow(clippy::all, clippy::pedantic)] -#[allow(unused_variables)] -#[allow(unused_imports)] -#[allow(dead_code)] -pub mod types {} -#[allow(clippy::all, clippy::pedantic)] -#[allow(unused_variables)] -#[allow(unused_imports)] -#[allow(dead_code)] -pub mod queries { - pub mod bench { - #[derive(Debug)] - pub struct InsertUserParams { - pub name: T1, - pub hair_color: Option, - } - #[derive(Debug, Clone, PartialEq)] - pub struct User { - pub id: i32, - pub name: String, - pub hair_color: Option, - } - pub struct UserBorrowed<'a> { - pub id: i32, - pub name: &'a str, - pub hair_color: Option<&'a str>, - } - impl<'a> From> for User { - fn from( - UserBorrowed { - id, - name, - hair_color, - }: UserBorrowed<'a>, - ) -> Self { - Self { - id, - name: name.into(), - hair_color: hair_color.map(|v| v.into()), - } - } - } - #[derive(Debug, Clone, PartialEq)] - pub struct Post { - pub id: i32, - pub user_id: i32, - pub title: String, - pub body: Option, - } - pub struct PostBorrowed<'a> { - pub id: i32, - pub user_id: i32, - pub title: &'a str, - pub body: Option<&'a str>, - } - impl<'a> From> for Post { - fn from( - PostBorrowed { - id, - user_id, - title, - body, - }: PostBorrowed<'a>, - ) -> Self { - Self { - id, - user_id, - title: title.into(), - body: body.map(|v| v.into()), - } - } - } - #[derive(Debug, Clone, PartialEq)] - pub struct Comment { - pub id: i32, - pub post_id: i32, - pub text: String, - } - pub struct CommentBorrowed<'a> { - pub id: i32, - pub post_id: i32, - pub text: &'a str, - } - impl<'a> From> for Comment { - fn from(CommentBorrowed { id, post_id, text }: CommentBorrowed<'a>) -> Self { - Self { - id, - post_id, - text: text.into(), - } - } - } - #[derive(Debug, Clone, PartialEq)] - pub struct SelectComplex { - pub myuser_id: i32, - pub name: String, - pub hair_color: Option, - pub post_id: Option, - pub user_id: Option, - pub title: Option, - pub body: Option, - } - pub struct SelectComplexBorrowed<'a> { - pub myuser_id: i32, - pub name: &'a str, - pub hair_color: Option<&'a str>, - pub post_id: Option, - pub user_id: Option, - pub title: Option<&'a str>, - pub body: Option<&'a str>, - } - impl<'a> From> for SelectComplex { - fn from( - SelectComplexBorrowed { - myuser_id, - name, - hair_color, - post_id, - user_id, - title, - body, - }: SelectComplexBorrowed<'a>, - ) -> Self { - Self { - myuser_id, - name: name.into(), - hair_color: hair_color.map(|v| v.into()), - post_id, - user_id, - title: title.map(|v| v.into()), - body: body.map(|v| v.into()), - } - } - } - pub mod sync { - use postgres::{fallible_iterator::FallibleIterator, GenericClient}; - pub struct UserQuery<'a, C: GenericClient, T, const N: usize> { - client: &'a mut C, - params: [&'a (dyn postgres_types::ToSql + Sync); N], - stmt: &'a mut crate::client::sync::private::Stmt, - extractor: fn(&postgres::Row) -> super::UserBorrowed, - mapper: fn(super::UserBorrowed) -> T, - } - impl<'a, C, T: 'a, const N: usize> UserQuery<'a, C, T, N> - where - C: GenericClient, - { - pub fn map( - self, - mapper: fn(super::UserBorrowed) -> R, - ) -> UserQuery<'a, C, R, N> { - UserQuery { - client: self.client, - params: self.params, - stmt: self.stmt, - extractor: self.extractor, - mapper, - } - } - pub fn one(self) -> Result { - let stmt = self.stmt.prepare(self.client)?; - let row = self.client.query_one(stmt, &self.params)?; - Ok((self.mapper)((self.extractor)(&row))) - } - pub fn all(self) -> Result, postgres::Error> { - self.iter()?.collect() - } - pub fn opt(self) -> Result, postgres::Error> { - let stmt = self.stmt.prepare(self.client)?; - Ok(self - .client - .query_opt(stmt, &self.params)? - .map(|row| (self.mapper)((self.extractor)(&row)))) - } - pub fn iter( - self, - ) -> Result> + 'a, postgres::Error> - { - let stmt = self.stmt.prepare(self.client)?; - let it = self - .client - .query_raw(stmt, crate::client::sync::private::slice_iter(&self.params))? - .iterator() - .map(move |res| res.map(|row| (self.mapper)((self.extractor)(&row)))); - Ok(it) - } - } - pub struct PostQuery<'a, C: GenericClient, T, const N: usize> { - client: &'a mut C, - params: [&'a (dyn postgres_types::ToSql + Sync); N], - stmt: &'a mut crate::client::sync::private::Stmt, - extractor: fn(&postgres::Row) -> super::PostBorrowed, - mapper: fn(super::PostBorrowed) -> T, - } - impl<'a, C, T: 'a, const N: usize> PostQuery<'a, C, T, N> - where - C: GenericClient, - { - pub fn map( - self, - mapper: fn(super::PostBorrowed) -> R, - ) -> PostQuery<'a, C, R, N> { - PostQuery { - client: self.client, - params: self.params, - stmt: self.stmt, - extractor: self.extractor, - mapper, - } - } - pub fn one(self) -> Result { - let stmt = self.stmt.prepare(self.client)?; - let row = self.client.query_one(stmt, &self.params)?; - Ok((self.mapper)((self.extractor)(&row))) - } - pub fn all(self) -> Result, postgres::Error> { - self.iter()?.collect() - } - pub fn opt(self) -> Result, postgres::Error> { - let stmt = self.stmt.prepare(self.client)?; - Ok(self - .client - .query_opt(stmt, &self.params)? - .map(|row| (self.mapper)((self.extractor)(&row)))) - } - pub fn iter( - self, - ) -> Result> + 'a, postgres::Error> - { - let stmt = self.stmt.prepare(self.client)?; - let it = self - .client - .query_raw(stmt, crate::client::sync::private::slice_iter(&self.params))? - .iterator() - .map(move |res| res.map(|row| (self.mapper)((self.extractor)(&row)))); - Ok(it) - } - } - pub struct CommentQuery<'a, C: GenericClient, T, const N: usize> { - client: &'a mut C, - params: [&'a (dyn postgres_types::ToSql + Sync); N], - stmt: &'a mut crate::client::sync::private::Stmt, - extractor: fn(&postgres::Row) -> super::CommentBorrowed, - mapper: fn(super::CommentBorrowed) -> T, - } - impl<'a, C, T: 'a, const N: usize> CommentQuery<'a, C, T, N> - where - C: GenericClient, - { - pub fn map( - self, - mapper: fn(super::CommentBorrowed) -> R, - ) -> CommentQuery<'a, C, R, N> { - CommentQuery { - client: self.client, - params: self.params, - stmt: self.stmt, - extractor: self.extractor, - mapper, - } - } - pub fn one(self) -> Result { - let stmt = self.stmt.prepare(self.client)?; - let row = self.client.query_one(stmt, &self.params)?; - Ok((self.mapper)((self.extractor)(&row))) - } - pub fn all(self) -> Result, postgres::Error> { - self.iter()?.collect() - } - pub fn opt(self) -> Result, postgres::Error> { - let stmt = self.stmt.prepare(self.client)?; - Ok(self - .client - .query_opt(stmt, &self.params)? - .map(|row| (self.mapper)((self.extractor)(&row)))) - } - pub fn iter( - self, - ) -> Result> + 'a, postgres::Error> - { - let stmt = self.stmt.prepare(self.client)?; - let it = self - .client - .query_raw(stmt, crate::client::sync::private::slice_iter(&self.params))? - .iterator() - .map(move |res| res.map(|row| (self.mapper)((self.extractor)(&row)))); - Ok(it) - } - } - pub struct SelectComplexQuery<'a, C: GenericClient, T, const N: usize> { - client: &'a mut C, - params: [&'a (dyn postgres_types::ToSql + Sync); N], - stmt: &'a mut crate::client::sync::private::Stmt, - extractor: fn(&postgres::Row) -> super::SelectComplexBorrowed, - mapper: fn(super::SelectComplexBorrowed) -> T, - } - impl<'a, C, T: 'a, const N: usize> SelectComplexQuery<'a, C, T, N> - where - C: GenericClient, - { - pub fn map( - self, - mapper: fn(super::SelectComplexBorrowed) -> R, - ) -> SelectComplexQuery<'a, C, R, N> { - SelectComplexQuery { - client: self.client, - params: self.params, - stmt: self.stmt, - extractor: self.extractor, - mapper, - } - } - pub fn one(self) -> Result { - let stmt = self.stmt.prepare(self.client)?; - let row = self.client.query_one(stmt, &self.params)?; - Ok((self.mapper)((self.extractor)(&row))) - } - pub fn all(self) -> Result, postgres::Error> { - self.iter()?.collect() - } - pub fn opt(self) -> Result, postgres::Error> { - let stmt = self.stmt.prepare(self.client)?; - Ok(self - .client - .query_opt(stmt, &self.params)? - .map(|row| (self.mapper)((self.extractor)(&row)))) - } - pub fn iter( - self, - ) -> Result> + 'a, postgres::Error> - { - let stmt = self.stmt.prepare(self.client)?; - let it = self - .client - .query_raw(stmt, crate::client::sync::private::slice_iter(&self.params))? - .iterator() - .map(move |res| res.map(|row| (self.mapper)((self.extractor)(&row)))); - Ok(it) - } - } - pub fn users() -> UsersStmt { - UsersStmt(crate::client::sync::private::Stmt::new( - "SELECT * FROM users", - )) - } - pub struct UsersStmt(crate::client::sync::private::Stmt); - impl UsersStmt { - pub fn bind<'a, C: GenericClient>( - &'a mut self, - client: &'a mut C, - ) -> UserQuery<'a, C, super::User, 0> { - UserQuery { - client, - params: [], - stmt: &mut self.0, - extractor: |row| super::UserBorrowed { - id: row.get(0), - name: row.get(1), - hair_color: row.get(2), - }, - mapper: |it| ::from(it), - } - } - } - pub fn insert_user() -> InsertUserStmt { - InsertUserStmt(crate::client::sync::private::Stmt::new( - "INSERT INTO users (name, hair_color) VALUES ($1, $2)", - )) - } - pub struct InsertUserStmt(crate::client::sync::private::Stmt); - impl InsertUserStmt { - pub fn bind< - 'a, - C: GenericClient, - T1: crate::client::StringSql, - T2: crate::client::StringSql, - >( - &'a mut self, - client: &'a mut C, - name: &'a T1, - hair_color: &'a Option, - ) -> Result { - let stmt = self.0.prepare(client)?; - client.execute(stmt, &[name, hair_color]) - } - } - impl< - 'a, - C: GenericClient, - T1: crate::client::StringSql, - T2: crate::client::StringSql, - > - crate::client::sync::Params< - 'a, - super::InsertUserParams, - Result, - C, - > for InsertUserStmt - { - fn params( - &'a mut self, - client: &'a mut C, - params: &'a super::InsertUserParams, - ) -> Result { - self.bind(client, ¶ms.name, ¶ms.hair_color) - } - } - pub fn posts() -> PostsStmt { - PostsStmt(crate::client::sync::private::Stmt::new( - "SELECT * FROM posts", - )) - } - pub struct PostsStmt(crate::client::sync::private::Stmt); - impl PostsStmt { - pub fn bind<'a, C: GenericClient>( - &'a mut self, - client: &'a mut C, - ) -> PostQuery<'a, C, super::Post, 0> { - PostQuery { - client, - params: [], - stmt: &mut self.0, - extractor: |row| super::PostBorrowed { - id: row.get(0), - user_id: row.get(1), - title: row.get(2), - body: row.get(3), - }, - mapper: |it| ::from(it), - } - } - } - pub fn post_by_user_ids() -> PostByUserIdsStmt { - PostByUserIdsStmt(crate::client::sync::private::Stmt::new( - "SELECT * FROM posts WHERE user_id = ANY($1)", - )) - } - pub struct PostByUserIdsStmt(crate::client::sync::private::Stmt); - impl PostByUserIdsStmt { - pub fn bind<'a, C: GenericClient, T1: crate::client::ArraySql>( - &'a mut self, - client: &'a mut C, - ids: &'a T1, - ) -> PostQuery<'a, C, super::Post, 1> { - PostQuery { - client, - params: [ids], - stmt: &mut self.0, - extractor: |row| super::PostBorrowed { - id: row.get(0), - user_id: row.get(1), - title: row.get(2), - body: row.get(3), - }, - mapper: |it| ::from(it), - } - } - } - pub fn comments() -> CommentsStmt { - CommentsStmt(crate::client::sync::private::Stmt::new( - "SELECT * FROM comments", - )) - } - pub struct CommentsStmt(crate::client::sync::private::Stmt); - impl CommentsStmt { - pub fn bind<'a, C: GenericClient>( - &'a mut self, - client: &'a mut C, - ) -> CommentQuery<'a, C, super::Comment, 0> { - CommentQuery { - client, - params: [], - stmt: &mut self.0, - extractor: |row| super::CommentBorrowed { - id: row.get(0), - post_id: row.get(1), - text: row.get(2), - }, - mapper: |it| ::from(it), - } - } - } - pub fn comments_by_post_id() -> CommentsByPostIdStmt { - CommentsByPostIdStmt(crate::client::sync::private::Stmt::new( - "SELECT * FROM comments WHERE post_id = ANY($1)", - )) - } - pub struct CommentsByPostIdStmt(crate::client::sync::private::Stmt); - impl CommentsByPostIdStmt { - pub fn bind<'a, C: GenericClient, T1: crate::client::ArraySql>( - &'a mut self, - client: &'a mut C, - ids: &'a T1, - ) -> CommentQuery<'a, C, super::Comment, 1> { - CommentQuery { - client, - params: [ids], - stmt: &mut self.0, - extractor: |row| super::CommentBorrowed { - id: row.get(0), - post_id: row.get(1), - text: row.get(2), - }, - mapper: |it| ::from(it), - } - } - } - pub fn select_complex() -> SelectComplexStmt { - SelectComplexStmt(crate::client::sync :: private :: Stmt :: new("SELECT u.id as myuser_id, u.name, u.hair_color, p.id as post_id, p.user_id, p.title, p.body FROM users as u LEFT JOIN posts as p on u.id = p.user_id")) - } - pub struct SelectComplexStmt(crate::client::sync::private::Stmt); - impl SelectComplexStmt { - pub fn bind<'a, C: GenericClient>( - &'a mut self, - client: &'a mut C, - ) -> SelectComplexQuery<'a, C, super::SelectComplex, 0> { - SelectComplexQuery { - client, - params: [], - stmt: &mut self.0, - extractor: |row| super::SelectComplexBorrowed { - myuser_id: row.get(0), - name: row.get(1), - hair_color: row.get(2), - post_id: row.get(3), - user_id: row.get(4), - title: row.get(5), - body: row.get(6), - }, - mapper: |it| ::from(it), - } - } - } - } - pub mod async_ { - use crate::client::async_::GenericClient; - use futures; - use futures::{StreamExt, TryStreamExt}; - pub struct UserQuery<'a, C: GenericClient, T, const N: usize> { - client: &'a C, - params: [&'a (dyn postgres_types::ToSql + Sync); N], - stmt: &'a mut crate::client::async_::private::Stmt, - extractor: fn(&tokio_postgres::Row) -> super::UserBorrowed, - mapper: fn(super::UserBorrowed) -> T, - } - impl<'a, C, T: 'a, const N: usize> UserQuery<'a, C, T, N> - where - C: GenericClient, - { - pub fn map( - self, - mapper: fn(super::UserBorrowed) -> R, - ) -> UserQuery<'a, C, R, N> { - UserQuery { - client: self.client, - params: self.params, - stmt: self.stmt, - extractor: self.extractor, - mapper, - } - } - pub async fn one(self) -> Result { - 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, tokio_postgres::Error> { - self.iter().await?.try_collect().await - } - pub async fn opt(self) -> Result, 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> + 'a, - tokio_postgres::Error, - > { - let stmt = self.stmt.prepare(self.client).await?; - let it = self - .client - .query_raw( - stmt, - crate::client::async_::private::slice_iter(&self.params), - ) - .await? - .map(move |res| res.map(|row| (self.mapper)((self.extractor)(&row)))) - .into_stream(); - Ok(it) - } - } - pub struct PostQuery<'a, C: GenericClient, T, const N: usize> { - client: &'a C, - params: [&'a (dyn postgres_types::ToSql + Sync); N], - stmt: &'a mut crate::client::async_::private::Stmt, - extractor: fn(&tokio_postgres::Row) -> super::PostBorrowed, - mapper: fn(super::PostBorrowed) -> T, - } - impl<'a, C, T: 'a, const N: usize> PostQuery<'a, C, T, N> - where - C: GenericClient, - { - pub fn map( - self, - mapper: fn(super::PostBorrowed) -> R, - ) -> PostQuery<'a, C, R, N> { - PostQuery { - client: self.client, - params: self.params, - stmt: self.stmt, - extractor: self.extractor, - mapper, - } - } - pub async fn one(self) -> Result { - 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, tokio_postgres::Error> { - self.iter().await?.try_collect().await - } - pub async fn opt(self) -> Result, 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> + 'a, - tokio_postgres::Error, - > { - let stmt = self.stmt.prepare(self.client).await?; - let it = self - .client - .query_raw( - stmt, - crate::client::async_::private::slice_iter(&self.params), - ) - .await? - .map(move |res| res.map(|row| (self.mapper)((self.extractor)(&row)))) - .into_stream(); - Ok(it) - } - } - pub struct CommentQuery<'a, C: GenericClient, T, const N: usize> { - client: &'a C, - params: [&'a (dyn postgres_types::ToSql + Sync); N], - stmt: &'a mut crate::client::async_::private::Stmt, - extractor: fn(&tokio_postgres::Row) -> super::CommentBorrowed, - mapper: fn(super::CommentBorrowed) -> T, - } - impl<'a, C, T: 'a, const N: usize> CommentQuery<'a, C, T, N> - where - C: GenericClient, - { - pub fn map( - self, - mapper: fn(super::CommentBorrowed) -> R, - ) -> CommentQuery<'a, C, R, N> { - CommentQuery { - client: self.client, - params: self.params, - stmt: self.stmt, - extractor: self.extractor, - mapper, - } - } - pub async fn one(self) -> Result { - 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, tokio_postgres::Error> { - self.iter().await?.try_collect().await - } - pub async fn opt(self) -> Result, 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> + 'a, - tokio_postgres::Error, - > { - let stmt = self.stmt.prepare(self.client).await?; - let it = self - .client - .query_raw( - stmt, - crate::client::async_::private::slice_iter(&self.params), - ) - .await? - .map(move |res| res.map(|row| (self.mapper)((self.extractor)(&row)))) - .into_stream(); - Ok(it) - } - } - pub struct SelectComplexQuery<'a, C: GenericClient, T, const N: usize> { - client: &'a C, - params: [&'a (dyn postgres_types::ToSql + Sync); N], - stmt: &'a mut crate::client::async_::private::Stmt, - extractor: fn(&tokio_postgres::Row) -> super::SelectComplexBorrowed, - mapper: fn(super::SelectComplexBorrowed) -> T, - } - impl<'a, C, T: 'a, const N: usize> SelectComplexQuery<'a, C, T, N> - where - C: GenericClient, - { - pub fn map( - self, - mapper: fn(super::SelectComplexBorrowed) -> R, - ) -> SelectComplexQuery<'a, C, R, N> { - SelectComplexQuery { - client: self.client, - params: self.params, - stmt: self.stmt, - extractor: self.extractor, - mapper, - } - } - pub async fn one(self) -> Result { - 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, tokio_postgres::Error> { - self.iter().await?.try_collect().await - } - pub async fn opt(self) -> Result, 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> + 'a, - tokio_postgres::Error, - > { - let stmt = self.stmt.prepare(self.client).await?; - let it = self - .client - .query_raw( - stmt, - crate::client::async_::private::slice_iter(&self.params), - ) - .await? - .map(move |res| res.map(|row| (self.mapper)((self.extractor)(&row)))) - .into_stream(); - Ok(it) - } - } - pub fn users() -> UsersStmt { - UsersStmt(crate::client::async_::private::Stmt::new( - "SELECT * FROM users", - )) - } - pub struct UsersStmt(crate::client::async_::private::Stmt); - impl UsersStmt { - pub fn bind<'a, C: GenericClient>( - &'a mut self, - client: &'a C, - ) -> UserQuery<'a, C, super::User, 0> { - UserQuery { - client, - params: [], - stmt: &mut self.0, - extractor: |row| super::UserBorrowed { - id: row.get(0), - name: row.get(1), - hair_color: row.get(2), - }, - mapper: |it| ::from(it), - } - } - } - pub fn insert_user() -> InsertUserStmt { - InsertUserStmt(crate::client::async_::private::Stmt::new( - "INSERT INTO users (name, hair_color) VALUES ($1, $2)", - )) - } - pub struct InsertUserStmt(crate::client::async_::private::Stmt); - impl InsertUserStmt { - pub async fn bind< - 'a, - C: GenericClient, - T1: crate::client::StringSql, - T2: crate::client::StringSql, - >( - &'a mut self, - client: &'a C, - name: &'a T1, - hair_color: &'a Option, - ) -> Result { - let stmt = self.0.prepare(client).await?; - client.execute(stmt, &[name, hair_color]).await - } - } - impl< - 'a, - C: GenericClient + Send + Sync, - T1: crate::client::StringSql, - T2: crate::client::StringSql, - > - crate::client::async_::Params< - 'a, - super::InsertUserParams, - std::pin::Pin< - Box< - dyn futures::Future> - + Send - + 'a, - >, - >, - C, - > for InsertUserStmt - { - fn params( - &'a mut self, - client: &'a C, - params: &'a super::InsertUserParams, - ) -> std::pin::Pin< - Box< - dyn futures::Future> - + Send - + 'a, - >, - > { - Box::pin(self.bind(client, ¶ms.name, ¶ms.hair_color)) - } - } - pub fn posts() -> PostsStmt { - PostsStmt(crate::client::async_::private::Stmt::new( - "SELECT * FROM posts", - )) - } - pub struct PostsStmt(crate::client::async_::private::Stmt); - impl PostsStmt { - pub fn bind<'a, C: GenericClient>( - &'a mut self, - client: &'a C, - ) -> PostQuery<'a, C, super::Post, 0> { - PostQuery { - client, - params: [], - stmt: &mut self.0, - extractor: |row| super::PostBorrowed { - id: row.get(0), - user_id: row.get(1), - title: row.get(2), - body: row.get(3), - }, - mapper: |it| ::from(it), - } - } - } - pub fn post_by_user_ids() -> PostByUserIdsStmt { - PostByUserIdsStmt(crate::client::async_::private::Stmt::new( - "SELECT * FROM posts WHERE user_id = ANY($1)", - )) - } - pub struct PostByUserIdsStmt(crate::client::async_::private::Stmt); - impl PostByUserIdsStmt { - pub fn bind<'a, C: GenericClient, T1: crate::client::ArraySql>( - &'a mut self, - client: &'a C, - ids: &'a T1, - ) -> PostQuery<'a, C, super::Post, 1> { - PostQuery { - client, - params: [ids], - stmt: &mut self.0, - extractor: |row| super::PostBorrowed { - id: row.get(0), - user_id: row.get(1), - title: row.get(2), - body: row.get(3), - }, - mapper: |it| ::from(it), - } - } - } - pub fn comments() -> CommentsStmt { - CommentsStmt(crate::client::async_::private::Stmt::new( - "SELECT * FROM comments", - )) - } - pub struct CommentsStmt(crate::client::async_::private::Stmt); - impl CommentsStmt { - pub fn bind<'a, C: GenericClient>( - &'a mut self, - client: &'a C, - ) -> CommentQuery<'a, C, super::Comment, 0> { - CommentQuery { - client, - params: [], - stmt: &mut self.0, - extractor: |row| super::CommentBorrowed { - id: row.get(0), - post_id: row.get(1), - text: row.get(2), - }, - mapper: |it| ::from(it), - } - } - } - pub fn comments_by_post_id() -> CommentsByPostIdStmt { - CommentsByPostIdStmt(crate::client::async_::private::Stmt::new( - "SELECT * FROM comments WHERE post_id = ANY($1)", - )) - } - pub struct CommentsByPostIdStmt(crate::client::async_::private::Stmt); - impl CommentsByPostIdStmt { - pub fn bind<'a, C: GenericClient, T1: crate::client::ArraySql>( - &'a mut self, - client: &'a C, - ids: &'a T1, - ) -> CommentQuery<'a, C, super::Comment, 1> { - CommentQuery { - client, - params: [ids], - stmt: &mut self.0, - extractor: |row| super::CommentBorrowed { - id: row.get(0), - post_id: row.get(1), - text: row.get(2), - }, - mapper: |it| ::from(it), - } - } - } - pub fn select_complex() -> SelectComplexStmt { - SelectComplexStmt(crate::client::async_ :: private :: Stmt :: new("SELECT u.id as myuser_id, u.name, u.hair_color, p.id as post_id, p.user_id, p.title, p.body FROM users as u LEFT JOIN posts as p on u.id = p.user_id")) - } - pub struct SelectComplexStmt(crate::client::async_::private::Stmt); - impl SelectComplexStmt { - pub fn bind<'a, C: GenericClient>( - &'a mut self, - client: &'a C, - ) -> SelectComplexQuery<'a, C, super::SelectComplex, 0> { - SelectComplexQuery { - client, - params: [], - stmt: &mut self.0, - extractor: |row| super::SelectComplexBorrowed { - myuser_id: row.get(0), - name: row.get(1), - hair_color: row.get(2), - post_id: row.get(3), - user_id: row.get(4), - title: row.get(5), - body: row.get(6), - }, - mapper: |it| ::from(it), - } - } - } - } - } -} -pub mod client; diff --git a/benches/temp/Cargo.toml b/benches/temp/Cargo.toml deleted file mode 100644 index 84fa4c05..00000000 --- a/benches/temp/Cargo.toml +++ /dev/null @@ -1,38 +0,0 @@ -# This file was generated with `cornucopia`. Do not modify -[package] -name = "generated" -version = "0.9.0" -edition = "2021" -[features] -default = ["deadpool"] -deadpool = ["dep:deadpool-postgres"] - -[dependencies] -## Core dependencies -# Postgres types -postgres-types = { version = "*", features = ["derive"] } -# Postgres interaction -postgres-protocol = "0.6.4" - -## Types dependencies -# JSON or JSONB -serde_json = "*" -serde = { version = "*", features = ["derive"] } - -## Sync client dependencies -# Postgres sync client -postgres = { version = "*", features = ["with-serde_json-1",] } -# Iterator utils required for working with `postgres_protocol::types::ArrayValues` -fallible-iterator = "0.2.0" - -## Async client dependencies -# Postgres async client -tokio-postgres = { version = "*", features = ["with-serde_json-1",] } -# ?? -async-trait = "0.1.63" -# ?? -futures = "*" - -## Async features dependencies -# Async connection pooling -deadpool-postgres = { version = "*", optional = true } diff --git a/benches/temp/src/client.rs b/benches/temp/src/client.rs deleted file mode 100644 index fb454cf4..00000000 --- a/benches/temp/src/client.rs +++ /dev/null @@ -1,11 +0,0 @@ - -mod array_iterator; -mod domain; -mod type_traits; -mod utils; -pub use array_iterator::ArrayIterator; -pub use domain::{Domain, DomainArray}; -pub use type_traits::{ArraySql, BytesSql, IterSql, StringSql}; -pub(crate) use utils::slice_iter; -pub mod async_; -pub mod sync; diff --git a/benches/temp/src/client/array_iterator.rs b/benches/temp/src/client/array_iterator.rs deleted file mode 100644 index c06e287d..00000000 --- a/benches/temp/src/client/array_iterator.rs +++ /dev/null @@ -1,57 +0,0 @@ -use super::utils::escape_domain; -use fallible_iterator::FallibleIterator; -use postgres_protocol::types::{array_from_sql, ArrayValues}; -use postgres_types::{FromSql, Kind, Type}; -use std::fmt::Debug; -use std::marker::PhantomData; -/// Iterator over the items in a PostgreSQL array. You only need this if you are -/// working with custom zero-cost type mapping of rows containing PostgreSQL arrays. -pub struct ArrayIterator<'a, T: FromSql<'a>> { - values: ArrayValues<'a>, - ty: Type, - _type: PhantomData, -} -impl<'a, T: FromSql<'a>> Debug for ArrayIterator<'a, T> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("ArrayIterator") - .field("values", &"[T]") - .field("ty", &self.ty) - .field("_type", &self._type) - .finish() - } -} -impl<'a, T: FromSql<'a>> Iterator for ArrayIterator<'a, T> { - type Item = T; - fn next(&mut self) -> Option { - self.values - .next() - .unwrap() - .map(|raw| T::from_sql_nullable(&self.ty, raw).unwrap()) - } -} -impl<'a, T: FromSql<'a>> FromSql<'a> for ArrayIterator<'a, T> { - fn from_sql( - ty: &Type, - raw: &'a [u8], - ) -> Result, Box> { - let member_type = match *escape_domain(ty).kind() { - Kind::Array(ref member) => escape_domain(member), - _ => panic!("expected array type got {ty}"), - }; - let array = array_from_sql(raw)?; - if array.dimensions().count()? > 1 { - return Err("array contains too many dimensions".into()); - } - Ok(ArrayIterator { - ty: member_type.clone(), - values: array.values(), - _type: PhantomData::default(), - }) - } - fn accepts(ty: &Type) -> bool { - match *ty.kind() { - Kind::Array(ref inner) => T::accepts(escape_domain(inner)), - _ => false, - } - } -} diff --git a/benches/temp/src/client/async_.rs b/benches/temp/src/client/async_.rs deleted file mode 100644 index f417b7bd..00000000 --- a/benches/temp/src/client/async_.rs +++ /dev/null @@ -1,34 +0,0 @@ -pub use generic_client::GenericClient; -use tokio_postgres::{Error, Statement}; -#[cfg(feature = "deadpool")] -mod deadpool; -mod generic_client; -/// This trait allows you to bind parameters to a query using a single -/// struct, rather than passing each bind parameter as a function parameter. -pub trait Params<'a, P, O, C> { - fn params(&'a mut self, client: &'a C, params: &'a P) -> O; -} -/// Cached statement -pub struct Stmt { - query: &'static str, - cached: Option, -} -impl Stmt { - #[must_use] - pub fn new(query: &'static str) -> Self { - Self { - query, - cached: None, - } - } - pub async fn prepare<'a, C: GenericClient>( - &'a mut self, - client: &C, - ) -> Result<&'a Statement, Error> { - if self.cached.is_none() { - let stmt = client.prepare(self.query).await?; - self.cached = Some(stmt); - } - Ok(unsafe { self.cached.as_ref().unwrap_unchecked() }) - } -} diff --git a/benches/temp/src/client/async_/deadpool.rs b/benches/temp/src/client/async_/deadpool.rs deleted file mode 100644 index db91fa0b..00000000 --- a/benches/temp/src/client/async_/deadpool.rs +++ /dev/null @@ -1,119 +0,0 @@ -use super::generic_client::GenericClient; -use async_trait::async_trait; -use deadpool_postgres::{ - Client as DeadpoolClient, ClientWrapper, Transaction as DeadpoolTransaction, -}; -use tokio_postgres::{ - types::BorrowToSql, Client as PgClient, Error, RowStream, Statement, ToStatement, - Transaction as PgTransaction, -}; -#[async_trait] -impl GenericClient for DeadpoolClient { - async fn prepare(&self, query: &str) -> Result { - ClientWrapper::prepare_cached(self, query).await - } - async fn execute( - &self, - query: &T, - params: &[&(dyn tokio_postgres::types::ToSql + Sync)], - ) -> Result - where - T: ?Sized + tokio_postgres::ToStatement + Sync + Send, - { - PgClient::execute(self, query, params).await - } - async fn query_one( - &self, - statement: &T, - params: &[&(dyn tokio_postgres::types::ToSql + Sync)], - ) -> Result - where - T: ?Sized + tokio_postgres::ToStatement + Sync + Send, - { - PgClient::query_one(self, statement, params).await - } - async fn query_opt( - &self, - statement: &T, - params: &[&(dyn tokio_postgres::types::ToSql + Sync)], - ) -> Result, Error> - where - T: ?Sized + tokio_postgres::ToStatement + Sync + Send, - { - PgClient::query_opt(self, statement, params).await - } - async fn query( - &self, - query: &T, - params: &[&(dyn tokio_postgres::types::ToSql + Sync)], - ) -> Result, Error> - where - T: ?Sized + tokio_postgres::ToStatement + Sync + Send, - { - PgClient::query(self, query, params).await - } - async fn query_raw(&self, statement: &T, params: I) -> Result - where - T: ?Sized + ToStatement + Sync + Send, - P: BorrowToSql, - I: IntoIterator + Sync + Send, - I::IntoIter: ExactSizeIterator, - { - PgClient::query_raw(self, statement, params).await - } -} -#[async_trait] -impl GenericClient for DeadpoolTransaction<'_> { - async fn prepare(&self, query: &str) -> Result { - DeadpoolTransaction::prepare_cached(self, query).await - } - async fn execute( - &self, - query: &T, - params: &[&(dyn tokio_postgres::types::ToSql + Sync)], - ) -> Result - where - T: ?Sized + tokio_postgres::ToStatement + Sync + Send, - { - PgTransaction::execute(self, query, params).await - } - async fn query_one( - &self, - statement: &T, - params: &[&(dyn tokio_postgres::types::ToSql + Sync)], - ) -> Result - where - T: ?Sized + tokio_postgres::ToStatement + Sync + Send, - { - PgTransaction::query_one(self, statement, params).await - } - async fn query_opt( - &self, - statement: &T, - params: &[&(dyn tokio_postgres::types::ToSql + Sync)], - ) -> Result, Error> - where - T: ?Sized + tokio_postgres::ToStatement + Sync + Send, - { - PgTransaction::query_opt(self, statement, params).await - } - async fn query( - &self, - query: &T, - params: &[&(dyn tokio_postgres::types::ToSql + Sync)], - ) -> Result, Error> - where - T: ?Sized + tokio_postgres::ToStatement + Sync + Send, - { - PgTransaction::query(self, query, params).await - } - async fn query_raw(&self, statement: &T, params: I) -> Result - where - T: ?Sized + ToStatement + Sync + Send, - P: BorrowToSql, - I: IntoIterator + Sync + Send, - I::IntoIter: ExactSizeIterator, - { - PgTransaction::query_raw(self, statement, params).await - } -} diff --git a/benches/temp/src/client/async_/generic_client.rs b/benches/temp/src/client/async_/generic_client.rs deleted file mode 100644 index 20620f68..00000000 --- a/benches/temp/src/client/async_/generic_client.rs +++ /dev/null @@ -1,157 +0,0 @@ -use async_trait::async_trait; -use tokio_postgres::{ - types::BorrowToSql, Client, Error, RowStream, Statement, ToStatement, Transaction, -}; -/// Abstraction over multiple types of asynchronous clients. -/// This allows you to use tokio_postgres clients and transactions interchangeably. -/// -/// In addition, when the `deadpool` feature is enabled (default), this trait also -/// abstracts over deadpool clients and transactions -#[async_trait] -pub trait GenericClient: Send + Sync { - async fn prepare(&self, query: &str) -> Result; - async fn execute( - &self, - query: &T, - params: &[&(dyn tokio_postgres::types::ToSql + Sync)], - ) -> Result - where - T: ?Sized + tokio_postgres::ToStatement + Sync + Send; - async fn query_one( - &self, - statement: &T, - params: &[&(dyn tokio_postgres::types::ToSql + Sync)], - ) -> Result - where - T: ?Sized + tokio_postgres::ToStatement + Sync + Send; - async fn query_opt( - &self, - statement: &T, - params: &[&(dyn tokio_postgres::types::ToSql + Sync)], - ) -> Result, Error> - where - T: ?Sized + tokio_postgres::ToStatement + Sync + Send; - async fn query( - &self, - query: &T, - params: &[&(dyn tokio_postgres::types::ToSql + Sync)], - ) -> Result, Error> - where - T: ?Sized + tokio_postgres::ToStatement + Sync + Send; - async fn query_raw(&self, statement: &T, params: I) -> Result - where - T: ?Sized + ToStatement + Sync + Send, - P: BorrowToSql, - I: IntoIterator + Sync + Send, - I::IntoIter: ExactSizeIterator; -} -#[async_trait] -impl GenericClient for Transaction<'_> { - async fn prepare(&self, query: &str) -> Result { - Transaction::prepare(self, query).await - } - async fn execute( - &self, - query: &T, - params: &[&(dyn tokio_postgres::types::ToSql + Sync)], - ) -> Result - where - T: ?Sized + tokio_postgres::ToStatement + Sync + Send, - { - Transaction::execute(self, query, params).await - } - async fn query_one( - &self, - statement: &T, - params: &[&(dyn tokio_postgres::types::ToSql + Sync)], - ) -> Result - where - T: ?Sized + tokio_postgres::ToStatement + Sync + Send, - { - Transaction::query_one(self, statement, params).await - } - async fn query_opt( - &self, - statement: &T, - params: &[&(dyn tokio_postgres::types::ToSql + Sync)], - ) -> Result, Error> - where - T: ?Sized + tokio_postgres::ToStatement + Sync + Send, - { - Transaction::query_opt(self, statement, params).await - } - async fn query( - &self, - query: &T, - params: &[&(dyn tokio_postgres::types::ToSql + Sync)], - ) -> Result, Error> - where - T: ?Sized + tokio_postgres::ToStatement + Sync + Send, - { - Transaction::query(self, query, params).await - } - async fn query_raw(&self, statement: &T, params: I) -> Result - where - T: ?Sized + ToStatement + Sync + Send, - P: BorrowToSql, - I: IntoIterator + Sync + Send, - I::IntoIter: ExactSizeIterator, - { - Transaction::query_raw(self, statement, params).await - } -} -#[async_trait] -impl GenericClient for Client { - async fn prepare(&self, query: &str) -> Result { - Client::prepare(self, query).await - } - async fn execute( - &self, - query: &T, - params: &[&(dyn tokio_postgres::types::ToSql + Sync)], - ) -> Result - where - T: ?Sized + tokio_postgres::ToStatement + Sync + Send, - { - Client::execute(self, query, params).await - } - async fn query_one( - &self, - statement: &T, - params: &[&(dyn tokio_postgres::types::ToSql + Sync)], - ) -> Result - where - T: ?Sized + tokio_postgres::ToStatement + Sync + Send, - { - Client::query_one(self, statement, params).await - } - async fn query_opt( - &self, - statement: &T, - params: &[&(dyn tokio_postgres::types::ToSql + Sync)], - ) -> Result, Error> - where - T: ?Sized + tokio_postgres::ToStatement + Sync + Send, - { - Client::query_opt(self, statement, params).await - } - async fn query( - &self, - query: &T, - params: &[&(dyn tokio_postgres::types::ToSql + Sync)], - ) -> Result, Error> - where - T: ?Sized + tokio_postgres::ToStatement + Sync + Send, - { - Client::query(self, query, params).await - } - async fn query_raw(&self, statement: &T, params: I) -> Result - where - T: ?Sized + ToStatement + Sync + Send, - P: BorrowToSql, - I: IntoIterator + Sync + Send, - I::IntoIter: ExactSizeIterator, - { - Client::query_raw(self, statement, params).await - } -} diff --git a/benches/temp/src/client/domain.rs b/benches/temp/src/client/domain.rs deleted file mode 100644 index 791bb075..00000000 --- a/benches/temp/src/client/domain.rs +++ /dev/null @@ -1,90 +0,0 @@ -use super::{type_traits::ArraySql, utils::escape_domain}; -use postgres_protocol::types::{array_to_sql, ArrayDimension}; -use postgres_types::{private::BytesMut, IsNull, Kind, ToSql, Type}; -use std::{ - error::Error, - fmt::{Debug, Formatter}, -}; -pub struct Domain(pub T); -impl Debug for Domain { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - f.debug_tuple("DomainWrapper").field(&self.0).finish() - } -} -impl ToSql for Domain { - fn to_sql(&self, ty: &Type, out: &mut BytesMut) -> Result> - where - Self: Sized, - { - postgres_types::ToSql::to_sql(&self.0, escape_domain(ty), out) - } - fn accepts(ty: &Type) -> bool - where - Self: Sized, - { - return T::accepts(escape_domain(ty)); - } - fn to_sql_checked( - &self, - ty: &Type, - out: &mut BytesMut, - ) -> Result> { - postgres_types::__to_sql_checked(self, ty, out) - } -} -pub struct DomainArray<'a, T: ToSql + Sync, A: ArraySql>(pub &'a A); -impl<'a, T: ToSql + Sync, A: ArraySql> Debug for DomainArray<'a, T, A> { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - f.debug_tuple("ArrayDomain").field(&self.0).finish() - } -} -impl<'a, T: ToSql + Sync + 'a, A: ArraySql> ToSql for DomainArray<'a, T, A> { - fn to_sql(&self, ty: &Type, w: &mut BytesMut) -> Result> { - self.0.escape_domain_to_sql(ty, w) - } - fn accepts(ty: &Type) -> bool { - match *ty.kind() { - Kind::Array(ref member) => T::accepts(escape_domain(member)), - _ => false, - } - } - fn to_sql_checked( - &self, - ty: &Type, - out: &mut BytesMut, - ) -> Result> { - postgres_types::__to_sql_checked(self, ty, out) - } -} -pub fn escape_domain_to_sql( - ty: &Type, - w: &mut BytesMut, - iter: impl Iterator + ExactSizeIterator, -) -> Result> { - let member_type = match *ty.kind() { - Kind::Array(ref member) => escape_domain(member), - _ => panic!("expected array type got {ty}"), - }; - let dimension = ArrayDimension { - len: downcast(iter.len())?, - lower_bound: 1, - }; - array_to_sql( - Some(dimension), - member_type.oid(), - iter, - |e, w| match Domain(e).to_sql(member_type, w)? { - IsNull::No => Ok(postgres_protocol::IsNull::No), - IsNull::Yes => Ok(postgres_protocol::IsNull::Yes), - }, - w, - )?; - Ok(IsNull::No) -} -fn downcast(len: usize) -> Result> { - if len > i32::max_value() as usize { - Err("value too large to transmit".into()) - } else { - Ok(len as i32) - } -} diff --git a/benches/temp/src/client/sync.rs b/benches/temp/src/client/sync.rs deleted file mode 100644 index 706a1e51..00000000 --- a/benches/temp/src/client/sync.rs +++ /dev/null @@ -1,30 +0,0 @@ -use postgres::Statement; -/// This trait allows you to bind parameters to a query using a single -/// struct, rather than passing each bind parameter as a function parameter. -pub trait Params<'a, P, O, C> { - fn params(&'a mut self, client: &'a mut C, params: &'a P) -> O; -} -/// Cached statement -pub(crate) struct Stmt { - query: &'static str, - cached: Option, -} -impl Stmt { - #[must_use] - pub fn new(query: &'static str) -> Self { - Self { - query, - cached: None, - } - } - pub fn prepare<'a, C: postgres::GenericClient>( - &'a mut self, - client: &mut C, - ) -> Result<&'a Statement, postgres::Error> { - if self.cached.is_none() { - let stmt = client.prepare(self.query)?; - self.cached = Some(stmt); - } - Ok(unsafe { self.cached.as_ref().unwrap_unchecked() }) - } -} diff --git a/benches/temp/src/client/type_traits.rs b/benches/temp/src/client/type_traits.rs deleted file mode 100644 index 0a4992d1..00000000 --- a/benches/temp/src/client/type_traits.rs +++ /dev/null @@ -1,120 +0,0 @@ - -use super::domain::escape_domain_to_sql; -use postgres_protocol::types::{self, ArrayDimension}; -use postgres_types::{private::BytesMut, to_sql_checked, IsNull, Kind, ToSql, Type}; -use std::borrow::Cow; -pub trait StringSql: std::fmt::Debug + ToSql + Sync {} -impl StringSql for &T {} -impl StringSql for String {} -impl StringSql for &str {} -impl StringSql for Cow<'_, str> {} -impl StringSql for Box {} -pub trait BytesSql: std::fmt::Debug + ToSql + Send + Sync {} -impl BytesSql for &T {} -impl BytesSql for Vec {} -impl BytesSql for &[u8] {} -pub trait ArraySql: std::fmt::Debug + ToSql + Send + Sync { - type Item; - fn escape_domain_to_sql( - &self, - ty: &Type, - w: &mut BytesMut, - ) -> Result>; -} -impl> ArraySql for &A { - type Item = T; - fn escape_domain_to_sql( - &self, - ty: &Type, - w: &mut BytesMut, - ) -> Result> { - A::escape_domain_to_sql(self, ty, w) - } -} -impl ArraySql for Vec { - type Item = T; - fn escape_domain_to_sql( - &self, - ty: &Type, - w: &mut BytesMut, - ) -> Result> { - escape_domain_to_sql(ty, w, self.iter()) - } -} -impl ArraySql for &[T] { - type Item = T; - fn escape_domain_to_sql( - &self, - ty: &Type, - w: &mut BytesMut, - ) -> Result> { - escape_domain_to_sql(ty, w, self.iter()) - } -} -impl< - T: std::fmt::Debug + ToSql + Send + Sync, - I: Iterator + ExactSizeIterator, - F: Fn() -> I + Send + Sync, - > ArraySql for IterSql -{ - type Item = T; - fn escape_domain_to_sql( - &self, - ty: &Type, - w: &mut BytesMut, - ) -> Result> { - escape_domain_to_sql(ty, w, (self.0)()) - } -} -pub struct IterSql + ExactSizeIterator, F: Fn() -> I + Sync>(pub F); -impl + ExactSizeIterator, F: Fn() -> I + Sync> std::fmt::Debug - for IterSql -{ - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_tuple("ArrayFn").finish() - } -} -impl + ExactSizeIterator, F: Fn() -> I + Sync> ToSql - for IterSql -{ - fn to_sql( - &self, - ty: &Type, - w: &mut BytesMut, - ) -> Result> { - let member_type = match *ty.kind() { - Kind::Array(ref member) => member, - _ => panic!("expected array type"), - }; - let iter = (self.0)(); - let dimension = ArrayDimension { - len: downcast(iter.len())?, - lower_bound: 1, - }; - types::array_to_sql( - Some(dimension), - member_type.oid(), - iter, - |e, w| match e.to_sql(member_type, w)? { - IsNull::No => Ok(postgres_protocol::IsNull::No), - IsNull::Yes => Ok(postgres_protocol::IsNull::Yes), - }, - w, - )?; - Ok(IsNull::No) - } - fn accepts(ty: &Type) -> bool { - match *ty.kind() { - Kind::Array(ref member) => T::accepts(member), - _ => false, - } - } - to_sql_checked!(); -} -fn downcast(len: usize) -> Result> { - if len > i32::max_value() as usize { - Err("value too large to transmit".into()) - } else { - Ok(len as i32) - } -} diff --git a/benches/temp/src/client/utils.rs b/benches/temp/src/client/utils.rs deleted file mode 100644 index ec54efdd..00000000 --- a/benches/temp/src/client/utils.rs +++ /dev/null @@ -1,12 +0,0 @@ -use postgres_types::{Kind, ToSql, Type}; -pub fn escape_domain(ty: &Type) -> &Type { - match ty.kind() { - Kind::Domain(ty) => ty, - _ => ty, - } -} -pub fn slice_iter<'a>( - s: &'a [&'a (dyn ToSql + Sync)], -) -> impl ExactSizeIterator + 'a { - s.iter().map(|s| *s as _) -} diff --git a/benches/temp/src/lib.rs b/benches/temp/src/lib.rs deleted file mode 100644 index 00aae724..00000000 --- a/benches/temp/src/lib.rs +++ /dev/null @@ -1,957 +0,0 @@ -// This file was generated with `cornucopia`. Do not modify. - -#[allow(clippy::all, clippy::pedantic)] -#[allow(unused_variables)] -#[allow(unused_imports)] -#[allow(dead_code)] -pub mod types {} -#[allow(clippy::all, clippy::pedantic)] -#[allow(unused_variables)] -#[allow(unused_imports)] -#[allow(dead_code)] -pub mod queries { - pub mod bench { - #[derive(Debug)] - pub struct InsertUserParams { - pub name: T1, - pub hair_color: Option, - } - #[derive(Debug, Clone, PartialEq)] - pub struct User { - pub id: i32, - pub name: String, - pub hair_color: Option, - } - pub struct UserBorrowed<'a> { - pub id: i32, - pub name: &'a str, - pub hair_color: Option<&'a str>, - } - impl<'a> From> for User { - fn from( - UserBorrowed { - id, - name, - hair_color, - }: UserBorrowed<'a>, - ) -> Self { - Self { - id, - name: name.into(), - hair_color: hair_color.map(|v| v.into()), - } - } - } - #[derive(Debug, Clone, PartialEq)] - pub struct Post { - pub id: i32, - pub user_id: i32, - pub title: String, - pub body: Option, - } - pub struct PostBorrowed<'a> { - pub id: i32, - pub user_id: i32, - pub title: &'a str, - pub body: Option<&'a str>, - } - impl<'a> From> for Post { - fn from( - PostBorrowed { - id, - user_id, - title, - body, - }: PostBorrowed<'a>, - ) -> Self { - Self { - id, - user_id, - title: title.into(), - body: body.map(|v| v.into()), - } - } - } - #[derive(Debug, Clone, PartialEq)] - pub struct Comment { - pub id: i32, - pub post_id: i32, - pub text: String, - } - pub struct CommentBorrowed<'a> { - pub id: i32, - pub post_id: i32, - pub text: &'a str, - } - impl<'a> From> for Comment { - fn from(CommentBorrowed { id, post_id, text }: CommentBorrowed<'a>) -> Self { - Self { - id, - post_id, - text: text.into(), - } - } - } - #[derive(Debug, Clone, PartialEq)] - pub struct SelectComplex { - pub myuser_id: i32, - pub name: String, - pub hair_color: Option, - pub post_id: Option, - pub user_id: Option, - pub title: Option, - pub body: Option, - } - pub struct SelectComplexBorrowed<'a> { - pub myuser_id: i32, - pub name: &'a str, - pub hair_color: Option<&'a str>, - pub post_id: Option, - pub user_id: Option, - pub title: Option<&'a str>, - pub body: Option<&'a str>, - } - impl<'a> From> for SelectComplex { - fn from( - SelectComplexBorrowed { - myuser_id, - name, - hair_color, - post_id, - user_id, - title, - body, - }: SelectComplexBorrowed<'a>, - ) -> Self { - Self { - myuser_id, - name: name.into(), - hair_color: hair_color.map(|v| v.into()), - post_id, - user_id, - title: title.map(|v| v.into()), - body: body.map(|v| v.into()), - } - } - } - pub mod sync { - use postgres::{fallible_iterator::FallibleIterator, GenericClient}; - pub struct UserQuery<'a, C: GenericClient, T, const N: usize> { - client: &'a mut C, - params: [&'a (dyn postgres_types::ToSql + Sync); N], - stmt: &'a mut crate::client::sync::Stmt, - extractor: fn(&postgres::Row) -> super::UserBorrowed, - mapper: fn(super::UserBorrowed) -> T, - } - impl<'a, C, T: 'a, const N: usize> UserQuery<'a, C, T, N> - where - C: GenericClient, - { - pub fn map( - self, - mapper: fn(super::UserBorrowed) -> R, - ) -> UserQuery<'a, C, R, N> { - UserQuery { - client: self.client, - params: self.params, - stmt: self.stmt, - extractor: self.extractor, - mapper, - } - } - pub fn one(self) -> Result { - let stmt = self.stmt.prepare(self.client)?; - let row = self.client.query_one(stmt, &self.params)?; - Ok((self.mapper)((self.extractor)(&row))) - } - pub fn all(self) -> Result, postgres::Error> { - self.iter()?.collect() - } - pub fn opt(self) -> Result, postgres::Error> { - let stmt = self.stmt.prepare(self.client)?; - Ok(self - .client - .query_opt(stmt, &self.params)? - .map(|row| (self.mapper)((self.extractor)(&row)))) - } - pub fn iter( - self, - ) -> Result> + 'a, postgres::Error> - { - let stmt = self.stmt.prepare(self.client)?; - let it = self - .client - .query_raw(stmt, crate::client::slice_iter(&self.params))? - .iterator() - .map(move |res| res.map(|row| (self.mapper)((self.extractor)(&row)))); - Ok(it) - } - } - pub struct PostQuery<'a, C: GenericClient, T, const N: usize> { - client: &'a mut C, - params: [&'a (dyn postgres_types::ToSql + Sync); N], - stmt: &'a mut crate::client::sync::Stmt, - extractor: fn(&postgres::Row) -> super::PostBorrowed, - mapper: fn(super::PostBorrowed) -> T, - } - impl<'a, C, T: 'a, const N: usize> PostQuery<'a, C, T, N> - where - C: GenericClient, - { - pub fn map( - self, - mapper: fn(super::PostBorrowed) -> R, - ) -> PostQuery<'a, C, R, N> { - PostQuery { - client: self.client, - params: self.params, - stmt: self.stmt, - extractor: self.extractor, - mapper, - } - } - pub fn one(self) -> Result { - let stmt = self.stmt.prepare(self.client)?; - let row = self.client.query_one(stmt, &self.params)?; - Ok((self.mapper)((self.extractor)(&row))) - } - pub fn all(self) -> Result, postgres::Error> { - self.iter()?.collect() - } - pub fn opt(self) -> Result, postgres::Error> { - let stmt = self.stmt.prepare(self.client)?; - Ok(self - .client - .query_opt(stmt, &self.params)? - .map(|row| (self.mapper)((self.extractor)(&row)))) - } - pub fn iter( - self, - ) -> Result> + 'a, postgres::Error> - { - let stmt = self.stmt.prepare(self.client)?; - let it = self - .client - .query_raw(stmt, crate::client::slice_iter(&self.params))? - .iterator() - .map(move |res| res.map(|row| (self.mapper)((self.extractor)(&row)))); - Ok(it) - } - } - pub struct CommentQuery<'a, C: GenericClient, T, const N: usize> { - client: &'a mut C, - params: [&'a (dyn postgres_types::ToSql + Sync); N], - stmt: &'a mut crate::client::sync::Stmt, - extractor: fn(&postgres::Row) -> super::CommentBorrowed, - mapper: fn(super::CommentBorrowed) -> T, - } - impl<'a, C, T: 'a, const N: usize> CommentQuery<'a, C, T, N> - where - C: GenericClient, - { - pub fn map( - self, - mapper: fn(super::CommentBorrowed) -> R, - ) -> CommentQuery<'a, C, R, N> { - CommentQuery { - client: self.client, - params: self.params, - stmt: self.stmt, - extractor: self.extractor, - mapper, - } - } - pub fn one(self) -> Result { - let stmt = self.stmt.prepare(self.client)?; - let row = self.client.query_one(stmt, &self.params)?; - Ok((self.mapper)((self.extractor)(&row))) - } - pub fn all(self) -> Result, postgres::Error> { - self.iter()?.collect() - } - pub fn opt(self) -> Result, postgres::Error> { - let stmt = self.stmt.prepare(self.client)?; - Ok(self - .client - .query_opt(stmt, &self.params)? - .map(|row| (self.mapper)((self.extractor)(&row)))) - } - pub fn iter( - self, - ) -> Result> + 'a, postgres::Error> - { - let stmt = self.stmt.prepare(self.client)?; - let it = self - .client - .query_raw(stmt, crate::client::slice_iter(&self.params))? - .iterator() - .map(move |res| res.map(|row| (self.mapper)((self.extractor)(&row)))); - Ok(it) - } - } - pub struct SelectComplexQuery<'a, C: GenericClient, T, const N: usize> { - client: &'a mut C, - params: [&'a (dyn postgres_types::ToSql + Sync); N], - stmt: &'a mut crate::client::sync::Stmt, - extractor: fn(&postgres::Row) -> super::SelectComplexBorrowed, - mapper: fn(super::SelectComplexBorrowed) -> T, - } - impl<'a, C, T: 'a, const N: usize> SelectComplexQuery<'a, C, T, N> - where - C: GenericClient, - { - pub fn map( - self, - mapper: fn(super::SelectComplexBorrowed) -> R, - ) -> SelectComplexQuery<'a, C, R, N> { - SelectComplexQuery { - client: self.client, - params: self.params, - stmt: self.stmt, - extractor: self.extractor, - mapper, - } - } - pub fn one(self) -> Result { - let stmt = self.stmt.prepare(self.client)?; - let row = self.client.query_one(stmt, &self.params)?; - Ok((self.mapper)((self.extractor)(&row))) - } - pub fn all(self) -> Result, postgres::Error> { - self.iter()?.collect() - } - pub fn opt(self) -> Result, postgres::Error> { - let stmt = self.stmt.prepare(self.client)?; - Ok(self - .client - .query_opt(stmt, &self.params)? - .map(|row| (self.mapper)((self.extractor)(&row)))) - } - pub fn iter( - self, - ) -> Result> + 'a, postgres::Error> - { - let stmt = self.stmt.prepare(self.client)?; - let it = self - .client - .query_raw(stmt, crate::client::slice_iter(&self.params))? - .iterator() - .map(move |res| res.map(|row| (self.mapper)((self.extractor)(&row)))); - Ok(it) - } - } - pub fn users() -> UsersStmt { - UsersStmt(crate::client::sync::Stmt::new("SELECT * FROM users")) - } - pub struct UsersStmt(crate::client::sync::Stmt); - impl UsersStmt { - pub fn bind<'a, C: GenericClient>( - &'a mut self, - client: &'a mut C, - ) -> UserQuery<'a, C, super::User, 0> { - UserQuery { - client, - params: [], - stmt: &mut self.0, - extractor: |row| super::UserBorrowed { - id: row.get(0), - name: row.get(1), - hair_color: row.get(2), - }, - mapper: |it| ::from(it), - } - } - } - pub fn insert_user() -> InsertUserStmt { - InsertUserStmt(crate::client::sync::Stmt::new( - "INSERT INTO users (name, hair_color) VALUES ($1, $2)", - )) - } - pub struct InsertUserStmt(crate::client::sync::Stmt); - impl InsertUserStmt { - pub fn bind< - 'a, - C: GenericClient, - T1: crate::client::StringSql, - T2: crate::client::StringSql, - >( - &'a mut self, - client: &'a mut C, - name: &'a T1, - hair_color: &'a Option, - ) -> Result { - let stmt = self.0.prepare(client)?; - client.execute(stmt, &[name, hair_color]) - } - } - impl< - 'a, - C: GenericClient, - T1: crate::client::StringSql, - T2: crate::client::StringSql, - > - crate::client::sync::Params< - 'a, - super::InsertUserParams, - Result, - C, - > for InsertUserStmt - { - fn params( - &'a mut self, - client: &'a mut C, - params: &'a super::InsertUserParams, - ) -> Result { - self.bind(client, ¶ms.name, ¶ms.hair_color) - } - } - pub fn posts() -> PostsStmt { - PostsStmt(crate::client::sync::Stmt::new("SELECT * FROM posts")) - } - pub struct PostsStmt(crate::client::sync::Stmt); - impl PostsStmt { - pub fn bind<'a, C: GenericClient>( - &'a mut self, - client: &'a mut C, - ) -> PostQuery<'a, C, super::Post, 0> { - PostQuery { - client, - params: [], - stmt: &mut self.0, - extractor: |row| super::PostBorrowed { - id: row.get(0), - user_id: row.get(1), - title: row.get(2), - body: row.get(3), - }, - mapper: |it| ::from(it), - } - } - } - pub fn post_by_user_ids() -> PostByUserIdsStmt { - PostByUserIdsStmt(crate::client::sync::Stmt::new( - "SELECT * FROM posts WHERE user_id = ANY($1)", - )) - } - pub struct PostByUserIdsStmt(crate::client::sync::Stmt); - impl PostByUserIdsStmt { - pub fn bind<'a, C: GenericClient, T1: crate::client::ArraySql>( - &'a mut self, - client: &'a mut C, - ids: &'a T1, - ) -> PostQuery<'a, C, super::Post, 1> { - PostQuery { - client, - params: [ids], - stmt: &mut self.0, - extractor: |row| super::PostBorrowed { - id: row.get(0), - user_id: row.get(1), - title: row.get(2), - body: row.get(3), - }, - mapper: |it| ::from(it), - } - } - } - pub fn comments() -> CommentsStmt { - CommentsStmt(crate::client::sync::Stmt::new("SELECT * FROM comments")) - } - pub struct CommentsStmt(crate::client::sync::Stmt); - impl CommentsStmt { - pub fn bind<'a, C: GenericClient>( - &'a mut self, - client: &'a mut C, - ) -> CommentQuery<'a, C, super::Comment, 0> { - CommentQuery { - client, - params: [], - stmt: &mut self.0, - extractor: |row| super::CommentBorrowed { - id: row.get(0), - post_id: row.get(1), - text: row.get(2), - }, - mapper: |it| ::from(it), - } - } - } - pub fn comments_by_post_id() -> CommentsByPostIdStmt { - CommentsByPostIdStmt(crate::client::sync::Stmt::new( - "SELECT * FROM comments WHERE post_id = ANY($1)", - )) - } - pub struct CommentsByPostIdStmt(crate::client::sync::Stmt); - impl CommentsByPostIdStmt { - pub fn bind<'a, C: GenericClient, T1: crate::client::ArraySql>( - &'a mut self, - client: &'a mut C, - ids: &'a T1, - ) -> CommentQuery<'a, C, super::Comment, 1> { - CommentQuery { - client, - params: [ids], - stmt: &mut self.0, - extractor: |row| super::CommentBorrowed { - id: row.get(0), - post_id: row.get(1), - text: row.get(2), - }, - mapper: |it| ::from(it), - } - } - } - pub fn select_complex() -> SelectComplexStmt { - SelectComplexStmt(crate::client::sync :: Stmt :: new("SELECT u.id as myuser_id, u.name, u.hair_color, p.id as post_id, p.user_id, p.title, p.body FROM users as u LEFT JOIN posts as p on u.id = p.user_id")) - } - pub struct SelectComplexStmt(crate::client::sync::Stmt); - impl SelectComplexStmt { - pub fn bind<'a, C: GenericClient>( - &'a mut self, - client: &'a mut C, - ) -> SelectComplexQuery<'a, C, super::SelectComplex, 0> { - SelectComplexQuery { - client, - params: [], - stmt: &mut self.0, - extractor: |row| super::SelectComplexBorrowed { - myuser_id: row.get(0), - name: row.get(1), - hair_color: row.get(2), - post_id: row.get(3), - user_id: row.get(4), - title: row.get(5), - body: row.get(6), - }, - mapper: |it| ::from(it), - } - } - } - } - pub mod async_ { - use crate::client::async_::GenericClient; - use futures; - use futures::{StreamExt, TryStreamExt}; - pub struct UserQuery<'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) -> super::UserBorrowed, - mapper: fn(super::UserBorrowed) -> T, - } - impl<'a, C, T: 'a, const N: usize> UserQuery<'a, C, T, N> - where - C: GenericClient, - { - pub fn map( - self, - mapper: fn(super::UserBorrowed) -> R, - ) -> UserQuery<'a, C, R, N> { - UserQuery { - client: self.client, - params: self.params, - stmt: self.stmt, - extractor: self.extractor, - mapper, - } - } - pub async fn one(self) -> Result { - 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, tokio_postgres::Error> { - self.iter().await?.try_collect().await - } - pub async fn opt(self) -> Result, 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> + 'a, - tokio_postgres::Error, - > { - let stmt = self.stmt.prepare(self.client).await?; - let it = self - .client - .query_raw(stmt, crate::client::slice_iter(&self.params)) - .await? - .map(move |res| res.map(|row| (self.mapper)((self.extractor)(&row)))) - .into_stream(); - Ok(it) - } - } - pub struct PostQuery<'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) -> super::PostBorrowed, - mapper: fn(super::PostBorrowed) -> T, - } - impl<'a, C, T: 'a, const N: usize> PostQuery<'a, C, T, N> - where - C: GenericClient, - { - pub fn map( - self, - mapper: fn(super::PostBorrowed) -> R, - ) -> PostQuery<'a, C, R, N> { - PostQuery { - client: self.client, - params: self.params, - stmt: self.stmt, - extractor: self.extractor, - mapper, - } - } - pub async fn one(self) -> Result { - 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, tokio_postgres::Error> { - self.iter().await?.try_collect().await - } - pub async fn opt(self) -> Result, 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> + 'a, - tokio_postgres::Error, - > { - let stmt = self.stmt.prepare(self.client).await?; - let it = self - .client - .query_raw(stmt, crate::client::slice_iter(&self.params)) - .await? - .map(move |res| res.map(|row| (self.mapper)((self.extractor)(&row)))) - .into_stream(); - Ok(it) - } - } - pub struct CommentQuery<'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) -> super::CommentBorrowed, - mapper: fn(super::CommentBorrowed) -> T, - } - impl<'a, C, T: 'a, const N: usize> CommentQuery<'a, C, T, N> - where - C: GenericClient, - { - pub fn map( - self, - mapper: fn(super::CommentBorrowed) -> R, - ) -> CommentQuery<'a, C, R, N> { - CommentQuery { - client: self.client, - params: self.params, - stmt: self.stmt, - extractor: self.extractor, - mapper, - } - } - pub async fn one(self) -> Result { - 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, tokio_postgres::Error> { - self.iter().await?.try_collect().await - } - pub async fn opt(self) -> Result, 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> + 'a, - tokio_postgres::Error, - > { - let stmt = self.stmt.prepare(self.client).await?; - let it = self - .client - .query_raw(stmt, crate::client::slice_iter(&self.params)) - .await? - .map(move |res| res.map(|row| (self.mapper)((self.extractor)(&row)))) - .into_stream(); - Ok(it) - } - } - pub struct SelectComplexQuery<'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) -> super::SelectComplexBorrowed, - mapper: fn(super::SelectComplexBorrowed) -> T, - } - impl<'a, C, T: 'a, const N: usize> SelectComplexQuery<'a, C, T, N> - where - C: GenericClient, - { - pub fn map( - self, - mapper: fn(super::SelectComplexBorrowed) -> R, - ) -> SelectComplexQuery<'a, C, R, N> { - SelectComplexQuery { - client: self.client, - params: self.params, - stmt: self.stmt, - extractor: self.extractor, - mapper, - } - } - pub async fn one(self) -> Result { - 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, tokio_postgres::Error> { - self.iter().await?.try_collect().await - } - pub async fn opt(self) -> Result, 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> + 'a, - tokio_postgres::Error, - > { - let stmt = self.stmt.prepare(self.client).await?; - let it = self - .client - .query_raw(stmt, crate::client::slice_iter(&self.params)) - .await? - .map(move |res| res.map(|row| (self.mapper)((self.extractor)(&row)))) - .into_stream(); - Ok(it) - } - } - pub fn users() -> UsersStmt { - UsersStmt(crate::client::async_::Stmt::new("SELECT * FROM users")) - } - pub struct UsersStmt(crate::client::async_::Stmt); - impl UsersStmt { - pub fn bind<'a, C: GenericClient>( - &'a mut self, - client: &'a C, - ) -> UserQuery<'a, C, super::User, 0> { - UserQuery { - client, - params: [], - stmt: &mut self.0, - extractor: |row| super::UserBorrowed { - id: row.get(0), - name: row.get(1), - hair_color: row.get(2), - }, - mapper: |it| ::from(it), - } - } - } - pub fn insert_user() -> InsertUserStmt { - InsertUserStmt(crate::client::async_::Stmt::new( - "INSERT INTO users (name, hair_color) VALUES ($1, $2)", - )) - } - pub struct InsertUserStmt(crate::client::async_::Stmt); - impl InsertUserStmt { - pub async fn bind< - 'a, - C: GenericClient, - T1: crate::client::StringSql, - T2: crate::client::StringSql, - >( - &'a mut self, - client: &'a C, - name: &'a T1, - hair_color: &'a Option, - ) -> Result { - let stmt = self.0.prepare(client).await?; - client.execute(stmt, &[name, hair_color]).await - } - } - impl< - 'a, - C: GenericClient + Send + Sync, - T1: crate::client::StringSql, - T2: crate::client::StringSql, - > - crate::client::async_::Params< - 'a, - super::InsertUserParams, - std::pin::Pin< - Box< - dyn futures::Future> - + Send - + 'a, - >, - >, - C, - > for InsertUserStmt - { - fn params( - &'a mut self, - client: &'a C, - params: &'a super::InsertUserParams, - ) -> std::pin::Pin< - Box< - dyn futures::Future> - + Send - + 'a, - >, - > { - Box::pin(self.bind(client, ¶ms.name, ¶ms.hair_color)) - } - } - pub fn posts() -> PostsStmt { - PostsStmt(crate::client::async_::Stmt::new("SELECT * FROM posts")) - } - pub struct PostsStmt(crate::client::async_::Stmt); - impl PostsStmt { - pub fn bind<'a, C: GenericClient>( - &'a mut self, - client: &'a C, - ) -> PostQuery<'a, C, super::Post, 0> { - PostQuery { - client, - params: [], - stmt: &mut self.0, - extractor: |row| super::PostBorrowed { - id: row.get(0), - user_id: row.get(1), - title: row.get(2), - body: row.get(3), - }, - mapper: |it| ::from(it), - } - } - } - pub fn post_by_user_ids() -> PostByUserIdsStmt { - PostByUserIdsStmt(crate::client::async_::Stmt::new( - "SELECT * FROM posts WHERE user_id = ANY($1)", - )) - } - pub struct PostByUserIdsStmt(crate::client::async_::Stmt); - impl PostByUserIdsStmt { - pub fn bind<'a, C: GenericClient, T1: crate::client::ArraySql>( - &'a mut self, - client: &'a C, - ids: &'a T1, - ) -> PostQuery<'a, C, super::Post, 1> { - PostQuery { - client, - params: [ids], - stmt: &mut self.0, - extractor: |row| super::PostBorrowed { - id: row.get(0), - user_id: row.get(1), - title: row.get(2), - body: row.get(3), - }, - mapper: |it| ::from(it), - } - } - } - pub fn comments() -> CommentsStmt { - CommentsStmt(crate::client::async_::Stmt::new("SELECT * FROM comments")) - } - pub struct CommentsStmt(crate::client::async_::Stmt); - impl CommentsStmt { - pub fn bind<'a, C: GenericClient>( - &'a mut self, - client: &'a C, - ) -> CommentQuery<'a, C, super::Comment, 0> { - CommentQuery { - client, - params: [], - stmt: &mut self.0, - extractor: |row| super::CommentBorrowed { - id: row.get(0), - post_id: row.get(1), - text: row.get(2), - }, - mapper: |it| ::from(it), - } - } - } - pub fn comments_by_post_id() -> CommentsByPostIdStmt { - CommentsByPostIdStmt(crate::client::async_::Stmt::new( - "SELECT * FROM comments WHERE post_id = ANY($1)", - )) - } - pub struct CommentsByPostIdStmt(crate::client::async_::Stmt); - impl CommentsByPostIdStmt { - pub fn bind<'a, C: GenericClient, T1: crate::client::ArraySql>( - &'a mut self, - client: &'a C, - ids: &'a T1, - ) -> CommentQuery<'a, C, super::Comment, 1> { - CommentQuery { - client, - params: [ids], - stmt: &mut self.0, - extractor: |row| super::CommentBorrowed { - id: row.get(0), - post_id: row.get(1), - text: row.get(2), - }, - mapper: |it| ::from(it), - } - } - } - pub fn select_complex() -> SelectComplexStmt { - SelectComplexStmt(crate::client::async_ :: Stmt :: new("SELECT u.id as myuser_id, u.name, u.hair_color, p.id as post_id, p.user_id, p.title, p.body FROM users as u LEFT JOIN posts as p on u.id = p.user_id")) - } - pub struct SelectComplexStmt(crate::client::async_::Stmt); - impl SelectComplexStmt { - pub fn bind<'a, C: GenericClient>( - &'a mut self, - client: &'a C, - ) -> SelectComplexQuery<'a, C, super::SelectComplex, 0> { - SelectComplexQuery { - client, - params: [], - stmt: &mut self.0, - extractor: |row| super::SelectComplexBorrowed { - myuser_id: row.get(0), - name: row.get(1), - hair_color: row.get(2), - post_id: row.get(3), - user_id: row.get(4), - title: row.get(5), - body: row.get(6), - }, - mapper: |it| ::from(it), - } - } - } - } - } -} -pub mod client; diff --git a/crates/cornucopia/src/codegen/cargo.rs b/crates/cornucopia/src/codegen/cargo.rs index fe42b628..e6cd8050 100644 --- a/crates/cornucopia/src/codegen/cargo.rs +++ b/crates/cornucopia/src/codegen/cargo.rs @@ -47,7 +47,6 @@ pub fn gen_cargo_file( dependency_analysis: &DependencyAnalysis, settings: CodegenSettings, ) -> String { - // TODO rework client codegen to make more dependencies optionals const VERSION: &str = env!("CARGO_PKG_VERSION"); let mut buf = formatdoc! {r#" # This file was generated with `cornucopia`. Do not modify diff --git a/examples/auto_build/_build.rs b/examples/auto_build/_build.rs index 2777d96f..94fbc461 100644 --- a/examples/auto_build/_build.rs +++ b/examples/auto_build/_build.rs @@ -5,10 +5,9 @@ use cornucopia::{CodegenSettings, Error}; // we could also generate it elsewhere and embed the generated // file with a `include_str` statement in your project. fn main() -> Result<(), Error> { - /* TODO replace with cargo-px let queries_path = "queries"; let schema_file = "schema.sql"; - let destination = "cornucopia"; + let destination = "auto_build_codegen"; let settings = CodegenSettings { is_async: true, derive_ser: false, @@ -16,14 +15,13 @@ fn main() -> Result<(), Error> { println!("cargo:rerun-if-changed={queries_path}"); println!("cargo:rerun-if-changed={schema_file}"); - cornucopia::generate_managed( + cornucopia::gen_managed( queries_path, &[schema_file], - Some(destination), + destination, false, settings, )?; - */ Ok(()) }