Search in sources :

Example 56 with Call

use of org.h2.command.dml.Call in project h2database by h2database.

the class TestCallableStatement method testArrayArgument.

private void testArrayArgument(Connection connection) throws SQLException {
    Array array = connection.createArrayOf("Int", new Object[] { 0, 1, 2 });
    try (Statement statement = connection.createStatement()) {
        statement.execute("CREATE ALIAS getArrayLength FOR \"" + getClass().getName() + ".getArrayLength\"");
        // test setArray
        try (CallableStatement callableStatement = connection.prepareCall("{call getArrayLength(?)}")) {
            callableStatement.setArray(1, array);
            assertTrue(callableStatement.execute());
            try (ResultSet resultSet = callableStatement.getResultSet()) {
                assertTrue(resultSet.next());
                assertEquals(3, resultSet.getInt(1));
                assertFalse(resultSet.next());
            }
        }
        // test setObject
        try (CallableStatement callableStatement = connection.prepareCall("{call getArrayLength(?)}")) {
            callableStatement.setObject(1, array);
            assertTrue(callableStatement.execute());
            try (ResultSet resultSet = callableStatement.getResultSet()) {
                assertTrue(resultSet.next());
                assertEquals(3, resultSet.getInt(1));
                assertFalse(resultSet.next());
            }
        }
    } finally {
        array.free();
    }
}
Also used : Array(java.sql.Array) Statement(java.sql.Statement) CallableStatement(java.sql.CallableStatement) CallableStatement(java.sql.CallableStatement) SimpleResultSet(org.h2.tools.SimpleResultSet) ResultSet(java.sql.ResultSet)

Example 57 with Call

use of org.h2.command.dml.Call in project h2database by h2database.

the class TestCallableStatement method testArrayReturnValue.

private void testArrayReturnValue(Connection connection) throws SQLException {
    Object[][] arraysToTest = new Object[][] { new Object[] { 0, 1, 2 }, new Object[] { 0, "1", 2 }, new Object[] { 0, null, 2 }, new Object[] { 0, new Object[] { "s", 1 }, new Object[] { null, 1L } } };
    try (Statement statement = connection.createStatement()) {
        statement.execute("CREATE ALIAS arrayIdentiy FOR \"" + getClass().getName() + ".arrayIdentiy\"");
        for (Object[] arrayToTest : arraysToTest) {
            Array sqlInputArray = connection.createArrayOf("ignored", arrayToTest);
            try {
                try (CallableStatement callableStatement = connection.prepareCall("{call arrayIdentiy(?)}")) {
                    callableStatement.setArray(1, sqlInputArray);
                    assertTrue(callableStatement.execute());
                    try (ResultSet resultSet = callableStatement.getResultSet()) {
                        assertTrue(resultSet.next());
                        // test getArray()
                        Array sqlReturnArray = resultSet.getArray(1);
                        try {
                            assertEquals((Object[]) sqlInputArray.getArray(), (Object[]) sqlReturnArray.getArray());
                        } finally {
                            sqlReturnArray.free();
                        }
                        // test getObject(Array.class)
                        sqlReturnArray = resultSet.getObject(1, Array.class);
                        try {
                            assertEquals((Object[]) sqlInputArray.getArray(), (Object[]) sqlReturnArray.getArray());
                        } finally {
                            sqlReturnArray.free();
                        }
                        assertFalse(resultSet.next());
                    }
                }
            } finally {
                sqlInputArray.free();
            }
        }
    }
}
Also used : Array(java.sql.Array) Statement(java.sql.Statement) CallableStatement(java.sql.CallableStatement) CallableStatement(java.sql.CallableStatement) SimpleResultSet(org.h2.tools.SimpleResultSet) ResultSet(java.sql.ResultSet)

Example 58 with Call

use of org.h2.command.dml.Call in project h2database by h2database.

the class TestCallableStatement method testCallWithResultSet.

