Search in sources :

Example 16 with DBSDataType

use of org.jkiss.dbeaver.model.struct.DBSDataType in project dbeaver by dbeaver.

the class PostgreValueParser method prepareToParseArray.

private static Object prepareToParseArray(DBCSession session, DBSTypedObject arrayType, String string) throws DBCException {
    DBSDataType arrayDataType = arrayType instanceof DBSDataType ? (DBSDataType) arrayType : ((DBSTypedObjectEx) arrayType).getDataType();
    try {
        DBSDataType componentType = arrayDataType.getComponentType(session.getProgressMonitor());
        if (componentType == null) {
            log.error("Can't get component type from array '" + arrayType.getFullTypeName() + "'");
            return string;
        } else {
            if (componentType instanceof PostgreDataType) {
                checkAmountOfBrackets(string);
                List<Object> itemStrings = parseArrayString(string, ",");
                return startTransformListOfValuesIntoArray(session, (PostgreDataType) componentType, itemStrings);
            } else {
                log.error("Incorrect type '" + arrayType.getFullTypeName() + "'");
                return string;
            }
        }
    } catch (Exception e) {
        throw new DBCException("Error extracting array '" + arrayType.getFullTypeName() + "' items", e);
    }
}
Also used : PostgreDataType(org.jkiss.dbeaver.ext.postgresql.model.PostgreDataType) DBSDataType(org.jkiss.dbeaver.model.struct.DBSDataType) DBSTypedObject(org.jkiss.dbeaver.model.struct.DBSTypedObject) DBCException(org.jkiss.dbeaver.model.exec.DBCException) IOException(java.io.IOException) DBCException(org.jkiss.dbeaver.model.exec.DBCException)

Example 17 with DBSDataType

use of org.jkiss.dbeaver.model.struct.DBSDataType in project dbeaver by dbeaver.

the class MySQLTableColumn method loadInfo.

private void loadInfo(ResultSet dbResult) throws DBException {
    name = JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_COLUMN_NAME);
    ordinalPosition = JDBCUtils.safeGetInt(dbResult, MySQLConstants.COL_ORDINAL_POSITION);
    String typeName = JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_DATA_TYPE);
    assert typeName != null;
    String keyTypeName = JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_COLUMN_KEY);
    if (!CommonUtils.isEmpty(keyTypeName)) {
        try {
            keyType = KeyType.valueOf(keyTypeName);
        } catch (IllegalArgumentException e) {
            log.debug(e);
        }
    }
    setTypeName(typeName);
    setValueType(MySQLUtils.typeNameToValueType(typeName));
    DBSDataType dataType = getDataSource().getLocalDataType(typeName);
    this.charLength = JDBCUtils.safeGetLong(dbResult, MySQLConstants.COL_CHARACTER_MAXIMUM_LENGTH);
    if (this.charLength <= 0) {
        if (dataType != null) {
            setMaxLength(CommonUtils.toInt(dataType.getPrecision()));
        }
    } else {
        setMaxLength(this.charLength);
    }
    this.comment = JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_COLUMN_COMMENT);
    this.required = !"YES".equals(JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_IS_NULLABLE));
    this.setScale(JDBCUtils.safeGetInteger(dbResult, MySQLConstants.COL_NUMERIC_SCALE));
    this.setPrecision(JDBCUtils.safeGetInteger(dbResult, MySQLConstants.COL_NUMERIC_PRECISION));
    String defaultValue = JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_COLUMN_DEFAULT);
    if (defaultValue != null) {
        switch(getDataKind()) {
            case STRING:
                // Although I didn't reproduce that locally - perhaps depends on server config.
                if (!SQLConstants.NULL_VALUE.equals(defaultValue) && !SQLUtils.isStringQuoted(defaultValue)) {
                    defaultValue = SQLUtils.quoteString(getDataSource(), defaultValue);
                }
                break;
            case DATETIME:
                if (!defaultValue.isEmpty() && Character.isDigit(defaultValue.charAt(0))) {
                    defaultValue = "'" + defaultValue + "'";
                }
                break;
        }
        setDefaultValue(defaultValue);
    }
    this.collation = getDataSource().getCollation(JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_COLLATION_NAME));
    this.extraInfo = JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_COLUMN_EXTRA);
    this.autoGenerated = extraInfo != null && extraInfo.contains(MySQLConstants.EXTRA_AUTO_INCREMENT);
    this.fullTypeName = JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_COLUMN_TYPE);
    if (!CommonUtils.isEmpty(fullTypeName) && (isTypeEnum() || isTypeSet())) {
        enumValues = parseEnumValues(fullTypeName);
    }
    if (!getDataSource().isMariaDB() && getDataSource().isServerVersionAtLeast(5, 7)) {
        genExpression = JDBCUtils.safeGetString(dbResult, "GENERATION_EXPRESSION");
    }
    for (String modifier : CommonUtils.notEmpty(fullTypeName).toLowerCase().split(" ")) {
        switch(modifier) {
            case "zerofill":
                modifiers |= DBSTypedObject.TYPE_MOD_NUMBER_LEADING_ZEROES;
                break;
            case "unsigned":
                modifiers |= DBSTypedObject.TYPE_MOD_NUMBER_UNSIGNED;
                break;
        }
    }
}
Also used : DBSDataType(org.jkiss.dbeaver.model.struct.DBSDataType)

