Search in sources :

Example 1 with JavaObjectData

use of org.hsqldb_voltpatches.types.JavaObjectData in project voltdb by VoltDB.

the class RowOutputBinary method getSize.

/**
     * Calculate the size of byte array required to store a row.
     *
     * @param data - the row data
     * @param l - number of data[] elements to include in calculation
     * @param types - array of java.sql.Types values
     * @return size of byte array
     */
private static int getSize(Object[] data, int l, Type[] types) {
    int s = 0;
    for (int i = 0; i < l; i++) {
        Object o = data[i];
        // type or null
        s += 1;
        if (o != null) {
            switch(types[i].typeCode) {
                case Types.SQL_ALL_TYPES:
                    throw Error.runtimeError(ErrorCode.U_S0500, "RowOutputBinary");
                case Types.SQL_CHAR:
                case Types.SQL_VARCHAR:
                case Types.VARCHAR_IGNORECASE:
                    s += 4;
                    s += StringConverter.getUTFSize((String) o);
                    break;
                case Types.TINYINT:
                case Types.SQL_SMALLINT:
                    s += 2;
                    break;
                case Types.SQL_INTEGER:
                    s += 4;
                    break;
                case Types.SQL_BIGINT:
                case Types.SQL_REAL:
                case Types.SQL_FLOAT:
                case Types.SQL_DOUBLE:
                    s += 8;
                    break;
                case Types.SQL_NUMERIC:
                case Types.SQL_DECIMAL:
                    s += 8;
                    BigDecimal bigdecimal = (BigDecimal) o;
                    BigInteger bigint = JavaSystem.unscaledValue(bigdecimal);
                    s += bigint.toByteArray().length;
                    break;
                case Types.SQL_BOOLEAN:
                    s += 1;
                    break;
                case Types.SQL_DATE:
                    s += 8;
                    break;
                case Types.SQL_TIME:
                    s += 8;
                    break;
                case Types.SQL_TIME_WITH_TIME_ZONE:
                    s += 12;
                    break;
                case Types.SQL_TIMESTAMP:
                    s += 12;
                    break;
                case Types.SQL_TIMESTAMP_WITH_TIME_ZONE:
                    s += 16;
                    break;
                case Types.SQL_INTERVAL_YEAR:
                case Types.SQL_INTERVAL_YEAR_TO_MONTH:
                case Types.SQL_INTERVAL_MONTH:
                    s += 8;
                    break;
                case Types.SQL_INTERVAL_DAY:
                case Types.SQL_INTERVAL_DAY_TO_HOUR:
                case Types.SQL_INTERVAL_DAY_TO_MINUTE:
                case Types.SQL_INTERVAL_DAY_TO_SECOND:
                case Types.SQL_INTERVAL_HOUR:
                case Types.SQL_INTERVAL_HOUR_TO_MINUTE:
                case Types.SQL_INTERVAL_HOUR_TO_SECOND:
                case Types.SQL_INTERVAL_MINUTE:
                case Types.SQL_INTERVAL_MINUTE_TO_SECOND:
                case Types.SQL_INTERVAL_SECOND:
                    s += 12;
                    break;
                case Types.SQL_BINARY:
                case Types.SQL_VARBINARY:
                    s += 4;
                    s += ((BinaryData) o).length(null);
                    break;
                case Types.SQL_BIT:
                case Types.SQL_BIT_VARYING:
                    s += 4;
                    s += ((BinaryData) o).length(null);
                    break;
                case Types.SQL_CLOB:
                case Types.SQL_BLOB:
                    s += 8;
                    break;
                case Types.OTHER:
                    JavaObjectData jo = (JavaObjectData) o;
                    s += 4;
                    s += jo.getBytesLength();
                    break;
                default:
                    throw Error.runtimeError(ErrorCode.U_S0500, "RowOutputBinary");
            }
        }
    }
    return s;
}
Also used : JavaObjectData(org.hsqldb_voltpatches.types.JavaObjectData) BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal)

Example 2 with JavaObjectData

use of org.hsqldb_voltpatches.types.JavaObjectData in project voltdb by VoltDB.

the class RowInputText method readOther.

protected Object readOther() throws IOException {
    String s = readString();
    if (s == null) {
        return null;
    }
    BinaryData data = scanner.convertToBinary(s);
    if (data.length(null) == 0) {
        return null;
    }
    return new JavaObjectData(data.getBytes());
}
Also used : JavaObjectData(org.hsqldb_voltpatches.types.JavaObjectData) BinaryData(org.hsqldb_voltpatches.types.BinaryData)

Example 3 with JavaObjectData

