Search in sources :

Example 36 with Table

use of org.h2.table.Table in project siena by mandubian.

the class FullText method init.

/**
     * Initializes full text search functionality for this database. This adds
     * the following Java functions to the database:
     * <ul>
     * <li>FT_CREATE_INDEX(schemaNameString, tableNameString,
     * columnListString)</li>
     * <li>FT_SEARCH(queryString, limitInt, offsetInt): result set</li>
     * <li>FT_REINDEX()</li>
     * <li>FT_DROP_ALL()</li>
     * </ul>
     * It also adds a schema FT to the database where bookkeeping information
     * is stored. This function may be called from a Java application, or by
     * using the SQL statements:
     *
     * <pre>
     * CREATE ALIAS IF NOT EXISTS FT_INIT FOR
     *      &quot;org.h2.fulltext.FullText.init&quot;;
     * CALL FT_INIT();
     * </pre>
     *
     * @param conn the connection
     */
public static void init(Connection conn) throws SQLException {
    Statement stat = conn.createStatement();
    stat.execute("CREATE SCHEMA IF NOT EXISTS " + SCHEMA);
    stat.execute("CREATE TABLE IF NOT EXISTS " + SCHEMA + ".FT_INDEXES(ID INT AUTO_INCREMENT PRIMARY KEY, SCHEMA VARCHAR, TABLE VARCHAR, COLUMNS VARCHAR, UNIQUE(SCHEMA, TABLE))");
    stat.execute("CREATE TABLE IF NOT EXISTS " + SCHEMA + ".WORDS(ID INT AUTO_INCREMENT PRIMARY KEY, NAME VARCHAR, UNIQUE(NAME))");
    stat.execute("CREATE TABLE IF NOT EXISTS " + SCHEMA + ".ROWS(ID IDENTITY, HASH INT, INDEXID INT, \"KEY\" VARCHAR, UNIQUE(HASH, INDEXID, \"KEY\"))");
    stat.execute("CREATE TABLE IF NOT EXISTS " + SCHEMA + ".MAP(ROWID INT, WORDID INT, PRIMARY KEY(WORDID, ROWID))");
    stat.execute("CREATE TABLE IF NOT EXISTS " + SCHEMA + ".IGNORELIST(LIST VARCHAR)");
    stat.execute("CREATE ALIAS IF NOT EXISTS FT_CREATE_INDEX FOR \"" + FullText.class.getName() + ".createIndex\"");
    stat.execute("CREATE ALIAS IF NOT EXISTS FT_DROP_INDEX FOR \"" + FullText.class.getName() + ".dropIndex\"");
    stat.execute("CREATE ALIAS IF NOT EXISTS FT_SEARCH FOR \"" + FullText.class.getName() + ".search\"");
    stat.execute("CREATE ALIAS IF NOT EXISTS FT_SEARCH_DATA FOR \"" + FullText.class.getName() + ".searchData\"");
    stat.execute("CREATE ALIAS IF NOT EXISTS FT_REINDEX FOR \"" + FullText.class.getName() + ".reindex\"");
    stat.execute("CREATE ALIAS IF NOT EXISTS FT_DROP_ALL FOR \"" + FullText.class.getName() + ".dropAll\"");
    FullTextSettings setting = FullTextSettings.getInstance(conn);
    ResultSet rs = stat.executeQuery("SELECT * FROM " + SCHEMA + ".IGNORELIST");
    while (rs.next()) {
        String commaSeparatedList = rs.getString(1);
        setIgnoreList(setting, commaSeparatedList);
    }
    rs = stat.executeQuery("SELECT * FROM " + SCHEMA + ".WORDS");
    HashMap<String, Integer> map = setting.getWordList();
    while (rs.next()) {
        String word = rs.getString("NAME");
        int id = rs.getInt("ID");
        word = setting.convertWord(word);
        if (word != null) {
            map.put(word, id);
        }
    }
    setting.setInitialized(true);
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) SimpleResultSet(org.h2.tools.SimpleResultSet) ResultSet(java.sql.ResultSet)

Example 37 with Table

use of org.h2.table.Table in project siena by mandubian.

the class FullText method indexExistingRows.

/**
     * Add the existing data to the index.
     *
     * @param conn the database connection
     * @param schema the schema name
     * @param table the table name
     */
