Skip to content
Merged
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 src/behavior.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub trait Concat {
pub trait TransactionQuery: Concat {}

/// Represents all commands that can be used inside the with method
#[cfg(any(feature = "postgresql", feature = "sqlite"))]
pub trait WithQuery: Concat {}

pub(crate) trait ConcatSqlStandard<Clause: PartialEq> {
Expand Down
11 changes: 8 additions & 3 deletions src/delete/delete.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use crate::{
behavior::{push_unique, Concat, TransactionQuery, WithQuery},
behavior::{push_unique, Concat, TransactionQuery},
fmt,
structure::{Delete, DeleteClause, LogicalOperator},
};

impl WithQuery for Delete {}

impl TransactionQuery for Delete {}

impl Delete {
Expand Down Expand Up @@ -267,6 +265,12 @@ impl Delete {
}
}

#[cfg(any(feature = "postgresql", feature = "sqlite"))]
use crate::behavior::WithQuery;

#[cfg(any(feature = "postgresql", feature = "sqlite"))]
impl WithQuery for Delete {}

#[cfg(any(doc, feature = "postgresql", feature = "sqlite"))]
#[cfg_attr(docsrs, doc(cfg(feature = "postgresql")))]
#[cfg_attr(docsrs, doc(cfg(feature = "sqlite")))]
Expand Down Expand Up @@ -338,6 +342,7 @@ impl Delete {
/// DELETE FROM users
/// WHERE id in (select * from deactivated_users)
/// ```
#[cfg(any(feature = "postgresql", feature = "sqlite"))]
pub fn with(mut self, name: &str, query: impl WithQuery + 'static) -> Self {
self._with.push((name.trim().to_string(), std::sync::Arc::new(query)));
self
Expand Down
271 changes: 271 additions & 0 deletions src/drop_index/drop_index.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,271 @@
use crate::{
behavior::{push_unique, Concat, TransactionQuery},
fmt,
structure::{DropIndex, DropIndexParams},
};

impl TransactionQuery for DropIndex {}

impl DropIndex {
/// Gets the current state of the [DropIndex] and returns it as string
///
/// ### Example
///
/// ```
/// # use sql_query_builder as sql;
/// let query = sql::DropIndex::new()
/// .drop_index("users_name_idx")
/// .as_string();
///
/// # let expected = "DROP INDEX users_name_idx";
/// # assert_eq!(expected, query);
/// ```
///
/// Output
///
/// ```sql
/// DROP INDEX users_name_idx
/// ```
pub fn as_string(&self) -> String {
let fmts = fmt::one_line();
self.concat(&fmts)
}

/// Defines a drop index command, this method overrides the previous value
///
/// ### Example 1
///
///```
/// # #[cfg(not(feature = "postgresql"))]
/// # {
/// # use sql_query_builder as sql;
/// let query = sql::DropIndex::new()
/// .drop_index("users_name_idx")
/// .drop_index("orders_product_name_idx")
/// .as_string();
///
/// # let expected = "DROP INDEX orders_product_name_idx";
/// # assert_eq!(expected, query);
/// # }
/// ```
///
/// Outputs
///
/// ```sql
/// DROP INDEX orders_product_name_idx
/// ```
///
/// ### Example 2 `crate features postgresql only`
///
/// Multiples call will concatenates all values
///
///```
/// # #[cfg(feature = "postgresql")]
/// # {
/// # use sql_query_builder as sql;
/// let query = sql::DropIndex::new()
/// .drop_index("users_name_idx")
/// .drop_index("orders_product_name_idx")
/// .as_string();
///
/// # let expected = "DROP INDEX users_name_idx, orders_product_name_idx";
/// # assert_eq!(expected, query);
/// # }
/// ```
///
/// Outputs
///
/// ```sql
/// DROP INDEX users_name_idx, orders_product_name_idx
/// ```
pub fn drop_index(mut self, table_name: &str) -> Self {
push_unique(&mut self._drop_index, table_name.trim().to_string());
self
}

/// Defines a drop index comand with the modifer `if exists`, this method overrides the previous value
///
/// ### Example 1
///
/// ```
/// # #[cfg(not(feature = "postgresql"))]
/// # {
/// # use sql_query_builder as sql;
/// let query = sql::DropIndex::new()
/// .drop_index("users_name_idx")
/// .drop_index_if_exists("orders_product_name_idx")
/// .to_string();
///
/// # let expected = "DROP INDEX IF EXISTS orders_product_name_idx";
/// # assert_eq!(expected, query);
/// # }
/// ```
///
/// Outputs
///
/// ```sql
/// DROP INDEX IF EXISTS orders_product_name_idx
/// ```
///
/// ### Example 2 `crate features postgresql only`
///
/// Multiples call will concatenates all values
///
/// ```
/// # #[cfg(feature = "postgresql")]
/// # {
/// # use sql_query_builder as sql;
/// let query = sql::DropIndex::new()
/// .drop_index("users_name_idx")
/// .drop_index_if_exists("orders_product_name_idx")
/// .to_string();
///
/// # let expected = "DROP INDEX IF EXISTS users_name_idx, orders_product_name_idx";
/// # assert_eq!(expected, query);
/// # }
/// ```
///
/// Outputs
///
/// ```sql
/// DROP INDEX IF EXISTS users_name_idx, orders_product_name_idx
/// ```
pub fn drop_index_if_exists(mut self, table_name: &str) -> Self {
push_unique(&mut self._drop_index, table_name.trim().to_string());
self._if_exists = true;
self
}

/// Prints the current state of the [DropIndex] to the standard output in a more ease to read version.
/// This method is useful to debug complex queries or just print the generated SQL while you type
///
/// ### Example
///
/// ```
/// # use sql_query_builder as sql;
/// let query = sql::DropIndex::new()
/// .drop_index("users_name_idx")
/// .debug()
/// .as_string();
/// ```
///
/// Prints to the standard output
///
/// ```sql
/// -- ------------------------------------------------------------------------------
/// DROP INDEX users_name_idx
/// -- ------------------------------------------------------------------------------
/// ```
pub fn debug(self) -> Self {
let fmts = fmt::multiline();
println!("{}", fmt::format(self.concat(&fmts), &fmts));
self
}

/// Creates instance of the [DropIndex] command
pub fn new() -> Self {
Self::default()
}

/// Prints the current state of the [DropIndex] to the standard output similar to debug method,
/// the difference is that this method prints in one line.
pub fn print(self) -> Self {
let fmts = fmt::one_line();
println!("{}", fmt::format(self.concat(&fmts), &fmts));
self
}

/// Adds at the beginning a raw SQL query. Is useful to create a more complex drop index signature like the example below.
///
/// ### Example
///
/// ```
/// # use sql_query_builder as sql;
/// let drop_index_query = sql::DropIndex::new()
/// .raw("/* drop index command */")
/// .drop_index("users_name_idx")
/// .as_string();
///
/// # let expected = "/* drop index command */ DROP INDEX users_name_idx";
/// # assert_eq!(expected, drop_index_query);
/// ```
///
/// Output
///
/// ```sql
/// /* drop index command */ DROP INDEX users_name_idx
/// ```
pub fn raw(mut self, raw_sql: &str) -> Self {
push_unique(&mut self._raw, raw_sql.trim().to_string());
self
}

/// Adds a raw SQL query after a specified parameter.
///
/// The `DropIndexParams::DropIndex` works for both `.drop_index` and `.drop_index_if_exist` methods
///
/// ### Example
///
/// ```
/// # use sql_query_builder as sql;
/// let query = sql::DropIndex::new()
/// .drop_index("users_name_idx")
/// .raw_after(sql::DropIndexParams::DropIndex, "/* end drop index */")
/// .as_string();
///
/// # let expected = "DROP INDEX users_name_idx /* end drop index */";
/// # assert_eq!(expected, query);
/// ```
///
/// Output
///
/// ```sql
/// DROP INDEX users_name_idx /* end drop index */
/// ```
pub fn raw_after(mut self, param: DropIndexParams, raw_sql: &str) -> Self {
self._raw_after.push((param, raw_sql.trim().to_string()));
self
}

/// Adds a raw SQL query before a specified parameter.
///
/// The `DropIndexParams::DropIndex` works for both `.drop_index` and `.drop_index_if_exist` methods
///
/// ### Example
///
/// ```
/// # use sql_query_builder as sql;
/// let raw = "/* drop index command */";
///
/// let query = sql::DropIndex::new()
/// .raw_before(sql::DropIndexParams::DropIndex, raw)
/// .drop_index("users_name_idx")
/// .as_string();
///
/// # let expected = "/* drop index command */ DROP INDEX users_name_idx";
/// # assert_eq!(expected, query);
/// ```
///
/// Output
///
/// ```sql
/// /* drop index command */ DROP INDEX users_name_idx
/// ```
pub fn raw_before(mut self, param: DropIndexParams, raw_sql: &str) -> Self {
self._raw_before.push((param, raw_sql.trim().to_string()));
self
}
}

impl std::fmt::Display for DropIndex {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self.as_string())
}
}

impl std::fmt::Debug for DropIndex {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let fmts = fmt::multiline();
write!(f, "{}", fmt::format(self.concat(&fmts), &fmts))
}
}
51 changes: 51 additions & 0 deletions src/drop_index/drop_index_internal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use crate::{
behavior::{concat_raw_before_after, Concat, ConcatSqlStandard},
fmt,
structure::{DropIndex, DropIndexParams},
};

impl ConcatSqlStandard<DropIndexParams> for DropIndex {}

impl DropIndex {
fn concat_drop_index(&self, query: String, fmts: &fmt::Formatter) -> String {
let fmt::Formatter { comma, lb, space, .. } = fmts;

let sql = if self._drop_index.len() != 0 {
let if_exists = if self._if_exists {
format!("IF EXISTS{space}")
} else {
"".to_string()
};

let index_names = if cfg!(any(feature = "postgresql")) {
self._drop_index.join(comma)
} else {
self._drop_index.last().unwrap().to_string()
};

format!("DROP INDEX{space}{if_exists}{index_names}{space}{lb}")
} else {
"".to_string()
};

concat_raw_before_after(
&self._raw_before,
&self._raw_after,
query,
fmts,
DropIndexParams::DropIndex,
sql,
)
}
}

impl Concat for DropIndex {
fn concat(&self, fmts: &fmt::Formatter) -> String {
let mut query = "".to_string();

query = self.concat_raw(query, &fmts, &self._raw);
query = self.concat_drop_index(query, &fmts);

query.trim_end().to_string()
}
}
2 changes: 2 additions & 0 deletions src/drop_index/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod drop_index;
mod drop_index_internal;
6 changes: 3 additions & 3 deletions src/drop_table/drop_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,17 +210,17 @@ impl DropTable {
/// # use sql_query_builder as sql;
/// let query = sql::DropTable::new()
/// .drop_table("users")
/// .raw_after(sql::DropTableParams::DropTable, "CASCADE RESTRICT")
/// .raw_after(sql::DropTableParams::DropTable, "CASCADE")
/// .as_string();
///
/// # let expected = "DROP TABLE users CASCADE RESTRICT";
/// # let expected = "DROP TABLE users CASCADE";
/// # assert_eq!(expected, query);
/// ```
///
/// Output
///
/// ```sql
/// DROP TABLE users CASCADE RESTRICT
/// DROP TABLE users CASCADE
/// ```
pub fn raw_after(mut self, param: DropTableParams, raw_sql: &str) -> Self {
self._raw_after.push((param, raw_sql.trim().to_string()));
Expand Down
Loading