Search in sources :

Example 46 with SimpleResultSet

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

the class TestTools method testSimpleResultSet.

private void testSimpleResultSet() throws Exception {
    SimpleResultSet rs;
    rs = new SimpleResultSet();
    rs.addColumn(null, 0, 0, 0);
    rs.addRow(1);
    createClassProxy(rs.getClass());
    assertThrows(IllegalStateException.class, rs).addColumn(null, 0, 0, 0);
    assertEquals(ResultSet.TYPE_FORWARD_ONLY, rs.getType());
    rs.next();
    assertEquals(1, rs.getInt(1));
    assertEquals("1", rs.getString(1));
    assertEquals("1", rs.getString("C1"));
    assertFalse(rs.wasNull());
    assertEquals("C1", rs.getMetaData().getColumnLabel(1));
    assertEquals("C1", rs.getColumnName(1));
    assertEquals(ResultSetMetaData.columnNullableUnknown, rs.getMetaData().isNullable(1));
    assertFalse(rs.getMetaData().isAutoIncrement(1));
    assertTrue(rs.getMetaData().isCaseSensitive(1));
    assertFalse(rs.getMetaData().isCurrency(1));
    assertFalse(rs.getMetaData().isDefinitelyWritable(1));
    assertTrue(rs.getMetaData().isReadOnly(1));
    assertTrue(rs.getMetaData().isSearchable(1));
    assertTrue(rs.getMetaData().isSigned(1));
    assertFalse(rs.getMetaData().isWritable(1));
    assertEquals(null, rs.getMetaData().getCatalogName(1));
    assertEquals(null, rs.getMetaData().getColumnClassName(1));
    assertEquals("NULL", rs.getMetaData().getColumnTypeName(1));
    assertEquals(null, rs.getMetaData().getSchemaName(1));
    assertEquals(null, rs.getMetaData().getTableName(1));
    assertEquals(ResultSet.HOLD_CURSORS_OVER_COMMIT, rs.getHoldability());
    assertEquals(1, rs.getColumnCount());
    rs = new SimpleResultSet();
    rs.setAutoClose(false);
    rs.addColumn("a", Types.BIGINT, 0, 0);
    rs.addColumn("b", Types.BINARY, 0, 0);
    rs.addColumn("c", Types.BOOLEAN, 0, 0);
    rs.addColumn("d", Types.DATE, 0, 0);
    rs.addColumn("e", Types.DECIMAL, 0, 0);
    rs.addColumn("f", Types.FLOAT, 0, 0);
    rs.addColumn("g", Types.VARCHAR, 0, 0);
    rs.addColumn("h", Types.ARRAY, 0, 0);
    rs.addColumn("i", Types.TIME, 0, 0);
    rs.addColumn("j", Types.TIMESTAMP, 0, 0);
    rs.addColumn("k", Types.CLOB, 0, 0);
    rs.addColumn("l", Types.BLOB, 0, 0);
    Date d = Date.valueOf("2001-02-03");
    byte[] b = { (byte) 0xab };
    Object[] a = { 1, 2 };
    Time t = Time.valueOf("10:20:30");
    Timestamp ts = Timestamp.valueOf("2002-03-04 10:20:30");
    Clob clob = new SimpleClob("Hello World");
    Blob blob = new SimpleBlob(new byte[] { (byte) 1, (byte) 2 });
    rs.addRow(1, b, true, d, "10.3", Math.PI, "-3", a, t, ts, clob, blob);
    rs.addRow(BigInteger.ONE, null, true, null, BigDecimal.ONE, 1d, null, null, null, null, null);
    rs.addRow(BigInteger.ZERO, null, false, null, BigDecimal.ZERO, 0d, null, null, null, null, null);
    rs.addRow(null, null, null, null, null, null, null, null, null, null, null);
    rs.next();
    assertEquals(1, rs.getLong(1));
    assertEquals((byte) 1, rs.getByte(1));
    assertEquals((short) 1, rs.getShort(1));
    assertEquals(1, rs.getLong("a"));
    assertEquals((byte) 1, rs.getByte("a"));
    assertEquals(1, rs.getInt("a"));
    assertEquals((short) 1, rs.getShort("a"));
    assertTrue(rs.getObject(1).getClass() == Integer.class);
    assertTrue(rs.getObject("a").getClass() == Integer.class);
    assertTrue(rs.getBoolean(1));
    assertEquals(b, rs.getBytes(2));
    assertEquals(b, rs.getBytes("b"));
    assertTrue(rs.getBoolean(3));
    assertTrue(rs.getBoolean("c"));
    assertEquals(d.getTime(), rs.getDate(4).getTime());
    assertEquals(d.getTime(), rs.getDate("d").getTime());
    assertTrue(new BigDecimal("10.3").equals(rs.getBigDecimal(5)));
    assertTrue(new BigDecimal("10.3").equals(rs.getBigDecimal("e")));
    assertEquals(10.3, rs.getDouble(5));
    assertEquals((float) 10.3, rs.getFloat(5));
    assertTrue(Math.PI == rs.getDouble(6));
    assertTrue(Math.PI == rs.getDouble("f"));
    assertTrue((float) Math.PI == rs.getFloat(6));
    assertTrue((float) Math.PI == rs.getFloat("f"));
    assertTrue(rs.getBoolean(6));
    assertEquals(-3, rs.getInt(7));
    assertEquals(-3, rs.getByte(7));
    assertEquals(-3, rs.getShort(7));
    assertEquals(-3, rs.getLong(7));
    Object[] a2 = (Object[]) rs.getArray(8).getArray();
    assertEquals(2, a2.length);
    assertTrue(a == a2);
    SimpleArray array = (SimpleArray) rs.getArray("h");
    assertEquals(Types.NULL, array.getBaseType());
    assertEquals("NULL", array.getBaseTypeName());
    a2 = (Object[]) array.getArray();
    array.free();
    assertEquals(2, a2.length);
    assertTrue(a == a2);
    assertTrue(t == rs.getTime("i"));
    assertTrue(t == rs.getTime(9));
    assertTrue(ts == rs.getTimestamp("j"));
    assertTrue(ts == rs.getTimestamp(10));
    assertTrue(clob == rs.getClob("k"));
    assertTrue(clob == rs.getClob(11));
    assertEquals("Hello World", rs.getString("k"));
    assertEquals("Hello World", rs.getString(11));
    assertTrue(blob == rs.getBlob("l"));
    assertTrue(blob == rs.getBlob(12));
    assertThrows(ErrorCode.INVALID_VALUE_2, (ResultSet) rs).getString(13);
    assertThrows(ErrorCode.COLUMN_NOT_FOUND_1, (ResultSet) rs).getString("NOT_FOUND");
    rs.next();
    assertTrue(rs.getBoolean(1));
    assertTrue(rs.getBoolean(3));
    assertTrue(rs.getBoolean(5));
    assertTrue(rs.getBoolean(6));
    rs.next();
    assertFalse(rs.getBoolean(1));
    assertFalse(rs.getBoolean(3));
    assertFalse(rs.getBoolean(5));
    assertFalse(rs.getBoolean(6));
    rs.next();
    assertEquals(0, rs.getLong(1));
    assertTrue(rs.wasNull());
    assertEquals(null, rs.getBytes(2));
    assertTrue(rs.wasNull());
    assertFalse(rs.getBoolean(3));
    assertTrue(rs.wasNull());
    assertNull(rs.getDate(4));
    assertTrue(rs.wasNull());
    assertNull(rs.getBigDecimal(5));
    assertTrue(rs.wasNull());
    assertEquals(0.0, rs.getDouble(5));
    assertTrue(rs.wasNull());
    assertEquals(0.0, rs.getDouble(6));
    assertTrue(rs.wasNull());
    assertEquals(0.0, rs.getFloat(6));
    assertTrue(rs.wasNull());
    assertEquals(0, rs.getInt(7));
    assertTrue(rs.wasNull());
    assertNull(rs.getArray(8));
    assertTrue(rs.wasNull());
    assertNull(rs.getTime(9));
    assertTrue(rs.wasNull());
    assertNull(rs.getTimestamp(10));
    assertTrue(rs.wasNull());
    assertNull(rs.getClob(11));
    assertTrue(rs.wasNull());
    assertNull(rs.getCharacterStream(11));
    assertTrue(rs.wasNull());
    assertNull(rs.getBlob(12));
    assertTrue(rs.wasNull());
    assertNull(rs.getBinaryStream(12));
    assertTrue(rs.wasNull());
    // all updateX methods
    for (Method m : rs.getClass().getMethods()) {
        if (m.getName().startsWith("update")) {
            if (m.getName().equals("updateRow")) {
                continue;
            }
            int len = m.getParameterTypes().length;
            if (m.getName().equals("updateObject") && m.getParameterTypes().length > 2) {
                Class<?> p3 = m.getParameterTypes()[2];
                if (p3.toString().indexOf("SQLType") >= 0) {
                    continue;
                }
            }
            Object[] params = new Object[len];
            int i = 0;
            String expectedValue = null;
            for (Class<?> type : m.getParameterTypes()) {
                Object o;
                String e = null;
                if (type == int.class) {
                    o = 1;
                    e = "1";
                } else if (type == byte.class) {
                    o = (byte) 2;
                    e = "2";
                } else if (type == double.class) {
                    o = (double) 3;
                    e = "3.0";
                } else if (type == float.class) {
                    o = (float) 4;
                    e = "4.0";
                } else if (type == long.class) {
                    o = (long) 5;
                    e = "5";
                } else if (type == short.class) {
                    o = (short) 6;
                    e = "6";
                } else if (type == boolean.class) {
                    o = false;
                    e = "false";
                } else if (type == String.class) {
                    // columnName or value
                    o = "a";
                    e = "a";
                } else {
                    o = null;
                }
                if (i == 1) {
                    expectedValue = e;
                }
                params[i] = o;
                i++;
            }
            m.invoke(rs, params);
            if (params.length == 1) {
                // updateNull
                assertEquals(null, rs.getString(1));
            } else {
                assertEquals(expectedValue, rs.getString(1));
            }
            // invalid column name / index
            Object invalidColumn;
            if (m.getParameterTypes()[0] == String.class) {
                invalidColumn = "x";
            } else {
                invalidColumn = 0;
            }
            params[0] = invalidColumn;
            try {
                m.invoke(rs, params);
                fail();
            } catch (InvocationTargetException e) {
                SQLException e2 = (SQLException) e.getTargetException();
                if (invalidColumn instanceof String) {
                    assertEquals(ErrorCode.COLUMN_NOT_FOUND_1, e2.getErrorCode());
                } else {
                    assertEquals(ErrorCode.INVALID_VALUE_2, e2.getErrorCode());
                }
            }
        }
    }
    assertEquals(ResultSet.FETCH_FORWARD, rs.getFetchDirection());
    assertEquals(0, rs.getFetchSize());
    assertEquals(ResultSet.TYPE_SCROLL_INSENSITIVE, rs.getType());
    assertNull(rs.getStatement());
    assertFalse(rs.isClosed());
    rs.beforeFirst();
    assertEquals(0, rs.getRow());
    assertTrue(rs.next());
    assertFalse(rs.isClosed());
    assertEquals(1, rs.getRow());
    assertTrue(rs.next());
    assertTrue(rs.next());
    assertTrue(rs.next());
    assertFalse(rs.next());
    assertThrows(ErrorCode.NO_DATA_AVAILABLE, (ResultSet) rs).getInt(1);
    assertEquals(0, rs.getRow());
    assertFalse(rs.isClosed());
    rs.close();
    assertTrue(rs.isClosed());
    rs = new SimpleResultSet();
    rs.addColumn("TEST", Types.BINARY, 0, 0);
    UUID uuid = UUID.randomUUID();
    rs.addRow(uuid);
    rs.next();
    assertEquals(uuid, rs.getObject(1));
    assertEquals(uuid, ValueUuid.get(rs.getBytes(1)).getObject());
}
Also used : Blob(java.sql.Blob) SimpleResultSet(org.h2.tools.SimpleResultSet) SQLException(java.sql.SQLException) Time(java.sql.Time) Method(java.lang.reflect.Method) Timestamp(java.sql.Timestamp) Date(java.sql.Date) BigDecimal(java.math.BigDecimal) InvocationTargetException(java.lang.reflect.InvocationTargetException) BigInteger(java.math.BigInteger) SimpleArray(org.h2.tools.SimpleResultSet.SimpleArray) ResultSet(java.sql.ResultSet) SimpleResultSet(org.h2.tools.SimpleResultSet) Clob(java.sql.Clob) UUID(java.util.UUID)

