Search in sources :

Example 76 with Call

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

the class TestFunctions method testWhiteSpacesInParameters.

private void testWhiteSpacesInParameters() throws SQLException {
    deleteDb("functions");
    Connection conn = getConnection("functions");
    Statement stat = conn.createStatement();
    // with white space
    stat.execute("CREATE ALIAS PARSE_INT2 FOR " + "\"java.lang.Integer.parseInt(java.lang.String, int)\"");
    ResultSet rs;
    rs = stat.executeQuery("CALL PARSE_INT2('473', 10)");
    rs.next();
    assertEquals(473, rs.getInt(1));
    stat.execute("DROP ALIAS PARSE_INT2");
    // without white space
    stat.execute("CREATE ALIAS PARSE_INT2 FOR " + "\"java.lang.Integer.parseInt(java.lang.String,int)\"");
    stat.execute("DROP ALIAS PARSE_INT2");
    conn.close();
}
Also used : PreparedStatement(java.sql.PreparedStatement) CallableStatement(java.sql.CallableStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) SimpleResultSet(org.h2.tools.SimpleResultSet)

Example 77 with Call

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

the class TestLob method testDeadlock.

/**
 * Test for issue 315: Java Level Deadlock on Database & Session Objects
 */
private void testDeadlock() throws Exception {
    deleteDb("lob");
    Connection conn = getConnection("lob");
    Statement stat = conn.createStatement();
    stat.execute("create table test(id int primary key, name clob)");
    stat.execute("insert into test select x, space(10000) from system_range(1, 3)");
    final Connection conn2 = getConnection("lob");
    Task task = new Task() {

        @Override
        public void call() throws Exception {
            Statement stat = conn2.createStatement();
            stat.setFetchSize(1);
            for (int i = 0; !stop; i++) {
                ResultSet rs = stat.executeQuery("select * from test where id > -" + i);
                while (rs.next()) {
                // ignore
                }
            }
        }
    };
    task.execute();
    stat.execute("create table test2(id int primary key, name clob)");
    for (int i = 0; i < 100; i++) {
        stat.execute("delete from test2");
        stat.execute("insert into test2 values(1, space(10000 + " + i + "))");
    }
    task.get();
    conn.close();
    conn2.close();
}
Also used : Task(org.h2.util.Task) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) ResultSet(java.sql.ResultSet) Savepoint(java.sql.Savepoint)

Example 78 with Call

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

the class TestLob method testLobVariable.

private void testLobVariable() throws SQLException {
    deleteDb("lob");
    Connection conn = reconnect(null);
    Statement stat = conn.createStatement();
    stat.execute("CREATE TABLE TEST(ID INT, DATA CLOB)");
    stat.execute("INSERT INTO TEST VALUES(1, SPACE(100000))");
    stat.execute("SET @TOTAL = SELECT DATA FROM TEST WHERE ID=1");
    stat.execute("DROP TABLE TEST");
    stat.execute("CALL @TOTAL LIKE '%X'");
    stat.execute("CREATE TABLE TEST(ID INT, DATA CLOB)");
    stat.execute("INSERT INTO TEST VALUES(1, @TOTAL)");
    stat.execute("INSERT INTO TEST VALUES(2, @TOTAL)");
    stat.execute("DROP TABLE TEST");
    stat.execute("CALL @TOTAL LIKE '%X'");
    conn.close();
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection)

Example 79 with Call

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

the class TestLob method testConcurrentCreate.

private void testConcurrentCreate() throws Exception {
    deleteDb("lob");
    final JdbcConnection conn1 = (JdbcConnection) getConnection("lob");
    final JdbcConnection conn2 = (JdbcConnection) getConnection("lob");
    conn1.setAutoCommit(false);
    conn2.setAutoCommit(false);
    final byte[] buffer = new byte[10000];
    Task task1 = new Task() {

        @Override
        public void call() throws Exception {
            while (!stop) {
                Blob b = conn1.createBlob();
                OutputStream out = b.setBinaryStream(1);
                out.write(buffer);
                out.close();
            }
        }
    };
    Task task2 = new Task() {

        @Override
        public void call() throws Exception {
            while (!stop) {
                Blob b = conn2.createBlob();
                OutputStream out = b.setBinaryStream(1);
                out.write(buffer);
                out.close();
            }
        }
    };
    task1.execute();
    task2.execute();
    Thread.sleep(1000);
    task1.get();
    task2.get();
    conn1.close();
    conn2.close();
}
Also used : Task(org.h2.util.Task) Blob(java.sql.Blob) OutputStream(java.io.OutputStream) JdbcConnection(org.h2.jdbc.JdbcConnection)

Example 80 with Call

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

the class TestCsv method testNull.

/**
 * Test custom NULL string.
 */
private void testNull() throws Exception {
    deleteDb("csv");
    String fileName = getBaseDir() + "/testNull.csv";
    FileUtils.delete(fileName);
    OutputStream out = FileUtils.newOutputStream(fileName, false);
    String csvContent = "\"A\",\"B\",\"C\",\"D\"\n\\N,\"\",\"\\N\",";
    byte[] b = csvContent.getBytes(StandardCharsets.UTF_8);
    out.write(b, 0, b.length);
    out.close();
    Csv csv = new Csv();
    csv.setNullString("\\N");
    ResultSet rs = csv.read(fileName, null, "UTF8");
    ResultSetMetaData meta = rs.getMetaData();
    assertEquals(4, meta.getColumnCount());
    assertEquals("A", meta.getColumnLabel(1));
    assertEquals("B", meta.getColumnLabel(2));
    assertEquals("C", meta.getColumnLabel(3));
    assertEquals("D", meta.getColumnLabel(4));
    assertTrue(rs.next());
    assertEquals(null, rs.getString(1));
    assertEquals("", rs.getString(2));
    // null is never quoted
    assertEquals("\\N", rs.getString(3));
    // an empty string is always parsed as null
    assertEquals(null, rs.getString(4));
    assertFalse(rs.next());
    Connection conn = getConnection("csv");
    Statement stat = conn.createStatement();
    stat.execute("call csvwrite('" + fileName + "', 'select NULL as a, '''' as b, ''\\N'' as c, NULL as d', " + "'UTF8', ',', '\"', NULL, '\\N', '\n')");
    InputStreamReader reader = new InputStreamReader(FileUtils.newInputStream(fileName));
    // on read, an empty string is treated like null,
    // but on write a null is always written with the nullString
    String data = IOUtils.readStringAndClose(reader, -1);
    assertEquals(csvContent + "\\N", data.trim());
    conn.close();
    FileUtils.delete(fileName);
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) InputStreamReader(java.io.InputStreamReader) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) Csv(org.h2.tools.Csv) ResultSet(java.sql.ResultSet) Connection(java.sql.Connection)

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