first commit
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
package com.andrewkydev.database.schema;
|
||||
|
||||
public final class ColumnSpec {
|
||||
private final String name;
|
||||
private final String type;
|
||||
private final boolean nullable;
|
||||
private final String defaultValue;
|
||||
private final boolean autoIncrement;
|
||||
private final boolean primaryKey;
|
||||
|
||||
private ColumnSpec(Builder builder) {
|
||||
this.name = builder.name;
|
||||
this.type = builder.type;
|
||||
this.nullable = builder.nullable;
|
||||
this.defaultValue = builder.defaultValue;
|
||||
this.autoIncrement = builder.autoIncrement;
|
||||
this.primaryKey = builder.primaryKey;
|
||||
}
|
||||
|
||||
public static Builder builder(String name, String type) {
|
||||
return new Builder(name, type);
|
||||
}
|
||||
|
||||
public String name() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String type() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public boolean nullable() {
|
||||
return nullable;
|
||||
}
|
||||
|
||||
public String defaultValue() {
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public boolean autoIncrement() {
|
||||
return autoIncrement;
|
||||
}
|
||||
|
||||
public boolean primaryKey() {
|
||||
return primaryKey;
|
||||
}
|
||||
|
||||
public static final class Builder {
|
||||
private final String name;
|
||||
private final String type;
|
||||
private boolean nullable = true;
|
||||
private String defaultValue;
|
||||
private boolean autoIncrement;
|
||||
private boolean primaryKey;
|
||||
|
||||
private Builder(String name, String type) {
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public Builder nullable(boolean nullable) {
|
||||
this.nullable = nullable;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder defaultValue(String defaultValue) {
|
||||
this.defaultValue = defaultValue;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder autoIncrement(boolean autoIncrement) {
|
||||
this.autoIncrement = autoIncrement;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder primaryKey(boolean primaryKey) {
|
||||
this.primaryKey = primaryKey;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ColumnSpec build() {
|
||||
return new ColumnSpec(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
29
src/main/java/com/andrewkydev/database/schema/IndexSpec.java
Normal file
29
src/main/java/com/andrewkydev/database/schema/IndexSpec.java
Normal file
@@ -0,0 +1,29 @@
|
||||
package com.andrewkydev.database.schema;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public final class IndexSpec {
|
||||
private final String name;
|
||||
private final List<String> columns;
|
||||
private final boolean unique;
|
||||
|
||||
public IndexSpec(String name, List<String> columns, boolean unique) {
|
||||
this.name = name;
|
||||
this.columns = Collections.unmodifiableList(new ArrayList<>(columns));
|
||||
this.unique = unique;
|
||||
}
|
||||
|
||||
public String name() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public List<String> columns() {
|
||||
return columns;
|
||||
}
|
||||
|
||||
public boolean unique() {
|
||||
return unique;
|
||||
}
|
||||
}
|
||||
42
src/main/java/com/andrewkydev/database/schema/Schema.java
Normal file
42
src/main/java/com/andrewkydev/database/schema/Schema.java
Normal file
@@ -0,0 +1,42 @@
|
||||
package com.andrewkydev.database.schema;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public interface Schema {
|
||||
|
||||
void createDatabase(String name);
|
||||
|
||||
void dropDatabase(String name);
|
||||
|
||||
void createTable(TableSpec spec);
|
||||
|
||||
void dropTable(String table);
|
||||
|
||||
void addColumn(String table, ColumnSpec column);
|
||||
|
||||
void updateColumn(String table, ColumnSpec column);
|
||||
|
||||
void dropColumn(String table, String column);
|
||||
|
||||
void addIndex(String table, IndexSpec index);
|
||||
|
||||
void dropIndex(String table, String indexName);
|
||||
|
||||
CompletableFuture<Void> createDatabaseAsync(String name);
|
||||
|
||||
CompletableFuture<Void> dropDatabaseAsync(String name);
|
||||
|
||||
CompletableFuture<Void> createTableAsync(TableSpec spec);
|
||||
|
||||
CompletableFuture<Void> dropTableAsync(String table);
|
||||
|
||||
CompletableFuture<Void> addColumnAsync(String table, ColumnSpec column);
|
||||
|
||||
CompletableFuture<Void> updateColumnAsync(String table, ColumnSpec column);
|
||||
|
||||
CompletableFuture<Void> dropColumnAsync(String table, String column);
|
||||
|
||||
CompletableFuture<Void> addIndexAsync(String table, IndexSpec index);
|
||||
|
||||
CompletableFuture<Void> dropIndexAsync(String table, String indexName);
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.andrewkydev.database.schema;
|
||||
|
||||
public enum SqlDialect {
|
||||
MYSQL,
|
||||
POSTGRESQL
|
||||
}
|
||||
80
src/main/java/com/andrewkydev/database/schema/TableSpec.java
Normal file
80
src/main/java/com/andrewkydev/database/schema/TableSpec.java
Normal file
@@ -0,0 +1,80 @@
|
||||
package com.andrewkydev.database.schema;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public final class TableSpec {
|
||||
private final String name;
|
||||
private final List<ColumnSpec> columns;
|
||||
private final List<String> primaryKey;
|
||||
private final List<IndexSpec> indexes;
|
||||
|
||||
private TableSpec(Builder builder) {
|
||||
this.name = builder.name;
|
||||
this.columns = Collections.unmodifiableList(new ArrayList<>(builder.columns));
|
||||
this.primaryKey = Collections.unmodifiableList(new ArrayList<>(builder.primaryKey));
|
||||
this.indexes = Collections.unmodifiableList(new ArrayList<>(builder.indexes));
|
||||
}
|
||||
|
||||
public static Builder builder(String name) {
|
||||
return new Builder(name);
|
||||
}
|
||||
|
||||
public String name() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public List<ColumnSpec> columns() {
|
||||
return columns;
|
||||
}
|
||||
|
||||
public List<String> primaryKey() {
|
||||
return primaryKey;
|
||||
}
|
||||
|
||||
public List<IndexSpec> indexes() {
|
||||
return indexes;
|
||||
}
|
||||
|
||||
public static final class Builder {
|
||||
private final String name;
|
||||
private final List<ColumnSpec> columns = new ArrayList<>();
|
||||
private final List<String> primaryKey = new ArrayList<>();
|
||||
private final List<IndexSpec> indexes = new ArrayList<>();
|
||||
|
||||
private Builder(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Builder column(ColumnSpec column) {
|
||||
this.columns.add(column);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder columns(List<ColumnSpec> columns) {
|
||||
this.columns.addAll(columns);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder primaryKey(List<String> columns) {
|
||||
this.primaryKey.clear();
|
||||
this.primaryKey.addAll(columns);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder index(IndexSpec index) {
|
||||
this.indexes.add(index);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder indexes(List<IndexSpec> indexes) {
|
||||
this.indexes.addAll(indexes);
|
||||
return this;
|
||||
}
|
||||
|
||||
public TableSpec build() {
|
||||
return new TableSpec(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user