Skip to content
Merged
Changes from 1 commit
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,6 +17,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.Callable;
Expand Down Expand Up @@ -44,6 +45,8 @@
import com.oceanbase.tools.dbbrowser.editor.DBTableEditor;
import com.oceanbase.tools.dbbrowser.model.DBConstraintType;
import com.oceanbase.tools.dbbrowser.model.DBTable;
import com.oceanbase.tools.dbbrowser.model.DBTableColumn;
import com.oceanbase.tools.dbbrowser.model.datatype.DataTypeUtil;

import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -104,6 +107,10 @@ public Void call() {
dbTableService.getTables(connectionSession, schemaName).entrySet().stream()
.filter(entry -> allRealTableNames.contains(entry.getKey()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
if (connectionSession.getDialectType().isMysql()) {
tableName2Tables.values().stream().forEach(table -> quoteColumnDefaultValues(table));
}

} catch (Exception ex) {
log.warn("fetch table meta information failed, ex={}", ex);
comparingEntities.forEach(tableComparingEntity -> {
Expand Down Expand Up @@ -209,4 +216,29 @@ public GenerateTableDDLResp generateUpdateDDLWithoutRenaming(@NotNull Connection
.previousIdentity(TableIdentity.of(req.getPrevious().getSchemaName(), req.getPrevious().getName()))
.build();
}

private void quoteColumnDefaultValues(DBTable table) {
if (!CollectionUtils.isEmpty(table.getColumns())) {
table.getColumns().forEach(column -> {
String defaultValue = column.getDefaultValue();
if (StringUtils.isNotEmpty(defaultValue)) {
if (!isDefaultValueBuiltInFunction(column)) {
column.setDefaultValue("'".concat(defaultValue.replace("'", "''")).concat("'"));
}
}
});
}
}

/**
* 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 com.oceanbase.tools.dbbrowser.util.StringUtils.isEmpty(column.getDefaultValue())
|| (!DataTypeUtil.isStringType(column.getTypeName())
&& column.getDefaultValue().trim().toUpperCase(Locale.getDefault())
.startsWith("CURRENT_TIMESTAMP"));
}
}