Skip to content

Commit

Permalink
Add support for use of modifying and changing in alter queries
Browse files Browse the repository at this point in the history
  • Loading branch information
shitzuu committed Sep 1, 2024
1 parent f56150e commit e7e314b
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/dev/shiza/bulbasaur/alter/Alter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
63 changes: 63 additions & 0 deletions src/dev/shiza/bulbasaur/alter/AlterQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/dev/shiza/bulbasaur/column/ColumnDefinitions.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 + "'";
}
Expand Down

0 comments on commit e7e314b

Please sign in to comment.