提交 94e3e885 编写于 作者: S Serge Rider

Column modifiers gen fix (dialect-specific)

上级 72acd155
......@@ -218,7 +218,7 @@ class DatabaseMappingAttribute implements DatabaseMappingObject {
}
}
String modifiers = SQLUtils.getColumnTypeModifiers(source, typeName, dataKind);
String modifiers = SQLUtils.getColumnTypeModifiers(parent.getSettings().getTargetDataSource(this), source, typeName, dataKind);
if (modifiers != null) {
typeName += modifiers;
}
......
......@@ -1149,8 +1149,10 @@ public final class DBUtils {
@NotNull
public static String getFullTypeName(@NotNull DBSTypedObject typedObject)
{
DBSObject structObject = getFromObject(typedObject);
DBPDataSource dataSource = structObject == null ? null : structObject.getDataSource();
String typeName = typedObject.getTypeName();
String typeModifiers = SQLUtils.getColumnTypeModifiers(typedObject, typeName, typedObject.getDataKind());
String typeModifiers = SQLUtils.getColumnTypeModifiers(dataSource, typedObject, typeName, typedObject.getDataKind());
return typeModifiers == null ? typeName : (typeName + CommonUtils.notEmpty(typeModifiers));
}
......
......@@ -18,18 +18,16 @@ package org.jkiss.dbeaver.model.impl.sql;
import org.jkiss.code.NotNull;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.model.DBPDataSource;
import org.jkiss.dbeaver.model.DBPIdentifierCase;
import org.jkiss.dbeaver.model.DBPKeywordType;
import org.jkiss.dbeaver.model.*;
import org.jkiss.dbeaver.model.data.DBDBinaryFormatter;
import org.jkiss.dbeaver.model.data.DBDDataFilter;
import org.jkiss.dbeaver.model.impl.data.formatters.BinaryFormatterHexNative;
import org.jkiss.dbeaver.model.sql.SQLConstants;
import org.jkiss.dbeaver.model.sql.SQLDialect;
import org.jkiss.dbeaver.model.sql.SQLStateType;
import org.jkiss.dbeaver.model.sql.SQLUtils;
import org.jkiss.dbeaver.model.sql.*;
import org.jkiss.dbeaver.model.sql.parser.SQLSemanticProcessor;
import org.jkiss.dbeaver.model.struct.DBSAttributeBase;
import org.jkiss.dbeaver.model.struct.DBSDataType;
import org.jkiss.dbeaver.model.struct.DBSObject;
import org.jkiss.dbeaver.model.struct.DBSTypedObject;
import org.jkiss.utils.ArrayUtils;
import org.jkiss.utils.CommonUtils;
import org.jkiss.utils.Pair;
......@@ -464,4 +462,57 @@ public class BasicSQLDialect implements SQLDialect {
}
}
@Override
public String getColumnTypeModifiers(@NotNull DBSTypedObject column, @NotNull String typeName, @NotNull DBPDataKind dataKind) {
typeName = CommonUtils.notEmpty(typeName).toUpperCase(Locale.ENGLISH);
if (column instanceof DBSObject) {
// If type is UDT (i.e. we can find it in type list) and type precision == column precision
// then do not use explicit precision in column definition
final DBSDataType dataType = DBUtils.getLocalDataType(((DBSObject) column).getDataSource(), column.getTypeName());
if (dataType != null && dataType.getScale() == column.getScale() &&
((dataType.getPrecision() > 0 && dataType.getPrecision() == column.getPrecision()) ||
(dataType.getMaxLength() > 0 && dataType.getMaxLength() == column.getMaxLength())))
{
return null;
}
}
if (dataKind == DBPDataKind.STRING) {
if (typeName.indexOf('(') == -1) {
final long maxLength = column.getMaxLength();
if (maxLength > 0) {
return "(" + maxLength + ")";
}
}
} else if (dataKind == DBPDataKind.CONTENT && !typeName.contains("LOB")) {
final long maxLength = column.getMaxLength();
if (maxLength > 0) {
return "(" + maxLength + ')';
}
} else if (dataKind == DBPDataKind.NUMERIC) {
if (typeName.equals("DECIMAL") || typeName.equals("NUMERIC") || typeName.equals("NUMBER")) {
int scale = column.getScale();
int precision = column.getPrecision();
if (precision == 0) {
precision = (int) column.getMaxLength();
if (precision > 0) {
// FIXME: max length is actually length in character.
// FIXME: On Oracle it returns bigger values than maximum (#1767)
// FIXME: in other DBs it equals to precision in most cases
//precision--; // One character for sign?
}
}
if (scale >= 0 && precision >= 0 && !(scale == 0 && precision == 0)) {
return "(" + precision + ',' + scale + ')';
}
} else if (typeName.equals("BIT")) {
// Bit string?
int precision = column.getPrecision();
if (precision > 1) {
return "(" + precision + ')';
}
}
}
return null;
}
}
......@@ -60,7 +60,7 @@ public abstract class SQLTableColumnManager<OBJECT_TYPE extends JDBCTableColumn<
} else {
dataKind = dataType.getDataKind();
}
String modifiers = SQLUtils.getColumnTypeModifiers(column, typeName, dataKind);
String modifiers = SQLUtils.getColumnTypeModifiers(column.getDataSource(), column, typeName, dataKind);
if (modifiers != null) {
sql.append(modifiers);
}
......
......@@ -18,12 +18,14 @@ package org.jkiss.dbeaver.model.sql;
import org.jkiss.code.NotNull;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.model.DBPDataKind;
import org.jkiss.dbeaver.model.DBPDataSource;
import org.jkiss.dbeaver.model.DBPIdentifierCase;
import org.jkiss.dbeaver.model.DBPKeywordType;
import org.jkiss.dbeaver.model.data.DBDBinaryFormatter;
import org.jkiss.dbeaver.model.data.DBDDataFilter;
import org.jkiss.dbeaver.model.struct.DBSAttributeBase;
import org.jkiss.dbeaver.model.struct.DBSTypedObject;
import org.jkiss.utils.Pair;
import java.util.List;
......@@ -289,4 +291,7 @@ public interface SQLDialect {
boolean isTransactionModifyingQuery(String queryString);
@Nullable
String getColumnTypeModifiers(@NotNull DBSTypedObject column, @NotNull String typeName, @NotNull DBPDataKind dataKind);
}
......@@ -561,59 +561,15 @@ public final class SQLUtils {
}
}
public static String getColumnTypeModifiers(DBSTypedObject column, @NotNull String typeName, @NotNull DBPDataKind dataKind) {
public static String getColumnTypeModifiers(@Nullable DBPDataSource dataSource, DBSTypedObject column, @NotNull String typeName, @NotNull DBPDataKind dataKind) {
if (column == null) {
return null;
}
typeName = CommonUtils.notEmpty(typeName).toUpperCase(Locale.ENGLISH);
if (column instanceof DBSObject) {
// If type is UDT (i.e. we can find it in type list) and type precision == column precision
// then do not use explicit precision in column definition
final DBSDataType dataType = DBUtils.getLocalDataType(((DBSObject) column).getDataSource(), column.getTypeName());
if (dataType != null && dataType.getScale() == column.getScale() &&
((dataType.getPrecision() > 0 && dataType.getPrecision() == column.getPrecision()) ||
(dataType.getMaxLength() > 0 && dataType.getMaxLength() == column.getMaxLength())))
{
return null;
}
}
if (dataKind == DBPDataKind.STRING) {
if (typeName.indexOf('(') == -1) {
final long maxLength = column.getMaxLength();
if (maxLength > 0) {
return "(" + maxLength + ")";
}
}
} else if (dataKind == DBPDataKind.CONTENT && !typeName.contains("LOB")) {
final long maxLength = column.getMaxLength();
if (maxLength > 0) {
return "(" + maxLength + ')';
}
} else if (dataKind == DBPDataKind.NUMERIC) {
if (typeName.equals("DECIMAL") || typeName.equals("NUMERIC") || typeName.equals("NUMBER")) {
int scale = column.getScale();
int precision = column.getPrecision();
if (precision == 0) {
precision = (int) column.getMaxLength();
if (precision > 0) {
// FIXME: max length is actually length in character.
// FIXME: On Oracle it returns bigger values than maximum (#1767)
// FIXME: in other DBs it equals to precision in most cases
//precision--; // One character for sign?
}
}
if (scale >= 0 && precision >= 0 && !(scale == 0 && precision == 0)) {
return "(" + precision + ',' + scale + ')';
}
} else if (typeName.equals("BIT")) {
// Bit string?
int precision = column.getPrecision();
if (precision > 1) {
return "(" + precision + ')';
}
}
if (!(dataSource instanceof SQLDataSource)) {
return null;
}
return null;
SQLDialect dialect = ((SQLDataSource) dataSource).getSQLDialect();
return dialect.getColumnTypeModifiers(column, typeName, dataKind);
}
public static boolean isExecQuery(@NotNull SQLDialect dialect, String query) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册