Search in sources :

Example 6 with Database

use of org.h2.engine.Database in project siena by mandubian.

the class FullText method reindex.

/**
     * Re-creates the full text index for this database. Calling this method is
     * usually not needed, as the index is kept up-to-date automatically.
     *
     * @param conn the connection
     */
public static void reindex(Connection conn) throws SQLException {
    init(conn);
    removeAllTriggers(conn, TRIGGER_PREFIX);
    FullTextSettings setting = FullTextSettings.getInstance(conn);
    setting.getWordList().clear();
    Statement stat = conn.createStatement();
    stat.execute("TRUNCATE TABLE " + SCHEMA + ".WORDS");
    stat.execute("TRUNCATE TABLE " + SCHEMA + ".ROWS");
    stat.execute("TRUNCATE TABLE " + SCHEMA + ".MAP");
    ResultSet rs = stat.executeQuery("SELECT * FROM " + SCHEMA + ".FT_INDEXES");
    while (rs.next()) {
        String schema = rs.getString("SCHEMA");
        String table = rs.getString("TABLE");
        createTrigger(conn, schema, table);
        indexExistingRows(conn, schema, table);
    }
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) SimpleResultSet(org.h2.tools.SimpleResultSet) ResultSet(java.sql.ResultSet)

Example 7 with Database

use of org.h2.engine.Database in project frostwire by frostwire.

the class FullTextLucene2 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 {
    FullTextLucene2.FullTextTrigger existing = new FullTextLucene2.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);
        existing.insert(row, false);
    }
    existing.commitIndex();
}
Also used : SimpleResultSet(org.h2.tools.SimpleResultSet)

Example 8 with Database

use of org.h2.engine.Database in project frostwire by frostwire.

the class FullTextLucene2 method getIndexPath.

/**
 * Get the path of the Lucene index for this database.
 *
 * @param conn the database connection
 * @return the path
 */
protected static String getIndexPath(Connection conn) throws SQLException {
    Statement stat = conn.createStatement();
    ResultSet rs = stat.executeQuery("CALL DATABASE_PATH()");
    rs.next();
    String path = rs.getString(1);
    if (path == null) {
        throw throwException("Fulltext search for in-memory databases is not supported.");
    }
    int index = path.lastIndexOf(':');
    // position 1 means a windows drive letter is used, ignore that
    if (index > 1) {
        path = path.substring(index + 1);
    }
    rs.close();
    return path;
}
Also used : SimpleResultSet(org.h2.tools.SimpleResultSet)

Example 9 with Database

use of org.h2.engine.Database in project rxjava2-jdbc by davidmoten.

the class DatabaseTest method testSelectConcurrencyTest.

