Skip to content
This repository was archived by the owner on Apr 25, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 7 additions & 0 deletions src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ pub trait Visitor<'a> {
where
Q: Into<Query<'a>>;

/// 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<Q>(query: Q, existing_params: Vec<Value<'a>>) -> crate::Result<(String, Vec<Value<'a>>)>
where
Q: Into<Query<'a>>;

/// Write to the query.
fn write<D: fmt::Display>(&mut self, s: D) -> Result;

Expand Down
19 changes: 14 additions & 5 deletions src/visitor/mssql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,26 @@ impl<'a> Visitor<'a> for Mssql<'a> {

fn build<Q>(query: Q) -> crate::Result<(String, Vec<Value<'a>>)>
where
Q: Into<crate::ast::Query<'a>>,
Q: Into<Query<'a>>,
{
let mut this = Mssql {
let params: Vec<Value<'a>> = Vec::with_capacity(128);

Self::build_with_params(query, params)
}

fn build_with_params<Q>(query: Q, existing_params: Vec<Value<'a>>) -> crate::Result<(String, Vec<Value<'a>>)>
where
Q: Into<Query<'a>>,
{
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<D: std::fmt::Display>(&mut self, s: D) -> visitor::Result {
Expand Down
11 changes: 10 additions & 1 deletion src/visitor/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,21 @@ impl<'a> Visitor<'a> for Mysql<'a> {
const C_WILDCARD: &'static str = "%";

fn build<Q>(query: Q) -> crate::Result<(String, Vec<Value<'a>>)>
where
Q: Into<Query<'a>>,
{
let params: Vec<Value<'a>> = Vec::with_capacity(128);

Self::build_with_params(query, params)
}

fn build_with_params<Q>(query: Q, existing_params: Vec<Value<'a>>) -> crate::Result<(String, Vec<Value<'a>>)>
where
Q: Into<Query<'a>>,
{
let mut mysql = Mysql {
query: String::with_capacity(4096),
parameters: Vec::with_capacity(128),
parameters: existing_params,
};

Mysql::visit_query(&mut mysql, query.into())?;
Expand Down
11 changes: 10 additions & 1 deletion src/visitor/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,21 @@ impl<'a> Visitor<'a> for Postgres<'a> {
const C_WILDCARD: &'static str = "%";

fn build<Q>(query: Q) -> crate::Result<(String, Vec<Value<'a>>)>
where
Q: Into<Query<'a>>,
{
let params: Vec<Value<'a>> = Vec::with_capacity(128);

Self::build_with_params(query, params)
}

fn build_with_params<Q>(query: Q, existing_params: Vec<Value<'a>>) -> crate::Result<(String, Vec<Value<'a>>)>
where
Q: Into<Query<'a>>,
{
let mut postgres = Postgres {
query: String::with_capacity(4096),
parameters: Vec::with_capacity(128),
parameters: existing_params,
};

Postgres::visit_query(&mut postgres, query.into())?;
Expand Down
11 changes: 10 additions & 1 deletion src/visitor/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,21 @@ impl<'a> Visitor<'a> for Sqlite<'a> {
const C_WILDCARD: &'static str = "%";

fn build<Q>(query: Q) -> crate::Result<(String, Vec<Value<'a>>)>
where
Q: Into<Query<'a>>,
{
let params: Vec<Value<'a>> = Vec::with_capacity(128);

Self::build_with_params(query, params)
}

fn build_with_params<Q>(query: Q, existing_params: Vec<Value<'a>>) -> crate::Result<(String, Vec<Value<'a>>)>
where
Q: Into<Query<'a>>,
{
let mut sqlite = Sqlite {
query: String::with_capacity(4096),
parameters: Vec::with_capacity(128),
parameters: existing_params,
};

Sqlite::visit_query(&mut sqlite, query.into())?;
Expand Down