Skip to content

Commit 7e3a245

Browse files
authored
Merge pull request #30 from belchior/adds-drop-table-builder
Adds DropTable builder
2 parents 74c2848 + 4770958 commit 7e3a245

File tree

13 files changed

+821
-19
lines changed

13 files changed

+821
-19
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ if is_admin {
2222

2323
let query = select.as_string();
2424

25-
println!("{}", query);
25+
println!("{query}");
2626
```
2727

2828
Output

scripts/watch_test.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

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

1415
case "$@" in
1516
"") features="";;
@@ -19,6 +20,5 @@ esac
1920

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

22-
# cargo watch -w ./src -w ./tests -x 'test --test command_alter_table_spec'
2323
# cargo watch -w ./src -w ./tests -x 'test --features postgresql -- --nocapture --color always'
24-
cargo watch -w ./src -w ./tests -x "test $features"
24+
cargo watch -w ./src -w ./tests -x "test $features $test_names"

src/create_table/create_table.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ impl CreateTable {
212212
self
213213
}
214214

215-
/// Creates instance of the CreateTable command
215+
/// Creates instance of the [CreateTable] command
216216
pub fn new() -> Self {
217217
Self::default()
218218
}

src/drop_table/drop_table.rs

Lines changed: 271 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,271 @@
1+
use crate::{
2+
behavior::{push_unique, Concat, TransactionQuery},
3+
fmt,
4+
structure::{DropTable, DropTableParams},
5+
};
6+
7+
impl TransactionQuery for DropTable {}
8+
9+
impl DropTable {
10+
/// Gets the current state of the [DropTable] and returns it as string
11+
///
12+
/// ### Example
13+
///
14+
/// ```
15+
/// # use sql_query_builder as sql;
16+
/// let query = sql::DropTable::new()
17+
/// .drop_table("users")
18+
/// .as_string();
19+
///
20+
/// # let expected = "DROP TABLE users";
21+
/// # assert_eq!(expected, query);
22+
/// ```
23+
///
24+
/// Output
25+
///
26+
/// ```sql
27+
/// DROP TABLE users
28+
/// ```
29+
pub fn as_string(&self) -> String {
30+
let fmts = fmt::one_line();
31+
self.concat(&fmts)
32+
}
33+
34+
/// Defines a drop table command, this method overrides the previous value
35+
///
36+
/// ### Example 1
37+
///
38+
///```
39+
/// # #[cfg(not(feature = "postgresql"))]
40+
/// # {
41+
/// # use sql_query_builder as sql;
42+
/// let query = sql::DropTable::new()
43+
/// .drop_table("users")
44+
/// .drop_table("orders")
45+
/// .as_string();
46+
///
47+
/// # let expected = "DROP TABLE orders";
48+
/// # assert_eq!(expected, query);
49+
/// # }
50+
/// ```
51+
///
52+
/// Outputs
53+
///
54+
/// ```sql
55+
/// DROP TABLE orders
56+
/// ```
57+
///
58+
/// ### Example 2 `crate features postgresql only`
59+
///
60+
/// Multiples call will concatenates all values
61+
///
62+
///```
63+
/// # #[cfg(feature = "postgresql")]
64+
/// # {
65+
/// # use sql_query_builder as sql;
66+
/// let query = sql::DropTable::new()
67+
/// .drop_table("users")
68+
/// .drop_table("orders")
69+
/// .as_string();
70+
///
71+
/// # let expected = "DROP TABLE users, orders";
72+
/// # assert_eq!(expected, query);
73+
/// # }
74+
/// ```
75+
///
76+
/// Outputs
77+
///
78+
/// ```sql
79+
/// DROP TABLE users, orders
80+
/// ```
81+
pub fn drop_table(mut self, table_name: &str) -> Self {
82+
push_unique(&mut self._drop_table, table_name.trim().to_string());
83+
self
84+
}
85+
86+
/// Defines a drop table comand with the modifer `if exists`, this method overrides the previous value
87+
///
88+
/// ### Example 1
89+
///
90+
/// ```
91+
/// # #[cfg(not(feature = "postgresql"))]
92+
/// # {
93+
/// # use sql_query_builder as sql;
94+
/// let query = sql::DropTable::new()
95+
/// .drop_table("users")
96+
/// .drop_table_if_exists("orders")
97+
/// .to_string();
98+
///
99+
/// # let expected = "DROP TABLE IF EXISTS orders";
100+
/// # assert_eq!(expected, query);
101+
/// # }
102+
/// ```
103+
///
104+
/// Outputs
105+
///
106+
/// ```sql
107+
/// DROP TABLE IF EXISTS orders
108+
/// ```
109+
///
110+
/// ### Example 2 `crate features postgresql only`
111+
///
112+
/// Multiples call will concatenates all values
113+
///
114+
/// ```
115+
/// # #[cfg(feature = "postgresql")]
116+
/// # {
117+
/// # use sql_query_builder as sql;
118+
/// let query = sql::DropTable::new()
119+
/// .drop_table("users")
120+
/// .drop_table_if_exists("orders")
121+
/// .to_string();
122+
///
123+
/// # let expected = "DROP TABLE IF EXISTS users, orders";
124+
/// # assert_eq!(expected, query);
125+
/// # }
126+
/// ```
127+
///
128+
/// Outputs
129+
///
130+
/// ```sql
131+
/// DROP TABLE IF EXISTS users, orders
132+
/// ```
133+
pub fn drop_table_if_exists(mut self, table_name: &str) -> Self {
134+
push_unique(&mut self._drop_table, table_name.trim().to_string());
135+
self._if_exists = true;
136+
self
137+
}
138+
139+
/// Prints the current state of the [DropTable] to the standard output in a more ease to read version.
140+
/// This method is useful to debug complex queries or just print the generated SQL while you type
141+
///
142+
/// ### Example
143+
///
144+
/// ```
145+
/// # use sql_query_builder as sql;
146+
/// let query = sql::DropTable::new()
147+
/// .drop_table("users")
148+
/// .debug()
149+
/// .as_string();
150+
/// ```
151+
///
152+
/// Prints to the standard output
153+
///
154+
/// ```sql
155+
/// -- ------------------------------------------------------------------------------
156+
/// DROP TABLE users
157+
/// -- ------------------------------------------------------------------------------
158+
/// ```
159+
pub fn debug(self) -> Self {
160+
let fmts = fmt::multiline();
161+
println!("{}", fmt::format(self.concat(&fmts), &fmts));
162+
self
163+
}
164+
165+
/// Creates instance of the [DropTable] command
166+
pub fn new() -> Self {
167+
Self::default()
168+
}
169+
170+
/// Prints the current state of the [DropTable] to the standard output similar to debug method,
171+
/// the difference is that this method prints in one line.
172+
pub fn print(self) -> Self {
173+
let fmts = fmt::one_line();
174+
println!("{}", fmt::format(self.concat(&fmts), &fmts));
175+
self
176+
}
177+
178+
/// Adds at the beginning a raw SQL query. Is useful to create a more complex drop table signature like the example below.
179+
///
180+
/// ### Example
181+
///
182+
/// ```
183+
/// # use sql_query_builder as sql;
184+
/// let drop_table_query = sql::DropTable::new()
185+
/// .raw("/* drop command */")
186+
/// .drop_table("users_temp")
187+
/// .as_string();
188+
///
189+
/// # let expected = "/* drop command */ DROP TABLE users_temp";
190+
/// # assert_eq!(expected, drop_table_query);
191+
/// ```
192+
///
193+
/// Output
194+
///
195+
/// ```sql
196+
/// /* drop command */ DROP TABLE users_temp
197+
/// ```
198+
pub fn raw(mut self, raw_sql: &str) -> Self {
199+
push_unique(&mut self._raw, raw_sql.trim().to_string());
200+
self
201+
}
202+
203+
/// Adds a raw SQL query after a specified parameter.
204+
///
205+
/// The `DropTableParams::DropTable` works for both `.drop_table` and `.drop_table_if_exist` methods
206+
///
207+
/// ### Example
208+
///
209+
/// ```
210+
/// # use sql_query_builder as sql;
211+
/// let query = sql::DropTable::new()
212+
/// .drop_table("users")
213+
/// .raw_after(sql::DropTableParams::DropTable, "CASCADE RESTRICT")
214+
/// .as_string();
215+
///
216+
/// # let expected = "DROP TABLE users CASCADE RESTRICT";
217+
/// # assert_eq!(expected, query);
218+
/// ```
219+
///
220+
/// Output
221+
///
222+
/// ```sql
223+
/// DROP TABLE users CASCADE RESTRICT
224+
/// ```
225+
pub fn raw_after(mut self, param: DropTableParams, raw_sql: &str) -> Self {
226+
self._raw_after.push((param, raw_sql.trim().to_string()));
227+
self
228+
}
229+
230+
/// Adds a raw SQL query before a specified parameter.
231+
///
232+
/// The `DropTableParams::DropTable` works for both `.drop_table` and `.drop_table_if_exist` methods
233+
///
234+
/// ### Example
235+
///
236+
/// ```
237+
/// # use sql_query_builder as sql;
238+
/// let raw = "CREATE TABLE users_temp;";
239+
///
240+
/// let query = sql::DropTable::new()
241+
/// .raw_before(sql::DropTableParams::DropTable, raw)
242+
/// .drop_table("users_temp")
243+
/// .as_string();
244+
///
245+
/// # let expected = "CREATE TABLE users_temp; DROP TABLE users_temp";
246+
/// # assert_eq!(expected, query);
247+
/// ```
248+
///
249+
/// Output
250+
///
251+
/// ```sql
252+
/// CREATE TABLE users_temp; DROP TABLE users_temp
253+
/// ```
254+
pub fn raw_before(mut self, param: DropTableParams, raw_sql: &str) -> Self {
255+
self._raw_before.push((param, raw_sql.trim().to_string()));
256+
self
257+
}
258+
}
259+
260+
impl std::fmt::Display for DropTable {
261+
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
262+
write!(f, "{}", self.as_string())
263+
}
264+
}
265+
266+
impl std::fmt::Debug for DropTable {
267+
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
268+
let fmts = fmt::multiline();
269+
write!(f, "{}", fmt::format(self.concat(&fmts), &fmts))
270+
}
271+
}

