提交 f01e9a37 编写于 作者: S Serge Rider

#6745 Data type mapping fixed. Script execution logging fixed


Former-commit-id: dc156e33
上级 22f15831
......@@ -23,13 +23,9 @@ import org.jkiss.dbeaver.model.data.DBDAttributeBinding;
import org.jkiss.dbeaver.model.impl.DBObjectNameCaseTransformer;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.runtime.VoidProgressMonitor;
import org.jkiss.dbeaver.model.sql.SQLDataSource;
import org.jkiss.dbeaver.model.sql.SQLDialect;
import org.jkiss.dbeaver.model.struct.*;
import org.jkiss.utils.CommonUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
......@@ -196,71 +192,7 @@ public class DatabaseMappingAttribute implements DatabaseMappingObject {
// Current solution looks like hack
String typeName = source.getTypeName();
DBPDataKind dataKind = source.getDataKind();
if (targetDataSource instanceof DBPDataTypeProvider) {
DBPDataTypeProvider dataTypeProvider = (DBPDataTypeProvider) targetDataSource;
DBSDataType dataType = dataTypeProvider.getLocalDataType(typeName);
if (dataType == null && typeName.equals("DOUBLE")) {
dataType = dataTypeProvider.getLocalDataType("DOUBLE PRECISION");
if (dataType != null) {
typeName = dataType.getTypeName();
}
}
if (dataType != null && !DBPDataKind.canConsume(dataKind, dataType.getDataKind())) {
// Type mismatch
dataType = null;
}
if (dataType == null) {
// Type not supported by target database
// Let's try to find something similar
List<DBSDataType> possibleTypes = new ArrayList<>();
for (DBSDataType type : dataTypeProvider.getLocalDataTypes()) {
if (type.getDataKind() == dataKind) {
possibleTypes.add(type);
}
}
DBSDataType targetType = null;
if (!possibleTypes.isEmpty()) {
// Try to get any partial match
for (DBSDataType type : possibleTypes) {
if (type.getName().equalsIgnoreCase(typeName)) {
targetType = type;
break;
}
}
}
if (targetType == null) {
typeName = DBUtils.getDefaultDataTypeName(targetDataSource, dataKind);
if (!possibleTypes.isEmpty()) {
for (DBSDataType type : possibleTypes) {
if (type.getName().equalsIgnoreCase(typeName)) {
targetType = type;
break;
}
}
}
}
if (targetType == null && !possibleTypes.isEmpty()) {
targetType = possibleTypes.get(0);
}
if (targetType != null) {
typeName = targetType.getTypeName();
}
}
if (dataType != null) {
dataKind = dataType.getDataKind();
}
}
// Get type modifiers from target datasource
if (source != null && targetDataSource instanceof SQLDataSource) {
SQLDialect dialect = ((SQLDataSource) targetDataSource).getSQLDialect();
String modifiers = dialect.getColumnTypeModifiers(targetDataSource, source, typeName, dataKind);
if (modifiers != null) {
typeName += modifiers;
}
}
return typeName;
return DBStructUtils.mapTargetDataType(targetDataSource, source);
}
public void setTargetType(String targetType)
......
......@@ -38,8 +38,10 @@ import org.jkiss.dbeaver.model.navigator.DBNDataSource;
import org.jkiss.dbeaver.model.navigator.DBNDatabaseNode;
import org.jkiss.dbeaver.model.navigator.DBNModel;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.sql.SQLUtils;
import org.jkiss.dbeaver.model.struct.DBSEntity;
import org.jkiss.dbeaver.model.struct.DBSEntityAttribute;
import org.jkiss.dbeaver.model.struct.DBStructUtils;
import org.jkiss.dbeaver.model.virtual.DBVContainer;
import org.jkiss.dbeaver.model.virtual.DBVEntity;
import org.jkiss.dbeaver.model.virtual.DBVEntityForeignKey;
......@@ -326,8 +328,9 @@ class PostgreFDWConfigWizard extends BaseWizard implements DBPContextProvider {
PostgreTableColumn newColumn = columnManager.createNewObject(monitor, commandContext, pgTable, null, options);
assert newColumn != null;
newColumn.setName(attr.getName());
String defTypeName = database.getDefaultDataTypeName(attr.getDataKind());
PostgreDataType dataType = database.getDataType(monitor, defTypeName);
String defTypeName = DBStructUtils.mapTargetDataType(database.getDataSource(), attr);
String plainTargetTypeName = SQLUtils.stripColumnTypeModifiers(defTypeName);
PostgreDataType dataType = database.getDataType(monitor, plainTargetTypeName);
newColumn.setDataType(dataType);
}
......
......@@ -231,7 +231,7 @@ public class DBExecUtils {
}
String script = action.getScript();
if (!CommonUtils.isEmpty(script)) {
try (final Statement statement = ((JDBCSession) session).getOriginal().createStatement()) {
try (final Statement statement = ((JDBCSession) session).createStatement()) {
statement.execute(script);
}
}
......
......@@ -19,13 +19,14 @@ package org.jkiss.dbeaver.model.struct;
import org.jkiss.code.NotNull;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.DBPScriptObject;
import org.jkiss.dbeaver.model.DBPScriptObjectExt2;
import org.jkiss.dbeaver.model.*;
import org.jkiss.dbeaver.model.edit.DBEPersistAction;
import org.jkiss.dbeaver.model.edit.DBERegistry;
import org.jkiss.dbeaver.model.impl.sql.edit.SQLObjectEditor;
import org.jkiss.dbeaver.model.impl.sql.edit.struct.SQLTableManager;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.sql.SQLDataSource;
import org.jkiss.dbeaver.model.sql.SQLDialect;
import org.jkiss.dbeaver.model.sql.SQLUtils;
import org.jkiss.dbeaver.model.struct.rdb.DBSTable;
import org.jkiss.dbeaver.model.struct.rdb.DBSView;
......@@ -184,4 +185,92 @@ public final class DBStructUtils {
cyclicTables.addAll(realTables);
}
public static String mapTargetDataType(DBPDataSource targetDataSource, DBSTypedObject typedObject) {
String typeName = typedObject.getTypeName();
String typeNameLower = typeName.toLowerCase(Locale.ENGLISH);
DBPDataKind dataKind = typedObject.getDataKind();
if (targetDataSource instanceof DBPDataTypeProvider) {
DBPDataTypeProvider dataTypeProvider = (DBPDataTypeProvider) targetDataSource;
DBSDataType dataType = dataTypeProvider.getLocalDataType(typeName);
if (dataType == null && typeNameLower.equals("double")) {
dataType = dataTypeProvider.getLocalDataType("DOUBLE PRECISION");
if (dataType != null) {
typeName = dataType.getTypeName();
}
}
if (dataType != null && !DBPDataKind.canConsume(dataKind, dataType.getDataKind())) {
// Type mismatch
dataType = null;
}
if (dataType == null) {
// Type not supported by target database
// Let's try to find something similar
Map<String, DBSDataType> possibleTypes = new HashMap<>();
for (DBSDataType type : dataTypeProvider.getLocalDataTypes()) {
if (type.getDataKind() == dataKind) {
possibleTypes.put(type.getTypeName().toLowerCase(Locale.ENGLISH), type);
}
}
DBSDataType targetType = null;
if (!possibleTypes.isEmpty()) {
// Try to get any partial match
targetType = possibleTypes.get(typeNameLower);
if (targetType == null && dataKind == DBPDataKind.NUMERIC) {
// Try to find appropriate type with the same scale/precision
for (DBSDataType type : possibleTypes.values()) {
if (CommonUtils.equalObjects(type.getScale(), typedObject.getScale()) &&
CommonUtils.equalObjects(type.getPrecision(), typedObject.getPrecision()))
{
targetType = type;
break;
}
}
if (targetType == null) {
if (typeNameLower.contains("float")) {
for (String psn : possibleTypes.keySet()) {
if (psn.contains("float")) {
targetType = possibleTypes.get(psn);
break;
}
}
} else if (typeNameLower.contains("double")) {
for (String psn : possibleTypes.keySet()) {
if (psn.contains("double")) {
targetType = possibleTypes.get(psn);
break;
}
}
}
}
}
}
if (targetType == null) {
typeName = DBUtils.getDefaultDataTypeName(targetDataSource, dataKind);
typeNameLower = typeName.toLowerCase(Locale.ENGLISH);
if (!possibleTypes.isEmpty()) {
targetType = possibleTypes.get(typeNameLower);
}
}
if (targetType == null && !possibleTypes.isEmpty()) {
targetType = possibleTypes.values().iterator().next();
}
if (targetType != null) {
typeName = targetType.getTypeName();
}
}
if (dataType != null) {
dataKind = dataType.getDataKind();
}
}
// Get type modifiers from target datasource
if (targetDataSource instanceof SQLDataSource) {
SQLDialect dialect = ((SQLDataSource) targetDataSource).getSQLDialect();
String modifiers = dialect.getColumnTypeModifiers(targetDataSource, typedObject, typeName, dataKind);
if (modifiers != null) {
typeName += modifiers;
}
}
return typeName;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册