Search in sources :

Example 1 with DropTable

use of org.h2.command.ddl.DropTable in project ignite by apache.

the class IgniteH2Indexing method unregisterCache.

/**
 * {@inheritDoc}
 */
@Override
public void unregisterCache(GridCacheContext cctx, boolean rmvIdx) {
    rowCache.onCacheUnregistered(cctx);
    String cacheName = cctx.name();
    String schemaName = schema(cacheName);
    H2Schema schema = schemas.get(schemaName);
    if (schema != null) {
        mapQryExec.onCacheStop(cacheName);
        dmlProc.onCacheStop(cacheName);
        // Remove this mapping only after callback to DML proc - it needs that mapping internally
        cacheName2schema.remove(cacheName);
        // Drop tables.
        Collection<H2TableDescriptor> rmvTbls = new HashSet<>();
        for (H2TableDescriptor tbl : schema.tables()) {
            if (F.eq(tbl.cache().name(), cacheName)) {
                try {
                    tbl.table().setRemoveIndexOnDestroy(rmvIdx);
                    dropTable(tbl);
                } catch (IgniteCheckedException e) {
                    U.error(log, "Failed to drop table on cache stop (will ignore): " + tbl.fullTableName(), e);
                }
                schema.drop(tbl);
                rmvTbls.add(tbl);
            }
        }
        if (!isDefaultSchema(schemaName)) {
            synchronized (schemaMux) {
                if (schema.decrementUsageCount() == 0) {
                    schemas.remove(schemaName);
                    try {
                        dropSchema(schemaName);
                    } catch (IgniteCheckedException e) {
                        U.error(log, "Failed to drop schema on cache stop (will ignore): " + cacheName, e);
                    }
                }
            }
        }
        stmtCache.clear();
        for (H2TableDescriptor tbl : rmvTbls) {
            for (Index idx : tbl.table().getIndexes()) idx.close(null);
        }
        int cacheId = CU.cacheId(cacheName);
        for (Iterator<Map.Entry<H2TwoStepCachedQueryKey, H2TwoStepCachedQuery>> it = twoStepCache.entrySet().iterator(); it.hasNext(); ) {
            Map.Entry<H2TwoStepCachedQueryKey, H2TwoStepCachedQuery> e = it.next();
            GridCacheTwoStepQuery qry = e.getValue().query();
            if (!F.isEmpty(qry.cacheIds()) && qry.cacheIds().contains(cacheId))
                it.remove();
        }
    }
}
Also used : GridCacheTwoStepQuery(org.apache.ignite.internal.processors.cache.query.GridCacheTwoStepQuery) Index(org.h2.index.Index) H2TreeIndex(org.apache.ignite.internal.processors.query.h2.database.H2TreeIndex) IgniteSystemProperties.getString(org.apache.ignite.IgniteSystemProperties.getString) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) GridBoundedConcurrentLinkedHashMap(org.apache.ignite.internal.util.GridBoundedConcurrentLinkedHashMap) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet)

Example 2 with DropTable

use of org.h2.command.ddl.DropTable in project nifi by apache.

the class DBCPServiceTest method createInsertSelectDrop.

protected void createInsertSelectDrop(Connection con) throws SQLException {
    final Statement st = con.createStatement();
    try {
        st.executeUpdate(dropTable);
    } catch (final Exception e) {
    // table may not exist, this is not serious problem.
    }
    st.executeUpdate(createTable);
    st.executeUpdate("insert into restaurants values (1, 'Irifunes', 'San Mateo')");
    st.executeUpdate("insert into restaurants values (2, 'Estradas', 'Daly City')");
    st.executeUpdate("insert into restaurants values (3, 'Prime Rib House', 'San Francisco')");
    int nrOfRows = 0;
    final ResultSet resultSet = st.executeQuery("select * from restaurants");
    while (resultSet.next()) nrOfRows++;
    assertEquals(3, nrOfRows);
    st.close();
}
Also used : Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) InitializationException(org.apache.nifi.reporting.InitializationException) ProcessException(org.apache.nifi.processor.exception.ProcessException) SQLException(java.sql.SQLException) ExpectedException(org.junit.rules.ExpectedException) MalformedURLException(java.net.MalformedURLException) JdbcSQLException(org.h2.jdbc.JdbcSQLException)

Example 3 with DropTable

use of org.h2.command.ddl.DropTable in project ignite by apache.

the class GridSqlQueryParser method parseDropTable.

/**
 * Parse {@code DROP TABLE} statement.
 *
 * @param dropTbl {@code DROP TABLE} statement.
 * @see <a href="http://h2database.com/html/grammar.html#drop_table">H2 {@code DROP TABLE} spec.</a>
 */
private GridSqlDropTable parseDropTable(DropTable dropTbl) {
    GridSqlDropTable res = new GridSqlDropTable();
    Schema schema = SCHEMA_COMMAND_SCHEMA.get(dropTbl);
    res.schemaName(schema.getName());
    res.ifExists(DROP_TABLE_IF_EXISTS.get(dropTbl));
    res.tableName(DROP_TABLE_NAME.get(dropTbl));
    return res;
}
Also used : Schema(org.h2.schema.Schema)

Example 4 with DropTable

use of org.h2.command.ddl.DropTable in project ignite by apache.

