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();
}
}
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();
}
}
}
}
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));
}
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();
}
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");
}
Aggregations