Search in sources :

Example 6 with DBSTypedObject

use of org.jkiss.dbeaver.model.struct.DBSTypedObject in project dbeaver by serge-rider.

the class PostgreStructValueHandler method bindParameter.

@Override
protected void bindParameter(JDBCSession session, JDBCPreparedStatement statement, DBSTypedObject paramType, int paramIndex, Object value) throws DBCException, SQLException {
    if (value == null) {
        statement.setNull(paramIndex, Types.STRUCT);
    } else if (value instanceof DBDComposite) {
        DBDComposite struct = (DBDComposite) value;
        if (struct.isNull()) {
            statement.setNull(paramIndex, Types.STRUCT);
        } else if (struct instanceof JDBCComposite) {
            final Object[] values = ((JDBCComposite) struct).getValues();
            final String string = PostgreUtils.generateObjectString(values);
            statement.setObject(paramIndex, string, Types.OTHER);
        }
    } else {
        throw new DBCException("Struct parameter type '" + value.getClass().getName() + "' not supported");
    }
}
Also used : DBDComposite(org.jkiss.dbeaver.model.data.DBDComposite) DBSTypedObject(org.jkiss.dbeaver.model.struct.DBSTypedObject) DBCException(org.jkiss.dbeaver.model.exec.DBCException) JDBCComposite(org.jkiss.dbeaver.model.impl.jdbc.data.JDBCComposite)

Example 7 with DBSTypedObject

use of org.jkiss.dbeaver.model.struct.DBSTypedObject in project dbeaver by serge-rider.

the class JDBCNumberValueHandler method fetchColumnValue.

@Nullable
@Override
protected Object fetchColumnValue(DBCSession session, JDBCResultSet resultSet, DBSTypedObject type, int index) throws DBCException, SQLException {
    Number value;
    switch(type.getTypeID()) {
        case Types.DOUBLE:
        case Types.REAL:
            value = resultSet.getDouble(index);
            break;
        case Types.FLOAT:
            try {
                // Read value with maximum precision. Some drivers reports FLOAT but means double [JDBC:SQLite]
                value = resultSet.getDouble(index);
            } catch (SQLException e) {
                value = resultSet.getFloat(index);
            }
            break;
        case Types.INTEGER:
            try {
                // Read value with maximum precision. Some drivers reports INTEGER but means long [JDBC:SQLite]
                value = resultSet.getLong(index);
            } catch (SQLException e) {
                value = resultSet.getInt(index);
            }
            break;
        case Types.SMALLINT:
            // Read int in case of unsigned shorts
            value = resultSet.getInt(index);
            break;
        case Types.TINYINT:
            // Read short in case of unsigned byte
            value = resultSet.getShort(index);
            break;
        case Types.BIT:
            if (type.getPrecision() <= 1) {
                // single bit
                value = resultSet.getByte(index);
            } else {
                // bit string
                return CommonUtils.toBinaryString(resultSet.getLong(index), type.getPrecision());
            }
            break;
        default:
            // Here may be any numeric value. BigDecimal or BigInteger for example
            boolean gotValue = false;
            value = null;
            try {
                Object objectValue = resultSet.getObject(index);
                if (objectValue == null || objectValue instanceof Number) {
                    value = (Number) objectValue;
                    gotValue = true;
                }
            } catch (SQLException e) {
                log.debug(e);
            }
            if (value == null && !gotValue) {
                if (type.getScale() > 0) {
                    value = resultSet.getDouble(index);
                } else {
                    value = resultSet.getLong(index);
                }
            }
            break;
    }
    if (resultSet.wasNull()) {
        return null;
    } else {
        return value;
    }
}
Also used : SQLException(java.sql.SQLException) DBSTypedObject(org.jkiss.dbeaver.model.struct.DBSTypedObject) Nullable(org.jkiss.code.Nullable)

Aggregations

DBSTypedObject (org.jkiss.dbeaver.model.struct.DBSTypedObject)7 SQLException (java.sql.SQLException)2 DBCException (org.jkiss.dbeaver.model.exec.DBCException)2 JDBCCollection (org.jkiss.dbeaver.model.impl.jdbc.data.JDBCCollection)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 CTabFolder (org.eclipse.swt.custom.CTabFolder)1 CTabItem (org.eclipse.swt.custom.CTabItem)1 StyledText (org.eclipse.swt.custom.StyledText)1 ModifyEvent (org.eclipse.swt.events.ModifyEvent)1 ModifyListener (org.eclipse.swt.events.ModifyListener)1 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)1 SelectionEvent (org.eclipse.swt.events.SelectionEvent)1 Point (org.eclipse.swt.graphics.Point)1 GridData (org.eclipse.swt.layout.GridData)1 NotNull (org.jkiss.code.NotNull)1 Nullable (org.jkiss.code.Nullable)1 PostgreDataType (org.jkiss.dbeaver.ext.postgresql.model.PostgreDataType)1 DBDAttributeBinding (org.jkiss.dbeaver.model.data.DBDAttributeBinding)1 DBDCollection (org.jkiss.dbeaver.model.data.DBDCollection)1