@@ -9,30 +9,51 @@ 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
+ ///
33
+ /// Available on crate feature `postgresql` only.
34
+ /// Multiples call of this method will build the SQL respecting the order of the calls
35
+ ///
36
+ /// ### Example
37
+ ///
38
+ /// ```
39
+ /// # #[cfg(any(feature = "postgresql"))]
40
+ /// # {
41
+ /// # use sql_query_builder as sql;
42
+ /// let query = sql::AlterTable::new()
43
+ /// .add("COLUMN login varchar not null")
44
+ /// .add("CONSTRAINT login_unique unique(login)")
45
+ /// .as_string();
46
+ ///
47
+ /// # let expected = "ADD COLUMN login varchar not null, ADD CONSTRAINT login_unique unique(login)";
48
+ /// # assert_eq!(expected, query);
49
+ /// # }
50
+ /// ```
51
+ ///
52
+ /// Outputs
53
+ ///
54
+ /// ```sql
55
+ /// ADD COLUMN login varchar not null,
56
+ /// ADD CONSTRAINT login_unique unique(login)
36
57
/// ```
37
58
pub fn add ( mut self , add_exp : & str ) -> Self {
38
59
let action = AlterTableActionItem ( AlterTableOrderedAction :: Add , add_exp. trim ( ) . to_string ( ) ) ;
@@ -102,8 +123,6 @@ impl AlterTable {
102
123
/// let query = sql::AlterTable::new()
103
124
/// .alter_table("users")
104
125
/// .add("name varchar(100) not null")
105
- /// .add("login varchar(40) not null")
106
- /// .add("constraint users_login_key unique(login)")
107
126
/// .debug()
108
127
/// .as_string();
109
128
/// ```
@@ -113,9 +132,7 @@ impl AlterTable {
113
132
/// ```sql
114
133
/// -- ------------------------------------------------------------------------------
115
134
/// 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)
135
+ /// ADD name varchar(100) not null
119
136
/// -- ------------------------------------------------------------------------------
120
137
/// ```
121
138
pub fn debug ( self ) -> Self {
@@ -124,8 +141,7 @@ impl AlterTable {
124
141
self
125
142
}
126
143
127
- /// Drops columns or table constraints.
128
- /// Multiples call of this method will build the SQL respecting the order of the calls
144
+ /// Drops columns or table constraints, this method overrides the previous value.
129
145
///
130
146
/// ### Example
131
147
///
@@ -144,6 +160,31 @@ impl AlterTable {
144
160
/// ```sql
145
161
/// DROP column login
146
162
/// ```
163
+ ///
164
+ /// Available on crate feature `postgresql` only.
165
+ /// Multiples call of this method will build the SQL respecting the order of the calls
166
+ ///
167
+ /// ### Example
168
+ ///
169
+ /// ```
170
+ /// # #[cfg(any(feature = "postgresql"))]
171
+ /// # {
172
+ /// # use sql_query_builder as sql;
173
+ /// let query = sql::AlterTable::new()
174
+ /// .drop("column login")
175
+ /// .drop("constraint login_unique")
176
+ /// .as_string();
177
+ ///
178
+ /// # let expected = "DROP column login, DROP constraint login_unique";
179
+ /// # assert_eq!(expected, query);
180
+ /// # }
181
+ /// ```
182
+ ///
183
+ /// Outputs
184
+ ///
185
+ /// ```sql
186
+ /// DROP column login, DROP constraint login_unique
187
+ /// ```
147
188
pub fn drop ( mut self , drop_exp : & str ) -> Self {
148
189
let action = AlterTableActionItem ( AlterTableOrderedAction :: Drop , drop_exp. trim ( ) . to_string ( ) ) ;
149
190
push_unique ( & mut self . _ordered_actions , action) ;
@@ -163,7 +204,7 @@ impl AlterTable {
163
204
self
164
205
}
165
206
166
- /// Adds at the beginning a raw SQL query. Is useful to create a more complex alter table signature like the example below .
207
+ /// Adds at the beginning a raw SQL query. Is useful to create a more complex alter table signature.
167
208
///
168
209
/// ### Example
169
210
///
@@ -221,21 +262,21 @@ impl AlterTable {
221
262
///
222
263
/// ```
223
264
/// # use sql_query_builder as sql;
224
- /// let raw = "ALTER TABLE users ";
265
+ /// let raw = "/* alter table command */ ";
225
266
///
226
267
/// let query = sql::AlterTable::new()
227
268
/// .raw_before(sql::AlterTableAction::AlterTable, raw)
228
- /// .add("COLUMN id ")
269
+ /// .alter_table("users ")
229
270
/// .as_string();
230
271
///
231
- /// # let expected = "ALTER TABLE users ADD COLUMN id ";
272
+ /// # let expected = "/* alter table command */ ALTER TABLE users ";
232
273
/// # assert_eq!(expected, query);
233
274
/// ```
234
275
///
235
276
/// Output
236
277
///
237
278
/// ```sql
238
- /// ALTER TABLE users RENAME TO users_old
279
+ /// /* alter table command */ ALTER TABLE users
239
280
/// ```
240
281
pub fn raw_before ( mut self , action : AlterTableAction , raw_sql : & str ) -> Self {
241
282
self . _raw_before . push ( ( action, raw_sql. trim ( ) . to_string ( ) ) ) ;
0 commit comments