Search in sources :

Example 1 with EmbedConnection

use of org.apache.derby.impl.jdbc.EmbedConnection in project derby by apache.

the class NoDBInternalsPermissionTest method test_002_EmbedConnection.

/**
 * <p>
 * Verify that user code can't call EmbedConnection.getContextManager().
 * </p>
 */
public void test_002_EmbedConnection() throws Exception {
    try {
        Connection conn = getConnection();
        ((EmbedConnection) conn).getContextManager();
        fail("Should have raised an AccessControlException");
    } catch (AccessControlException e) {
        println("Caught an AccessControlException");
    }
}
Also used : Connection(java.sql.Connection) EmbedConnection(org.apache.derby.impl.jdbc.EmbedConnection) AccessControlException(java.security.AccessControlException) EmbedConnection(org.apache.derby.impl.jdbc.EmbedConnection)

Example 2 with EmbedConnection

use of org.apache.derby.impl.jdbc.EmbedConnection in project derby by apache.

the class NoDBInternalsPermissionTest method test_005_EmbedConnection_getLCC.

/**
 * <p>
 * Verify that you need usederbyinternals permission to get the LCC from a Connection.
 * See DERBY-6751.
 * </p>
 */
public void test_005_EmbedConnection_getLCC() throws Exception {
    try {
        Connection conn = getConnection();
        ((EmbedConnection) conn).getLanguageConnection();
        fail("Should have raised an AccessControlException");
    } catch (AccessControlException e) {
        println("Caught an AccessControlException");
    }
}
Also used : Connection(java.sql.Connection) EmbedConnection(org.apache.derby.impl.jdbc.EmbedConnection) AccessControlException(java.security.AccessControlException) EmbedConnection(org.apache.derby.impl.jdbc.EmbedConnection)

Example 3 with EmbedConnection

use of org.apache.derby.impl.jdbc.EmbedConnection in project derby by apache.

the class RawDBReader method createViews.

/**
 * Create table functions and views on corrupt user tables. These objects
 * are created in the healthy database. Write the recovery
 * script.
 */
private void createViews(Connection conn, String recoveryScriptName, String controlSchema, String schemaPrefix, String corruptDBLocation, String encryptionAttributes, String dbo, String dboPassword) throws SQLException {
    File recoveryScript = new File(recoveryScriptName);
    PrintWriter scriptWriter = null;
    try {
        scriptWriter = new PrintWriter(recoveryScript);
    } catch (Exception e) {
        throw wrap(e);
    }
    String localDBName = ((EmbedConnection) conn).getDBName();
    scriptWriter.println("connect 'jdbc:derby:" + localDBName + "';\n");
    PreparedStatement ps = prepareStatement(conn, "select s.schemaName, t.tableName, g.conglomerateNumber, c.columnName, c.columnNumber, c.columnDatatype\n" + "from " + controlSchema + ".sysschemas s,\n" + controlSchema + ".systables t,\n" + controlSchema + ".sysconglomerates g,\n" + controlSchema + ".syscolumns c\n" + "where s.schemaName not like 'SYS%' and schemaName != 'NULLID' and schemaName != 'SQLJ'\n" + "and s.schemaID = t.schemaID\n" + "and t.tableID = g.tableID and not g.isindex\n" + "and t.tableID = c.referenceID\n" + "order by s.schemaName, t.tableName, c.columnNumber");
    ResultSet rs = ps.executeQuery();
    ArrayList<String> columnNames = new ArrayList<String>();
    ArrayList<TypeDescriptor> columnTypes = new ArrayList<TypeDescriptor>();
    String corruptSchemaName = null;
    String corruptTableName = null;
    String schemaName = null;
    String tableName = null;
    long conglomerateNumber = -1L;
    while (rs.next()) {
        int col = 1;
        String currentCorruptSchemaName = rs.getString(col++);
        String currentCorruptTableName = rs.getString(col++);
        if (!currentCorruptSchemaName.equals(corruptSchemaName)) {
            scriptWriter.println("create schema " + IdUtil.normalToDelimited(currentCorruptSchemaName) + ";\n");
        }
        String newSchemaName = makeSchemaName(schemaPrefix, currentCorruptSchemaName);
        String newTableName = IdUtil.normalToDelimited(currentCorruptTableName);
        if (schemaName != null) {
            if (!schemaName.equals(newSchemaName) || !tableName.equals(newTableName)) {
                createView(conn, scriptWriter, controlSchema, corruptSchemaName, corruptTableName, schemaName, tableName, conglomerateNumber, columnNames, columnTypes, corruptDBLocation, encryptionAttributes, dbo, dboPassword);
                columnNames.clear();
                columnTypes.clear();
            }
        }
        corruptSchemaName = currentCorruptSchemaName;
        corruptTableName = currentCorruptTableName;
        schemaName = newSchemaName;
        tableName = newTableName;
        conglomerateNumber = rs.getLong(col++);
        columnNames.add(normalizeColumnName(rs.getString(col++)));
        // only need the column number to order the results
        col++;
        columnTypes.add((TypeDescriptor) rs.getObject(col++));
    }
    // create last view
    if (schemaName != null) {
        createView(conn, scriptWriter, controlSchema, corruptSchemaName, corruptTableName, schemaName, tableName, conglomerateNumber, columnNames, columnTypes, corruptDBLocation, encryptionAttributes, dbo, dboPassword);
    }
    rs.close();
    ps.close();
    scriptWriter.flush();
    scriptWriter.close();
}
Also used : ArrayList(java.util.ArrayList) EmbedConnection(org.apache.derby.impl.jdbc.EmbedConnection) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) SQLException(java.sql.SQLException) TypeDescriptor(org.apache.derby.catalog.TypeDescriptor) ResultSet(java.sql.ResultSet) File(java.io.File) PrintWriter(java.io.PrintWriter)

