Unverified Commit 8f8c08a2 authored by Zzm0809's avatar Zzm0809 Committed by GitHub

[Optimization]optimizing mysql sql generation logic (#3463)

parent eeaaa70c
......@@ -37,6 +37,8 @@ import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
/**
......@@ -106,14 +108,19 @@ public class MySqlDriver extends AbstractJdbcDriver {
} else if (null != column.getLength()) {
unit = String.format("(%s)", column.getLength());
}
// Avoid parsing mismatches when the numeric data type column declared by UNSIGNED/ZEROFILL keyword
String columnType = column.getType();
final String dv = column.getDefaultValue();
// If it defaults to a numeric type, there is no need to include single quotes or a bit type
String defaultValueTag = " DEFAULT '%s'";
if (NumberUtil.isNumber(dv) || columnType.startsWith("bit")) {
defaultValueTag = " DEFAULT %s";
}
String defaultValue = Asserts.isNotNull(dv)
? String.format(" DEFAULT '%s'", dv.isEmpty() ? "''" : dv)
? String.format(defaultValueTag, StrUtil.isEmpty(dv) ? "''" : dv)
: String.format("%s NULL ", !column.isNullable() ? " NOT " : "");
// Avoid parsing mismatches when the numeric data type column declared by UNSIGNED/ZEROFILL keyword
String columnType = column.getType();
if (columnType.contains("unsigned") || columnType.contains("zerofill")) {
String[] arr = columnType.split(" ");
arr[0] = arr[0].concat(unit);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment