Search in sources :

Example 56 with AssertThrows

use of org.h2.test.utils.AssertThrows in project h2database by h2database.

the class TestSynonymForTable method testCreateForUnknownTable.

private void testCreateForUnknownTable() throws SQLException {
    Connection conn = getConnection("synonym");
    Statement stat = conn.createStatement();
    assertThrows(JdbcSQLException.class, stat).execute("CREATE SYNONYM someSynonym FOR nonexistingTable");
    conn.close();
}
Also used : Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) Connection(java.sql.Connection) JdbcSQLException(org.h2.jdbc.JdbcSQLException)

Example 57 with AssertThrows

use of org.h2.test.utils.AssertThrows in project h2database by h2database.

the class TestSynonymForTable method testCreateOrReplaceExistingTable.

private void testCreateOrReplaceExistingTable() throws SQLException {
    Connection conn = getConnection("synonym");
    Statement stat = conn.createStatement();
    stat.execute("CREATE TABLE IF NOT EXISTS backingtable(id INT PRIMARY KEY)");
    assertThrows(JdbcSQLException.class, stat).execute("CREATE OR REPLACE SYNONYM backingtable FOR backingtable");
    conn.close();
}
Also used : Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) Connection(java.sql.Connection) JdbcSQLException(org.h2.jdbc.JdbcSQLException)

Example 58 with AssertThrows

use of org.h2.test.utils.AssertThrows in project h2database by h2database.

the class TestSynonymForTable method testDropTable.

private void testDropTable() throws SQLException {
    Connection conn = getConnection("synonym");
    createTableWithSynonym(conn);
    Statement stat = conn.createStatement();
    stat.execute("DROP TABLE backingtable");
    // Backing table does not exist anymore.
    assertThrows(JdbcSQLException.class, stat).execute("SELECT id FROM testsynonym");
    // Synonym should be dropped as well
    ResultSet synonyms = conn.createStatement().executeQuery("SELECT * FROM INFORMATION_SCHEMA.SYNONYMS WHERE SYNONYM_NAME='TESTSYNONYM'");
    assertFalse(synonyms.next());
    conn.close();
    // Reopening should work with dropped synonym
    Connection conn2 = getConnection("synonym");
    assertThrows(JdbcSQLException.class, stat).execute("SELECT id FROM testsynonym");
    conn2.close();
}
Also used : Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) JdbcSQLException(org.h2.jdbc.JdbcSQLException)

Example 59 with AssertThrows

use of org.h2.test.utils.AssertThrows in project h2database by h2database.

the class TestSynonymForTable method testDropSynonym.

private void testDropSynonym() throws SQLException {
    Connection conn = getConnection("synonym");
    createTableWithSynonym(conn);
    Statement stat = conn.createStatement();
    stat.execute("DROP SYNONYM testsynonym");
    // Synonym does not exist anymore.
    assertThrows(JdbcSQLException.class, stat).execute("SELECT id FROM testsynonym");
    // Dropping with "if exists" should succeed even if the synonym does not exist anymore.
    stat.execute("DROP SYNONYM IF EXISTS testsynonym");
    // Without "if exists" the command should fail if the synonym does not exist.
    assertThrows(JdbcSQLException.class, stat).execute("DROP SYNONYM testsynonym");
    conn.close();
}
Also used : Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) Connection(java.sql.Connection) JdbcSQLException(org.h2.jdbc.JdbcSQLException)

Example 60 with AssertThrows

use of org.h2.test.utils.AssertThrows in project h2database by h2database.

the class TestView method testParameterizedView.

private void testParameterizedView() throws SQLException {
    deleteDb("view");
    Connection conn = getConnection("view");
    Statement stat = conn.createStatement();
    stat.execute("CREATE TABLE Test(id INT AUTO_INCREMENT NOT NULL, " + "f1 VARCHAR NOT NULL, f2 VARCHAR NOT NULL)");
    stat.execute("INSERT INTO Test(f1, f2) VALUES ('value1','value2')");
    stat.execute("INSERT INTO Test(f1, f2) VALUES ('value1','value3')");
    PreparedStatement ps = conn.prepareStatement("CREATE VIEW Test_View AS SELECT f2 FROM Test WHERE f1=?");
    ps.setString(1, "value1");
    assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, ps).executeUpdate();
    // ResultSet rs;
    // rs = stat.executeQuery("SELECT * FROM Test_View");
    // assertTrue(rs.next());
    // assertFalse(rs.next());
    // rs = stat.executeQuery("select VIEW_DEFINITION " +
    // "from information_schema.views " +
    // "where TABLE_NAME='TEST_VIEW'");
    // rs.next();
    // assertEquals("...", rs.getString(1));
    conn.close();
}
Also used : Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) PreparedStatement(java.sql.PreparedStatement)

Aggregations

Connection (java.sql.Connection)40 Statement (java.sql.Statement)37 PreparedStatement (java.sql.PreparedStatement)35 ResultSet (java.sql.ResultSet)17 JdbcConnection (org.h2.jdbc.JdbcConnection)16 AssertThrows (org.h2.test.utils.AssertThrows)12 SQLException (java.sql.SQLException)8 JdbcSQLException (org.h2.jdbc.JdbcSQLException)8 SimpleResultSet (org.h2.tools.SimpleResultSet)8 CallableStatement (java.sql.CallableStatement)7 Server (org.h2.tools.Server)7 IOException (java.io.IOException)4 Clob (java.sql.Clob)4 Task (org.h2.util.Task)4 Reader (java.io.Reader)3 StringReader (java.io.StringReader)3 Method (java.lang.reflect.Method)3 BigDecimal (java.math.BigDecimal)3 FileChannel (java.nio.channels.FileChannel)3 Savepoint (java.sql.Savepoint)3