the class IgniteH2Indexing method unregisterCache.

/** {@inheritDoc} */
@Override
public void unregisterCache(String cacheName) {
    String schemaName = schema(cacheName);
    boolean dflt = isDefaultSchema(schemaName);
    H2Schema schema = dflt ? schemas.get(schemaName) : schemas.remove(schemaName);
    if (schema != null) {
        mapQryExec.onCacheStop(cacheName);
        dmlProc.onCacheStop(cacheName);
        // Remove this mapping only after callback to DML proc - it needs that mapping internally
        cacheName2schema.remove(cacheName);
        // Drop tables.
        Collection<H2TableDescriptor> rmvTbls = new HashSet<>();
        for (H2TableDescriptor tbl : schema.tables()) {
            if (F.eq(tbl.cache().name(), cacheName)) {
                try {
                    dropTable(tbl);
                } catch (IgniteCheckedException e) {
                    U.error(log, "Failed to drop table on cache stop (will ignore): " + tbl.fullTableName(), e);
                }
                schema.drop(tbl);
                rmvTbls.add(tbl);
            }
        }
        if (!dflt) {
            try {
                dropSchema(schemaName);
            } catch (IgniteCheckedException e) {
                U.error(log, "Failed to drop schema on cache stop (will ignore): " + cacheName, e);
            }
        }
        for (H2TableDescriptor tbl : rmvTbls) {
            for (Index idx : tbl.table().getIndexes()) idx.close(null);
        }
        int cacheId = CU.cacheId(cacheName);
        for (Iterator<Map.Entry<H2TwoStepCachedQueryKey, H2TwoStepCachedQuery>> it = twoStepCache.entrySet().iterator(); it.hasNext(); ) {
            Map.Entry<H2TwoStepCachedQueryKey, H2TwoStepCachedQuery> e = it.next();
            GridCacheTwoStepQuery qry = e.getValue().query();
            if (!F.isEmpty(qry.cacheIds()) && qry.cacheIds().contains(cacheId))
                it.remove();
        }
    }
}
Also used : GridCacheTwoStepQuery(org.apache.ignite.internal.processors.cache.query.GridCacheTwoStepQuery) Index(org.h2.index.Index) H2TreeIndex(org.apache.ignite.internal.processors.query.h2.database.H2TreeIndex) H2PkHashIndex(org.apache.ignite.internal.processors.query.h2.database.H2PkHashIndex) IgniteSystemProperties.getString(org.apache.ignite.IgniteSystemProperties.getString) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) GridBoundedConcurrentLinkedHashMap(org.apache.ignite.internal.util.GridBoundedConcurrentLinkedHashMap) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet)

Example 5 with DropTable

use of org.h2.command.ddl.DropTable in project ignite by apache.

the class IgniteH2Indexing method dropTable.

/**
 * Drops table form h2 database and clear all related indexes (h2 text, lucene).
 *
 * @param tbl Table to unregister.
 * @throws IgniteCheckedException If failed to unregister.
 */
private void dropTable(H2TableDescriptor tbl) throws IgniteCheckedException {
    assert tbl != null;
    if (log.isDebugEnabled())
        log.debug("Removing query index table: " + tbl.fullTableName());
    Connection c = connectionForThread(tbl.schemaName());
    Statement stmt = null;
    try {
        stmt = c.createStatement();
        String sql = "DROP TABLE IF EXISTS " + tbl.fullTableName();
        if (log.isDebugEnabled())
            log.debug("Dropping database index table with SQL: " + sql);
        stmt.executeUpdate(sql);
    } catch (SQLException e) {
        onSqlException();
        throw new IgniteSQLException("Failed to drop database index table [type=" + tbl.type().name() + ", table=" + tbl.fullTableName() + "]", IgniteQueryErrorCode.TABLE_DROP_FAILED, e);
    } finally {
        U.close(stmt, log);
    }
}
Also used : SQLException(java.sql.SQLException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) PreparedStatement(java.sql.PreparedStatement) JdbcStatement(org.h2.jdbc.JdbcStatement) Statement(java.sql.Statement) GridSqlStatement(org.apache.ignite.internal.processors.query.h2.sql.GridSqlStatement) Connection(java.sql.Connection) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) IgniteSystemProperties.getString(org.apache.ignite.IgniteSystemProperties.getString)

Aggregations

SQLException (java.sql.SQLException)3 HashSet (java.util.HashSet)3 LinkedHashSet (java.util.LinkedHashSet)3 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)3 IgniteSystemProperties.getString (org.apache.ignite.IgniteSystemProperties.getString)3 Index (org.h2.index.Index)3 Statement (java.sql.Statement)2 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ConcurrentMap (java.util.concurrent.ConcurrentMap)2 GridCacheTwoStepQuery (org.apache.ignite.internal.processors.cache.query.GridCacheTwoStepQuery)2 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)2 H2TreeIndex (org.apache.ignite.internal.processors.query.h2.database.H2TreeIndex)2 GridBoundedConcurrentLinkedHashMap (org.apache.ignite.internal.util.GridBoundedConcurrentLinkedHashMap)2 MalformedURLException (java.net.MalformedURLException)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 IgniteException (org.apache.ignite.IgniteException)1 H2PkHashIndex (org.apache.ignite.internal.processors.query.h2.database.H2PkHashIndex)1