Search in sources :

Example 1 with JDBCDataType

use of org.jkiss.dbeaver.model.impl.jdbc.struct.JDBCDataType in project dbeaver by serge-rider.

the class JDBCCollection method makeCollectionFromResultSet.

@NotNull
private static JDBCCollection makeCollectionFromResultSet(@NotNull JDBCSession session, @NotNull Array array, @Nullable DBSDataType elementType) throws SQLException, DBException {
    ResultSet dbResult = array.getResultSet();
    if (dbResult == null) {
        throw new DBCException("JDBC array type was not resolved and result set was not provided by driver. Return NULL.");
    }
    DBDValueHandler valueHandler;
    if (elementType == null) {
        JDBCColumnMetaData itemMeta = new JDBCColumnMetaData(session.getDataSource(), dbResult.getMetaData(), 1);
        elementType = DBUtils.resolveDataType(session.getProgressMonitor(), session.getDataSource(), itemMeta.getTypeName());
        if (elementType == null) {
            elementType = new JDBCDataType(session.getDataSource(), itemMeta);
        }
        valueHandler = DBUtils.findValueHandler(session, itemMeta);
    } else {
        valueHandler = DBUtils.findValueHandler(session, elementType);
    }
    try {
        try (DBCResultSet resultSet = JDBCResultSetImpl.makeResultSet(session, null, dbResult, ModelMessages.model_jdbc_array_result_set, true)) {
            List<Object> data = new ArrayList<>();
            while (dbResult.next()) {
                // Fetch second column - it contains value
                data.add(valueHandler.fetchValueObject(session, resultSet, elementType, 1));
            }
            return new JDBCCollection(elementType, valueHandler, data.toArray());
        }
    } finally {
        try {
            dbResult.close();
        } catch (SQLException e) {
            //$NON-NLS-1$
            log.debug("Can't close array result set", e);
        }
    }
}
Also used : JDBCDataType(org.jkiss.dbeaver.model.impl.jdbc.struct.JDBCDataType) SQLException(java.sql.SQLException) DBCResultSet(org.jkiss.dbeaver.model.exec.DBCResultSet) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) DBCException(org.jkiss.dbeaver.model.exec.DBCException) DBSTypedObject(org.jkiss.dbeaver.model.struct.DBSTypedObject) JDBCColumnMetaData(org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCColumnMetaData) DBCResultSet(org.jkiss.dbeaver.model.exec.DBCResultSet) NotNull(org.jkiss.code.NotNull)

Example 2 with JDBCDataType

use of org.jkiss.dbeaver.model.impl.jdbc.struct.JDBCDataType in project dbeaver by serge-rider.

the class JDBCBasicDataTypeCache method fillStandardTypes.

// SQL-92 standard types
// plus a few de-facto standard types
@SuppressWarnings("unchecked")
public void fillStandardTypes(DBSObject owner) {
    List<DBSDataType> standardTypes = new ArrayList<>();
    Collections.addAll(standardTypes, new JDBCDataType(owner, Types.INTEGER, "INTEGER", null, false, true, 0, 0, 0), new JDBCDataType(owner, Types.FLOAT, "FLOAT", null, false, true, 0, 0, 0), new JDBCDataType(owner, Types.REAL, "REAL", null, false, true, 0, 0, 0), new JDBCDataType(owner, Types.DOUBLE, "DOUBLE PRECISION", null, false, true, 0, 0, 0), new JDBCDataType(owner, Types.NUMERIC, "NUMBER", null, false, true, 0, 0, 0), new JDBCDataType(owner, Types.DECIMAL, "DECIMAL", null, false, true, 0, 0, 0), new JDBCDataType(owner, Types.SMALLINT, "SMALLINT", null, false, true, 0, 0, 0), new JDBCDataType(owner, Types.BIGINT, "BIGINT", null, false, true, 0, 0, 0), new JDBCDataType(owner, Types.BIT, "BIT", null, false, true, 0, 0, 0), new JDBCDataType(owner, Types.VARCHAR, "VARCHAR", null, false, true, 0, 0, 0), new JDBCDataType(owner, Types.VARBINARY, "VARBINARY", null, false, true, 0, 0, 0), new JDBCDataType(owner, Types.DATE, "DATE", null, false, true, 0, 0, 0), new JDBCDataType(owner, Types.TIME, "TIME", null, false, true, 0, 0, 0), new JDBCDataType(owner, Types.TIMESTAMP, "TIMESTAMP", null, false, true, 0, 0, 0), new JDBCDataType(owner, Types.BLOB, "BLOB", null, false, true, 0, 0, 0), new JDBCDataType(owner, Types.CLOB, "CLOB", null, false, true, 0, 0, 0), new JDBCDataType(owner, Types.BOOLEAN, "BOOLEAN", null, false, true, 0, 0, 0), new JDBCDataType(owner, Types.OTHER, "OBJECT", null, false, true, 0, 0, 0));
    setCache(standardTypes);
}
Also used : JDBCDataType(org.jkiss.dbeaver.model.impl.jdbc.struct.JDBCDataType) DBSDataType(org.jkiss.dbeaver.model.struct.DBSDataType)

