use of com.alibaba.druid.sql.ast.statement.SQLColumnConstraint in project canal by alibaba.
the class MemoryTableMeta method processTableElement.
private void processTableElement(SQLTableElement element, TableMeta tableMeta) {
if (element instanceof SQLColumnDefinition) {
FieldMeta fieldMeta = new FieldMeta();
SQLColumnDefinition column = (SQLColumnDefinition) element;
String name = getSqlName(column.getName());
// String charset = getSqlName(column.getCharsetExpr());
SQLDataType dataType = column.getDataType();
String dataTypStr = dataType.getName();
if (StringUtils.equalsIgnoreCase(dataTypStr, "float")) {
if (dataType.getArguments().size() == 1) {
int num = Integer.valueOf(dataType.getArguments().get(0).toString());
if (num > 24) {
dataTypStr = "double";
}
}
}
if (dataType.getArguments().size() > 0) {
dataTypStr += "(";
for (int i = 0; i < column.getDataType().getArguments().size(); i++) {
if (i != 0) {
dataTypStr += ",";
}
SQLExpr arg = column.getDataType().getArguments().get(i);
dataTypStr += arg.toString();
}
dataTypStr += ")";
}
if (dataType instanceof SQLDataTypeImpl) {
SQLDataTypeImpl dataTypeImpl = (SQLDataTypeImpl) dataType;
if (dataTypeImpl.isUnsigned()) {
dataTypStr += " unsigned";
}
if (dataTypeImpl.isZerofill()) {
// 如果设置了zerofill,自动给列添加unsigned属性
if (!dataTypeImpl.isUnsigned()) {
dataTypStr += " unsigned";
}
dataTypStr += " zerofill";
}
}
if (column.getDefaultExpr() == null || column.getDefaultExpr() instanceof SQLNullExpr) {
fieldMeta.setDefaultValue(null);
} else {
fieldMeta.setDefaultValue(DruidDdlParser.unescapeQuotaName(getSqlName(column.getDefaultExpr())));
}
fieldMeta.setColumnName(name);
fieldMeta.setColumnType(dataTypStr);
fieldMeta.setNullable(true);
List<SQLColumnConstraint> constraints = column.getConstraints();
for (SQLColumnConstraint constraint : constraints) {
if (constraint instanceof SQLNotNullConstraint) {
fieldMeta.setNullable(false);
} else if (constraint instanceof SQLNullConstraint) {
fieldMeta.setNullable(true);
} else if (constraint instanceof SQLColumnPrimaryKey) {
fieldMeta.setKey(true);
fieldMeta.setNullable(false);
} else if (constraint instanceof SQLColumnUniqueKey) {
fieldMeta.setUnique(true);
}
}
tableMeta.addFieldMeta(fieldMeta);
} else if (element instanceof MySqlPrimaryKey) {
MySqlPrimaryKey column = (MySqlPrimaryKey) element;
List<SQLSelectOrderByItem> pks = column.getColumns();
for (SQLSelectOrderByItem pk : pks) {
String name = getSqlName(pk.getExpr());
FieldMeta field = tableMeta.getFieldMetaByName(name);
field.setKey(true);
field.setNullable(false);
}
} else if (element instanceof MySqlUnique) {
MySqlUnique column = (MySqlUnique) element;
List<SQLSelectOrderByItem> uks = column.getColumns();
for (SQLSelectOrderByItem uk : uks) {
String name = getSqlName(uk.getExpr());
FieldMeta field = tableMeta.getFieldMetaByName(name);
field.setUnique(true);
}
}
}
Aggregations