Example 47 with SimpleResultSet

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

the class TestValue method testResultSetOperations.

private void testResultSetOperations() throws SQLException {
    SimpleResultSet rs = new SimpleResultSet();
    rs.setAutoClose(false);
    rs.addColumn("X", Types.INTEGER, 10, 0);
    rs.addRow(new Object[] { null });
    rs.next();
    for (int type = Value.NULL; type < Value.TYPE_COUNT; type++) {
        if (type == 23) {
        // a defunct experimental type
        } else {
            Value v = DataType.readValue(null, rs, 1, type);
            assertTrue(v == ValueNull.INSTANCE);
        }
    }
    testResultSetOperation(new byte[0]);
    testResultSetOperation(1);
    testResultSetOperation(Boolean.TRUE);
    testResultSetOperation((byte) 1);
    testResultSetOperation((short) 2);
    testResultSetOperation((long) 3);
    testResultSetOperation(4.0f);
    testResultSetOperation(5.0d);
    testResultSetOperation(new Date(6));
    testResultSetOperation(new Time(7));
    testResultSetOperation(new Timestamp(8));
    testResultSetOperation(new BigDecimal("9"));
    testResultSetOperation(UUID.randomUUID());
    SimpleResultSet rs2 = new SimpleResultSet();
    rs2.setAutoClose(false);
    rs2.addColumn("X", Types.INTEGER, 10, 0);
    rs2.addRow(new Object[] { 1 });
    rs2.next();
    testResultSetOperation(rs2);
}
Also used : SimpleResultSet(org.h2.tools.SimpleResultSet) Value(org.h2.value.Value) Time(java.sql.Time) Timestamp(java.sql.Timestamp) ValueTimestamp(org.h2.value.ValueTimestamp) Date(java.sql.Date) BigDecimal(java.math.BigDecimal)

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