Example 3 with JDBCDataType

use of org.jkiss.dbeaver.model.impl.jdbc.struct.JDBCDataType in project dbeaver by serge-rider.

the class JDBCReferenceValueHandler method getValueFromObject.

@Override
public JDBCReference getValueFromObject(@NotNull DBCSession session, @NotNull DBSTypedObject type, Object object, boolean copy) throws DBCException {
    String typeName;
    try {
        if (object instanceof Ref) {
            typeName = ((Ref) object).getBaseTypeName();
        } else {
            typeName = type.getTypeName();
        }
    } catch (SQLException e) {
        throw new DBCException(e, session.getDataSource());
    }
    DBSDataType dataType = null;
    try {
        dataType = DBUtils.resolveDataType(session.getProgressMonitor(), session.getDataSource(), typeName);
    } catch (DBException e) {
        log.error("Error resolving data type '" + typeName + "'", e);
    }
    if (dataType == null) {
        dataType = new JDBCDataType(session.getDataSource().getContainer(), Types.REF, typeName, "Synthetic struct type for reference '" + typeName + "'", false, false, 0, 0, 0);
    }
    if (object == null) {
        return new JDBCReference(dataType, null);
    } else if (object instanceof JDBCReference) {
        return (JDBCReference) object;
    } else if (object instanceof Ref) {
        return new JDBCReference(dataType, (Ref) object);
    } else {
        throw new DBCException("Unsupported struct type: " + object.getClass().getName());
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) JDBCDataType(org.jkiss.dbeaver.model.impl.jdbc.struct.JDBCDataType) Ref(java.sql.Ref) SQLException(java.sql.SQLException) DBSDataType(org.jkiss.dbeaver.model.struct.DBSDataType) DBCException(org.jkiss.dbeaver.model.exec.DBCException) JDBCReference(org.jkiss.dbeaver.model.impl.jdbc.data.JDBCReference)

Aggregations

JDBCDataType (org.jkiss.dbeaver.model.impl.jdbc.struct.JDBCDataType)3 SQLException (java.sql.SQLException)2 DBCException (org.jkiss.dbeaver.model.exec.DBCException)2 DBSDataType (org.jkiss.dbeaver.model.struct.DBSDataType)2 Ref (java.sql.Ref)1 ResultSet (java.sql.ResultSet)1 ArrayList (java.util.ArrayList)1 NotNull (org.jkiss.code.NotNull)1 DBException (org.jkiss.dbeaver.DBException)1 DBCResultSet (org.jkiss.dbeaver.model.exec.DBCResultSet)1 JDBCReference (org.jkiss.dbeaver.model.impl.jdbc.data.JDBCReference)1 JDBCColumnMetaData (org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCColumnMetaData)1 DBSTypedObject (org.jkiss.dbeaver.model.struct.DBSTypedObject)1