use of org.hsqldb_voltpatches.types.JavaObjectData in project voltdb by VoltDB.

the class JDBCPreparedStatement method setParameter.

/**
     * The internal parameter value setter always converts the parameter to
     * the Java type required for data transmission.
     *
     * @param i parameter index
     * @param o object
     * @throws SQLException if either argument is not acceptable.
     */
void setParameter(int i, Object o) throws SQLException {
    checkSetParameterIndex(i, false);
    i--;
    if (o == null) {
        parameterValues[i] = null;
        return;
    }
    Type outType = parameterTypes[i];
    switch(outType.typeCode) {
        case Types.OTHER:
            try {
                if (o instanceof Serializable) {
                    o = new JavaObjectData((Serializable) o);
                    break;
                }
            } catch (HsqlException e) {
                Util.throwError(e);
            }
            Util.throwError(Error.error(ErrorCode.X_42565));
            break;
        case Types.SQL_BIT:
        case Types.SQL_BIT_VARYING:
            if (o instanceof Boolean) {
                if (outType.precision == 1) {
                    byte[] bytes = ((Boolean) o).booleanValue() ? new byte[] { -0x80 } : new byte[] { 0 };
                    o = new BinaryData(bytes, 1);
                    break;
                }
                Util.throwError(Error.error(ErrorCode.X_42565));
            }
            try {
                if (o instanceof byte[]) {
                    o = outType.convertToDefaultType(connection.sessionProxy, o);
                    break;
                }
                if (o instanceof String) {
                    o = outType.convertToDefaultType(connection.sessionProxy, o);
                    break;
                }
            } catch (HsqlException e) {
                Util.throwError(e);
            }
            Util.throwError(Error.error(ErrorCode.X_42565));
        // $FALL-THROUGH$
        case Types.SQL_BINARY:
        case Types.SQL_VARBINARY:
            if (o instanceof byte[]) {
                o = new BinaryData((byte[]) o, !connection.isNetConn);
                break;
            }
            try {
                if (o instanceof String) {
                    o = outType.convertToDefaultType(connection.sessionProxy, o);
                    break;
                }
            } catch (HsqlException e) {
                Util.throwError(e);
            }
            Util.throwError(Error.error(ErrorCode.X_42565));
            break;
        case Types.SQL_BLOB:
            setBlobParameter(i + 1, o);
            return;
        case Types.SQL_CLOB:
            setClobParameter(i + 1, o);
            return;
        case Types.SQL_DATE:
        case Types.SQL_TIME_WITH_TIME_ZONE:
        case Types.SQL_TIMESTAMP_WITH_TIME_ZONE:
        case Types.SQL_TIME:
        case Types.SQL_TIMESTAMP:
            {
                try {
                    if (o instanceof String) {
                        o = outType.convertToType(connection.sessionProxy, o, Type.SQL_VARCHAR);
                        break;
                    }
                    o = outType.convertJavaToSQL(connection.sessionProxy, o);
                    break;
                } catch (HsqlException e) {
                    Util.throwError(e);
                }
            }
        // $FALL-THROUGH$
        case Types.TINYINT:
        case Types.SQL_SMALLINT:
        case Types.SQL_INTEGER:
        case Types.SQL_BIGINT:
        case Types.SQL_REAL:
        case Types.SQL_FLOAT:
        case Types.SQL_DOUBLE:
        case Types.SQL_NUMERIC:
        case Types.SQL_DECIMAL:
            try {
                if (o instanceof String) {
                    o = outType.convertToType(connection.sessionProxy, o, Type.SQL_VARCHAR);
                    break;
                }
                o = outType.convertToDefaultType(connection.sessionProxy, o);
                break;
            } catch (HsqlException e) {
                Util.throwError(e);
            }
        // $FALL-THROUGH$
        default:
            try {
                o = outType.convertToDefaultType(connection.sessionProxy, o);
                break;
            } catch (HsqlException e) {
                Util.throwError(e);
            }
    }
    parameterValues[i] = o;
}
Also used : Type(org.hsqldb_voltpatches.types.Type) Serializable(java.io.Serializable) JavaObjectData(org.hsqldb_voltpatches.types.JavaObjectData) BinaryData(org.hsqldb_voltpatches.types.BinaryData) HsqlException(org.hsqldb_voltpatches.HsqlException)

Aggregations

JavaObjectData (org.hsqldb_voltpatches.types.JavaObjectData)3 BinaryData (org.hsqldb_voltpatches.types.BinaryData)2 Serializable (java.io.Serializable)1 BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1 HsqlException (org.hsqldb_voltpatches.HsqlException)1 Type (org.hsqldb_voltpatches.types.Type)1