Search in sources :

Example 16 with JdbcConnection

use of org.h2.jdbc.JdbcConnection in project h2database by h2database.

the class TestPowerOff method testCrash.

private void testCrash() throws SQLException {
    if (config.networked) {
        return;
    }
    deleteDb(dir, DB_NAME);
    Random random = new Random(1);
    int repeat = getSize(1, 20);
    for (int i = 0; i < repeat; i++) {
        Connection conn = getConnection(url);
        conn.close();
        conn = getConnection(url);
        Statement stat = conn.createStatement();
        stat.execute("SET WRITE_DELAY 0");
        ((JdbcConnection) conn).setPowerOffCount(random.nextInt(100));
        try {
            stat.execute("DROP TABLE IF EXISTS TEST");
            stat.execute("CREATE TABLE TEST" + "(ID INT PRIMARY KEY, NAME VARCHAR(255))");
            conn.setAutoCommit(false);
            int len = getSize(3, 100);
            for (int j = 0; j < len; j++) {
                stat.execute("INSERT INTO TEST VALUES(" + j + ", 'Hello')");
                if (random.nextInt(5) == 0) {
                    conn.commit();
                }
                if (random.nextInt(10) == 0) {
                    stat.execute("DROP TABLE IF EXISTS TEST");
                    stat.execute("CREATE TABLE TEST" + "(ID INT PRIMARY KEY, NAME VARCHAR(255))");
                }
            }
            stat.execute("DROP TABLE IF EXISTS TEST");
            conn.close();
        } catch (SQLException e) {
            if (!e.getSQLState().equals("90098")) {
                TestBase.logError("power", e);
            }
        }
    }
}
Also used : Random(java.util.Random) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) JdbcConnection(org.h2.jdbc.JdbcConnection)

Example 17 with JdbcConnection

use of org.h2.jdbc.JdbcConnection in project h2database by h2database.

the class TestPowerOff method testLobCrash.

private void testLobCrash() throws SQLException {
    if (config.networked) {
        return;
    }
    deleteDb(dir, DB_NAME);
    Connection conn = getConnection(url);
    Statement stat = conn.createStatement();
    stat.execute("create table test(id identity, data clob)");
    conn.close();
    conn = getConnection(url);
    stat = conn.createStatement();
    stat.execute("set write_delay 0");
    ((JdbcConnection) conn).setPowerOffCount(Integer.MAX_VALUE);
    stat.execute("insert into test values(null, space(11000))");
    int max = Integer.MAX_VALUE - ((JdbcConnection) conn).getPowerOffCount();
    for (int i = 0; i < max + 10; i++) {
        conn.close();
        conn = getConnection(url);
        stat = conn.createStatement();
        stat.execute("insert into test values(null, space(11000))");
        stat.execute("set write_delay 0");
        ((JdbcConnection) conn).setPowerOffCount(i);
        try {
            stat.execute("insert into test values(null, space(11000))");
        } catch (SQLException e) {
        // ignore
        }
        JdbcUtils.closeSilently(conn);
    }
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) JdbcConnection(org.h2.jdbc.JdbcConnection)

Example 18 with JdbcConnection

use of org.h2.jdbc.JdbcConnection in project siena by mandubian.

the class FullText method parseKey.

/**
 * Parse a primary key condition into the primary key columns.
 *
 * @param conn the database connection
 * @param key the primary key condition as a string
 * @return an array containing the column name list and the data list
 */
protected static Object[][] parseKey(Connection conn, String key) {
    ArrayList<String> columns = New.arrayList();
    ArrayList<String> data = New.arrayList();
    JdbcConnection c = (JdbcConnection) conn;
    Session session = (Session) c.getSession();
    Parser p = new Parser(session);
    Expression expr = p.parseExpression(key);
    addColumnData(columns, data, expr);
    Object[] col = new Object[columns.size()];
    columns.toArray(col);
    Object[] dat = new Object[columns.size()];
    data.toArray(dat);
    Object[][] columnData = { col, dat };
    return columnData;
}
Also used : ValueExpression(org.h2.expression.ValueExpression) Expression(org.h2.expression.Expression) JdbcConnection(org.h2.jdbc.JdbcConnection) Session(org.h2.engine.Session) Parser(org.h2.command.Parser)

Example 19 with JdbcConnection

use of org.h2.jdbc.JdbcConnection in project ignite by apache.

the class ValidateIndexesClosure method mvccSession.

/**
 * Get session with MVCC snapshot and QueryContext.
 *
 * @param cctx Cache context.
 * @return Session with QueryContext and MVCC snapshot.
 * @throws IgniteCheckedException If failed.
 */
private Session mvccSession(GridCacheContext<?, ?> cctx) throws IgniteCheckedException {
    Session session = null;
    boolean mvccEnabled = cctx.mvccEnabled();
    if (mvccEnabled) {
        ConnectionManager connMgr = ((IgniteH2Indexing) ignite.context().query().getIndexing()).connections();
        JdbcConnection connection = (JdbcConnection) connMgr.connection().connection();
        session = (Session) connection.getSession();
        MvccQueryTracker tracker = MvccUtils.mvccTracker(cctx, true);
        MvccSnapshot mvccSnapshot = tracker.snapshot();
        final QueryContext qctx = new QueryContext(0, cacheName -> null, null, mvccSnapshot, null, true);
        session.setVariable(H2Utils.QCTX_VARIABLE_NAME, new H2Utils.ValueRuntimeSimpleObject<>(qctx));
    }
    return session;
}
Also used : H2Utils(org.apache.ignite.internal.processors.query.h2.H2Utils) MvccQueryTracker(org.apache.ignite.internal.processors.cache.mvcc.MvccQueryTracker) MvccSnapshot(org.apache.ignite.internal.processors.cache.mvcc.MvccSnapshot) ConnectionManager(org.apache.ignite.internal.processors.query.h2.ConnectionManager) JdbcConnection(org.h2.jdbc.JdbcConnection) QueryContext(org.apache.ignite.internal.processors.query.h2.opt.QueryContext) IgniteH2Indexing(org.apache.ignite.internal.processors.query.h2.IgniteH2Indexing) Session(org.h2.engine.Session)