Example 4 with EmbedConnection

use of org.apache.derby.impl.jdbc.EmbedConnection in project derby by apache.

the class BaseJDBCTestCase method checkEstimatedRowCount.

/**
 * Return estimated row count for runtime statistics.
 * Requires caller first turned on RuntimeStatistics, executed a query and closed the ResultSet.
 *
 * For client calls we just return as we can't find out this information.
 * @param conn
 * @param expectedCount
 * @throws SQLException
 */
public static void checkEstimatedRowCount(Connection conn, double expectedCount) throws SQLException {
    if (!(conn instanceof EmbedConnection)) {
        return;
    }
    EmbedConnection econn = (EmbedConnection) conn;
    LanguageConnectionContext lcc = (LanguageConnectionContext) getLanguageConnectionContext(econn);
    RunTimeStatistics rts = lcc.getRunTimeStatisticsObject();
    assertNotNull(" RuntimeStatistics is null. Did you call SYSCS_UTIL.SYSCS_SET_RUNTIMESTATISTICS(1)?", rts);
    assertEquals((long) expectedCount, (long) rts.getEstimatedRowCount());
}
Also used : RunTimeStatistics(org.apache.derby.iapi.sql.execute.RunTimeStatistics) LanguageConnectionContext(org.apache.derby.iapi.sql.conn.LanguageConnectionContext) EmbedConnection(org.apache.derby.impl.jdbc.EmbedConnection)

Example 5 with EmbedConnection

use of org.apache.derby.impl.jdbc.EmbedConnection in project derby by apache.

the class EmbedPooledConnection method openRealConnection.

final void openRealConnection() throws SQLException {
    // first time we establish a connection
    Connection rc = dataSource.getConnection(username, password, requestPassword);
    this.realConnection = (EmbedConnection) rc;
    defaultIsolationLevel = rc.getTransactionIsolation();
    defaultReadOnly = rc.isReadOnly();
    if (currentConnectionHandle != null)
        realConnection.setApplicationConnection(currentConnectionHandle);
}
Also used : Connection(java.sql.Connection) BrokeredConnection(org.apache.derby.iapi.jdbc.BrokeredConnection) EngineConnection(org.apache.derby.iapi.jdbc.EngineConnection) EmbedConnection(org.apache.derby.impl.jdbc.EmbedConnection)

Aggregations

EmbedConnection (org.apache.derby.impl.jdbc.EmbedConnection)7 Connection (java.sql.Connection)4 BrokeredConnection (org.apache.derby.iapi.jdbc.BrokeredConnection)3 AccessControlException (java.security.AccessControlException)2 SQLException (java.sql.SQLException)2 EngineConnection (org.apache.derby.iapi.jdbc.EngineConnection)2 File (java.io.File)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 ArrayList (java.util.ArrayList)1 TypeDescriptor (org.apache.derby.catalog.TypeDescriptor)1 LanguageConnectionContext (org.apache.derby.iapi.sql.conn.LanguageConnectionContext)1 RunTimeStatistics (org.apache.derby.iapi.sql.execute.RunTimeStatistics)1