Search in sources :

Example 6 with SimpleResultSet

use of org.h2.tools.SimpleResultSet in project h2database by h2database.

the class TestValue method testValueResultSet.

private void testValueResultSet() throws SQLException {
    SimpleResultSet rs = new SimpleResultSet();
    rs.setAutoClose(false);
    rs.addColumn("ID", Types.INTEGER, 0, 0);
    rs.addColumn("NAME", Types.VARCHAR, 255, 0);
    rs.addRow(1, "Hello");
    rs.addRow(2, "World");
    rs.addRow(3, "Peace");
    ValueResultSet v;
    v = ValueResultSet.get(rs);
    assertTrue(rs == v.getObject());
    v = ValueResultSet.getCopy(rs, 2);
    assertEquals(0, v.hashCode());
    assertEquals(Integer.MAX_VALUE, v.getDisplaySize());
    assertEquals(Integer.MAX_VALUE, v.getPrecision());
    assertEquals(0, v.getScale());
    assertEquals("", v.getSQL());
    assertEquals(Value.RESULT_SET, v.getType());
    assertEquals("((1, Hello), (2, World))", v.getString());
    rs.beforeFirst();
    ValueResultSet v2 = ValueResultSet.getCopy(rs, 2);
    assertTrue(v.equals(v));
    assertFalse(v.equals(v2));
    rs.beforeFirst();
    ResultSet rs2 = v.getResultSet();
    rs2.next();
    rs.next();
    assertEquals(rs.getInt(1), rs2.getInt(1));
    assertEquals(rs.getString(2), rs2.getString(2));
    rs2.next();
    rs.next();
    assertEquals(rs.getInt(1), rs2.getInt(1));
    assertEquals(rs.getString(2), rs2.getString(2));
    assertFalse(rs2.next());
    assertTrue(rs.next());
}
Also used : ValueResultSet(org.h2.value.ValueResultSet) SimpleResultSet(org.h2.tools.SimpleResultSet) SimpleResultSet(org.h2.tools.SimpleResultSet) ResultSet(java.sql.ResultSet) ValueResultSet(org.h2.value.ValueResultSet)

Example 7 with SimpleResultSet

use of org.h2.tools.SimpleResultSet in project h2database by h2database.

the class TestValue method testCastTrim.

private void testCastTrim() {
    Value v;
    String spaces = new String(new char[100]).replace((char) 0, ' ');
    v = ValueArray.get(new Value[] { ValueString.get("hello"), ValueString.get("world") });
    assertEquals(10, v.getPrecision());
    assertEquals(5, v.convertPrecision(5, true).getPrecision());
    v = ValueArray.get(new Value[] { ValueString.get(""), ValueString.get("") });
    assertEquals(0, v.getPrecision());
    assertEquals("('')", v.convertPrecision(1, true).toString());
    v = ValueBytes.get(spaces.getBytes());
    assertEquals(100, v.getPrecision());
    assertEquals(10, v.convertPrecision(10, false).getPrecision());
    assertEquals(10, v.convertPrecision(10, false).getBytes().length);
    assertEquals(32, v.convertPrecision(10, false).getBytes()[9]);
    assertEquals(10, v.convertPrecision(10, true).getPrecision());
    final Value vd = ValueDecimal.get(new BigDecimal("1234567890.123456789"));
    assertEquals(19, vd.getPrecision());
    assertEquals("1234567890.1234567", vd.convertPrecision(10, true).getString());
    new AssertThrows(ErrorCode.NUMERIC_VALUE_OUT_OF_RANGE_1) {

        @Override
        public void test() {
            vd.convertPrecision(10, false);
        }
    };
    v = ValueLobDb.createSmallLob(Value.CLOB, spaces.getBytes(), 100);
    assertEquals(100, v.getPrecision());
    assertEquals(10, v.convertPrecision(10, false).getPrecision());
    assertEquals(10, v.convertPrecision(10, false).getString().length());
    assertEquals("          ", v.convertPrecision(10, false).getString());
    assertEquals(10, v.convertPrecision(10, true).getPrecision());
    v = ValueLobDb.createSmallLob(Value.BLOB, spaces.getBytes(), 100);
    assertEquals(100, v.getPrecision());
    assertEquals(10, v.convertPrecision(10, false).getPrecision());
    assertEquals(10, v.convertPrecision(10, false).getBytes().length);
    assertEquals(32, v.convertPrecision(10, false).getBytes()[9]);
    assertEquals(10, v.convertPrecision(10, true).getPrecision());
    ResultSet rs = new SimpleResultSet();
    v = ValueResultSet.get(rs);
    assertEquals(Integer.MAX_VALUE, v.getPrecision());
    assertEquals(Integer.MAX_VALUE, v.convertPrecision(10, false).getPrecision());
    assertTrue(rs == v.convertPrecision(10, false).getObject());
    assertFalse(rs == v.convertPrecision(10, true).getObject());
    assertEquals(Integer.MAX_VALUE, v.convertPrecision(10, true).getPrecision());
    v = ValueString.get(spaces);
    assertEquals(100, v.getPrecision());
    assertEquals(10, v.convertPrecision(10, false).getPrecision());
    assertEquals("          ", v.convertPrecision(10, false).getString());
    assertEquals("          ", v.convertPrecision(10, true).getString());
}
Also used : AssertThrows(org.h2.test.utils.AssertThrows) SimpleResultSet(org.h2.tools.SimpleResultSet) Value(org.h2.value.Value) SimpleResultSet(org.h2.tools.SimpleResultSet) ResultSet(java.sql.ResultSet) ValueResultSet(org.h2.value.ValueResultSet) ValueString(org.h2.value.ValueString) BigDecimal(java.math.BigDecimal)