@Test(timeout = 5000)
public void testSelectConcurrencyTest() throws InterruptedException, TimeoutException {
    debug();
    try {
        try (Database db = db(1)) {
            Scheduler scheduler = Schedulers.from(Executors.newFixedThreadPool(2));
            int n = 2;
            CountDownLatch latch = new CountDownLatch(n);
            AtomicInteger count = new AtomicInteger();
            for (int i = 0; i < n; i++) {
                // 
                db.select("select score from person where name=?").parameters("FRED", // 
                "JOSEPH").getAs(// 
                Integer.class).subscribeOn(// 
                scheduler).toList().doOnSuccess(x -> {
                    if (!x.equals(Lists.newArrayList(21, 34))) {
                        throw new RuntimeException("run broken");
                    }
                }).doOnSuccess(x -> {
                    count.incrementAndGet();
                    latch.countDown();
                }).doOnError(// 
                x -> latch.countDown()).subscribe();
                log.info("submitted " + i);
            }
            if (!latch.await(5000, TimeUnit.SECONDS)) {
                throw new TimeoutException("timeout");
            }
            assertEquals(n, count.get());
        }
    } finally {
        debug();
    }
}
Also used : MethodSorters(org.junit.runners.MethodSorters) Arrays(java.util.Arrays) Connection(java.sql.Connection) Time(java.sql.Time) Date(java.util.Date) ZonedDateTime(java.time.ZonedDateTime) Completable(io.reactivex.Completable) SQLRuntimeException(org.davidmoten.rx.jdbc.exceptions.SQLRuntimeException) PoolClosedException(org.davidmoten.rx.pool.PoolClosedException) LoggerFactory(org.slf4j.LoggerFactory) Scanner(java.util.Scanner) TimeoutException(java.util.concurrent.TimeoutException) ColumnIndexOutOfRangeException(org.davidmoten.rx.jdbc.exceptions.ColumnIndexOutOfRangeException) MoreColumnsRequestedThanExistException(org.davidmoten.rx.jdbc.exceptions.MoreColumnsRequestedThanExistException) JDBCBlobFile(org.hsqldb.jdbc.JDBCBlobFile) JDBCClobFile(org.hsqldb.jdbc.JDBCClobFile) NonBlockingConnectionPool(org.davidmoten.rx.jdbc.pool.NonBlockingConnectionPool) ByteArrayInputStream(java.io.ByteArrayInputStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ResultSet(java.sql.ResultSet) Level(org.apache.log4j.Level) DatabaseCreator(org.davidmoten.rx.jdbc.pool.DatabaseCreator) Schedulers(io.reactivex.schedulers.Schedulers) ZoneOffset(java.time.ZoneOffset) NamedParameterFoundButSqlDoesNotHaveNamesException(org.davidmoten.rx.jdbc.exceptions.NamedParameterFoundButSqlDoesNotHaveNamesException) Sets(com.github.davidmoten.guavamini.Sets) CannotForkTransactedConnection(org.davidmoten.rx.jdbc.exceptions.CannotForkTransactedConnection) GregorianCalendar(java.util.GregorianCalendar) QueryAnnotationMissingException(org.davidmoten.rx.jdbc.exceptions.QueryAnnotationMissingException) Pools(org.davidmoten.rx.jdbc.pool.Pools) Timestamp(java.sql.Timestamp) Set(java.util.Set) Reader(java.io.Reader) PreparedStatement(java.sql.PreparedStatement) Instant(java.time.Instant) Column(org.davidmoten.rx.jdbc.annotations.Column) FileNotFoundException(java.io.FileNotFoundException) Executors(java.util.concurrent.Executors) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Index(org.davidmoten.rx.jdbc.annotations.Index) Assert.assertFalse(org.junit.Assert.assertFalse) CallableStatement(java.sql.CallableStatement) Optional(java.util.Optional) TestScheduler(io.reactivex.schedulers.TestScheduler) FixMethodOrder(org.junit.FixMethodOrder) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) Types(java.sql.Types) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) AnnotationsNotFoundException(org.davidmoten.rx.jdbc.exceptions.AnnotationsNotFoundException) Tuple6(org.davidmoten.rx.jdbc.tuple.Tuple6) NamedParameterMissingException(org.davidmoten.rx.jdbc.exceptions.NamedParameterMissingException) Tuple7(org.davidmoten.rx.jdbc.tuple.Tuple7) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Tuple2(org.davidmoten.rx.jdbc.tuple.Tuple2) Tuple3(org.davidmoten.rx.jdbc.tuple.Tuple3) Tuple4(org.davidmoten.rx.jdbc.tuple.Tuple4) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) Tuple5(org.davidmoten.rx.jdbc.tuple.Tuple5) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Lists(com.github.davidmoten.guavamini.Lists) Single(io.reactivex.Single) HashSet(java.util.HashSet) ColumnNotFoundException(org.davidmoten.rx.jdbc.exceptions.ColumnNotFoundException) SQLException(java.sql.SQLException) Calendar(java.util.Calendar) Scheduler(io.reactivex.Scheduler) Flowable(io.reactivex.Flowable) Observable(io.reactivex.Observable) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Clob(java.sql.Clob) Query(org.davidmoten.rx.jdbc.annotations.Query) AutomappedInterfaceInaccessibleException(org.davidmoten.rx.jdbc.exceptions.AutomappedInterfaceInaccessibleException) Logger(org.slf4j.Logger) DatabaseType(org.davidmoten.rx.jdbc.pool.DatabaseType) TupleN(org.davidmoten.rx.jdbc.tuple.TupleN) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) Predicate(io.reactivex.functions.Predicate) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) JdbcSQLException(org.h2.jdbc.JdbcSQLException) Ignore(org.junit.Ignore) Statement(java.sql.Statement) FileReader(java.io.FileReader) LogManager(org.apache.log4j.LogManager) Blob(java.sql.Blob) DelegatedConnection(org.davidmoten.rx.jdbc.internal.DelegatedConnection) Assert(org.junit.Assert) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) SQLRuntimeException(org.davidmoten.rx.jdbc.exceptions.SQLRuntimeException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestScheduler(io.reactivex.schedulers.TestScheduler) Scheduler(io.reactivex.Scheduler) CountDownLatch(java.util.concurrent.CountDownLatch) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Example 10 with Database

use of org.h2.engine.Database in project rxjava2-jdbc by davidmoten.

the class DatabaseTest method testDelayedCallsAreNonBlocking.

@Test
public void testDelayedCallsAreNonBlocking() throws InterruptedException {
    List<String> list = new CopyOnWriteArrayList<String>();
    try (Database db = db(1)) {
        // 
        // 
        db.select("select score from person where name=?").parameter(// 
        "FRED").getAs(// 
        Integer.class).doOnNext(// 
        x -> Thread.sleep(1000)).subscribeOn(// 
        Schedulers.io()).subscribe();
        Thread.sleep(100);
        CountDownLatch latch = new CountDownLatch(1);
        // 
        db.select("select score from person where name=?").parameter(// 
        "FRED").getAs(// 
        Integer.class).doOnNext(// 
        x -> list.add("emitted")).doOnNext(// 
        x -> log.debug("emitted on " + Thread.currentThread().getName())).doOnNext(// 
        x -> latch.countDown()).subscribe();
        list.add("subscribed");
        assertTrue(latch.await(TIMEOUT_SECONDS, TimeUnit.SECONDS));
        assertEquals(Arrays.asList("subscribed", "emitted"), list);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MethodSorters(org.junit.runners.MethodSorters) Arrays(java.util.Arrays) Connection(java.sql.Connection) Time(java.sql.Time) Date(java.util.Date) ZonedDateTime(java.time.ZonedDateTime) Completable(io.reactivex.Completable) SQLRuntimeException(org.davidmoten.rx.jdbc.exceptions.SQLRuntimeException) PoolClosedException(org.davidmoten.rx.pool.PoolClosedException) LoggerFactory(org.slf4j.LoggerFactory) Scanner(java.util.Scanner) TimeoutException(java.util.concurrent.TimeoutException) ColumnIndexOutOfRangeException(org.davidmoten.rx.jdbc.exceptions.ColumnIndexOutOfRangeException) MoreColumnsRequestedThanExistException(org.davidmoten.rx.jdbc.exceptions.MoreColumnsRequestedThanExistException) JDBCBlobFile(org.hsqldb.jdbc.JDBCBlobFile) JDBCClobFile(org.hsqldb.jdbc.JDBCClobFile) NonBlockingConnectionPool(org.davidmoten.rx.jdbc.pool.NonBlockingConnectionPool) ByteArrayInputStream(java.io.ByteArrayInputStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ResultSet(java.sql.ResultSet) Level(org.apache.log4j.Level) DatabaseCreator(org.davidmoten.rx.jdbc.pool.DatabaseCreator) Schedulers(io.reactivex.schedulers.Schedulers) ZoneOffset(java.time.ZoneOffset) NamedParameterFoundButSqlDoesNotHaveNamesException(org.davidmoten.rx.jdbc.exceptions.NamedParameterFoundButSqlDoesNotHaveNamesException) Sets(com.github.davidmoten.guavamini.Sets) CannotForkTransactedConnection(org.davidmoten.rx.jdbc.exceptions.CannotForkTransactedConnection) GregorianCalendar(java.util.GregorianCalendar) QueryAnnotationMissingException(org.davidmoten.rx.jdbc.exceptions.QueryAnnotationMissingException) Pools(org.davidmoten.rx.jdbc.pool.Pools) Timestamp(java.sql.Timestamp) Set(java.util.Set) Reader(java.io.Reader) PreparedStatement(java.sql.PreparedStatement) Instant(java.time.Instant) Column(org.davidmoten.rx.jdbc.annotations.Column) FileNotFoundException(java.io.FileNotFoundException) Executors(java.util.concurrent.Executors) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Index(org.davidmoten.rx.jdbc.annotations.Index) Assert.assertFalse(org.junit.Assert.assertFalse) CallableStatement(java.sql.CallableStatement) Optional(java.util.Optional) TestScheduler(io.reactivex.schedulers.TestScheduler) FixMethodOrder(org.junit.FixMethodOrder) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) Types(java.sql.Types) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) AnnotationsNotFoundException(org.davidmoten.rx.jdbc.exceptions.AnnotationsNotFoundException) Tuple6(org.davidmoten.rx.jdbc.tuple.Tuple6) NamedParameterMissingException(org.davidmoten.rx.jdbc.exceptions.NamedParameterMissingException) Tuple7(org.davidmoten.rx.jdbc.tuple.Tuple7) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Tuple2(org.davidmoten.rx.jdbc.tuple.Tuple2) Tuple3(org.davidmoten.rx.jdbc.tuple.Tuple3) Tuple4(org.davidmoten.rx.jdbc.tuple.Tuple4) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) Tuple5(org.davidmoten.rx.jdbc.tuple.Tuple5) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Lists(com.github.davidmoten.guavamini.Lists) Single(io.reactivex.Single) HashSet(java.util.HashSet) ColumnNotFoundException(org.davidmoten.rx.jdbc.exceptions.ColumnNotFoundException) SQLException(java.sql.SQLException) Calendar(java.util.Calendar) Scheduler(io.reactivex.Scheduler) Flowable(io.reactivex.Flowable) Observable(io.reactivex.Observable) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Clob(java.sql.Clob) Query(org.davidmoten.rx.jdbc.annotations.Query) AutomappedInterfaceInaccessibleException(org.davidmoten.rx.jdbc.exceptions.AutomappedInterfaceInaccessibleException) Logger(org.slf4j.Logger) DatabaseType(org.davidmoten.rx.jdbc.pool.DatabaseType) TupleN(org.davidmoten.rx.jdbc.tuple.TupleN) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) Predicate(io.reactivex.functions.Predicate) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) JdbcSQLException(org.h2.jdbc.JdbcSQLException) Ignore(org.junit.Ignore) Statement(java.sql.Statement) FileReader(java.io.FileReader) LogManager(org.apache.log4j.LogManager) Blob(java.sql.Blob) DelegatedConnection(org.davidmoten.rx.jdbc.internal.DelegatedConnection) Assert(org.junit.Assert) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) CountDownLatch(java.util.concurrent.CountDownLatch) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.Test)

Aggregations

Database (org.h2.engine.Database)79 SQLException (java.sql.SQLException)45 PreparedStatement (java.sql.PreparedStatement)38 DbException (org.h2.message.DbException)37 ResultSet (java.sql.ResultSet)34 Statement (java.sql.Statement)32 SimpleResultSet (org.h2.tools.SimpleResultSet)32 Connection (java.sql.Connection)27 Table (org.h2.table.Table)25 Value (org.h2.value.Value)25 Column (org.h2.table.Column)22 IOException (java.io.IOException)19 Constraint (org.h2.constraint.Constraint)18 Expression (org.h2.expression.Expression)17 ExpressionColumn (org.h2.expression.ExpressionColumn)17 ValueString (org.h2.value.ValueString)15 ValueExpression (org.h2.expression.ValueExpression)14 Session (org.h2.engine.Session)13 JdbcConnection (org.h2.jdbc.JdbcConnection)13 Schema (org.h2.schema.Schema)13