Skip to content

Commit

Permalink
⚡️ Generic client.
Browse files Browse the repository at this point in the history
  • Loading branch information
LouisGariepy committed Apr 19, 2022
1 parent 5109bc2 commit 3ccbf0a
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 473 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 9 additions & 27 deletions src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ use crate::{

use super::prepare_queries::PreparedModule;

pub(crate) fn generate_query(
module_name: &str,
query: &PreparedQuery,
) -> Result<(String, String), Error> {
pub(crate) fn generate_query(module_name: &str, query: &PreparedQuery) -> Result<String, Error> {
let name = &query.name;
let query_struct = generate_query_struct(query)?.unwrap_or_default();
let params = generate_query_params(query)?;
Expand All @@ -29,12 +26,10 @@ pub(crate) fn generate_query(
let body = generate_query_body(query, ret_ty)?;
let query_string = format!(
"{query_struct}
pub async fn {name}(client:&Client, {params}) -> Result<{ret},Error> {{{body}}}"
pub async fn {name}<T: GenericClient>(client:&T, {params}) -> Result<{ret},Error> {{{body}}}"
);

let transaction_string = format!("pub async fn {name}<'a>(client:&Transaction<'a>, {params}) -> Result<{ret}, Error> {{{body}}}");

Ok((query_string, transaction_string))
Ok(query_string)
}

pub(crate) fn generate_custom_type(ty: &CornucopiaType) -> String {
Expand Down Expand Up @@ -281,7 +276,7 @@ pub(crate) fn generate_query_body(query: &PreparedQuery, ret_ty: String) -> Resu
};

Ok(format!(
"let stmt = client.prepare_cached({query_string}).await?;
"let stmt = client.prepare({query_string}).await?;
let {res_var_name} = client.{query_method}(&stmt, &[{query_param_values}]).await?;
{ret_value}"
Expand All @@ -293,47 +288,34 @@ pub(crate) fn generate(
modules: Vec<PreparedModule>,
destination: &str,
) -> Result<(), Error> {
let query_imports = r#"use deadpool_postgres::Client;
use tokio_postgres::{error::Error};"#;
let transaction_imports = r#"use deadpool_postgres::Transaction;
use tokio_postgres::{error::Error};"#;
let query_imports = r#"
use cornucopia::GenericClient;
use tokio_postgres::Error;"#;

let type_modules = generate_type_modules(type_registrar);

let mut query_modules = Vec::new();
let mut transaction_modules = Vec::new();
for module in modules {
let mut query_strings = Vec::new();
let mut transaction_strings = Vec::new();
for query in module.queries {
let (query_string, transaction_string) = generate_query(&module.name, &query)?;
let query_string = generate_query(&module.name, &query)?;
query_strings.push(query_string);
transaction_strings.push(transaction_string)
}
let queries_string = query_strings.join("\n\n");
let transactions_string = transaction_strings.join("\n\n");
let module_name = module.name;

query_modules.push(format!(
r#"pub mod {module_name} {{
{query_imports}
{queries_string}
}}"#
));
transaction_modules.push(format!(
r#"pub mod {module_name} {{
{transaction_imports}
{transactions_string}
}}"#
));
}

let generated_modules = format!(
"{type_modules} \n pub mod queries {{ {} }} \n pub mod transactions {{ {} }}",
"{type_modules} \n pub mod queries {{ {} }}",
query_modules.join("\n\n"),
transaction_modules.join("\n\n")
);

std::fs::write(destination, generated_modules)?;
Expand Down
13 changes: 6 additions & 7 deletions src/generic_client.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use async_trait::async_trait;
use deadpool_postgres::{Client, Transaction, ClientWrapper};
use tokio_postgres::{Client as PgClient, Error, Statement, Transaction as PgTransaction};

// This trait acts as an umbrella for all four of
// - `tokio_postgres::Client`
// - `deadpool_postgres::Client`
// - `tokio_postgres::Transaction`
// - `deadpool_postgres::Transaction`

use async_trait::async_trait;
use deadpool_postgres::{Client, Transaction};
use tokio_postgres::{Client as PgClient, Error, Statement, Transaction as PgTransaction};

#[async_trait]
pub trait GenericClient {
async fn prepare(&self, query: &str) -> Result<Statement, Error>;
Expand Down Expand Up @@ -44,7 +43,7 @@ pub trait GenericClient {
#[async_trait]
impl GenericClient for Transaction<'_> {
async fn prepare(&self, query: &str) -> Result<Statement, Error> {
PgTransaction::prepare(self, query).await
Transaction::prepare_cached(&self, query).await
}

async fn execute<T>(
Expand Down Expand Up @@ -146,7 +145,7 @@ impl GenericClient for PgTransaction<'_> {
#[async_trait]
impl GenericClient for Client {
async fn prepare(&self, query: &str) -> Result<Statement, Error> {
PgClient::prepare(self, query).await
ClientWrapper::prepare_cached(self, query).await
}

async fn execute<T>(
Expand Down
Loading

0 comments on commit 3ccbf0a

Please sign in to comment.