Skip to content

Commit 13a6e47

Browse files
committed
Adds Mysql syntax to AlterTable builder
1 parent 424f324 commit 13a6e47

File tree

4 files changed

+141
-34
lines changed

4 files changed

+141
-34
lines changed

src/alter_table/alter_table.rs

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,13 @@ impl AlterTable {
2929
/// ADD COLUMN age int not null
3030
/// ```
3131
///
32-
///
33-
/// Available on crate feature `postgresql` only.
32+
/// ### Available on crate feature `postgresql` and `mysql` only.
3433
/// Multiples call of this method will build the SQL respecting the order of the calls
3534
///
3635
/// ### Example
3736
///
3837
/// ```
39-
/// # #[cfg(any(feature = "postgresql"))]
38+
/// # #[cfg(any(feature = "postgresql", feature = "mysql"))]
4039
/// # {
4140
/// # use sql_query_builder as sql;
4241
/// let query = sql::AlterTable::new()
@@ -161,13 +160,13 @@ impl AlterTable {
161160
/// DROP column login
162161
/// ```
163162
///
164-
/// Available on crate feature `postgresql` only.
163+
/// ### Available on crate feature `postgresql` and `mysql` only.
165164
/// Multiples call of this method will build the SQL respecting the order of the calls
166165
///
167166
/// ### Example
168167
///
169168
/// ```
170-
/// # #[cfg(any(feature = "postgresql"))]
169+
/// # #[cfg(any(feature = "postgresql", feature = "mysql"))]
171170
/// # {
172171
/// # use sql_query_builder as sql;
173172
/// let query = sql::AlterTable::new()
@@ -284,16 +283,17 @@ impl AlterTable {
284283
}
285284
}
286285

287-
#[cfg(any(doc, feature = "postgresql", feature = "sqlite"))]
286+
#[cfg(any(doc, feature = "postgresql", feature = "sqlite", feature = "mysql"))]
288287
#[cfg_attr(docsrs, doc(cfg(feature = "postgresql")))]
289288
#[cfg_attr(docsrs, doc(cfg(feature = "sqlite")))]
289+
#[cfg_attr(docsrs, doc(cfg(feature = "mysql")))]
290290
impl AlterTable {
291291
/// Changes the column name or table constraints, this method overrides the previous value
292292
///
293293
/// ### Example
294294
///
295295
///```
296-
/// # #[cfg(any(feature = "postgresql", feature = "sqlite"))]
296+
/// # #[cfg(any(feature = "postgresql", feature = "sqlite", feature = "mysql"))]
297297
/// # {
298298
/// # use sql_query_builder as sql;
299299
/// let query = sql::AlterTable::new()
@@ -311,11 +311,54 @@ impl AlterTable {
311311
/// ```sql
312312
/// ALTER TABLE users RENAME COLUMN address TO city
313313
/// ```
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+
/// ```
314343
pub fn rename(mut self, action: &str) -> Self {
315-
self._rename = action.trim().to_string();
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+
316354
self
317355
}
356+
}
318357

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 {
319362
/// Changes the name of the table, this method overrides the previous value
320363
///
321364
/// ### Example
@@ -345,16 +388,17 @@ impl AlterTable {
345388
}
346389
}
347390

348-
#[cfg(any(doc, feature = "postgresql"))]
391+
#[cfg(any(doc, feature = "postgresql", feature = "mysql"))]
349392
#[cfg_attr(docsrs, doc(cfg(feature = "postgresql")))]
393+
#[cfg_attr(docsrs, doc(cfg(feature = "mysql")))]
350394
impl AlterTable {
351395
/// Alter columns or table constraints.
352396
/// Multiples call of this method will build the SQL respecting the order of the calls
353397
///
354398
/// ### Example
355399
///
356400
///```
357-
/// # #[cfg(any(feature = "postgresql"))]
401+
/// # #[cfg(any(feature = "postgresql", feature = "mysql"))]
358402
/// # {
359403
/// # use sql_query_builder as sql;
360404
/// let query = sql::AlterTable::new()

src/alter_table/alter_table_internal.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ impl AlterTable {
4545
fn concat_ordered_actions(&self, query: String, fmts: &fmt::Formatter) -> String {
4646
let actions = self._ordered_actions.iter().filter(|item| item.1.is_empty() == false);
4747

48-
#[cfg(any(feature = "postgresql"))]
48+
#[cfg(any(feature = "postgresql", feature = "mysql"))]
4949
{
5050
use crate::structure::AlterTableActionItem;
5151

@@ -63,8 +63,9 @@ impl AlterTable {
6363
match action {
6464
AlterTableOrderedAction::Add => format!("{lb}{indent}ADD{space}{content}"),
6565
AlterTableOrderedAction::Drop => format!("{lb}{indent}DROP{space}{content}"),
66-
#[cfg(any(feature = "postgresql"))]
6766
AlterTableOrderedAction::Alter => format!("{lb}{indent}ALTER{space}{content}"),
67+
#[cfg(feature = "mysql")]
68+
AlterTableOrderedAction::Rename => format!("{lb}{indent}RENAME{space}{content}"),
6869
}
6970
})
7071
.collect::<Vec<_>>()
@@ -74,7 +75,7 @@ impl AlterTable {
7475
format!("{query}{sql}{space}")
7576
}
7677

77-
#[cfg(not(any(feature = "postgresql")))]
78+
#[cfg(not(any(feature = "postgresql", feature = "mysql")))]
7879
{
7980
let fmt::Formatter { lb, space, .. } = fmts;
8081

src/structure.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,11 @@ pub(crate) enum AlterTableOrderedAction {
5151
Add,
5252
Drop,
5353

54-
#[cfg(any(feature = "postgresql"))]
54+
#[cfg(any(feature = "postgresql", feature = "mysql"))]
5555
Alter,
56+
57+
#[cfg(feature = "mysql")]
58+
Rename,
5659
}
5760

5861
/// All available params to be used in [AlterTable::raw_before] and [AlterTable::raw_after] methods on [AlterTable] builder

0 commit comments

Comments
 (0)