Search in sources :

Example 46 with Db

use of org.h2.jaqu.Db in project h2database by h2database.

the class TestTools method testChangeFileEncryption.

private void testChangeFileEncryption(boolean split) throws SQLException {
    org.h2.Driver.load();
    final String dir = (split ? "split:19:" : "") + getBaseDir();
    String url = "jdbc:h2:" + dir + "/testChangeFileEncryption;CIPHER=AES";
    DeleteDbFiles.execute(dir, "testChangeFileEncryption", true);
    Connection conn = getConnection(url, "sa", "abc 123");
    Statement stat = conn.createStatement();
    stat.execute("CREATE TABLE TEST(ID INT PRIMARY KEY, DATA CLOB) " + "AS SELECT X, SPACE(3000) FROM SYSTEM_RANGE(1, 300)");
    conn.close();
    String[] args = { "-dir", dir, "-db", "testChangeFileEncryption", "-cipher", "AES", "-decrypt", "abc", "-quiet" };
    ChangeFileEncryption.main(args);
    args = new String[] { "-dir", dir, "-db", "testChangeFileEncryption", "-cipher", "AES", "-encrypt", "def", "-quiet" };
    ChangeFileEncryption.main(args);
    conn = getConnection(url, "sa", "def 123");
    stat = conn.createStatement();
    stat.execute("SELECT * FROM TEST");
    new AssertThrows(ErrorCode.CANNOT_CHANGE_SETTING_WHEN_OPEN_1) {

        @Override
        public void test() throws SQLException {
            ChangeFileEncryption.main(new String[] { "-dir", dir, "-db", "testChangeFileEncryption", "-cipher", "AES", "-decrypt", "def", "-quiet" });
        }
    };
    conn.close();
    args = new String[] { "-dir", dir, "-db", "testChangeFileEncryption", "-quiet" };
    DeleteDbFiles.main(args);
}
Also used : AssertThrows(org.h2.test.utils.AssertThrows) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection)

Example 47 with Db

use of org.h2.jaqu.Db in project h2database by h2database.

the class TestTools method testRemove.

private void testRemove() throws SQLException {
    if (config.mvStore) {
        return;
    }
    deleteDb("toolsRemove");
    org.h2.Driver.load();
    String url = "jdbc:h2:" + getBaseDir() + "/toolsRemove";
    Connection conn = getConnection(url, "sa", "sa");
    Statement stat = conn.createStatement();
    stat.execute("create table test(id int primary key, name varchar)");
    stat.execute("insert into test values(1, 'Hello')");
    conn.close();
    Recover.main("-dir", getBaseDir(), "-db", "toolsRemove", "-removePassword");
    conn = getConnection(url, "sa", "");
    stat = conn.createStatement();
    ResultSet rs;
    rs = stat.executeQuery("select * from test");
    rs.next();
    assertEquals(1, rs.getInt(1));
    assertEquals("Hello", rs.getString(2));
    conn.close();
    deleteDb("toolsRemove");
    FileUtils.delete(getBaseDir() + "/toolsRemove.h2.sql");
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) SimpleResultSet(org.h2.tools.SimpleResultSet)

Example 48 with Db

use of org.h2.jaqu.Db in project h2database by h2database.

the class TestTools method testScriptRunscriptLob.

