Skip to content

Commit

Permalink
feat-DataType-优化Column支持DataType
Browse files Browse the repository at this point in the history
  • Loading branch information
aruis committed Nov 8, 2024
1 parent 67cd088 commit 8e014d8
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.ximatai.muyun.core.security.AbstractEncryptor;
import net.ximatai.muyun.database.builder.Column;
import net.ximatai.muyun.database.builder.DataType;

import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -34,7 +35,7 @@ default List<Column> getSignColumns() {
return getColumnsForSigning().stream()
.map(this::column2SignColumn)
.map(Column::of)
.map(c -> c.setType("varchar"))
.map(c -> c.setType(DataType.VARCHAR))
.toList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,19 @@ public class Column {
private boolean sequence = false;
private boolean indexed = false;

public enum Type {
VARCHAR, INT, BOOL
}
IColumnTypeTransform columnTypeTransform = IColumnTypeTransform.POSTGRES;

public static final Column ID_POSTGRES = new Column("id")
.setPrimaryKey()
.setType(Type.VARCHAR)
.setType(DataType.VARCHAR)
.setDefaultValue("gen_random_uuid()");

public static final Column DELETE_FLAG = new Column("b_delete")
.setType(Type.BOOL)
.setType(DataType.BOOLEAN)
.setDefaultValue(false);

public static final Column TREE_PID = new Column("pid")
.setType(Type.VARCHAR)
.setType(DataType.VARCHAR)
.setIndexed();

public static final Column ORDER = new Column("n_order")
Expand Down Expand Up @@ -55,12 +53,7 @@ public Column setType(String type) {
}

public Column setType(DataType type) {
this.type = type.toString();
return this;
}

public Column setType(Type type) {
this.type = type.toString();
this.type = type.getType(columnTypeTransform);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package net.ximatai.muyun.database.builder;

public enum DataType {
VARCHAR("varchar"),
INT("int"),
BOOLEAN("boolean"),
TIMESTAMP("timestamp"),
DATE("date"),
NUMERIC("numeric"),
JSON("jsonb"),
VARCHAR_ARRAY("varchar[]");
VARCHAR,
INT,
BOOLEAN,
TIMESTAMP,
DATE,
NUMERIC,
JSON,
VARCHAR_ARRAY;

private final String type;

DataType(String type) {
this.type = type;
String getType(IColumnTypeTransform transform) {
return transform.transform(this);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package net.ximatai.muyun.database.builder;

public interface IColumnTypeTransform {

IColumnTypeTransform POSTGRES = type -> switch (type) {
case VARCHAR_ARRAY -> "varchar[]";
case JSON -> "jsonb";
default -> type.name();
};

IColumnTypeTransform MYSQL = type -> switch (type) {
case VARCHAR_ARRAY -> "varchar";
case JSON -> "json";
default -> type.name();
};

String transform(DataType type);

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public TableWrapper setInherit(TableBase inherit) {
}

public TableWrapper setPrimaryKey(String name) {
primaryKey = Column.of(name).setPrimaryKey().setType("varchar").setNullable(false);
primaryKey = Column.of(name).setPrimaryKey().setType(DataType.VARCHAR).setNullable(false);
return this;
}

Expand Down

0 comments on commit 8e014d8

Please sign in to comment.