diff --git a/src/dev/shiza/bulbasaur/alter/Alter.java b/src/dev/shiza/bulbasaur/alter/Alter.java index f7a9148..3a4e07f 100644 --- a/src/dev/shiza/bulbasaur/alter/Alter.java +++ b/src/dev/shiza/bulbasaur/alter/Alter.java @@ -18,5 +18,26 @@ static Alter alter(final @NotNull String table) { Alter drop(final @NotNull String column); + Alter modify(final @NotNull String column, final @NotNull String definition); + + Alter modify(final @NotNull String column, final @NotNull ColumnDefinition definition); + + Alter modify(final @NotNull String column, final @NotNull ColumnDefinition... definitions); + + Alter change( + final @NotNull String oldColumn, + final @NotNull String newColumn, + final @NotNull String definition); + + Alter change( + final @NotNull String oldColumn, + final @NotNull String newColumn, + final @NotNull ColumnDefinition definition); + + Alter change( + final @NotNull String oldColumn, + final @NotNull String newColumn, + final @NotNull ColumnDefinition... definitions); + Alter rename(final @NotNull String oldColumn, final @NotNull String newColumn); } diff --git a/src/dev/shiza/bulbasaur/alter/AlterQuery.java b/src/dev/shiza/bulbasaur/alter/AlterQuery.java index 7365850..8ac1ff3 100644 --- a/src/dev/shiza/bulbasaur/alter/AlterQuery.java +++ b/src/dev/shiza/bulbasaur/alter/AlterQuery.java @@ -34,6 +34,53 @@ public Alter add(final @NotNull String column, final @NotNull ColumnDefinition.. Arrays.stream(definitions).reduce(ColumnDefinitions.empty(), ColumnDefinition::and)); } + @Override + public Alter modify(final @NotNull String column, final @NotNull String definition) { + this.operation = new Modifying(column, definition); + return this; + } + + @Override + public Alter modify(final @NotNull String column, final @NotNull ColumnDefinition definition) { + return modify(column, definition.generate()); + } + + @Override + public Alter modify( + final @NotNull String column, final @NotNull ColumnDefinition... definitions) { + return modify( + column, + Arrays.stream(definitions).reduce(ColumnDefinitions.empty(), ColumnDefinition::and)); + } + + @Override + public Alter change( + final @NotNull String oldColumn, + final @NotNull String newColumn, + final @NotNull String definition) { + this.operation = new Changing(oldColumn, newColumn, definition); + return this; + } + + @Override + public Alter change( + final @NotNull String oldColumn, + final @NotNull String newColumn, + final @NotNull ColumnDefinition definition) { + return change(oldColumn, newColumn, definition.generate()); + } + + @Override + public Alter change( + final @NotNull String oldColumn, + final @NotNull String newColumn, + final @NotNull ColumnDefinition... definitions) { + return change( + oldColumn, + newColumn, + Arrays.stream(definitions).reduce(ColumnDefinitions.empty(), ColumnDefinition::and)); + } + @Override public Alter drop(final @NotNull String column) { this.operation = new Dropping(column); @@ -75,6 +122,22 @@ public String query() { } } + record Modifying(String column, String definition) implements Query { + + @Override + public String query() { + return "MODIFY " + column + " " + definition; + } + } + + record Changing(String oldColumn, String newColumn, String definition) implements Query { + + @Override + public String query() { + return "CHANGE " + oldColumn + " " + newColumn + " " + definition; + } + } + record Renaming(String oldColumn, String newColumn) implements Query { @Override diff --git a/src/dev/shiza/bulbasaur/column/ColumnDefinitions.java b/src/dev/shiza/bulbasaur/column/ColumnDefinitions.java index a188cb2..06220ed 100644 --- a/src/dev/shiza/bulbasaur/column/ColumnDefinitions.java +++ b/src/dev/shiza/bulbasaur/column/ColumnDefinitions.java @@ -27,6 +27,10 @@ public static ColumnDefinition references( return () -> "REFERENCES " + referencedTable + " (" + referencedColumn + ")"; } + public static ColumnDefinition unique() { + return () -> "UNIQUE"; + } + public static ColumnDefinition defaults(final @NotNull Object defaultValue) { return () -> "DEFAULT '" + defaultValue + "'"; }