private void testScriptRunscriptLob() throws Exception {
    org.h2.Driver.load();
    String url = getURL("jdbc:h2:" + getBaseDir() + "/testScriptRunscriptLob", true);
    String user = "sa", password = "abc";
    String fileName = getBaseDir() + "/b2.sql";
    Connection conn = getConnection(url, user, password);
    conn.createStatement().execute("CREATE TABLE TEST(ID INT PRIMARY KEY, BDATA BLOB, CDATA CLOB)");
    PreparedStatement prep = conn.prepareStatement("INSERT INTO TEST VALUES(?, ?, ?)");
    prep.setInt(1, 1);
    prep.setNull(2, Types.BLOB);
    prep.setNull(3, Types.CLOB);
    prep.execute();
    prep.setInt(1, 2);
    prep.setString(2, "face");
    prep.setString(3, "face");
    prep.execute();
    Random random = new Random(1);
    prep.setInt(1, 3);
    byte[] large = new byte[getSize(10 * 1024, 100 * 1024)];
    random.nextBytes(large);
    prep.setBytes(2, large);
    String largeText = new String(large, StandardCharsets.ISO_8859_1);
    prep.setString(3, largeText);
    prep.execute();
    for (int i = 0; i < 2; i++) {
        ResultSet rs = conn.createStatement().executeQuery("SELECT * FROM TEST ORDER BY ID");
        rs.next();
        assertEquals(1, rs.getInt(1));
        assertNull(rs.getString(2));
        assertNull(rs.getString(3));
        rs.next();
        assertEquals(2, rs.getInt(1));
        assertEquals("face", rs.getString(2));
        assertEquals("face", rs.getString(3));
        rs.next();
        assertEquals(3, rs.getInt(1));
        assertEquals(large, rs.getBytes(2));
        assertEquals(largeText, rs.getString(3));
        assertFalse(rs.next());
        conn.close();
        Script.main("-url", url, "-user", user, "-password", password, "-script", fileName);
        DeleteDbFiles.main("-dir", getBaseDir(), "-db", "testScriptRunscriptLob", "-quiet");
        RunScript.main("-url", url, "-user", user, "-password", password, "-script", fileName);
        conn = getConnection("jdbc:h2:" + getBaseDir() + "/testScriptRunscriptLob", "sa", "abc");
    }
    conn.close();
}
Also used : Random(java.util.Random) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) SimpleResultSet(org.h2.tools.SimpleResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 49 with Db

use of org.h2.jaqu.Db in project h2database by h2database.

the class TestTools method testChangeFileEncryptionWithWrongPassword.

private void testChangeFileEncryptionWithWrongPassword() throws SQLException {
    if (config.mvStore) {
        // doesn't detect wrong passwords
        return;
    }
    org.h2.Driver.load();
    final String dir = getBaseDir();
    // TODO: this doesn't seem to work in MVSTORE mode yet
    String url = "jdbc:h2:" + dir + "/testChangeFileEncryption;CIPHER=AES";
    DeleteDbFiles.execute(dir, "testChangeFileEncryption", true);
    Connection conn = getConnection(url, "sa", "abc 123");
    Statement stat = conn.createStatement();
    stat.execute("CREATE TABLE TEST(ID INT PRIMARY KEY, DATA CLOB) " + "AS SELECT X, SPACE(3000) FROM SYSTEM_RANGE(1, 300)");
    conn.close();
    // try with wrong password, this used to have a bug where it kept the
    // file handle open
    new AssertThrows(SQLException.class) {

        @Override
        public void test() throws SQLException {
            ChangeFileEncryption.execute(dir, "testChangeFileEncryption", "AES", "wrong".toCharArray(), "def".toCharArray(), true);
        }
    };
    ChangeFileEncryption.execute(dir, "testChangeFileEncryption", "AES", "abc".toCharArray(), "def".toCharArray(), true);
    conn = getConnection(url, "sa", "def 123");
    stat = conn.createStatement();
    stat.execute("SELECT * FROM TEST");
    conn.close();
    String[] args = new String[] { "-dir", dir, "-db", "testChangeFileEncryption", "-quiet" };
    DeleteDbFiles.main(args);
}
Also used : AssertThrows(org.h2.test.utils.AssertThrows) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection)

Example 50 with Db

use of org.h2.jaqu.Db in project h2database by h2database.

the class TestRecovery method testCompressedAndUncompressed.

private void testCompressedAndUncompressed() throws SQLException {
    DeleteDbFiles.execute(getBaseDir(), "recovery", true);
    DeleteDbFiles.execute(getBaseDir(), "recovery2", true);
    org.h2.Driver.load();
    Connection conn = getConnection("recovery");
    Statement stat = conn.createStatement();
    stat.execute("create table test(id int primary key, data clob)");
    stat.execute("insert into test values(1, space(10000))");
    stat.execute("set compress_lob lzf");
    stat.execute("insert into test values(2, space(10000))");
    conn.close();
    Recover rec = new Recover();
    rec.runTool("-dir", getBaseDir(), "-db", "recovery");
    Connection conn2 = getConnection("recovery2");
    Statement stat2 = conn2.createStatement();
    String name = "recovery.h2.sql";
    stat2.execute("runscript from '" + getBaseDir() + "/" + name + "'");
    stat2.execute("select * from test");
    conn2.close();
    conn = getConnection("recovery");
    stat = conn.createStatement();
    conn2 = getConnection("recovery2");
    stat2 = conn2.createStatement();
    assertEqualDatabases(stat, stat2);
    conn.close();
    conn2.close();
    DeleteDbFiles.execute(getBaseDir(), "recovery", true);
    DeleteDbFiles.execute(getBaseDir(), "recovery2", true);
}
Also used : Statement(java.sql.Statement) Connection(java.sql.Connection) Recover(org.h2.tools.Recover)

Aggregations

Database (org.h2.engine.Database)70 Connection (java.sql.Connection)31 Statement (java.sql.Statement)20 Table (org.h2.table.Table)19 PreparedStatement (java.sql.PreparedStatement)18 ResultSet (java.sql.ResultSet)13 SQLException (java.sql.SQLException)13 Column (org.h2.table.Column)12 JdbcDataSource (org.h2.jdbcx.JdbcDataSource)9 StatementBuilder (org.h2.util.StatementBuilder)9 DbObject (org.h2.engine.DbObject)8 File (java.io.File)7 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)7 DbException (org.h2.message.DbException)7 Schema (org.h2.schema.Schema)7 Before (org.junit.Before)7 InputStream (java.io.InputStream)6 ExpressionColumn (org.h2.expression.ExpressionColumn)6 JdbcConnection (org.h2.jdbc.JdbcConnection)6