Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove separate WithQuery to ensure it can be specified everywhere #814

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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: 0 additions & 1 deletion sea-query-binder/src/sqlx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,3 @@ impl_sqlx_binder!(SelectStatement);
impl_sqlx_binder!(UpdateStatement);
impl_sqlx_binder!(InsertStatement);
impl_sqlx_binder!(DeleteStatement);
impl_sqlx_binder!(WithQuery);
2 changes: 0 additions & 2 deletions sea-query-diesel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use diesel::backend::Backend;
use diesel::result::QueryResult;
use sea_query::{
DeleteStatement, InsertStatement, QueryStatementWriter, SelectStatement, UpdateStatement,
WithQuery,
};

use self::backend::{ExtractBuilder, TransformValue};
Expand Down Expand Up @@ -40,4 +39,3 @@ impl_diesel_binder!(SelectStatement);
impl_diesel_binder!(UpdateStatement);
impl_diesel_binder!(InsertStatement);
impl_diesel_binder!(DeleteStatement);
impl_diesel_binder!(WithQuery);
1 change: 0 additions & 1 deletion sea-query-postgres/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ impl_postgres_binder!(SelectStatement);
impl_postgres_binder!(UpdateStatement);
impl_postgres_binder!(InsertStatement);
impl_postgres_binder!(DeleteStatement);
impl_postgres_binder!(WithQuery);

