From e7e314b0c2abdfebc3c60b6535fd3b22c55ce875 Mon Sep 17 00:00:00 2001 From: shitzuu Date: Sun, 1 Sep 2024 22:48:36 +0200 Subject: [PATCH] Add support for use of modifying and changing in alter queries --- src/dev/shiza/bulbasaur/alter/Alter.java | 21 +++++++ src/dev/shiza/bulbasaur/alter/AlterQuery.java | 63 +++++++++++++++++++ .../bulbasaur/column/ColumnDefinitions.java | 4 ++ 3 files changed, 88 insertions(+) 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 + "'"; }