|
| 1 | +use crate::{ |
| 2 | + behavior::{push_unique, Concat, TransactionQuery}, |
| 3 | + fmt, |
| 4 | + structure::{CreateTable, CreateTableParams}, |
| 5 | +}; |
| 6 | + |
| 7 | +impl TransactionQuery for CreateTable {} |
| 8 | + |
| 9 | +impl CreateTable { |
| 10 | + /// Gets the current state of the [CreateTable] and returns it as string |
| 11 | + /// |
| 12 | + /// ### Example |
| 13 | + /// |
| 14 | + /// ``` |
| 15 | + /// # use sql_query_builder as sql; |
| 16 | + /// let query = sql::CreateTable::new() |
| 17 | + /// .create_table("users") |
| 18 | + /// .column("name varchar(100) not null") |
| 19 | + /// .as_string(); |
| 20 | + /// |
| 21 | + /// # let expected = "\ |
| 22 | + /// # CREATE TABLE users (\ |
| 23 | + /// # name varchar(100) not null\ |
| 24 | + /// # )\ |
| 25 | + /// # "; |
| 26 | + /// # assert_eq!(expected, query); |
| 27 | + /// ``` |
| 28 | + /// |
| 29 | + /// Output |
| 30 | + /// |
| 31 | + /// ```sql |
| 32 | + /// CREATE TABLE users ( |
| 33 | + /// name varchar(100) not null |
| 34 | + /// ) |
| 35 | + /// ``` |
| 36 | + pub fn as_string(&self) -> String { |
| 37 | + let fmts = fmt::one_line(); |
| 38 | + self.concat(&fmts) |
| 39 | + } |
| 40 | + |
| 41 | + /// Define a column to be passed as arguments to the create table command, multiples call will concatenates all column parameters |
| 42 | + /// |
| 43 | + /// ### Example |
| 44 | + /// |
| 45 | + /// ``` |
| 46 | + /// # use sql_query_builder as sql; |
| 47 | + /// let query = sql::CreateTable::new() |
| 48 | + /// .column("id serial not null primary key") |
| 49 | + /// .column("name varchar(100) not null") |
| 50 | + /// .as_string(); |
| 51 | + /// |
| 52 | + /// # let expected = "(\ |
| 53 | + /// # id serial not null primary key, \ |
| 54 | + /// # name varchar(100) not null\ |
| 55 | + /// # )"; |
| 56 | + /// # assert_eq!(expected, query); |
| 57 | + /// ``` |
| 58 | + /// |
| 59 | + /// Outputs |
| 60 | + /// |
| 61 | + /// ```sql |
| 62 | + /// ( |
| 63 | + /// id serial not null primary key, |
| 64 | + /// name varchar(100) not null |
| 65 | + /// ) |
| 66 | + /// ``` |
| 67 | + pub fn column(mut self, column: &str) -> Self { |
| 68 | + push_unique(&mut self._column, column.trim().to_string()); |
| 69 | + self |
| 70 | + } |
| 71 | + |
| 72 | + /// Defines a table constraint, multiples call will concatenates all constraints |
| 73 | + /// |
| 74 | + /// ### Example |
| 75 | + /// |
| 76 | + /// ``` |
| 77 | + /// # use sql_query_builder as sql; |
| 78 | + /// let query = sql::CreateTable::new() |
| 79 | + /// .constraint("users_id_key PRIMARY KEY(id)") |
| 80 | + /// .constraint("users_login_key UNIQUE(login)") |
| 81 | + /// .as_string(); |
| 82 | + /// |
| 83 | + /// # let expected = "(\ |
| 84 | + /// # CONSTRAINT users_id_key PRIMARY KEY(id), \ |
| 85 | + /// # CONSTRAINT users_login_key UNIQUE(login)\ |
| 86 | + /// # )"; |
| 87 | + /// # assert_eq!(expected, query); |
| 88 | + /// ``` |
| 89 | + /// |
| 90 | + /// Outputs |
| 91 | + /// |
| 92 | + /// ```sql |
| 93 | + /// ( |
| 94 | + /// CONSTRAINT users_id_key PRIMARY KEY(id), |
| 95 | + /// CONSTRAINT users_login_key UNIQUE(login) |
| 96 | + /// ) |
| 97 | + /// ``` |
| 98 | + pub fn constraint(mut self, column: &str) -> Self { |
| 99 | + push_unique(&mut self._constraint, column.trim().to_string()); |
| 100 | + self |
| 101 | + } |
| 102 | + |
| 103 | + /// Defines a create table signature. Multiples calls will overrides the previous value |
| 104 | + /// |
| 105 | + /// ### Example |
| 106 | + /// |
| 107 | + ///``` |
| 108 | + /// # use sql_query_builder as sql; |
| 109 | + /// let create_table = sql::CreateTable::new() |
| 110 | + /// .create_table("users") |
| 111 | + /// .create_table("orders"); |
| 112 | + /// |
| 113 | + /// # let expected = "CREATE TABLE orders"; |
| 114 | + /// # assert_eq!(expected, create_table.to_string()); |
| 115 | + /// ``` |
| 116 | + /// |
| 117 | + /// Outputs |
| 118 | + /// |
| 119 | + /// ```sql |
| 120 | + /// CREATE TABLE orders |
| 121 | + /// ``` |
| 122 | + pub fn create_table(mut self, table_name: &str) -> Self { |
| 123 | + self._create_table = table_name.trim().to_string(); |
| 124 | + self |
| 125 | + } |
| 126 | + |
| 127 | + /// Defines a create table signature with the modifer `if not exists`. Multiples calls will overrides the previous value |
| 128 | + /// |
| 129 | + /// ### Example |
| 130 | + /// |
| 131 | + /// ``` |
| 132 | + /// # use sql_query_builder as sql; |
| 133 | + /// let create_table = sql::CreateTable::new() |
| 134 | + /// .create_table("users") |
| 135 | + /// .create_table_if_not_exists("orders"); |
| 136 | + /// |
| 137 | + /// # let expected = "CREATE TABLE IF NOT EXISTS orders"; |
| 138 | + /// # assert_eq!(expected, create_table.to_string()); |
| 139 | + /// ``` |
| 140 | + /// |
| 141 | + /// Outputs |
| 142 | + /// |
| 143 | + /// ```sql |
| 144 | + /// CREATE TABLE IF NOT EXISTS orders |
| 145 | + /// ``` |
| 146 | + pub fn create_table_if_not_exists(mut self, table_name: &str) -> Self { |
| 147 | + self._create_table = format!("IF NOT EXISTS {}", table_name.trim()); |
| 148 | + self |
| 149 | + } |
| 150 | + |
| 151 | + /// Prints the current state of the [CreateTable] to the standard output in a more ease to read version. |
| 152 | + /// This method is useful to debug complex queries or just print the generated SQL while you type |
| 153 | + /// |
| 154 | + /// ### Example |
| 155 | + /// |
| 156 | + /// ``` |
| 157 | + /// # use sql_query_builder as sql; |
| 158 | + /// let query = sql::CreateTable::new() |
| 159 | + /// .create_table("users") |
| 160 | + /// .column("name varchar(100) not null") |
| 161 | + /// .column("login varchar(40) not null") |
| 162 | + /// .constraint("users_login_key unique(login)") |
| 163 | + /// .debug() |
| 164 | + /// .as_string(); |
| 165 | + /// ``` |
| 166 | + /// |
| 167 | + /// Prints to the standard output |
| 168 | + /// |
| 169 | + /// ```sql |
| 170 | + /// -- ------------------------------------------------------------------------------ |
| 171 | + /// CREATE TABLE users ( |
| 172 | + /// name varchar(100) not null, |
| 173 | + /// login varchar(40) not null, |
| 174 | + /// CONSTRAINT users_login_key unique(login) |
| 175 | + /// ) |
| 176 | + /// -- ------------------------------------------------------------------------------ |
| 177 | + /// ``` |
| 178 | + pub fn debug(self) -> Self { |
| 179 | + let fmts = fmt::multiline(); |
| 180 | + println!("{}", fmt::format(self.concat(&fmts), &fmts)); |
| 181 | + self |
| 182 | + } |
| 183 | + |
| 184 | + /// Defines a foreign key constraint, multiples call will concatenates all foreign keys |
| 185 | + /// |
| 186 | + /// ### Example |
| 187 | + /// |
| 188 | + /// ``` |
| 189 | + /// # use sql_query_builder as sql; |
| 190 | + /// let query = sql::CreateTable::new() |
| 191 | + /// .foreign_key("(user_id) refereces users") |
| 192 | + /// .foreign_key("(address_id) refereces address(id)") |
| 193 | + /// .as_string(); |
| 194 | + /// |
| 195 | + /// # let expected = "(\ |
| 196 | + /// # FOREIGN KEY(user_id) refereces users, \ |
| 197 | + /// # FOREIGN KEY(address_id) refereces address(id)\ |
| 198 | + /// # )"; |
| 199 | + /// # assert_eq!(expected, query); |
| 200 | + /// ``` |
| 201 | + /// |
| 202 | + /// Outputs |
| 203 | + /// |
| 204 | + /// ```sql |
| 205 | + /// ( |
| 206 | + /// FOREIGN KEY(user_id) refereces users, |
| 207 | + /// FOREIGN KEY(address_id) refereces address (id) |
| 208 | + /// ) |
| 209 | + /// ``` |
| 210 | + pub fn foreign_key(mut self, column: &str) -> Self { |
| 211 | + push_unique(&mut self._foreign_key, column.trim().to_string()); |
| 212 | + self |
| 213 | + } |
| 214 | + |
| 215 | + /// Creates instance of the CreateTable command |
| 216 | + pub fn new() -> Self { |
| 217 | + Self::default() |
| 218 | + } |
| 219 | + |
| 220 | + /// Defines a primary key constraint. Multiples calls will overrides the previous value |
| 221 | + /// |
| 222 | + /// ### Example |
| 223 | + /// |
| 224 | + /// ``` |
| 225 | + /// # use sql_query_builder as sql; |
| 226 | + /// let query = sql::CreateTable::new() |
| 227 | + /// .create_table("users") |
| 228 | + /// .primary_key("id") |
| 229 | + /// .as_string(); |
| 230 | + /// |
| 231 | + /// # let expected = "\ |
| 232 | + /// # CREATE TABLE users (\ |
| 233 | + /// # PRIMARY KEY(id)\ |
| 234 | + /// # )\ |
| 235 | + /// # "; |
| 236 | + /// # assert_eq!(expected, query); |
| 237 | + /// ``` |
| 238 | + /// |
| 239 | + /// Outputs |
| 240 | + /// |
| 241 | + /// ```sql |
| 242 | + /// CREATE TABLE users ( |
| 243 | + /// PRIMARY KEY(id) |
| 244 | + /// ) |
| 245 | + /// ``` |
| 246 | + pub fn primary_key(mut self, column: &str) -> Self { |
| 247 | + self._primary_key = column.trim().to_string(); |
| 248 | + self |
| 249 | + } |
| 250 | + |
| 251 | + /// Prints the current state of the [CreateTable] to the standard output similar to debug method, |
| 252 | + /// the difference is that this method prints in one line. |
| 253 | + pub fn print(self) -> Self { |
| 254 | + let fmts = fmt::one_line(); |
| 255 | + println!("{}", fmt::format(self.concat(&fmts), &fmts)); |
| 256 | + self |
| 257 | + } |
| 258 | + |
| 259 | + /// Adds at the beginning a raw SQL query. Is useful to create a more complex create table signature like the example below. |
| 260 | + /// |
| 261 | + /// ### Example |
| 262 | + /// |
| 263 | + /// ``` |
| 264 | + /// # use sql_query_builder as sql; |
| 265 | + /// let create_table_query = sql::CreateTable::new() |
| 266 | + /// .raw("CREATE LOCAL TEMP TABLE IF NOT EXISTS users_temp") |
| 267 | + /// .column("login VARCHAR(40) NOT NULL") |
| 268 | + /// .as_string(); |
| 269 | + /// |
| 270 | + /// # let expected = "\ |
| 271 | + /// # CREATE LOCAL TEMP TABLE IF NOT EXISTS users_temp (\ |
| 272 | + /// # login VARCHAR(40) NOT NULL\ |
| 273 | + /// # )\ |
| 274 | + /// # "; |
| 275 | + /// # assert_eq!(expected, create_table_query); |
| 276 | + /// ``` |
| 277 | + /// |
| 278 | + /// Output |
| 279 | + /// |
| 280 | + /// ```sql |
| 281 | + /// CREATE LOCAL TEMP TABLE IF NOT EXISTS users_temp ( |
| 282 | + /// login VARCHAR(40) NOT NULL |
| 283 | + /// ) |
| 284 | + /// ``` |
| 285 | + pub fn raw(mut self, raw_sql: &str) -> Self { |
| 286 | + push_unique(&mut self._raw, raw_sql.trim().to_string()); |
| 287 | + self |
| 288 | + } |
| 289 | + |
| 290 | + /// Adds a raw SQL query after a specified parameter. |
| 291 | + /// |
| 292 | + /// ### Example |
| 293 | + /// |
| 294 | + /// ``` |
| 295 | + /// # use sql_query_builder as sql; |
| 296 | + /// let raw = "(name varchar(100) not null)"; |
| 297 | + /// |
| 298 | + /// let query = sql::CreateTable::new() |
| 299 | + /// .create_table("users") |
| 300 | + /// .raw_after(sql::CreateTableParams::CreateTable, raw) |
| 301 | + /// .as_string(); |
| 302 | + /// |
| 303 | + /// # let expected = "CREATE TABLE users (name varchar(100) not null)"; |
| 304 | + /// # assert_eq!(expected, query); |
| 305 | + /// ``` |
| 306 | + /// |
| 307 | + /// Output |
| 308 | + /// |
| 309 | + /// ```sql |
| 310 | + /// CREATE TABLE users (name varchar(100) not null) |
| 311 | + /// ``` |
| 312 | + pub fn raw_after(mut self, param: CreateTableParams, raw_sql: &str) -> Self { |
| 313 | + self._raw_after.push((param, raw_sql.trim().to_string())); |
| 314 | + self |
| 315 | + } |
| 316 | + |
| 317 | + /// Adds a raw SQL query before a specified parameter. |
| 318 | + /// |
| 319 | + /// ### Example |
| 320 | + /// |
| 321 | + /// ``` |
| 322 | + /// # use sql_query_builder as sql; |
| 323 | + /// let raw = "name varchar(100) not null, "; |
| 324 | + /// |
| 325 | + /// let query = sql::CreateTable::new() |
| 326 | + /// .raw_before(sql::CreateTableParams::Column, raw) |
| 327 | + /// .column("login varchar(40) not null") |
| 328 | + /// .as_string(); |
| 329 | + /// |
| 330 | + /// # let expected = "(name varchar(100) not null, login varchar(40) not null)"; |
| 331 | + /// # assert_eq!(expected, query); |
| 332 | + /// ``` |
| 333 | + /// |
| 334 | + /// Output |
| 335 | + /// |
| 336 | + /// ```sql |
| 337 | + /// (name varchar(100) not null, login varchar(40) not null) |
| 338 | + /// ``` |
| 339 | + pub fn raw_before(mut self, param: CreateTableParams, raw_sql: &str) -> Self { |
| 340 | + self._raw_before.push((param, raw_sql.trim().to_string())); |
| 341 | + self |
| 342 | + } |
| 343 | +} |
| 344 | + |
| 345 | +impl std::fmt::Display for CreateTable { |
| 346 | + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { |
| 347 | + write!(f, "{}", self.as_string()) |
| 348 | + } |
| 349 | +} |
| 350 | + |
| 351 | +impl std::fmt::Debug for CreateTable { |
| 352 | + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { |
| 353 | + let fmts = fmt::multiline(); |
| 354 | + write!(f, "{}", fmt::format(self.concat(&fmts), &fmts)) |
| 355 | + } |
| 356 | +} |
0 commit comments