Example 8 with SimpleResultSet

use of org.h2.tools.SimpleResultSet in project h2database by h2database.

the class TestValueMemory method create.

private Value create(int type) throws SQLException {
    switch(type) {
        case Value.NULL:
            return ValueNull.INSTANCE;
        case Value.BOOLEAN:
            return ValueBoolean.FALSE;
        case Value.BYTE:
            return ValueByte.get((byte) random.nextInt());
        case Value.SHORT:
            return ValueShort.get((short) random.nextInt());
        case Value.INT:
            return ValueInt.get(random.nextInt());
        case Value.LONG:
            return ValueLong.get(random.nextLong());
        case Value.DECIMAL:
            return ValueDecimal.get(new BigDecimal(random.nextInt()));
        // + "12123344563456345634565234523451312312"
        case Value.DOUBLE:
            return ValueDouble.get(random.nextDouble());
        case Value.FLOAT:
            return ValueFloat.get(random.nextFloat());
        case Value.TIME:
            return ValueTime.get(new java.sql.Time(random.nextLong()));
        case Value.DATE:
            return ValueDate.get(new java.sql.Date(random.nextLong()));
        case Value.TIMESTAMP:
            return ValueTimestamp.fromMillis(random.nextLong());
        case Value.TIMESTAMP_TZ:
            // clamp to max legal value
            long nanos = Math.max(Math.min(random.nextLong(), 24L * 60 * 60 * 1000 * 1000 * 1000 - 1), 0);
            int timeZoneOffsetMins = (int) (random.nextFloat() * (24 * 60)) - (12 * 60);
            return ValueTimestampTimeZone.fromDateValueAndNanos(random.nextLong(), nanos, (short) timeZoneOffsetMins);
        case Value.BYTES:
            return ValueBytes.get(randomBytes(random.nextInt(1000)));
        case Value.STRING:
            return ValueString.get(randomString(random.nextInt(100)));
        case Value.STRING_IGNORECASE:
            return ValueStringIgnoreCase.get(randomString(random.nextInt(100)));
        case Value.BLOB:
            {
                int len = (int) Math.abs(random.nextGaussian() * 10);
                byte[] data = randomBytes(len);
                return getLobStorage().createBlob(new ByteArrayInputStream(data), len);
            }
        case Value.CLOB:
            {
                int len = (int) Math.abs(random.nextGaussian() * 10);
                String s = randomString(len);
                return getLobStorage().createClob(new StringReader(s), len);
            }
        case Value.ARRAY:
            {
                int len = random.nextInt(20);
                Value[] list = new Value[len];
                for (int i = 0; i < list.length; i++) {
                    list[i] = create(Value.STRING);
                }
                return ValueArray.get(list);
            }
        case Value.RESULT_SET:
            return ValueResultSet.get(new SimpleResultSet());
        case Value.JAVA_OBJECT:
            return ValueJavaObject.getNoCopy(null, randomBytes(random.nextInt(100)), this);
        case Value.UUID:
            return ValueUuid.get(random.nextLong(), random.nextLong());
        case Value.STRING_FIXED:
            return ValueStringFixed.get(randomString(random.nextInt(100)));
        case Value.GEOMETRY:
            if (DataType.GEOMETRY_CLASS == null) {
                return ValueNull.INSTANCE;
            }
            return ValueGeometry.get("POINT (" + random.nextInt(100) + " " + random.nextInt(100) + ")");
        default:
            throw new AssertionError("type=" + type);
    }
}
Also used : SimpleResultSet(org.h2.tools.SimpleResultSet) ByteArrayInputStream(java.io.ByteArrayInputStream) StringReader(java.io.StringReader) ValueString(org.h2.value.ValueString) BigDecimal(java.math.BigDecimal) MemoryFootprint(org.h2.test.utils.MemoryFootprint)