Example 18 with DBSDataType

use of org.jkiss.dbeaver.model.struct.DBSDataType in project dbeaver by dbeaver.

the class JDBCArrayValueHandler method createNewValueObject.

@Override
public Object createNewValueObject(@NotNull DBCSession session, @NotNull DBSTypedObject type) throws DBCException {
    DBSDataType dataType;
    if (type instanceof DBSDataType) {
        dataType = (DBSDataType) type;
    } else if (type instanceof DBSTypedObjectEx) {
        dataType = ((DBSTypedObjectEx) type).getDataType();
    } else {
        throw new DBCException("Can't determine array element data type: " + type.getFullTypeName());
    }
    try {
        DBSDataType componentType = dataType.getComponentType(session.getProgressMonitor());
        if (componentType == null) {
            throw new DBCException("Can't determine component data type from " + dataType.getFullTypeName());
        }
        Array array = ((JDBCSession) session).createArrayOf(componentType.getFullTypeName(), new Object[0]);
        return getValueFromObject(session, type, array, false, false);
    } catch (Exception e) {
        throw new DBCException("Error creating JDBC array " + type.getFullTypeName());
    }
}
Also used : Array(java.sql.Array) JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) DBSDataType(org.jkiss.dbeaver.model.struct.DBSDataType) DBSTypedObjectEx(org.jkiss.dbeaver.model.struct.DBSTypedObjectEx) DBCException(org.jkiss.dbeaver.model.exec.DBCException) DBCException(org.jkiss.dbeaver.model.exec.DBCException) SQLException(java.sql.SQLException)

Example 19 with DBSDataType

use of org.jkiss.dbeaver.model.struct.DBSDataType in project dbeaver by dbeaver.

the class JDBCStructValueHandler method getValueFromObject.

