@@ -9,30 +9,50 @@ use crate::{
9
9
impl TransactionQuery for AlterTable { }
10
10
11
11
impl AlterTable {
12
- /// Adds columns or table constraints.
13
- /// Multiples call of this method will build the SQL respecting the order of the calls
12
+ /// Adds columns or table constraints, this method overrides the previous value
14
13
///
15
14
/// ### Example
16
15
///
17
16
/// ```
18
17
/// # use sql_query_builder as sql;
19
18
/// let query = sql::AlterTable::new()
20
19
/// .add("COLUMN age int not null")
21
- /// .add("CONSTRAINT age check(age >= 0)")
22
20
/// .as_string();
23
21
///
24
- /// # let expected = "\
25
- /// # ADD COLUMN age int not null, \
26
- /// # ADD CONSTRAINT age check(age >= 0)\
27
- /// # ";
22
+ /// # let expected = "ADD COLUMN age int not null";
28
23
/// # assert_eq!(expected, query);
29
24
/// ```
30
25
///
31
26
/// Outputs
32
27
///
33
28
/// ```sql
34
- /// ADD COLUMN age int not null,
35
- /// ADD CONSTRAINT age check(age >= 0)
29
+ /// ADD COLUMN age int not null
30
+ /// ```
31
+ ///
32
+ /// ### Available on crate feature `postgresql` and `mysql` only.
33
+ /// Multiples call of this method will build the SQL respecting the order of the calls
34
+ ///
35
+ /// ### Example
36
+ ///
37
+ /// ```
38
+ /// # #[cfg(any(feature = "postgresql", feature = "mysql"))]
39
+ /// # {
40
+ /// # use sql_query_builder as sql;
41
+ /// let query = sql::AlterTable::new()
42
+ /// .add("COLUMN login varchar not null")
43
+ /// .add("CONSTRAINT login_unique unique(login)")
44
+ /// .as_string();
45
+ ///
46
+ /// # let expected = "ADD COLUMN login varchar not null, ADD CONSTRAINT login_unique unique(login)";
47
+ /// # assert_eq!(expected, query);
48
+ /// # }
49
+ /// ```
50
+ ///
51
+ /// Outputs
52
+ ///
53
+ /// ```sql
54
+ /// ADD COLUMN login varchar not null,
55
+ /// ADD CONSTRAINT login_unique unique(login)
36
56
/// ```
37
57
pub fn add ( mut self , add_exp : & str ) -> Self {
38
58
let action = AlterTableActionItem ( AlterTableOrderedAction :: Add , add_exp. trim ( ) . to_string ( ) ) ;
@@ -102,8 +122,6 @@ impl AlterTable {
102
122
/// let query = sql::AlterTable::new()
103
123
/// .alter_table("users")
104
124
/// .add("name varchar(100) not null")
105
- /// .add("login varchar(40) not null")
106
- /// .add("constraint users_login_key unique(login)")
107
125
/// .debug()
108
126
/// .as_string();
109
127
/// ```
@@ -113,9 +131,7 @@ impl AlterTable {
113
131
/// ```sql
114
132
/// -- ------------------------------------------------------------------------------
115
133
/// ALTER TABLE users
116
- /// ADD name varchar(100) not null,
117
- /// ADD login varchar(40) not null,
118
- /// ADD constraint users_login_key unique(login)
134
+ /// ADD name varchar(100) not null
119
135
/// -- ------------------------------------------------------------------------------
120
136
/// ```
121
137
pub fn debug ( self ) -> Self {
@@ -124,8 +140,7 @@ impl AlterTable {
124
140
self
125
141
}
126
142
127
- /// Drops columns or table constraints.
128
- /// Multiples call of this method will build the SQL respecting the order of the calls
143
+ /// Drops columns or table constraints, this method overrides the previous value.
129
144
///
130
145
/// ### Example
131
146
///
@@ -144,6 +159,31 @@ impl AlterTable {
144
159
/// ```sql
145
160
/// DROP column login
146
161
/// ```
162
+ ///
163
+ /// ### Available on crate feature `postgresql` and `mysql` only.
164
+ /// Multiples call of this method will build the SQL respecting the order of the calls
165
+ ///
166
+ /// ### Example
167
+ ///
168
+ /// ```
169
+ /// # #[cfg(any(feature = "postgresql", feature = "mysql"))]
170
+ /// # {
171
+ /// # use sql_query_builder as sql;
172
+ /// let query = sql::AlterTable::new()
173
+ /// .drop("column login")
174
+ /// .drop("constraint login_unique")
175
+ /// .as_string();
176
+ ///
177
+ /// # let expected = "DROP column login, DROP constraint login_unique";
178
+ /// # assert_eq!(expected, query);
179
+ /// # }
180
+ /// ```
181
+ ///
182
+ /// Outputs
183
+ ///
184
+ /// ```sql
185
+ /// DROP column login, DROP constraint login_unique
186
+ /// ```
147
187
pub fn drop ( mut self , drop_exp : & str ) -> Self {
148
188
let action = AlterTableActionItem ( AlterTableOrderedAction :: Drop , drop_exp. trim ( ) . to_string ( ) ) ;
149
189
push_unique ( & mut self . _ordered_actions , action) ;
@@ -163,7 +203,7 @@ impl AlterTable {
163
203
self
164
204
}
165
205
166
- /// Adds at the beginning a raw SQL query. Is useful to create a more complex alter table signature like the example below .
206
+ /// Adds at the beginning a raw SQL query. Is useful to create a more complex alter table signature.
167
207
///
168
208
/// ### Example
169
209
///
@@ -221,39 +261,39 @@ impl AlterTable {
221
261
///
222
262
/// ```
223
263
/// # use sql_query_builder as sql;
224
- /// let raw = "ALTER TABLE users ";
264
+ /// let raw = "/* alter table command */ ";
225
265
///
226
266
/// let query = sql::AlterTable::new()
227
267
/// .raw_before(sql::AlterTableAction::AlterTable, raw)
228
- /// .add("COLUMN id ")
268
+ /// .alter_table("users ")
229
269
/// .as_string();
230
270
///
231
- /// # let expected = "ALTER TABLE users ADD COLUMN id ";
271
+ /// # let expected = "/* alter table command */ ALTER TABLE users ";
232
272
/// # assert_eq!(expected, query);
233
273
/// ```
234
274
///
235
275
/// Output
236
276
///
237
277
/// ```sql
238
- /// ALTER TABLE users RENAME TO users_old
278
+ /// /* alter table command */ ALTER TABLE users
239
279
/// ```
240
280
pub fn raw_before ( mut self , action : AlterTableAction , raw_sql : & str ) -> Self {
241
281
self . _raw_before . push ( ( action, raw_sql. trim ( ) . to_string ( ) ) ) ;
242
282
self
243
283
}
244
284
}
245
285
246
- #[ cfg( any( doc, feature = "postgresql" , feature = "sqlite" ) ) ]
286
+ #[ cfg( any( doc, feature = "postgresql" , feature = "sqlite" , feature = "mysql" ) ) ]
247
287
#[ cfg_attr( docsrs, doc( cfg( feature = "postgresql" ) ) ) ]
248
288
#[ cfg_attr( docsrs, doc( cfg( feature = "sqlite" ) ) ) ]
289
+ #[ cfg_attr( docsrs, doc( cfg( feature = "mysql" ) ) ) ]
249
290
impl AlterTable {
250
- /// Changes the column names or table constraints.
251
- /// Multiples call of this method will build the SQL respecting the order of the calls
291
+ /// Changes the column name or table constraints, this method overrides the previous value
252
292
///
253
293
/// ### Example
254
294
///
255
295
///```
256
- /// # #[cfg(any(feature = "postgresql", feature = "sqlite"))]
296
+ /// # #[cfg(any(feature = "postgresql", feature = "sqlite", feature = "mysql" ))]
257
297
/// # {
258
298
/// # use sql_query_builder as sql;
259
299
/// let query = sql::AlterTable::new()
@@ -271,13 +311,55 @@ impl AlterTable {
271
311
/// ```sql
272
312
/// ALTER TABLE users RENAME COLUMN address TO city
273
313
/// ```
274
- pub fn rename ( mut self , rename_exp : & str ) -> Self {
275
- let action = AlterTableActionItem ( AlterTableOrderedAction :: Rename , rename_exp. trim ( ) . to_string ( ) ) ;
276
- push_unique ( & mut self . _ordered_actions , action) ;
314
+ ///
315
+ /// ### Available on crate feature `mysql` only.
316
+ /// Changes the table name, column name or table constraints,
317
+ /// multiples call of this method will build the SQL respecting the order of the calls
318
+ ///
319
+ /// ### Example
320
+ ///
321
+ ///```
322
+ /// # #[cfg(feature = "mysql")]
323
+ /// # {
324
+ /// # use sql_query_builder as sql;
325
+ /// let query = sql::AlterTable::new()
326
+ /// .alter_table("users")
327
+ /// .rename("TO users_old")
328
+ /// .rename("COLUMN name TO full_name")
329
+ /// .to_string();
330
+ ///
331
+ /// # let expected = "ALTER TABLE users RENAME TO users_old, RENAME COLUMN name TO full_name";
332
+ /// # assert_eq!(expected, query);
333
+ /// # }
334
+ /// ```
335
+ ///
336
+ /// Outputs
337
+ ///
338
+ /// ```sql
339
+ /// ALTER TABLE users
340
+ /// RENAME TO users_old,
341
+ /// RENAME COLUMN name TO full_name
342
+ /// ```
343
+ pub fn rename ( mut self , action : & str ) -> Self {
344
+ #[ cfg( feature = "mysql" ) ]
345
+ {
346
+ let action = AlterTableActionItem ( AlterTableOrderedAction :: Rename , action. trim ( ) . to_string ( ) ) ;
347
+ push_unique ( & mut self . _ordered_actions , action) ;
348
+ }
349
+ #[ cfg( not( feature = "mysql" ) ) ]
350
+ {
351
+ self . _rename = action. trim ( ) . to_string ( ) ;
352
+ }
353
+
277
354
self
278
355
}
356
+ }
279
357
280
- /// Changes the name of the table
358
+ #[ cfg( any( doc, feature = "postgresql" , feature = "sqlite" ) ) ]
359
+ #[ cfg_attr( docsrs, doc( cfg( feature = "postgresql" ) ) ) ]
360
+ #[ cfg_attr( docsrs, doc( cfg( feature = "sqlite" ) ) ) ]
361
+ impl AlterTable {
362
+ /// Changes the name of the table, this method overrides the previous value
281
363
///
282
364
/// ### Example
283
365
///
@@ -306,16 +388,17 @@ impl AlterTable {
306
388
}
307
389
}
308
390
309
- #[ cfg( any( doc, feature = "postgresql" ) ) ]
391
+ #[ cfg( any( doc, feature = "postgresql" , feature = "mysql" ) ) ]
310
392
#[ cfg_attr( docsrs, doc( cfg( feature = "postgresql" ) ) ) ]
393
+ #[ cfg_attr( docsrs, doc( cfg( feature = "mysql" ) ) ) ]
311
394
impl AlterTable {
312
395
/// Alter columns or table constraints.
313
396
/// Multiples call of this method will build the SQL respecting the order of the calls
314
397
///
315
398
/// ### Example
316
399
///
317
400
///```
318
- /// # #[cfg(any(feature = "postgresql"))]
401
+ /// # #[cfg(any(feature = "postgresql", feature = "mysql" ))]
319
402
/// # {
320
403
/// # use sql_query_builder as sql;
321
404
/// let query = sql::AlterTable::new()
0 commit comments