src/drop_table/drop_table_internal.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
use crate::{
2+
behavior::{concat_raw_before_after, Concat, ConcatSqlStandard},
3+
fmt,
4+
structure::{DropTable, DropTableParams},
5+
};
6+
7+
impl ConcatSqlStandard<DropTableParams> for DropTable {}
8+
9+
impl DropTable {
10+
fn concat_drop_table(&self, query: String, fmts: &fmt::Formatter) -> String {
11+
let fmt::Formatter { comma, lb, space, .. } = fmts;
12+
13+
let sql = if self._drop_table.len() != 0 {
14+
let if_exists = if self._if_exists {
15+
format!("IF EXISTS{space}")
16+
} else {
17+
"".to_string()
18+
};
19+
20+
let table_names = if cfg!(any(feature = "postgresql")) {
21+
self._drop_table.join(comma)
22+
} else {
23+
self._drop_table.last().unwrap().to_string()
24+
};
25+
26+
format!("DROP TABLE{space}{if_exists}{table_names}{space}{lb}")
27+
} else {
28+
"".to_string()
29+
};
30+
31+
concat_raw_before_after(
32+
&self._raw_before,
33+
&self._raw_after,
34+
query,
35+
fmts,
36+
DropTableParams::DropTable,
37+
sql,
38+
)
39+
}
40+
}
41+
42+
impl Concat for DropTable {
43+
fn concat(&self, fmts: &fmt::Formatter) -> String {
44+
let mut query = "".to_string();
45+
46+
query = self.concat_raw(query, &fmts, &self._raw);
47+
query = self.concat_drop_table(query, &fmts);
48+
49+
query.trim_end().to_string()
50+
}
51+
}

src/drop_table/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
mod drop_table;
2+
mod drop_table_internal;

src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ mod alter_table;
55
mod behavior;
66
mod create_table;
77
mod delete;
8+
mod drop_table;
89
mod fmt;
910
mod insert;
1011
mod select;
@@ -14,6 +15,6 @@ mod update;
1415
mod values;
1516

1617
pub use crate::structure::{
17-
AlterTable, AlterTableAction, CreateTable, CreateTableParams, Delete, DeleteClause, Insert, InsertClause, Select,
18-
SelectClause, Transaction, Update, UpdateClause, Values, ValuesClause,
18+
AlterTable, AlterTableAction, CreateTable, CreateTableParams, Delete, DeleteClause, DropTable, DropTableParams,
19+
Insert, InsertClause, Select, SelectClause, Transaction, Update, UpdateClause, Values, ValuesClause,
1920
};

0 commit comments

Comments
 (0)