forked from cornucopia-rs/cornucopia
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🎨 Split client into
core
, async
and sync
crates.
- Loading branch information
1 parent
5312849
commit e400630
Showing
25 changed files
with
261 additions
and
946 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
[workspace] | ||
members = [ | ||
"examples/*", | ||
"cornucopia_client", | ||
"clients/*", | ||
"integration", | ||
"codegen_test", | ||
"bench", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#[doc(hidden)] | ||
pub mod private; | ||
|
||
pub use crate::generic_client::GenericClient; | ||
pub use cornucopia_client_core::ArrayIterator; | ||
|
||
#[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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
pub use cornucopia_client_core::{slice_iter, Domain, DomainArray}; | ||
|
||
use crate::generic_client::GenericClient; | ||
use tokio_postgres::{Error, Statement}; | ||
|
||
/// Cached statement | ||
pub struct Stmt { | ||
query: &'static str, | ||
cached: Option<Statement>, | ||
} | ||
|
||
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() }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[package] | ||
name = "cornucopia_client_core" | ||
version = "0.2.2" | ||
edition = "2021" | ||
license = "MIT/Apache-2.0" | ||
description = "Generic client trait for Cornucopia users" | ||
homepage = "https://github.com/cornucopia-rs/cornucopia" | ||
repository = "https://github.com/cornucopia-rs/cornucopia" | ||
readme = "README.md" | ||
categories = ["database"] | ||
keywords = ["postgresql", "query", "generator", "sql", "tokio-postgres"] | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
postgres-protocol = "0.6.4" | ||
postgres-types = { version = "0.2.3" } | ||
fallible-iterator = "0.2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<div align="center"> <img src="https://raw.githubusercontent.com/cornucopia-rs/cornucopia/main/assets/logo.svg" width=200 /> </div> | ||
<h1 align="center">Cornucopia</h1> | ||
<div align="center"> | ||
<strong> | ||
Generate type checked Rust from your SQL | ||
</strong> | ||
</div> | ||
|
||
<br /> | ||
|
||
<div align="center"> | ||
<!-- Github Actions --> | ||
<img src="https://img.shields.io/github/workflow/status/cornucopia-rs/cornucopia/ci" alt="actions status" /> | ||
<!-- Version --> | ||
<a href="https://crates.io/crates/cornucopia"> | ||
<img src="https://img.shields.io/crates/v/cornucopia.svg?style=flat-square" | ||
alt="Crates.io version" /> | ||
</a> | ||
<!-- Downloads --> | ||
<a href="https://crates.io/crates/cornucopia"> | ||
<img src="https://img.shields.io/crates/d/cornucopia.svg?style=flat-square" | ||
alt="Download" /> | ||
</a> | ||
</div> | ||
|
||
<div align="center"> | ||
<h4> | ||
<a href="#install"> | ||
Install | ||
</a> | ||
<span> | </span> | ||
<a href="/examples/basic_async/README.md"> | ||
Example | ||
</a> | ||
</h4> | ||
</div> | ||
|
||
--- | ||
|
||
This crate is a small library exposing Cornucopia's `GenericClient`. You probably need this if you're a Cornucopia user. | ||
|
||
The `GenericClient` is an abstraction over four types of connections (`deadpool_postgres::Client`, `deadpool_postgres::Transaction`, `tokio_postgres::Client`, `tokio_postgres::Transaction`). Its meant to allow you to mix-and-match these connection types in Cornucopia Queries. | ||
|
||
| | non-pooled | pooled | | ||
| ---------------- | ----------------------------- | -------------------------------- | | ||
| single-statement | `tokio_postgres::Client` | `deadpool_postgres::Client` | | ||
| multi-statement | `tokio_postgres::Transaction` | `deadpool_postgres::Transaction` | |
5 changes: 3 additions & 2 deletions
5
cornucopia_client/src/array_iterator.rs → ...nucopia_client_core/src/array_iterator.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
mod array_iterator; | ||
mod domain; | ||
mod utils; | ||
|
||
pub use array_iterator::ArrayIterator; | ||
pub use domain::{Domain, DomainArray}; | ||
pub use utils::slice_iter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
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<Item = &'a dyn ToSql> + 'a { | ||
s.iter().map(|s| *s as _) | ||
} |
Oops, something went wrong.