Skip to content

Commit 1cef484

Browse files
authored
Merge pull request #45 from belchior/mysql-alter-table
Mysql alter table
2 parents 00f7021 + 13a6e47 commit 1cef484

File tree

7 files changed

+569
-259
lines changed

7 files changed

+569
-259
lines changed

scripts/coverage_test.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ echo "\n-- ---------------------------------------------------------------------
2727
echo "-- Testing SQLite syntax"
2828
echo "-- ------------------------------------------------------------------------------\n"
2929
RUSTFLAGS="-C instrument-coverage" LLVM_PROFILE_FILE="$COVERAGE_TARGET/$PKG_NAME-%m.profraw" cargo test --target-dir $COVERAGE_TARGET --features sqlite;
30+
RUSTFLAGS="-C instrument-coverage" LLVM_PROFILE_FILE="$COVERAGE_TARGET/$PKG_NAME-%m.profraw" cargo test --target-dir $COVERAGE_TARGET --features mysql;
3031

3132
echo "\n-- ------------------------------------------------------------------------------"
3233
echo "-- Testing MySQL syntax"

scripts/test.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ echo "\n-- ---------------------------------------------------------------------
1717
echo "-- Testing SQLite syntax"
1818
echo "-- ------------------------------------------------------------------------------\n"
1919
cargo test $test_names --features sqlite
20+
cargo test $test_names --features mysql
2021

2122
echo "\n-- ------------------------------------------------------------------------------"
2223
echo "-- Testing MySQL syntax"

src/alter_table/alter_table.rs

Lines changed: 114 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,50 @@ use crate::{
99
impl TransactionQuery for AlterTable {}
1010

1111
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
1413
///
1514
/// ### Example
1615
///
1716
/// ```
1817
/// # use sql_query_builder as sql;
1918
/// let query = sql::AlterTable::new()
2019
/// .add("COLUMN age int not null")
21-
/// .add("CONSTRAINT age check(age >= 0)")
2220
/// .as_string();
2321
///
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";
2823
/// # assert_eq!(expected, query);
2924
/// ```
3025
///
3126
/// Outputs
3227
///
3328
/// ```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)
3656
/// ```
3757
pub fn add(mut self, add_exp: &str) -> Self {
3858
let action = AlterTableActionItem(AlterTableOrderedAction::Add, add_exp.trim().to_string());
@@ -102,8 +122,6 @@ impl AlterTable {
102122
/// let query = sql::AlterTable::new()
103123
/// .alter_table("users")
104124
/// .add("name varchar(100) not null")
105-
/// .add("login varchar(40) not null")
106-
/// .add("constraint users_login_key unique(login)")
107125
/// .debug()
108126
/// .as_string();
109127
/// ```
@@ -113,9 +131,7 @@ impl AlterTable {
113131
/// ```sql
114132
/// -- ------------------------------------------------------------------------------
115133
/// 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
119135
/// -- ------------------------------------------------------------------------------
120136
/// ```
121137
pub fn debug(self) -> Self {
@@ -124,8 +140,7 @@ impl AlterTable {
124140
self
125141
}
126142

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.
129144
///
130145
/// ### Example
131146
///
@@ -144,6 +159,31 @@ impl AlterTable {
144159
/// ```sql
145160
/// DROP column login
146161
/// ```
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+
/// ```
147187
pub fn drop(mut self, drop_exp: &str) -> Self {
148188
let action = AlterTableActionItem(AlterTableOrderedAction::Drop, drop_exp.trim().to_string());
149189
push_unique(&mut self._ordered_actions, action);
@@ -163,7 +203,7 @@ impl AlterTable {
163203
self
164204
}
165205

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.
167207
///
168208
/// ### Example
169209
///
@@ -221,39 +261,39 @@ impl AlterTable {
221261
///
222262
/// ```
223263
/// # use sql_query_builder as sql;
224-
/// let raw = "ALTER TABLE users";
264+
/// let raw = "/* alter table command */";
225265
///
226266
/// let query = sql::AlterTable::new()
227267
/// .raw_before(sql::AlterTableAction::AlterTable, raw)
228-
/// .add("COLUMN id")
268+
/// .alter_table("users")
229269
/// .as_string();
230270
///
231-
/// # let expected = "ALTER TABLE users ADD COLUMN id";
271+
/// # let expected = "/* alter table command */ ALTER TABLE users";
232272
/// # assert_eq!(expected, query);
233273
/// ```
234274
///
235275
/// Output
236276
///
237277
/// ```sql
238-
/// ALTER TABLE users RENAME TO users_old
278+
/// /* alter table command */ ALTER TABLE users
239279
/// ```
240280
pub fn raw_before(mut self, action: AlterTableAction, raw_sql: &str) -> Self {
241281
self._raw_before.push((action, raw_sql.trim().to_string()));
242282
self
243283
}
244284
}
245285

246-
#[cfg(any(doc, feature = "postgresql", feature = "sqlite"))]
286+
#[cfg(any(doc, feature = "postgresql", feature = "sqlite", feature = "mysql"))]
247287
#[cfg_attr(docsrs, doc(cfg(feature = "postgresql")))]
248288
#[cfg_attr(docsrs, doc(cfg(feature = "sqlite")))]
289+
#[cfg_attr(docsrs, doc(cfg(feature = "mysql")))]
249290
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
252292
///
253293
/// ### Example
254294
///
255295
///```
256-
/// # #[cfg(any(feature = "postgresql", feature = "sqlite"))]
296+
/// # #[cfg(any(feature = "postgresql", feature = "sqlite", feature = "mysql"))]
257297
/// # {
258298
/// # use sql_query_builder as sql;
259299
/// let query = sql::AlterTable::new()
@@ -271,13 +311,55 @@ impl AlterTable {
271311
/// ```sql
272312
/// ALTER TABLE users RENAME COLUMN address TO city
273313
/// ```
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+
277354
self
278355
}
356+
}
279357

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
281363
///
282364
/// ### Example
283365
///
@@ -306,16 +388,17 @@ impl AlterTable {
306388
}
307389
}
308390

309-
#[cfg(any(doc, feature = "postgresql"))]
391+
#[cfg(any(doc, feature = "postgresql", feature = "mysql"))]
310392
#[cfg_attr(docsrs, doc(cfg(feature = "postgresql")))]
393+
#[cfg_attr(docsrs, doc(cfg(feature = "mysql")))]
311394
impl AlterTable {
312395
/// Alter columns or table constraints.
313396
/// Multiples call of this method will build the SQL respecting the order of the calls
314397
///
315398
/// ### Example
316399
///
317400
///```
318-
/// # #[cfg(any(feature = "postgresql"))]
401+
/// # #[cfg(any(feature = "postgresql", feature = "mysql"))]
319402
/// # {
320403
/// # use sql_query_builder as sql;
321404
/// let query = sql::AlterTable::new()

0 commit comments

Comments
 (0)