Search in sources :

Example 31 with Out

use of org.h2.dev.util.BitStream.Out in project h2database by h2database.

the class TestFunctions method testFileRead.

private void testFileRead() throws Exception {
    Connection conn = getConnection("functions");
    Statement stat = conn.createStatement();
    String fileName = getBaseDir() + "/test.txt";
    Properties prop = System.getProperties();
    OutputStream out = FileUtils.newOutputStream(fileName, false);
    prop.store(out, "");
    out.close();
    ResultSet rs = stat.executeQuery("SELECT LENGTH(FILE_READ('" + fileName + "')) LEN");
    rs.next();
    assertEquals(FileUtils.size(fileName), rs.getInt(1));
    rs = stat.executeQuery("SELECT FILE_READ('" + fileName + "') PROP");
    rs.next();
    Properties p2 = new Properties();
    p2.load(rs.getBinaryStream(1));
    assertEquals(prop.size(), p2.size());
    rs = stat.executeQuery("SELECT FILE_READ('" + fileName + "', NULL) PROP");
    rs.next();
    String ps = rs.getString(1);
    InputStreamReader r = new InputStreamReader(FileUtils.newInputStream(fileName));
    String ps2 = IOUtils.readStringAndClose(r, -1);
    assertEquals(ps, ps2);
    conn.close();
    FileUtils.delete(fileName);
}
Also used : InputStreamReader(java.io.InputStreamReader) PreparedStatement(java.sql.PreparedStatement) CallableStatement(java.sql.CallableStatement) Statement(java.sql.Statement) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) SimpleResultSet(org.h2.tools.SimpleResultSet) Properties(java.util.Properties)

Example 32 with Out

use of org.h2.dev.util.BitStream.Out in project h2database by h2database.

the class TestLob method testRemovedAfterTimeout.

private void testRemovedAfterTimeout() throws Exception {
    if (config.lazy) {
        return;
    }
    deleteDb("lob");
    final String url = getURL("lob;lob_timeout=50", true);
    Connection conn = getConnection(url);
    Statement stat = conn.createStatement();
    stat.execute("create table test(id int primary key, data clob)");
    PreparedStatement prep = conn.prepareStatement("insert into test values(?, ?)");
    prep.setInt(1, 1);
    prep.setString(2, "aaa" + new String(new char[1024 * 16]).replace((char) 0, 'x'));
    prep.execute();
    prep.setInt(1, 2);
    prep.setString(2, "bbb" + new String(new char[1024 * 16]).replace((char) 0, 'x'));
    prep.execute();
    ResultSet rs = stat.executeQuery("select * from test order by id");
    rs.next();
    Clob c1 = rs.getClob(2);
    assertEquals("aaa", c1.getSubString(1, 3));
    rs.next();
    assertEquals("aaa", c1.getSubString(1, 3));
    rs.close();
    assertEquals("aaa", c1.getSubString(1, 3));
    stat.execute("delete from test");
    c1.getSubString(1, 3);
    // wait until it times out
    Thread.sleep(100);
    // start a new transaction, to be sure
    stat.execute("delete from test");
    assertThrows(SQLException.class, c1).getSubString(1, 3);
    conn.close();
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Clob(java.sql.Clob)

Example 33 with Out

use of org.h2.dev.util.BitStream.Out 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 34 with Out

use of org.h2.dev.util.BitStream.Out in project h2database by h2database.

the class TestCsv method testPseudoBom.

private void testPseudoBom() throws Exception {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    // UTF-8 "BOM" / marker
    out.write(StringUtils.convertHexToBytes("ef" + "bb" + "bf"));
    out.write("\"ID\", \"NAME\"\n1, Hello".getBytes(StandardCharsets.UTF_8));
    byte[] buff = out.toByteArray();
    Reader r = new InputStreamReader(new ByteArrayInputStream(buff), StandardCharsets.UTF_8);
    ResultSet rs = new Csv().read(r, null);
    assertEquals("ID", rs.getMetaData().getColumnLabel(1));
    assertEquals("NAME", rs.getMetaData().getColumnLabel(2));
    assertTrue(rs.next());
    assertEquals(1, rs.getInt(1));
    assertEquals("Hello", rs.getString(2));
    assertFalse(rs.next());
}
Also used : InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) Csv(org.h2.tools.Csv) ResultSet(java.sql.ResultSet) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 35 with Out

use of org.h2.dev.util.BitStream.Out 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

ByteArrayOutputStream (java.io.ByteArrayOutputStream)27 IOException (java.io.IOException)23 OutputStream (java.io.OutputStream)19 ByteArrayInputStream (java.io.ByteArrayInputStream)17 SQLException (java.sql.SQLException)17 DbException (org.h2.message.DbException)17 Random (java.util.Random)11 ResultSet (java.sql.ResultSet)10 InputStream (java.io.InputStream)9 Statement (java.sql.Statement)9 Connection (java.sql.Connection)7 PrintStream (java.io.PrintStream)6 Properties (java.util.Properties)6 Task (org.h2.util.Task)6 BufferedOutputStream (java.io.BufferedOutputStream)5 File (java.io.File)5 FileOutputStream (java.io.FileOutputStream)5 InputStreamReader (java.io.InputStreamReader)5 PipedInputStream (java.io.PipedInputStream)5 OutputStreamWriter (java.io.OutputStreamWriter)4