Example 9 with SimpleResultSet

use of org.h2.tools.SimpleResultSet in project h2database by h2database.

the class TestDataPage method testValues.

private void testValues() {
    testValue(ValueNull.INSTANCE);
    testValue(ValueBoolean.FALSE);
    testValue(ValueBoolean.TRUE);
    for (int i = 0; i < 256; i++) {
        testValue(ValueByte.get((byte) i));
    }
    for (int i = 0; i < 256 * 256; i += 10) {
        testValue(ValueShort.get((short) i));
    }
    for (int i = 0; i < 256 * 256; i += 10) {
        testValue(ValueInt.get(i));
        testValue(ValueInt.get(-i));
        testValue(ValueLong.get(i));
        testValue(ValueLong.get(-i));
    }
    testValue(ValueInt.get(Integer.MAX_VALUE));
    testValue(ValueInt.get(Integer.MIN_VALUE));
    for (long i = 0; i < Integer.MAX_VALUE; i += 10 + i / 4) {
        testValue(ValueInt.get((int) i));
        testValue(ValueInt.get((int) -i));
    }
    testValue(ValueLong.get(Long.MAX_VALUE));
    testValue(ValueLong.get(Long.MIN_VALUE));
    for (long i = 0; i >= 0; i += 10 + i / 4) {
        testValue(ValueLong.get(i));
        testValue(ValueLong.get(-i));
    }
    testValue(ValueDecimal.get(BigDecimal.ZERO));
    testValue(ValueDecimal.get(BigDecimal.ONE));
    testValue(ValueDecimal.get(BigDecimal.TEN));
    testValue(ValueDecimal.get(BigDecimal.ONE.negate()));
    testValue(ValueDecimal.get(BigDecimal.TEN.negate()));
    for (long i = 0; i >= 0; i += 10 + i / 4) {
        testValue(ValueDecimal.get(new BigDecimal(i)));
        testValue(ValueDecimal.get(new BigDecimal(-i)));
        for (int j = 0; j < 200; j += 50) {
            testValue(ValueDecimal.get(new BigDecimal(i).setScale(j)));
            testValue(ValueDecimal.get(new BigDecimal(i * i).setScale(j)));
        }
        testValue(ValueDecimal.get(new BigDecimal(i * i)));
    }
    testValue(ValueDate.get(new Date(System.currentTimeMillis())));
    testValue(ValueDate.get(new Date(0)));
    testValue(ValueTime.get(new Time(System.currentTimeMillis())));
    testValue(ValueTime.get(new Time(0)));
    testValue(ValueTimestamp.fromMillis(System.currentTimeMillis()));
    testValue(ValueTimestamp.fromMillis(0));
    testValue(ValueTimestampTimeZone.parse("2000-01-01 10:00:00"));
    testValue(ValueJavaObject.getNoCopy(null, new byte[0], this));
    testValue(ValueJavaObject.getNoCopy(null, new byte[100], this));
    for (int i = 0; i < 300; i++) {
        testValue(ValueBytes.getNoCopy(new byte[i]));
    }
    for (int i = 0; i < 65000; i += 10 + i) {
        testValue(ValueBytes.getNoCopy(new byte[i]));
    }
    testValue(ValueUuid.getNewRandom());
    for (int i = 0; i < 100; i++) {
        testValue(ValueString.get(new String(new char[i])));
    }
    for (int i = 0; i < 65000; i += 10 + i) {
        testValue(ValueString.get(new String(new char[i])));
        testValue(ValueStringFixed.get(new String(new char[i])));
        testValue(ValueStringIgnoreCase.get(new String(new char[i])));
    }
    testValue(ValueFloat.get(0f));
    testValue(ValueFloat.get(1f));
    testValue(ValueFloat.get(-1f));
    testValue(ValueDouble.get(0));
    testValue(ValueDouble.get(1));
    testValue(ValueDouble.get(-1));
    for (int i = 0; i < 65000; i += 10 + i) {
        for (double j = 0.1; j < 65000; j += 10 + j) {
            testValue(ValueFloat.get((float) (i / j)));
            testValue(ValueDouble.get(i / j));
            testValue(ValueFloat.get((float) -(i / j)));
            testValue(ValueDouble.get(-(i / j)));
        }
    }
    testValue(ValueArray.get(new Value[0]));
    testValue(ValueArray.get(new Value[] { ValueBoolean.TRUE, ValueInt.get(10) }));
    SimpleResultSet rs = new SimpleResultSet();
    rs.setAutoClose(false);
    rs.addColumn("ID", Types.INTEGER, 0, 0);
    rs.addColumn("NAME", Types.VARCHAR, 255, 0);
    rs.addRow(1, "Hello");
    rs.addRow(2, "World");
    rs.addRow(3, "Peace");
    testValue(ValueResultSet.get(rs));
}
Also used : SimpleResultSet(org.h2.tools.SimpleResultSet) Value(org.h2.value.Value) Time(java.sql.Time) ValueTime(org.h2.value.ValueTime) ValueString(org.h2.value.ValueString) BigDecimal(java.math.BigDecimal) ValueDate(org.h2.value.ValueDate) Date(java.sql.Date)

