sql_query_builder/drop_table/
drop_table.rs

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