@Override
public Object getValueFromObject(@NotNull DBCSession session, @NotNull DBSTypedObject type, Object object, boolean copy, boolean validateValue) throws DBCException {
    if (object instanceof JDBCComposite) {
        return copy ? ((JDBCComposite) object).cloneValue(session.getProgressMonitor()) : object;
    }
    String typeName;
    try {
        if (object instanceof Struct) {
            typeName = ((Struct) object).getSQLTypeName();
        } else {
            typeName = type.getTypeName();
        }
    } catch (SQLException e) {
        throw new DBCException(e, session.getExecutionContext());
    }
    DBSDataType dataType = null;
    try {
        dataType = DBUtils.resolveDataType(session.getProgressMonitor(), session.getDataSource(), typeName);
    } catch (DBException e) {
        log.debug("Error resolving data type '" + typeName + "'", e);
    }
    if (dataType == null) {
        if (object instanceof Struct) {
            return new JDBCCompositeDynamic(session, (Struct) object, null);
        } else {
            return new JDBCCompositeUnknown(session, object);
        }
    }
    if (object == null) {
        return new JDBCCompositeStatic(session, dataType, new JDBCStructImpl(dataType.getTypeName(), null, ""));
    } else if (object instanceof Struct) {
        return new JDBCCompositeStatic(session, dataType, (Struct) object);
    } else {
        return new JDBCCompositeUnknown(session, object);
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) JDBCCompositeStatic(org.jkiss.dbeaver.model.impl.jdbc.data.JDBCCompositeStatic) SQLException(java.sql.SQLException) DBSDataType(org.jkiss.dbeaver.model.struct.DBSDataType) JDBCCompositeDynamic(org.jkiss.dbeaver.model.impl.jdbc.data.JDBCCompositeDynamic) JDBCStructImpl(org.jkiss.dbeaver.model.impl.jdbc.JDBCStructImpl) JDBCCompositeUnknown(org.jkiss.dbeaver.model.impl.jdbc.data.JDBCCompositeUnknown) DBCException(org.jkiss.dbeaver.model.exec.DBCException) JDBCComposite(org.jkiss.dbeaver.model.impl.jdbc.data.JDBCComposite) Struct(java.sql.Struct)

Example 20 with DBSDataType

use of org.jkiss.dbeaver.model.struct.DBSDataType in project dbeaver by dbeaver.

the class JDBCDataSource method getDefaultDataTypeName.

@Override
public String getDefaultDataTypeName(@NotNull DBPDataKind dataKind) {
    String typeName = getStandardSQLDataTypeName(dataKind);
    DBSDataType dataType = getLocalDataType(typeName);
    if (dataType == null) {
        // Try to find first data type of this kind
        for (DBSDataType type : getLocalDataTypes()) {
            if (type.getDataKind() == dataKind) {
                return type.getName();
            }
        }
    }
    return typeName;
}
Also used : DBSDataType(org.jkiss.dbeaver.model.struct.DBSDataType)

Aggregations

DBSDataType (org.jkiss.dbeaver.model.struct.DBSDataType)46 DBException (org.jkiss.dbeaver.DBException)10 DBCException (org.jkiss.dbeaver.model.exec.DBCException)8 DBSEntity (org.jkiss.dbeaver.model.struct.DBSEntity)8 DBSTypedObjectEx (org.jkiss.dbeaver.model.struct.DBSTypedObjectEx)8 ArrayList (java.util.ArrayList)7 SQLException (java.sql.SQLException)6 GridData (org.eclipse.swt.layout.GridData)6 Composite (org.eclipse.swt.widgets.Composite)6 DBPDataTypeProvider (org.jkiss.dbeaver.model.DBPDataTypeProvider)6 DBSEntityAttribute (org.jkiss.dbeaver.model.struct.DBSEntityAttribute)6 Text (org.eclipse.swt.widgets.Text)4 GenericTableColumn (org.jkiss.dbeaver.ext.generic.model.GenericTableColumn)4 OracleTableColumn (org.jkiss.dbeaver.ext.oracle.model.OracleTableColumn)4 PostgreDataType (org.jkiss.dbeaver.ext.postgresql.model.PostgreDataType)4 DBSTypedObject (org.jkiss.dbeaver.model.struct.DBSTypedObject)4 KeyAdapter (org.eclipse.swt.events.KeyAdapter)3 KeyEvent (org.eclipse.swt.events.KeyEvent)3 GridLayout (org.eclipse.swt.layout.GridLayout)3 TableItem (org.eclipse.swt.widgets.TableItem)3