Example 20 with JdbcConnection

use of org.h2.jdbc.JdbcConnection in project frostwire by frostwire.

the class FullTextLucene2 method search.

/**
 * Do the search.
 *
 * @param conn the database connection
 * @param text the query
 * @param limit the limit
 * @param offset the offset
 * @param data whether the raw data should be returned
 * @return the result set
 */
protected static ResultSet search(Connection conn, String text, int limit, int offset, boolean data) throws SQLException {
    SimpleResultSet result = createResultSet(data);
    if (conn.getMetaData().getURL().startsWith("jdbc:columnlist:")) {
        // this is just to query the result set columns
        return result;
    }
    if (text == null || text.trim().length() == 0) {
        return result;
    }
    try {
        IndexAccess access = getIndexAccess(conn);
        /*## LUCENE2 ##
            access.modifier.flush();
            String path = getIndexPath(conn);
            IndexReader reader = IndexReader.open(path);
            Analyzer analyzer = new StandardAnalyzer();
            Searcher searcher = new IndexSearcher(reader);
            QueryParser parser = new QueryParser(LUCENE_FIELD_DATA, analyzer);
            Query query = parser.parse(text);
            Hits hits = searcher.search(query);
            int max = hits.length();
            if (limit == 0) {
                limit = max;
            }
            for (int i = 0; i < limit && i + offset < max; i++) {
                Document doc = hits.doc(i + offset);
                float score = hits.score(i + offset);
            //*/
        // ## LUCENE3 ##
        // take a reference as the searcher may change
        Searcher searcher = access.searcher;
        // reuse the same analyzer; it's thread-safe;
        // also allows subclasses to control the analyzer used.
        Analyzer analyzer = access.writer.getAnalyzer();
        QueryParser parser = new QueryParser(Version.LUCENE_30, LUCENE_FIELD_DATA, analyzer);
        Query query = parser.parse(text);
        // Lucene 3 insists on a hard limit and will not provide
        // a total hits value. Take at least 100 which is
        // an optimal limit for Lucene as any more
        // will trigger writing results to disk.
        int maxResults = (limit == 0 ? 100 : limit) + offset;
        TopDocs docs = searcher.search(query, maxResults);
        if (limit == 0) {
            limit = docs.totalHits;
        }
        for (int i = 0, len = docs.scoreDocs.length; i < limit && i + offset < docs.totalHits && i + offset < len; i++) {
            ScoreDoc sd = docs.scoreDocs[i + offset];
            Document doc = searcher.doc(sd.doc);
            float score = sd.score;
            // */
            String q = doc.get(LUCENE_FIELD_QUERY);
            if (data) {
                int idx = q.indexOf(" WHERE ");
                JdbcConnection c = (JdbcConnection) conn;
                Session session = (Session) c.getSession();
                Parser p = new Parser(session);
                String tab = q.substring(0, idx);
                ExpressionColumn expr = (ExpressionColumn) p.parseExpression(tab);
                String schemaName = expr.getOriginalTableAliasName();
                String tableName = expr.getColumnName();
                q = q.substring(idx + " WHERE ".length());
                Object[][] columnData = parseKey(conn, q);
                result.addRow(schemaName, tableName, columnData[0], columnData[1], score);
            } else {
                result.addRow(q, score);
            }
        }
    /*## LUCENE2 ##
            // TODO keep it open if possible
            reader.close();
            //*/
    } catch (Exception e) {
        throw convertException(e);
    }
    return result;
}
Also used : SimpleResultSet(org.h2.tools.SimpleResultSet) JdbcConnection(org.h2.jdbc.JdbcConnection) Analyzer(org.apache.lucene.analysis.Analyzer) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) Document(org.apache.lucene.document.Document) IOException(java.io.IOException) Parser(org.h2.command.Parser) QueryParser(org.apache.lucene.queryParser.QueryParser) ExpressionColumn(org.h2.expression.ExpressionColumn) QueryParser(org.apache.lucene.queryParser.QueryParser) Session(org.h2.engine.Session)

Aggregations

JdbcConnection (org.h2.jdbc.JdbcConnection)27 SQLException (java.sql.SQLException)15 Connection (java.sql.Connection)14 Statement (java.sql.Statement)13 PreparedStatement (java.sql.PreparedStatement)9 Session (org.h2.engine.Session)8 ResultSet (java.sql.ResultSet)6 IOException (java.io.IOException)5 DbException (org.h2.message.DbException)4 Parser (org.h2.command.Parser)3 Task (org.h2.util.Task)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 File (java.io.File)2 PipedInputStream (java.io.PipedInputStream)2 PipedOutputStream (java.io.PipedOutputStream)2 Savepoint (java.sql.Savepoint)2 Properties (java.util.Properties)2 IgniteH2Indexing (org.apache.ignite.internal.processors.query.h2.IgniteH2Indexing)2 Analyzer (org.apache.lucene.analysis.Analyzer)2 Expression (org.h2.expression.Expression)2