protected static void indexExistingRows(Connection conn, String schema, String table) throws SQLException {
    FullText.FullTextTrigger existing = new FullText.FullTextTrigger();
    existing.init(conn, schema, null, table, false, Trigger.INSERT);
    String sql = "SELECT * FROM " + StringUtils.quoteIdentifier(schema) + "." + StringUtils.quoteIdentifier(table);
    ResultSet rs = conn.createStatement().executeQuery(sql);
    int columnCount = rs.getMetaData().getColumnCount();
    while (rs.next()) {
        Object[] row = new Object[columnCount];
        for (int i = 0; i < columnCount; i++) {
            row[i] = rs.getObject(i + 1);
        }
        existing.fire(conn, null, row);
    }
}
Also used : SimpleResultSet(org.h2.tools.SimpleResultSet) ResultSet(java.sql.ResultSet)

Example 38 with Table

use of org.h2.table.Table in project siena by mandubian.

the class FullText method dropIndex.

/**
     * Drop an existing full text index for a table. This method returns
     * silently if no index for this table exists.
     *
     * @param conn the connection
     * @param schema the schema name of the table (case sensitive)
     * @param table the table name (case sensitive)
     */
public static void dropIndex(Connection conn, String schema, String table) throws SQLException {
    init(conn);
    PreparedStatement prep = conn.prepareStatement("SELECT ID FROM " + SCHEMA + ".FT_INDEXES WHERE SCHEMA=? AND TABLE=?");
    prep.setString(1, schema);
    prep.setString(2, table);
    ResultSet rs = prep.executeQuery();
    if (!rs.next()) {
        return;
    }
    int indexId = rs.getInt(1);
    prep = conn.prepareStatement("DELETE FROM " + SCHEMA + ".FT_INDEXES WHERE ID=?");
    prep.setInt(1, indexId);
    prep.execute();
    createOrDropTrigger(conn, schema, table, false);
    prep = conn.prepareStatement("DELETE FROM " + SCHEMA + ".ROWS WHERE INDEXID=? AND ROWNUM<10000");
    while (true) {
        prep.setInt(1, indexId);
        int deleted = prep.executeUpdate();
        if (deleted == 0) {
            break;
        }
    }
    prep = conn.prepareStatement("DELETE FROM " + SCHEMA + ".MAP M " + "WHERE NOT EXISTS (SELECT * FROM " + SCHEMA + ".ROWS R WHERE R.ID=M.ROWID) AND ROWID<10000");
    while (true) {
        int deleted = prep.executeUpdate();
        if (deleted == 0) {
            break;
        }
    }
}
Also used : SimpleResultSet(org.h2.tools.SimpleResultSet) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 39 with Table

use of org.h2.table.Table in project ratpack by ratpack.

the class Example method main.

public static void main(String[] args) throws Exception {
    JdbcDataSource ds = new JdbcDataSource();
    ds.setURL("jdbc:h2:mem:transactionExamples;DB_CLOSE_DELAY=-1");
    txDs = Transaction.dataSource(ds);
    tx = Transaction.create(ds::getConnection);
    try (Connection connection = txDs.getConnection()) {
        try (Statement statement = connection.createStatement()) {
            statement.executeUpdate("CREATE TABLE tbl (value VARCHAR(50)) ");
        }
    }
    List<Block> examples = Arrays.asList(Example::singleTransactionExample, Example::singleTransactionRollbackExample, Example::nestedTransactionExample, Example::nestedTransactionRollbackExample, () -> manualTransactionExample(true), () -> manualTransactionExample(false));
    try (ExecHarness harness = ExecHarness.harness()) {
        for (Block example : examples) {
            harness.execute(Operation.of(example));
            reset();
        }
    }
}
Also used : Statement(java.sql.Statement) JdbcDataSource(org.h2.jdbcx.JdbcDataSource) Connection(java.sql.Connection) Block(ratpack.func.Block) ExecHarness(ratpack.test.exec.ExecHarness)

Example 40 with Table

use of org.h2.table.Table 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)

Aggregations

Column (org.h2.table.Column)20 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)18 Statement (java.sql.Statement)13 SQLException (java.sql.SQLException)12 GridH2Table (org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)12 IndexColumn (org.h2.table.IndexColumn)11 ResultSet (java.sql.ResultSet)9 IgniteException (org.apache.ignite.IgniteException)9 GridH2RowDescriptor (org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor)8 AlterTableAddConstraint (org.h2.command.ddl.AlterTableAddConstraint)8 SimpleResultSet (org.h2.tools.SimpleResultSet)8 GridSqlColumn (org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn)7 Table (org.h2.table.Table)7 Connection (java.sql.Connection)6 HashSet (java.util.HashSet)6 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)6 GridSqlElement (org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement)6 Prepared (org.h2.command.Prepared)6 ValueString (org.h2.value.ValueString)6 PreparedStatement (java.sql.PreparedStatement)5