From 0fcc7ea9b851ec3de3c26ef773a0f34e3ee97a15 Mon Sep 17 00:00:00 2001 From: Francis Le Roy Date: Fri, 26 Feb 2021 17:35:45 +0100 Subject: [PATCH] Added 'build_with_params' function the visitor trait, allowing to build query from concatenations of queries Updated changelog --- CHANGELOG.md | 1 + src/visitor.rs | 7 +++++++ src/visitor/mssql.rs | 19 ++++++++++++++----- src/visitor/mysql.rs | 11 ++++++++++- src/visitor/postgres.rs | 11 ++++++++++- src/visitor/sqlite.rs | 11 ++++++++++- 6 files changed, 52 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ce39bfaea..5d930abcc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased - Added support for `ROW_TO_JSON` function in postgresql +- Added `build_with_params` function the `Visitor` trait ## v0.2.0-alpha.13 diff --git a/src/visitor.rs b/src/visitor.rs index ba32cbd46..f7e29589b 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -65,6 +65,13 @@ pub trait Visitor<'a> { where Q: Into>; + /// Behave exactly like [build](Visitor::build) but takes a vector of `Value<'a>` as arguments. + /// The next arguments to be inserted into the query will be **appended**, allowing to + /// build more complex queries. + fn build_with_params(query: Q, existing_params: Vec>) -> crate::Result<(String, Vec>)> + where + Q: Into>; + /// Write to the query. fn write(&mut self, s: D) -> Result; diff --git a/src/visitor/mssql.rs b/src/visitor/mssql.rs index a67ff309a..834d8b0af 100644 --- a/src/visitor/mssql.rs +++ b/src/visitor/mssql.rs @@ -128,17 +128,26 @@ impl<'a> Visitor<'a> for Mssql<'a> { fn build(query: Q) -> crate::Result<(String, Vec>)> where - Q: Into>, + Q: Into>, { - let mut this = Mssql { + let params: Vec> = Vec::with_capacity(128); + + Self::build_with_params(query, params) + } + + fn build_with_params(query: Q, existing_params: Vec>) -> crate::Result<(String, Vec>)> + where + Q: Into>, + { + let mut mssql = Mssql { query: String::with_capacity(4096), - parameters: Vec::with_capacity(128), + parameters: existing_params, order_by_set: false, }; - Mssql::visit_query(&mut this, query.into())?; + Mssql::visit_query(&mut mssql, query.into())?; - Ok((this.query, this.parameters)) + Ok((mssql.query, mssql.parameters)) } fn write(&mut self, s: D) -> visitor::Result { diff --git a/src/visitor/mysql.rs b/src/visitor/mysql.rs index cf97a491e..427f1b9bb 100644 --- a/src/visitor/mysql.rs +++ b/src/visitor/mysql.rs @@ -38,12 +38,21 @@ impl<'a> Visitor<'a> for Mysql<'a> { const C_WILDCARD: &'static str = "%"; fn build(query: Q) -> crate::Result<(String, Vec>)> + where + Q: Into>, + { + let params: Vec> = Vec::with_capacity(128); + + Self::build_with_params(query, params) + } + + fn build_with_params(query: Q, existing_params: Vec>) -> crate::Result<(String, Vec>)> where Q: Into>, { let mut mysql = Mysql { query: String::with_capacity(4096), - parameters: Vec::with_capacity(128), + parameters: existing_params, }; Mysql::visit_query(&mut mysql, query.into())?; diff --git a/src/visitor/postgres.rs b/src/visitor/postgres.rs index 5b1739e8a..b297318e9 100644 --- a/src/visitor/postgres.rs +++ b/src/visitor/postgres.rs @@ -20,12 +20,21 @@ impl<'a> Visitor<'a> for Postgres<'a> { const C_WILDCARD: &'static str = "%"; fn build(query: Q) -> crate::Result<(String, Vec>)> + where + Q: Into>, + { + let params: Vec> = Vec::with_capacity(128); + + Self::build_with_params(query, params) + } + + fn build_with_params(query: Q, existing_params: Vec>) -> crate::Result<(String, Vec>)> where Q: Into>, { let mut postgres = Postgres { query: String::with_capacity(4096), - parameters: Vec::with_capacity(128), + parameters: existing_params, }; Postgres::visit_query(&mut postgres, query.into())?; diff --git a/src/visitor/sqlite.rs b/src/visitor/sqlite.rs index 503dd6129..8bd02ab9b 100644 --- a/src/visitor/sqlite.rs +++ b/src/visitor/sqlite.rs @@ -22,12 +22,21 @@ impl<'a> Visitor<'a> for Sqlite<'a> { const C_WILDCARD: &'static str = "%"; fn build(query: Q) -> crate::Result<(String, Vec>)> + where + Q: Into>, + { + let params: Vec> = Vec::with_capacity(128); + + Self::build_with_params(query, params) + } + + fn build_with_params(query: Q, existing_params: Vec>) -> crate::Result<(String, Vec>)> where Q: Into>, { let mut sqlite = Sqlite { query: String::with_capacity(4096), - parameters: Vec::with_capacity(128), + parameters: existing_params, }; Sqlite::visit_query(&mut sqlite, query.into())?;