private void testCallWithResultSet(Connection conn) throws SQLException {
    CallableStatement call;
    ResultSet rs;
    call = conn.prepareCall("select 10 as a");
    call.execute();
    rs = call.getResultSet();
    rs.next();
    assertEquals(10, rs.getInt(1));
}
Also used : CallableStatement(java.sql.CallableStatement) SimpleResultSet(org.h2.tools.SimpleResultSet) ResultSet(java.sql.ResultSet)

Example 59 with Call

use of org.h2.command.dml.Call in project h2database by h2database.

the class TestConcurrentConnectionUsage method testAutoCommit.

private void testAutoCommit() throws SQLException {
    deleteDb(getTestName());
    final Connection conn = getConnection(getTestName());
    final PreparedStatement p1 = conn.prepareStatement("select 1 from dual");
    Task t = new Task() {

        @Override
        public void call() throws Exception {
            while (!stop) {
                p1.executeQuery();
                conn.setAutoCommit(true);
                conn.setAutoCommit(false);
            }
        }
    }.execute();
    PreparedStatement prep = conn.prepareStatement("select ? from dual");
    for (int i = 0; i < 10; i++) {
        prep.setBinaryStream(1, new ByteArrayInputStream(new byte[1024]));
        prep.executeQuery();
    }
    t.get();
    conn.close();
}
Also used : Task(org.h2.util.Task) ByteArrayInputStream(java.io.ByteArrayInputStream) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) SQLException(java.sql.SQLException)

Example 60 with Call

use of org.h2.command.dml.Call in project h2database by h2database.

the class TestGetGeneratedKeys method testTrigger.

/**
 * Test for keys generated by trigger.
 *
 * @param conn
 *            connection
 * @throws Exception
 *             on exception
 */
private void testTrigger(Connection conn) throws Exception {
    Statement stat = conn.createStatement();
    stat.execute("CREATE TABLE TEST(ID UUID, VALUE INT)");
    stat.execute("CREATE TRIGGER TEST_INSERT BEFORE INSERT ON TEST FOR EACH ROW CALL \"" + TestGetGeneratedKeysTrigger.class.getName() + '"');
    stat.executeUpdate("INSERT INTO TEST(VALUE) VALUES (10), (20)", Statement.RETURN_GENERATED_KEYS);
    ResultSet rs = stat.getGeneratedKeys();
    rs.next();
    UUID u1 = (UUID) rs.getObject(1);
    rs.next();
    UUID u2 = (UUID) rs.getObject(1);
    assertFalse(rs.next());
    rs = stat.executeQuery("SELECT ID FROM TEST ORDER BY VALUE");
    rs.next();
    assertEquals(u1, rs.getObject(1));
    rs.next();
    assertEquals(u2, rs.getObject(1));
    stat.execute("DROP TRIGGER TEST_INSERT");
    stat.execute("DROP TABLE TEST");
}
Also used : JdbcStatement(org.h2.jdbc.JdbcStatement) Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) JdbcPreparedStatement(org.h2.jdbc.JdbcPreparedStatement) ResultSet(java.sql.ResultSet) UUID(java.util.UUID)

Aggregations

Task (org.h2.util.Task)69 Connection (java.sql.Connection)68 Statement (java.sql.Statement)64 PreparedStatement (java.sql.PreparedStatement)60 ResultSet (java.sql.ResultSet)48 SQLException (java.sql.SQLException)42 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)24 SimpleResultSet (org.h2.tools.SimpleResultSet)24 MVStore (org.h2.mvstore.MVStore)20 Random (java.util.Random)19 JdbcConnection (org.h2.jdbc.JdbcConnection)19 CallableStatement (java.sql.CallableStatement)14 DbException (org.h2.message.DbException)13 IOException (java.io.IOException)10 JdbcSQLException (org.h2.jdbc.JdbcSQLException)7 ArrayList (java.util.ArrayList)6 Expression (org.h2.expression.Expression)6 ValueString (org.h2.value.ValueString)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 OutputStream (java.io.OutputStream)4