impl ToSql for PostgresValue {
fn to_sql(
Expand Down
32 changes: 26 additions & 6 deletions sea-query-rbatis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl_rbs_binder!(SelectStatement);
impl_rbs_binder!(UpdateStatement);
impl_rbs_binder!(InsertStatement);
impl_rbs_binder!(DeleteStatement);
impl_rbs_binder!(WithQuery);

trait ToRbV {
fn to(self) -> RbValue;
}
Expand Down Expand Up @@ -79,19 +79,35 @@ fn to_rb_values(values: Values) -> Vec<rbs::Value> {
}
#[cfg(feature = "with-chrono")]
Value::ChronoDateTime(t) => {
args.push(Value::ChronoDateTime(t).chrono_as_naive_utc_in_string().to());
args.push(
Value::ChronoDateTime(t)
.chrono_as_naive_utc_in_string()
.to(),
);
}
#[cfg(feature = "with-chrono")]
Value::ChronoDateTimeUtc(t) => {
args.push(Value::ChronoDateTimeUtc(t).chrono_as_naive_utc_in_string().to());
args.push(
Value::ChronoDateTimeUtc(t)
.chrono_as_naive_utc_in_string()
.to(),
);
}
#[cfg(feature = "with-chrono")]
Value::ChronoDateTimeLocal(t) => {
args.push(Value::ChronoDateTimeLocal(t).chrono_as_naive_utc_in_string().to());
args.push(
Value::ChronoDateTimeLocal(t)
.chrono_as_naive_utc_in_string()
.to(),
);
}
#[cfg(feature = "with-chrono")]
Value::ChronoDateTimeWithTimeZone(t) => {
args.push(Value::ChronoDateTimeWithTimeZone(t).chrono_as_naive_utc_in_string().to());
args.push(
Value::ChronoDateTimeWithTimeZone(t)
.chrono_as_naive_utc_in_string()
.to(),
);
}
#[cfg(feature = "with-time")]
Value::TimeDate(t) => {
Expand All @@ -107,7 +123,11 @@ fn to_rb_values(values: Values) -> Vec<rbs::Value> {
}
#[cfg(feature = "with-time")]
Value::TimeDateTimeWithTimeZone(t) => {
args.push(Value::TimeDateTimeWithTimeZone(t).time_as_naive_utc_in_string().to());
args.push(
Value::TimeDateTimeWithTimeZone(t)
.time_as_naive_utc_in_string()
.to(),
);
}
#[cfg(feature = "with-uuid")]
Value::Uuid(uuid) => {
Expand Down
1 change: 0 additions & 1 deletion sea-query-rusqlite/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ impl_rusqlite_binder!(SelectStatement);
impl_rusqlite_binder!(UpdateStatement);
impl_rusqlite_binder!(InsertStatement);
impl_rusqlite_binder!(DeleteStatement);
impl_rusqlite_binder!(WithQuery);

impl ToSql for RusqliteValue {
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
Expand Down
21 changes: 15 additions & 6 deletions src/backend/query_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ pub trait QueryBuilder:

/// Translate [`InsertStatement`] into SQL statement.
fn prepare_insert_statement(&self, insert: &InsertStatement, sql: &mut dyn SqlWriter) {
if let Some(with) = &insert.with {
self.prepare_with_clause(with, sql);
}

self.prepare_insert(insert.replace, sql);

if let Some(table) = &insert.table {
Expand Down Expand Up @@ -95,6 +99,9 @@ pub trait QueryBuilder:

/// Translate [`SelectStatement`] into SQL statement.
fn prepare_select_statement(&self, select: &SelectStatement, sql: &mut dyn SqlWriter) {
if let Some(with) = select.with.as_ref() {
self.prepare_with_clause(with, sql);
}
write!(sql, "SELECT ").unwrap();

if let Some(distinct) = &select.distinct {
Expand Down Expand Up @@ -191,6 +198,10 @@ pub trait QueryBuilder:

/// Translate [`UpdateStatement`] into SQL statement.
fn prepare_update_statement(&self, update: &UpdateStatement, sql: &mut dyn SqlWriter) {
if let Some(with) = &update.with {
self.prepare_with_clause(with, sql);
}

write!(sql, "UPDATE ").unwrap();

if let Some(table) = &update.table {
Expand Down Expand Up @@ -245,6 +256,10 @@ pub trait QueryBuilder:

/// Translate [`DeleteStatement`] into SQL statement.
fn prepare_delete_statement(&self, delete: &DeleteStatement, sql: &mut dyn SqlWriter) {
if let Some(with) = &delete.with {
self.prepare_with_clause(with, sql);
}

write!(sql, "DELETE ").unwrap();

if let Some(table) = &delete.table {
Expand Down Expand Up @@ -701,11 +716,6 @@ pub trait QueryBuilder:
/// Translate [`QueryStatement`] into SQL statement.
fn prepare_query_statement(&self, query: &SubQueryStatement, sql: &mut dyn SqlWriter);

fn prepare_with_query(&self, query: &WithQuery, sql: &mut dyn SqlWriter) {
self.prepare_with_clause(&query.with_clause, sql);
self.prepare_query_statement(query.query.as_ref().unwrap().deref(), sql);
}

fn prepare_with_clause(&self, with_clause: &WithClause, sql: &mut dyn SqlWriter) {
self.prepare_with_clause_start(with_clause, sql);
self.prepare_with_clause_common_tables(with_clause, sql);
Expand Down Expand Up @@ -1521,7 +1531,6 @@ impl SubQueryStatement {
InsertStatement(stmt) => query_builder.prepare_insert_statement(stmt, sql),
UpdateStatement(stmt) => query_builder.prepare_update_statement(stmt, sql),
DeleteStatement(stmt) => query_builder.prepare_delete_statement(stmt, sql),
WithStatement(stmt) => query_builder.prepare_with_query(stmt, sql),
}
}
}
Expand Down
14 changes: 8 additions & 6 deletions src/query/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
types::*,
value::*,
QueryStatementBuilder, QueryStatementWriter, ReturningClause, SimpleExpr, SubQueryStatement,
WithClause, WithQuery,
WithClause,
};
use inherent::inherent;

Expand Down Expand Up @@ -44,6 +44,7 @@ pub struct DeleteStatement {
pub(crate) orders: Vec<OrderExpr>,
pub(crate) limit: Option<Value>,
pub(crate) returning: Option<ReturningClause>,
pub(crate) with: Option<WithClause>,
}

impl DeleteStatement {
Expand Down Expand Up @@ -186,7 +187,7 @@ impl DeleteStatement {
self.returning(ReturningClause::All)
}

/// Create a [WithQuery] by specifying a [WithClause] to execute this query with.
/// Specify a [WithClause] to execute this query with.
///
/// # Examples
///
Expand All @@ -204,11 +205,11 @@ impl DeleteStatement {
/// .table_name(Alias::new("cte"))
/// .to_owned();
/// let with_clause = WithClause::new().cte(cte).to_owned();
/// let update = DeleteStatement::new()
/// let query = DeleteStatement::new()
/// .from_table(Glyph::Table)
/// .and_where(Expr::col(Glyph::Id).in_subquery(SelectStatement::new().column(Glyph::Id).from(Alias::new("cte")).to_owned()))
/// .with(with_clause)
/// .to_owned();
/// let query = update.with(with_clause);
///
/// assert_eq!(
/// query.to_string(MysqlQueryBuilder),
Expand All @@ -223,8 +224,9 @@ impl DeleteStatement {
/// r#"WITH "cte" ("id") AS (SELECT "id" FROM "glyph" WHERE "image" LIKE '0%') DELETE FROM "glyph" WHERE "id" IN (SELECT "id" FROM "cte")"#
/// );
/// ```
pub fn with(self, clause: WithClause) -> WithQuery {
clause.query(self)
pub fn with(&mut self, clause: WithClause) -> &mut Self {
self.with = Some(clause);
self
}
}

Expand Down
18 changes: 10 additions & 8 deletions src/query/insert.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
backend::QueryBuilder, error::*, prepare::*, types::*, OnConflict, QueryStatementBuilder,
QueryStatementWriter, ReturningClause, SelectStatement, SimpleExpr, SubQueryStatement, Values,
WithClause, WithQuery,
WithClause,
};
use inherent::inherent;

Expand Down Expand Up @@ -51,6 +51,7 @@ pub struct InsertStatement {
pub(crate) on_conflict: Option<OnConflict>,
pub(crate) returning: Option<ReturningClause>,
pub(crate) default_values: Option<u32>,
pub(crate) with: Option<WithClause>,
}

impl InsertStatement {
Expand Down Expand Up @@ -425,7 +426,7 @@ impl InsertStatement {
self.returning(ReturningClause::All)
}

/// Create a [WithQuery] by specifying a [WithClause] to execute this query with.
/// Specify a [WithClause] to execute this query with.
///
/// # Examples
///
Expand All @@ -448,13 +449,13 @@ impl InsertStatement {
/// .columns([Glyph::Id, Glyph::Image, Glyph::Aspect])
/// .from(Alias::new("cte"))
/// .to_owned();
/// let mut insert = Query::insert();
/// insert
/// let mut query = Query::insert();
/// query
/// .into_table(Glyph::Table)
/// .columns([Glyph::Id, Glyph::Image, Glyph::Aspect])
/// .select_from(select)
/// .unwrap();
/// let query = insert.with(with_clause);
/// .unwrap()
/// .with(with_clause);
///
/// assert_eq!(
/// query.to_string(MysqlQueryBuilder),
Expand All @@ -469,8 +470,9 @@ impl InsertStatement {
/// r#"WITH "cte" ("id", "image", "aspect") AS (SELECT "id", "image", "aspect" FROM "glyph") INSERT INTO "glyph" ("id", "image", "aspect") SELECT "id", "image", "aspect" FROM "cte""#
/// );
/// ```
pub fn with(self, clause: WithClause) -> WithQuery {
clause.query(self)
pub fn with(&mut self, clause: WithClause) -> &mut Self {
self.with = Some(clause);
self
}

/// Insert with default values if columns and values are not supplied.
Expand Down
1 change: 0 additions & 1 deletion src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ pub enum SubQueryStatement {
InsertStatement(InsertStatement),
UpdateStatement(UpdateStatement),
DeleteStatement(DeleteStatement),
WithStatement(WithQuery),
}

impl Query {
Expand Down
21 changes: 11 additions & 10 deletions src/query/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
types::*,
value::*,
FunctionCall, QueryStatementBuilder, QueryStatementWriter, SubQueryStatement, WindowStatement,
WithClause, WithQuery,
WithClause,
};
use inherent::inherent;

Expand Down Expand Up @@ -56,6 +56,7 @@ pub struct SelectStatement {
pub(crate) window: Option<(DynIden, WindowStatement)>,
#[cfg(feature = "backend-mysql")]
pub(crate) index_hints: Vec<crate::extension::mysql::IndexHint>,
pub(crate) with: Option<WithClause>,
}

/// List of distinct keywords that can be used in select statement
Expand Down Expand Up @@ -164,6 +165,7 @@ impl SelectStatement {
window: self.window.take(),
#[cfg(feature = "backend-mysql")]
index_hints: std::mem::take(&mut self.index_hints),
with: self.with.take(),
}
}

Expand Down Expand Up @@ -2280,7 +2282,7 @@ impl SelectStatement {
self
}

/// Create a [WithQuery] by specifying a [WithClause] to execute this query with.
/// Specify a [WithClause] to execute this query with.
///
/// # Examples
///
Expand Down Expand Up @@ -2316,18 +2318,16 @@ impl SelectStatement {
/// .table_name(Alias::new("cte_traversal"))
/// .to_owned();
///
/// let select = SelectStatement::new()
/// .column(ColumnRef::Asterisk)
/// .from(Alias::new("cte_traversal"))
/// .to_owned();
///
/// let with_clause = WithClause::new()
/// .recursive(true)
/// .cte(common_table_expression)
/// .cycle(Cycle::new_from_expr_set_using(SimpleExpr::Column(ColumnRef::Column(Alias::new("id").into_iden())), Alias::new("looped"), Alias::new("traversal_path")))
/// .to_owned();
///
/// let query = select.with(with_clause).to_owned();
/// let query = SelectStatement::new()
/// .column(ColumnRef::Asterisk)
/// .from(Alias::new("cte_traversal"))
/// .with(with_clause).to_owned();
///
/// assert_eq!(
/// query.to_string(MysqlQueryBuilder),
Expand All @@ -2342,8 +2342,9 @@ impl SelectStatement {
/// r#"WITH RECURSIVE "cte_traversal" ("id", "depth", "next", "value") AS (SELECT "id", 1, "next", "value" FROM "table" UNION ALL SELECT "id", "depth" + 1, "next", "value" FROM "table" INNER JOIN "cte_traversal" ON "cte_traversal"."next" = "table"."id") SELECT * FROM "cte_traversal""#
/// );
/// ```
pub fn with(self, clause: WithClause) -> WithQuery {
clause.query(self)
pub fn with(&mut self, clause: WithClause) -> &mut Self {
self.with = Some(clause);
self
}

/// WINDOW
Expand Down
13 changes: 7 additions & 6 deletions src/query/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crate::{
types::*,
value::*,
QueryStatementBuilder, QueryStatementWriter, ReturningClause, SubQueryStatement, WithClause,
WithQuery,
};
use inherent::inherent;

Expand Down Expand Up @@ -44,6 +43,7 @@ pub struct UpdateStatement {
pub(crate) orders: Vec<OrderExpr>,
pub(crate) limit: Option<Value>,
pub(crate) returning: Option<ReturningClause>,
pub(crate) with: Option<WithClause>,
}

impl UpdateStatement {
Expand Down Expand Up @@ -247,7 +247,7 @@ impl UpdateStatement {
self.returning(ReturningClause::All)
}

/// Create a [WithQuery] by specifying a [WithClause] to execute this query with.
/// Specify a [WithClause] to execute this query with.
///
/// # Examples
///
Expand All @@ -265,12 +265,12 @@ impl UpdateStatement {
/// .table_name(Alias::new("cte"))
/// .to_owned();
/// let with_clause = WithClause::new().cte(cte).to_owned();
/// let update = UpdateStatement::new()
/// let query = UpdateStatement::new()
/// .table(Glyph::Table)
/// .and_where(Expr::col(Glyph::Id).in_subquery(SelectStatement::new().column(Glyph::Id).from(Alias::new("cte")).to_owned()))
/// .value(Glyph::Aspect, Expr::cust("60 * 24 * 24"))
/// .with(with_clause)
/// .to_owned();
/// let query = update.with(with_clause);
///
/// assert_eq!(
/// query.to_string(MysqlQueryBuilder),
Expand All @@ -285,8 +285,9 @@ impl UpdateStatement {
/// r#"WITH "cte" ("id") AS (SELECT "id" FROM "glyph" WHERE "image" LIKE '0%') UPDATE "glyph" SET "aspect" = 60 * 24 * 24 WHERE "id" IN (SELECT "id" FROM "cte")"#
/// );
/// ```
pub fn with(self, clause: WithClause) -> WithQuery {
clause.query(self)
pub fn with(&mut self, clause: WithClause) -> &mut Self {
self.with = Some(clause);
self
}

/// Get column values
Expand Down
Loading
Loading