|
| 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 | +} |
0 commit comments