Example 10 with SimpleResultSet

use of org.h2.tools.SimpleResultSet in project h2database by h2database.

the class DataType method convertToValue1.

private static Value convertToValue1(SessionInterface session, Object x, int type) {
    if (x == null) {
        return ValueNull.INSTANCE;
    }
    if (type == Value.JAVA_OBJECT) {
        return ValueJavaObject.getNoCopy(x, null, session.getDataHandler());
    }
    if (x instanceof String) {
        return ValueString.get((String) x);
    } else if (x instanceof Value) {
        return (Value) x;
    } else if (x instanceof Long) {
        return ValueLong.get(((Long) x).longValue());
    } else if (x instanceof Integer) {
        return ValueInt.get(((Integer) x).intValue());
    } else if (x instanceof BigInteger) {
        return ValueDecimal.get(new BigDecimal((BigInteger) x));
    } else if (x instanceof BigDecimal) {
        return ValueDecimal.get((BigDecimal) x);
    } else if (x instanceof Boolean) {
        return ValueBoolean.get(((Boolean) x).booleanValue());
    } else if (x instanceof Byte) {
        return ValueByte.get(((Byte) x).byteValue());
    } else if (x instanceof Short) {
        return ValueShort.get(((Short) x).shortValue());
    } else if (x instanceof Float) {
        return ValueFloat.get(((Float) x).floatValue());
    } else if (x instanceof Double) {
        return ValueDouble.get(((Double) x).doubleValue());
    } else if (x instanceof byte[]) {
        return ValueBytes.get((byte[]) x);
    } else if (x instanceof Date) {
        return ValueDate.get((Date) x);
    } else if (x instanceof Time) {
        return ValueTime.get((Time) x);
    } else if (x instanceof Timestamp) {
        return ValueTimestamp.get((Timestamp) x);
    } else if (x instanceof java.util.Date) {
        return ValueTimestamp.fromMillis(((java.util.Date) x).getTime());
    } else if (x instanceof java.io.Reader) {
        Reader r = new BufferedReader((java.io.Reader) x);
        return session.getDataHandler().getLobStorage().createClob(r, -1);
    } else if (x instanceof java.sql.Clob) {
        try {
            java.sql.Clob clob = (java.sql.Clob) x;
            Reader r = new BufferedReader(clob.getCharacterStream());
            return session.getDataHandler().getLobStorage().createClob(r, clob.length());
        } catch (SQLException e) {
            throw DbException.convert(e);
        }
    } else if (x instanceof java.io.InputStream) {
        return session.getDataHandler().getLobStorage().createBlob((java.io.InputStream) x, -1);
    } else if (x instanceof java.sql.Blob) {
        try {
            java.sql.Blob blob = (java.sql.Blob) x;
            return session.getDataHandler().getLobStorage().createBlob(blob.getBinaryStream(), blob.length());
        } catch (SQLException e) {
            throw DbException.convert(e);
        }
    } else if (x instanceof java.sql.Array) {
        java.sql.Array array = (java.sql.Array) x;
        try {
            return convertToValue(session, array.getArray(), Value.ARRAY);
        } catch (SQLException e) {
            throw DbException.convert(e);
        }
    } else if (x instanceof ResultSet) {
        if (x instanceof SimpleResultSet) {
            return ValueResultSet.get((ResultSet) x);
        }
        return ValueResultSet.getCopy((ResultSet) x, Integer.MAX_VALUE);
    } else if (x instanceof UUID) {
        return ValueUuid.get((UUID) x);
    }
    Class<?> clazz = x.getClass();
    if (x instanceof Object[]) {
        // (a.getClass().isArray());
        // (a.getClass().getComponentType().isPrimitive());
        Object[] o = (Object[]) x;
        int len = o.length;
        Value[] v = new Value[len];
        for (int i = 0; i < len; i++) {
            v[i] = convertToValue(session, o[i], type);
        }
        return ValueArray.get(clazz.getComponentType(), v);
    } else if (x instanceof Character) {
        return ValueStringFixed.get(((Character) x).toString());
    } else if (isGeometry(x)) {
        return ValueGeometry.getFromGeometry(x);
    } else if (clazz == LocalDateTimeUtils.LOCAL_DATE) {
        return LocalDateTimeUtils.localDateToDateValue(x);
    } else if (clazz == LocalDateTimeUtils.LOCAL_TIME) {
        return LocalDateTimeUtils.localTimeToTimeValue(x);
    } else if (clazz == LocalDateTimeUtils.LOCAL_DATE_TIME) {
        return LocalDateTimeUtils.localDateTimeToValue(x);
    } else if (clazz == LocalDateTimeUtils.INSTANT) {
        return LocalDateTimeUtils.instantToValue(x);
    } else if (clazz == LocalDateTimeUtils.OFFSET_DATE_TIME) {
        return LocalDateTimeUtils.offsetDateTimeToValue(x);
    } else if (x instanceof TimestampWithTimeZone) {
        return ValueTimestampTimeZone.get((TimestampWithTimeZone) x);
    } else {
        if (JdbcUtils.customDataTypesHandler != null) {
            return JdbcUtils.customDataTypesHandler.getValue(type, x, session.getDataHandler());
        }
        return ValueJavaObject.getNoCopy(x, null, session.getDataHandler());
    }
}
Also used : SimpleResultSet(org.h2.tools.SimpleResultSet) SQLException(java.sql.SQLException) Reader(java.io.Reader) BufferedReader(java.io.BufferedReader) Time(java.sql.Time) Timestamp(java.sql.Timestamp) Array(java.sql.Array) TimestampWithTimeZone(org.h2.api.TimestampWithTimeZone) SimpleResultSet(org.h2.tools.SimpleResultSet) ResultSet(java.sql.ResultSet) UUID(java.util.UUID) Clob(java.sql.Clob) JdbcBlob(org.h2.jdbc.JdbcBlob) Blob(java.sql.Blob) InputStream(java.io.InputStream) BigDecimal(java.math.BigDecimal) Date(java.sql.Date) BigInteger(java.math.BigInteger) Array(java.sql.Array) JdbcArray(org.h2.jdbc.JdbcArray) BufferedReader(java.io.BufferedReader) BigInteger(java.math.BigInteger) JdbcClob(org.h2.jdbc.JdbcClob) Clob(java.sql.Clob) Blob(java.sql.Blob)

Aggregations

SimpleResultSet (org.h2.tools.SimpleResultSet)46 ResultSet (java.sql.ResultSet)11 SQLException (java.sql.SQLException)10 BigDecimal (java.math.BigDecimal)9 BigInteger (java.math.BigInteger)5 Value (org.h2.value.Value)5 Date (java.sql.Date)4 ResultSetMetaData (java.sql.ResultSetMetaData)4 Time (java.sql.Time)4 Timestamp (java.sql.Timestamp)4 ValueString (org.h2.value.ValueString)4 IOException (java.io.IOException)3 PreparedStatement (java.sql.PreparedStatement)3 Document (org.apache.lucene.document.Document)3 JdbcSQLException (org.h2.jdbc.JdbcSQLException)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2 Array (java.sql.Array)2 Blob (java.sql.Blob)2 Clob (java.sql.Clob)2