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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ if is_admin {

let query = select.as_string();

println!("{}", query);
println!("{query}");
```

Output
Expand Down
4 changes: 2 additions & 2 deletions scripts/watch_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

all_features='postgresql sqlite'
features=''
test_names=$(git status -s | grep tests/ | sed -e 's/.* //' -e 's/tests\//--test /' -e 's/.rs//' | tr '\n' ' ')

case "$@" in
"") features="";;
Expand All @@ -19,6 +20,5 @@ esac

[ ! -z "$features" ] && features="--features $features"

# cargo watch -w ./src -w ./tests -x 'test --test command_alter_table_spec'
# cargo watch -w ./src -w ./tests -x 'test --features postgresql -- --nocapture --color always'
cargo watch -w ./src -w ./tests -x "test $features"
cargo watch -w ./src -w ./tests -x "test $features $test_names"
2 changes: 1 addition & 1 deletion src/create_table/create_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ impl CreateTable {
self
}

/// Creates instance of the CreateTable command
/// Creates instance of the [CreateTable] command
pub fn new() -> Self {
Self::default()
}
Expand Down
271 changes: 271 additions & 0 deletions src/drop_table/drop_table.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,271 @@
use crate::{
behavior::{push_unique, Concat, TransactionQuery},
fmt,
structure::{DropTable, DropTableParams},
};

impl TransactionQuery for DropTable {}

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

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

/// Defines a drop table 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::DropTable::new()
/// .drop_table("users")
/// .drop_table_if_exists("orders")
/// .to_string();
///
/// # let expected = "DROP TABLE IF EXISTS orders";
/// # assert_eq!(expected, query);
/// # }
/// ```
///
/// Outputs
///
/// ```sql
/// DROP TABLE IF EXISTS orders
/// ```
///
/// ### Example 2 `crate features postgresql only`
///
/// Multiples call will concatenates all values
///
/// ```
/// # #[cfg(feature = "postgresql")]
/// # {
/// # use sql_query_builder as sql;
/// let query = sql::DropTable::new()
/// .drop_table("users")
/// .drop_table_if_exists("orders")
/// .to_string();
///
/// # let expected = "DROP TABLE IF EXISTS users, orders";
/// # assert_eq!(expected, query);
/// # }
/// ```
///
/// Outputs
///
/// ```sql
/// DROP TABLE IF EXISTS users, orders
/// ```
pub fn drop_table_if_exists(mut self, table_name: &str) -> Self {
push_unique(&mut self._drop_table, table_name.trim().to_string());
self._if_exists = true;
self
}

/// Prints the current state of the [DropTable] 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::DropTable::new()
/// .drop_table("users")
/// .debug()
/// .as_string();
/// ```
///
/// Prints to the standard output
///
/// ```sql
/// -- ------------------------------------------------------------------------------
/// DROP TABLE users
/// -- ------------------------------------------------------------------------------
/// ```
pub fn debug(self) -> Self {
let fmts = fmt::multiline();
println!("{}", fmt::format(self.concat(&fmts), &fmts));
self
}

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

/// Prints the current state of the [DropTable] 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 table signature like the example below.
///
/// ### Example
///
/// ```
/// # use sql_query_builder as sql;
/// let drop_table_query = sql::DropTable::new()
/// .raw("/* drop command */")
/// .drop_table("users_temp")
/// .as_string();
///
/// # let expected = "/* drop command */ DROP TABLE users_temp";
/// # assert_eq!(expected, drop_table_query);
/// ```
///
/// Output
///
/// ```sql
/// /* drop command */ DROP TABLE users_temp
/// ```
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 `DropTableParams::DropTable` works for both `.drop_table` and `.drop_table_if_exist` methods
///
/// ### Example
///
/// ```
/// # use sql_query_builder as sql;
/// let query = sql::DropTable::new()
/// .drop_table("users")
/// .raw_after(sql::DropTableParams::DropTable, "CASCADE RESTRICT")
/// .as_string();
///
/// # let expected = "DROP TABLE users CASCADE RESTRICT";
/// # assert_eq!(expected, query);
/// ```
///
/// Output
///
/// ```sql
/// DROP TABLE users CASCADE RESTRICT
/// ```
pub fn raw_after(mut self, param: DropTableParams, 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 `DropTableParams::DropTable` works for both `.drop_table` and `.drop_table_if_exist` methods
///
/// ### Example
///
/// ```
/// # use sql_query_builder as sql;
/// let raw = "CREATE TABLE users_temp;";
///
/// let query = sql::DropTable::new()
/// .raw_before(sql::DropTableParams::DropTable, raw)
/// .drop_table("users_temp")
/// .as_string();
///
/// # let expected = "CREATE TABLE users_temp; DROP TABLE users_temp";
/// # assert_eq!(expected, query);
/// ```
///
/// Output
///
/// ```sql
/// CREATE TABLE users_temp; DROP TABLE users_temp
/// ```
pub fn raw_before(mut self, param: DropTableParams, raw_sql: &str) -> Self {
self._raw_before.push((param, raw_sql.trim().to_string()));
self
}
}

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

impl std::fmt::Debug for DropTable {
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_table/drop_table_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::{DropTable, DropTableParams},
};

impl ConcatSqlStandard<DropTableParams> for DropTable {}

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

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

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

format!("DROP TABLE{space}{if_exists}{table_names}{space}{lb}")
} else {
"".to_string()
};

concat_raw_before_after(
&self._raw_before,
&self._raw_after,
query,
fmts,
DropTableParams::DropTable,
sql,
)
}
}

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

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

query.trim_end().to_string()
}
}
2 changes: 2 additions & 0 deletions src/drop_table/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod drop_table;
mod drop_table_internal;
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod alter_table;
mod behavior;
mod create_table;
mod delete;
mod drop_table;
mod fmt;
mod insert;
mod select;
Expand All @@ -14,6 +15,6 @@ mod update;
mod values;

pub use crate::structure::{
AlterTable, AlterTableAction, CreateTable, CreateTableParams, Delete, DeleteClause, Insert, InsertClause, Select,
SelectClause, Transaction, Update, UpdateClause, Values, ValuesClause,
AlterTable, AlterTableAction, CreateTable, CreateTableParams, Delete, DeleteClause, DropTable, DropTableParams,
Insert, InsertClause, Select, SelectClause, Transaction, Update, UpdateClause, Values, ValuesClause,
};
Loading