Skip to content
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
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ description = "Write SQL queries in a simple and composable way"
documentation = "https://docs.rs/sql_query_builder"
repository = "https://github.com/belchior/sql_query_builder"
authors = ["Belchior Oliveira <belchior@outlook.com>"]
version = "2.5.2"
version = "2.6.2"
edition = "2021"
rust-version = "1.62"
license = "MIT"
keywords = ["sql", "query", "postgres", "sqlite", "mysql"]
exclude = [".github", ".vscode", "scripts"]

[features]
#! SQL Query Builder comes with the following optional features:
Expand Down
59 changes: 33 additions & 26 deletions src/delete/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,39 @@ impl Delete {
}
}

#[cfg(any(doc, feature = "sqlite", feature = "mysql"))]
#[cfg_attr(docsrs, doc(cfg(feature = "sqlite")))]
#[cfg_attr(docsrs, doc(cfg(feature = "mysql")))]
impl Delete {
/// The `order by` clause.
///
/// # Example
///
/// ```
/// # #[cfg(any(feature = "sqlite", feature = "mysql"))]
/// # {
/// # use sql_query_builder as sql;
/// let delete = sql::Delete::new()
/// .order_by("created_at asc");
///
/// # let expected = "ORDER BY created_at asc";
/// # assert_eq!(expected, delete.as_string());
/// # }
/// ```
///
/// Output
///
/// ```sql
/// ORDER BY created_at asc
/// ```
///
/// For crate feature `sqlite` you should enable this statement at SQLite before use, [more info](https://sqlite.org/lang_delete.html#optional_limit_and_order_by_clauses).
pub fn order_by(mut self, column: &str) -> Self {
push_unique(&mut self._order_by, column.trim().to_string());
self
}
}

