package com.core.generators.domain;
|
|
import com.core.common.Cools;
|
import com.core.generators.CoolGenerator;
|
import com.core.generators.constant.SqlOsType;
|
import com.core.generators.utils.GeneratorUtils;
|
|
import java.sql.Connection;
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.regex.Matcher;
|
import java.util.regex.Pattern;
|
|
public class Column {
|
|
private String name;
|
private String type;
|
private String comment;
|
private String humpName;
|
private boolean primaryKey;
|
private boolean mainKey;
|
private boolean notNull;
|
private boolean major;
|
private boolean image;
|
private boolean checkBox;
|
private String foreignKey;
|
private String foreignKeyMajor;
|
private List<Map<String, Object>> enums;
|
private Integer length;
|
|
public Column(Connection connection, String name, String type, String comment, boolean primaryKey,
|
boolean mainKey, boolean notNull, Integer length, boolean withForeignKey, SqlOsType sqlOsType) {
|
this.name = name;
|
this.type = type;
|
this.comment = "";
|
if (!Cools.isEmpty(comment)) {
|
Pattern enumTitlePattern = Pattern.compile("(.+?)(?=\\{)");
|
Matcher enumTitleMatcher = enumTitlePattern.matcher(comment);
|
Pattern foreignTitlePattern = Pattern.compile("(.+?)(?=\\[)");
|
Matcher foreignTitleMatcher = foreignTitlePattern.matcher(comment);
|
if (enumTitleMatcher.find()) {
|
this.comment = enumTitleMatcher.group();
|
Pattern enumPattern = Pattern.compile("(?<=\\{)(.+?)(?=})");
|
Matcher enumMatcher = enumPattern.matcher(comment);
|
if (enumMatcher.find()) {
|
String enumText = enumMatcher.group();
|
if (!Cools.isEmpty(enumText)) {
|
String[] enumArr = enumText.split(",");
|
this.enums = new ArrayList<>();
|
for (String item : enumArr) {
|
Map<String, Object> map = new HashMap<>();
|
String[] pair = item.split(":");
|
if (pair.length >= 2) {
|
map.put(pair[0], pair[1]);
|
this.enums.add(map);
|
}
|
}
|
}
|
}
|
} else if (foreignTitleMatcher.find()) {
|
this.comment = foreignTitleMatcher.group();
|
Pattern foreignPattern = Pattern.compile("(?<=\\[)(.+?)(?=])");
|
Matcher foreignMatcher = foreignPattern.matcher(comment);
|
if (foreignMatcher.find()) {
|
String foreignTable = foreignMatcher.group();
|
if (!Cools.isEmpty(foreignTable)) {
|
this.foreignKey = GeneratorUtils.getNameSpace(foreignTable);
|
List<Column> foreignColumns = new ArrayList<>();
|
if (withForeignKey) {
|
try {
|
switch (sqlOsType) {
|
case MYSQL:
|
foreignColumns = CoolGenerator.getMysqlColumns(connection, foreignTable, false, sqlOsType);
|
break;
|
case SQL_SERVER:
|
foreignColumns = CoolGenerator.getSqlServerColumns(connection, foreignTable, false, sqlOsType);
|
break;
|
default:
|
break;
|
}
|
} catch (Exception ex) {
|
ex.printStackTrace();
|
}
|
}
|
if (!Cools.isEmpty(foreignColumns)) {
|
for (Column column : foreignColumns) {
|
if (column.isMajor()) {
|
this.foreignKeyMajor = GeneratorUtils.firstCharConvert(column.getHumpName(), false);
|
}
|
}
|
if (Cools.isEmpty(this.foreignKeyMajor)) {
|
this.foreignKeyMajor = "Id";
|
}
|
}
|
}
|
}
|
} else {
|
this.comment = comment;
|
}
|
if (comment.endsWith("(*)")) {
|
this.comment = comment.substring(0, comment.length() - 3);
|
this.major = true;
|
}
|
if (comment.endsWith("(img)")) {
|
this.comment = comment.substring(0, comment.length() - 5);
|
this.image = true;
|
}
|
if (comment.endsWith("(checkbox)")) {
|
this.comment = comment.substring(0, comment.length() - 10);
|
this.checkBox = true;
|
}
|
}
|
this.primaryKey = primaryKey;
|
this.mainKey = mainKey;
|
this.notNull = notNull;
|
this.length = length;
|
this.humpName = GeneratorUtils._convert(name);
|
}
|
|
public String getName() {
|
return name;
|
}
|
|
public void setName(String name) {
|
this.name = name;
|
}
|
|
public String getType() {
|
return type;
|
}
|
|
public void setType(String type) {
|
this.type = type;
|
}
|
|
public String getComment() {
|
return comment;
|
}
|
|
public String getWholeComment() {
|
if (!Cools.isEmpty(this.enums)) {
|
StringBuilder builder = new StringBuilder("");
|
for (Map<String, Object> map : this.enums) {
|
for (Map.Entry<String, Object> entry : map.entrySet()) {
|
builder.append(entry.getKey()).append(":").append(entry.getValue()).append(" ");
|
}
|
}
|
return new StringBuilder().append(this.comment).append(builder.toString()).toString();
|
}
|
return this.comment;
|
}
|
|
public void setComment(String comment) {
|
this.comment = comment;
|
}
|
|
public String getHumpName() {
|
return humpName;
|
}
|
|
public boolean isPrimaryKey() {
|
return primaryKey;
|
}
|
|
public boolean isOnly() {
|
return primaryKey;
|
}
|
|
public void setPrimaryKey(boolean primaryKey) {
|
this.primaryKey = primaryKey;
|
}
|
|
public boolean isNotNull() {
|
return notNull;
|
}
|
|
public void setNotNull(boolean notNull) {
|
this.notNull = notNull;
|
}
|
|
public String getForeignKey() {
|
return foreignKey;
|
}
|
|
public void setForeignKey(String foreignKey) {
|
this.foreignKey = foreignKey;
|
}
|
|
public String getForeignKeyMajor() {
|
return foreignKeyMajor;
|
}
|
|
public void setForeignKeyMajor(String foreignKeyMajor) {
|
this.foreignKeyMajor = foreignKeyMajor;
|
}
|
|
public List<Map<String, Object>> getEnums() {
|
return enums;
|
}
|
|
public void setEnums(List<Map<String, Object>> enums) {
|
this.enums = enums;
|
}
|
|
public Integer getLength() {
|
return length;
|
}
|
|
public void setLength(Integer length) {
|
this.length = length;
|
}
|
|
public boolean isMajor() {
|
return major;
|
}
|
|
public void setMajor(boolean major) {
|
this.major = major;
|
}
|
|
public boolean isImage() {
|
return image;
|
}
|
|
public void setImage(boolean image) {
|
this.image = image;
|
}
|
|
public boolean isCheckBox() {
|
return checkBox;
|
}
|
|
public void setCheckBox(boolean checkBox) {
|
this.checkBox = checkBox;
|
}
|
|
public boolean isMainKey() {
|
return mainKey;
|
}
|
|
public void setMainKey(boolean mainKey) {
|
this.mainKey = mainKey;
|
}
|
|
@Override
|
public String toString() {
|
return new StringBuilder()
|
.append("Column{name='").append(name).append('\'')
|
.append(", type='").append(type).append('\'')
|
.append(", comment='").append(comment).append('\'')
|
.append(", humpName='").append(humpName).append('\'')
|
.append(", primaryKey=").append(primaryKey)
|
.append(", notNull=").append(notNull)
|
.append(", major=").append(major)
|
.append(", image=").append(image)
|
.append(", foreignKey='").append(foreignKey).append('\'')
|
.append(", foreignKeyMajor='").append(foreignKeyMajor).append('\'')
|
.append(", enums=").append(enums)
|
.append(", length=").append(length)
|
.append('}')
|
.toString();
|
}
|
}
|