Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Objects;

import javax.validation.constraints.NotNull;
Expand Down Expand Up @@ -125,31 +124,11 @@ protected class DefaultOptionModifier implements DBColumnModifier {
@Override
public void appendModifier(DBTableColumn column, SqlBuilder sqlBuilder) {
String defaultValue = column.getDefaultValue();
if (StringUtils.isNotEmpty(defaultValue) && (Objects.isNull(column.getVirtual()) || !column.getVirtual())) {
sqlBuilder.append(" DEFAULT ");
if (isDefaultValueBuiltInFunction(column)) {
// 默认值是 内置函数,则不需要用引号转义,直接拼接即可
sqlBuilder.append(defaultValue);
} else {
/**
* 不是内置函数作为默认值的话,都可以用单引号包围,不是字符串类型的,也会被转义成对应的类型值。比如: col1 int(11) DEFAULT '2' 会被内核自动转换为 col1 int(11)
* DEFAULT 2
*/
sqlBuilder.append("'").append(defaultValue.replace("'", "''")).append("'");
if ((Objects.isNull(column.getVirtual()) || !column.getVirtual())) {
if (StringUtils.isNotEmpty(defaultValue)) {
sqlBuilder.append(" DEFAULT " + defaultValue);
}
}
}
}

/**
* Check whether the data_default contain built in function. Any of the synonyms for
* CURRENT_TIMESTAMP have the same meaning as CURRENT_TIMESTAMP. These are CURRENT_TIMESTAMP(),
* NOW(), LOCALTIME, LOCALTIME(), LOCALTIMESTAMP, and LOCALTIMESTAMP().
*/
private boolean isDefaultValueBuiltInFunction(DBTableColumn column) {
return StringUtils.isEmpty(column.getDefaultValue())
|| (!DataTypeUtil.isStringType(column.getTypeName())
&& column.getDefaultValue().trim().toUpperCase(Locale.getDefault())
.startsWith("CURRENT_TIMESTAMP"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public void setUp() {
public void generateCreateObjectDDL() {
String ddl = tableEditor.generateCreateObjectDDL(DBObjectUtilsTest.getNewTable());
Assert.assertEquals("CREATE TABLE `whatever_schema`.`whatever_table` (\n"
+ "`COL1` NUMBER(2, 1) DEFAULT '1' NOT NULL,\n"
+ "`OLD_COL1` NUMBER(2, 1) DEFAULT '1' NOT NULL,\n"
+ "`COL1` NUMBER(2, 1) DEFAULT 1 NOT NULL,\n"
+ "`OLD_COL1` NUMBER(2, 1) DEFAULT 1 NOT NULL,\n"
+ "INDEX `new_index` USING HASH (`col1`, `col2`) GLOBAL COMMENT 'whatever_comment',\n"
+ "INDEX `old_index` USING BTREE (`col1`, `col2`) GLOBAL COMMENT 'whatever_comment',\n"
+ "CONSTRAINT `old_constraint` FOREIGN KEY (`col1`, `col2`) REFERENCES `ref_schema`.`ref_table` (`ref_col1`, `ref_col2`) "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
"precision": 120,
"maxLength": 120,
"nullable": false,
"defaultValue": "default value",
"defaultValue": "'default value'",
"virtual": false,
"comment": null,
"charsetName": "utf8mb4",
Expand Down Expand Up @@ -173,7 +173,7 @@
"name": "a",
"typeName": "enum",
"nullable": false,
"defaultValue": "\"red\"",
"defaultValue": "'\"red\"'",
"virtual": false,
"comment": null,
"charsetName": "utf8mb4",
Expand Down