#[cfg(any(doc, feature = "mysql"))]
#[cfg_attr(docsrs, doc(cfg(feature = "mysql")))]
impl Delete {
Expand Down Expand Up @@ -641,32 +674,6 @@ impl Delete {
self
}

/// The `order by` clause
///
/// # Example
///
/// ```
/// # #[cfg(feature = "mysql")]
/// # {
/// # use sql_query_builder as sql;
/// let delete = sql::Delete::new()
/// .order_by("created_at asc");
///
/// # let expected = "ORDER BY created_at asc";
/// # assert_eq!(expected, delete.as_string());
/// # }
/// ```
///
/// Output
///
/// ```sql
/// ORDER BY created_at asc
/// ```
pub fn order_by(mut self, column: &str) -> Self {
push_unique(&mut self._order_by, column.trim().to_string());
self
}

/// The `partition` clause
///
/// # Example
Expand Down
20 changes: 14 additions & 6 deletions src/delete/delete_internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ impl Concat for Delete {
DeleteClause::Where,
&self._where,
);
query = self.concat_order_by(
&self._raw_before,
&self._raw_after,
query,
&fmts,
DeleteClause::OrderBy,
&self._order_by,
);
query = self.concat_returning(
&self._raw_before,
&self._raw_after,
Expand Down Expand Up @@ -166,35 +174,35 @@ impl Delete {

#[cfg(any(feature = "postgresql", feature = "sqlite", feature = "mysql"))]
use crate::concat::non_standard::ConcatWith;

#[cfg(any(feature = "postgresql", feature = "sqlite", feature = "mysql"))]
impl ConcatWith<DeleteClause> for Delete {}

#[cfg(any(feature = "postgresql", feature = "sqlite"))]
use crate::concat::non_standard::ConcatReturning;

#[cfg(any(feature = "postgresql", feature = "sqlite"))]
impl ConcatReturning<DeleteClause> for Delete {}

#[cfg(any(feature = "sqlite", feature = "mysql"))]
use crate::concat::sql_standard::ConcatOrderBy;
#[cfg(any(feature = "sqlite", feature = "mysql"))]
impl ConcatOrderBy<DeleteClause> for Delete {}

#[cfg(feature = "mysql")]
use crate::{
concat::{
mysql::ConcatPartition,
non_standard::ConcatLimit,
sql_standard::{ConcatFrom, ConcatJoin, ConcatOrderBy},
sql_standard::{ConcatFrom, ConcatJoin},
},
utils,
};

#[cfg(feature = "mysql")]
impl ConcatFrom<DeleteClause> for Delete {}
#[cfg(feature = "mysql")]
impl ConcatJoin<DeleteClause> for Delete {}
#[cfg(feature = "mysql")]
impl ConcatLimit<DeleteClause> for Delete {}
#[cfg(feature = "mysql")]
impl ConcatOrderBy<DeleteClause> for Delete {}
#[cfg(feature = "mysql")]
impl ConcatPartition<DeleteClause> for Delete {}

#[cfg(feature = "mysql")]
Expand Down
30 changes: 16 additions & 14 deletions src/structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,9 @@ pub struct Delete {
#[cfg(any(feature = "postgresql", feature = "sqlite"))]
pub(crate) _returning: Vec<String>,

#[cfg(any(feature = "sqlite", feature = "mysql"))]
pub(crate) _order_by: Vec<String>,

#[cfg(feature = "mysql")]
pub(crate) _delete: Vec<String>,

Expand All @@ -281,9 +284,6 @@ pub struct Delete {
#[cfg(feature = "mysql")]
pub(crate) _limit: String,

#[cfg(feature = "mysql")]
pub(crate) _order_by: Vec<String>,

#[cfg(feature = "mysql")]
pub(crate) _partition: Vec<String>,
}
Expand All @@ -305,6 +305,11 @@ pub enum DeleteClause {
#[cfg_attr(docsrs, doc(cfg(feature = "sqlite")))]
Returning,

#[cfg(any(feature = "sqlite", feature = "mysql"))]
#[cfg_attr(docsrs, doc(cfg(feature = "sqlite")))]
#[cfg_attr(docsrs, doc(cfg(feature = "mysql")))]
OrderBy,

#[cfg(feature = "mysql")]
#[cfg_attr(docsrs, doc(cfg(feature = "mysql")))]
Limit,
Expand All @@ -321,10 +326,6 @@ pub enum DeleteClause {
#[cfg_attr(docsrs, doc(cfg(feature = "mysql")))]
Join,

#[cfg(feature = "mysql")]
#[cfg_attr(docsrs, doc(cfg(feature = "mysql")))]
OrderBy,

#[cfg(feature = "mysql")]
#[cfg_attr(docsrs, doc(cfg(feature = "mysql")))]
Partition,
Expand Down Expand Up @@ -820,6 +821,9 @@ pub struct Update {
#[cfg(any(feature = "postgresql", feature = "sqlite"))]
pub(crate) _with: Vec<(String, std::sync::Arc<dyn crate::behavior::WithQuery + Send + Sync>)>,

#[cfg(any(feature = "sqlite", feature = "mysql"))]
pub(crate) _order_by: Vec<String>,

#[cfg(not(feature = "sqlite"))]
pub(crate) _update: String,

Expand All @@ -831,9 +835,6 @@ pub struct Update {

#[cfg(feature = "mysql")]
pub(crate) _limit: String,

#[cfg(feature = "mysql")]
pub(crate) _order_by: Vec<String>,
}

#[cfg(feature = "sqlite")]
Expand Down Expand Up @@ -866,6 +867,11 @@ pub enum UpdateClause {
#[cfg_attr(docsrs, doc(cfg(feature = "sqlite")))]
With,

#[cfg(any(feature = "sqlite", feature = "mysql"))]
#[cfg_attr(docsrs, doc(cfg(feature = "sqlite")))]
#[cfg_attr(docsrs, doc(cfg(feature = "mysql")))]
OrderBy,

#[cfg(feature = "sqlite")]
#[cfg_attr(docsrs, doc(cfg(feature = "sqlite")))]
UpdateOr,
Expand All @@ -877,10 +883,6 @@ pub enum UpdateClause {
#[cfg(feature = "mysql")]
#[cfg_attr(docsrs, doc(cfg(feature = "mysql")))]
Limit,

#[cfg(feature = "mysql")]
#[cfg_attr(docsrs, doc(cfg(feature = "mysql")))]
OrderBy,
}

/// Builder of [Values] command.
Expand Down
48 changes: 28 additions & 20 deletions src/update/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,33 @@ impl Update {
}
}

#[cfg(any(doc, feature = "sqlite", feature = "mysql"))]
#[cfg_attr(docsrs, doc(cfg(feature = "sqlite")))]
#[cfg_attr(docsrs, doc(cfg(feature = "mysql")))]
impl Update {
/// The `order by` clause
///
/// # Example
///
/// ```
/// # #[cfg(feature = "mysql")]
/// # {
/// # use sql_query_builder as sql;
/// let update = sql::Update::new()
/// .order_by("login asc");
///
/// # let expected = "ORDER BY login asc";
/// # assert_eq!(expected, update.as_string());
/// # }
/// ```
///
/// For crate feature `sqlite` you should enable this statement at SQLite before use, [more info](https://sqlite.org/lang_update.html#optional_limit_and_order_by_clauses).
pub fn order_by(mut self, column: &str) -> Self {
push_unique(&mut self._order_by, column.trim().to_string());
self
}
}

#[cfg(feature = "sqlite")]
use crate::structure::UpdateVars;

Expand Down Expand Up @@ -626,27 +653,8 @@ impl Update {
self._limit = num.trim().to_string();
self
}

/// The `order by` clause
///
/// # Example
///
/// ```
/// # #[cfg(feature = "mysql")]
/// # {
/// # use sql_query_builder as sql;
/// let update = sql::Update::new()
/// .order_by("login asc");
///
/// # let expected = "ORDER BY login asc";
/// # assert_eq!(expected, update.as_string());
/// # }
/// ```
pub fn order_by(mut self, column: &str) -> Self {
push_unique(&mut self._order_by, column.trim().to_string());
self
}
}

impl std::fmt::Display for Update {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self.as_string())
Expand Down
20 changes: 14 additions & 6 deletions src/update/update_internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ impl Concat for Update {
UpdateClause::Where,
&self._where,
);
query = self.concat_order_by(
&self._raw_before,
&self._raw_after,
query,
&fmts,
UpdateClause::OrderBy,
&self._order_by,
);
query = self.concat_returning(
&self._raw_before,
&self._raw_after,
Expand Down Expand Up @@ -182,15 +190,18 @@ impl Concat for Update {

#[cfg(any(feature = "postgresql", feature = "sqlite"))]
use crate::concat::non_standard::{ConcatReturning, ConcatWith};

#[cfg(any(feature = "postgresql", feature = "sqlite"))]
impl ConcatReturning<UpdateClause> for Update {}
#[cfg(any(feature = "postgresql", feature = "sqlite"))]
impl ConcatWith<UpdateClause> for Update {}

#[cfg(any(feature = "sqlite", feature = "mysql"))]
use crate::concat::sql_standard::ConcatOrderBy;
#[cfg(any(feature = "sqlite", feature = "mysql"))]
impl ConcatOrderBy<UpdateClause> for Update {}

#[cfg(feature = "sqlite")]
use crate::concat::sqlite::ConcatUpdate;

#[cfg(feature = "sqlite")]
impl ConcatUpdate for Update {}

Expand Down Expand Up @@ -220,9 +231,6 @@ impl Update {
}

#[cfg(feature = "mysql")]
use crate::concat::{non_standard::ConcatLimit, sql_standard::ConcatOrderBy};

use crate::concat::non_standard::ConcatLimit;
#[cfg(feature = "mysql")]
impl ConcatLimit<UpdateClause> for Update {}
#[cfg(feature = "mysql")]
impl ConcatOrderBy<UpdateClause> for Update {}
4 changes: 2 additions & 2 deletions tests/clause_order_by_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ mod select_command {
}
}

#[cfg(feature = "mysql")]
#[cfg(any(feature = "sqlite", feature = "mysql"))]
mod delete_command {
use pretty_assertions::assert_eq;
use sql_query_builder as sql;
Expand Down Expand Up @@ -188,7 +188,7 @@ mod delete_command {
}
}

#[cfg(feature = "mysql")]
#[cfg(any(feature = "sqlite", feature = "mysql"))]
mod update_command {
use pretty_assertions::assert_eq;
use sql_query_builder as sql;
Expand Down
Loading