Search in sources :

Example 1 with JDBCColumnMetaData

use of org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCColumnMetaData in project dbeaver by dbeaver.

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 JDBCColumnMetaData

use of org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCColumnMetaData 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) ArrayList(java.util.ArrayList) JDBCColumnMetaData(org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCColumnMetaData) NotNull(org.jkiss.code.NotNull)

Aggregations

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