Search in sources :

Example 1 with Database

use of org.h2.engine.Database in project che by eclipse.

the class H2TestHelper method inMemoryDefault.

/**
     * Creates new default datasource to in memory database
     * with url {@value #DEFAULT_IN_MEMORY_DB_URL}.
     * Boots database if this is invoked first time, database
     * won't be shutdown until 'SHUTDOWN' query is executed
     * or {@link #shutdownDefault()} is called directly.
     *
     * @return datasource to the in memory database
     * @deprecated use {@link H2DBTestServer}.
     */
@Deprecated
public static DataSource inMemoryDefault() {
    final JdbcDataSource dataSource = new JdbcDataSource();
    dataSource.setUrl(DEFAULT_IN_MEMORY_DB_URL);
    return dataSource;
}
Also used : JdbcDataSource(org.h2.jdbcx.JdbcDataSource)

Example 2 with Database

use of org.h2.engine.Database in project aries by apache.

the class TransactionLogTest method setupServerAndDataSource.

private void setupServerAndDataSource() throws SQLException {
    server = Server.createTcpServer("-tcpPort", "0");
    server.start();
    File dbPath = new File("target/recovery-test/database");
    dataSource = new JdbcDataSource();
    dataSource.setUrl("jdbc:h2:tcp://127.0.0.1:" + server.getPort() + "/" + dbPath.getAbsolutePath());
}
Also used : JdbcDataSource(org.h2.jdbcx.JdbcDataSource) File(java.io.File)

Example 3 with Database

use of org.h2.engine.Database 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 4 with Database

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

the class FullText 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;
    }
    FullTextSettings setting = FullTextSettings.getInstance(conn);
    if (!setting.isInitialized()) {
        init(conn);
    }
    HashSet<String> words = New.hashSet();
    addWords(setting, words, text);
    HashSet<Integer> rIds = null, lastRowIds = null;
    HashMap<String, Integer> allWords = setting.getWordList();
    PreparedStatement prepSelectMapByWordId = setting.prepare(conn, SELECT_MAP_BY_WORD_ID);
    for (String word : words) {
        lastRowIds = rIds;
        rIds = New.hashSet();
        Integer wId = allWords.get(word);
        if (wId == null) {
            continue;
        }
        prepSelectMapByWordId.setInt(1, wId.intValue());
        ResultSet rs = prepSelectMapByWordId.executeQuery();
        while (rs.next()) {
            Integer rId = rs.getInt(1);
            if (lastRowIds == null || lastRowIds.contains(rId)) {
                rIds.add(rId);
            }
        }
    }
    if (rIds == null || rIds.size() == 0) {
        return result;
    }
    PreparedStatement prepSelectRowById = setting.prepare(conn, SELECT_ROW_BY_ID);
    int rowCount = 0;
    for (int rowId : rIds) {
        prepSelectRowById.setInt(1, rowId);
        ResultSet rs = prepSelectRowById.executeQuery();
        if (!rs.next()) {
            continue;
        }
        if (offset > 0) {
            offset--;
        } else {
            String key = rs.getString(1);
            int indexId = rs.getInt(2);
            IndexInfo index = setting.getIndexInfo(indexId);
            if (data) {
                /*Object[][] columnData = parseKey(conn, key);
                	result.addRow(
                            index.schema,
                            index.table,
                            columnData[0],
                            columnData[1],
                            1.0);*/
                String[] splits = key.split("=");
                String[] col0 = new String[1];
                col0[0] = splits[0];
                String[] col1 = new String[1];
                col1[0] = splits[1];
                result.addRow(index.schema, index.table, col0, col1, 1.0);
            } else {
                String query = StringUtils.quoteIdentifier(index.schema) + "." + StringUtils.quoteIdentifier(index.table) + " WHERE " + key;
                result.addRow(query, 1.0);
            }
            rowCount++;
            if (limit > 0 && rowCount >= limit) {
                break;
            }
        }
    }
    return result;
}
Also used : SimpleResultSet(org.h2.tools.SimpleResultSet) SimpleResultSet(org.h2.tools.SimpleResultSet) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 5 with Database

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

the class FullText method removeAllTriggers.

/**
     * Remove all triggers that start with the given prefix.
     *
     * @param conn the database connection
     * @param prefix the prefix
     */
protected static void removeAllTriggers(Connection conn, String prefix) throws SQLException {
    Statement stat = conn.createStatement();
    ResultSet rs = stat.executeQuery("SELECT * FROM INFORMATION_SCHEMA.TRIGGERS");
    Statement stat2 = conn.createStatement();
    while (rs.next()) {
        String schema = rs.getString("TRIGGER_SCHEMA");
        String name = rs.getString("TRIGGER_NAME");
        if (name.startsWith(prefix)) {
            name = StringUtils.quoteIdentifier(schema) + "." + StringUtils.quoteIdentifier(name);
            stat2.execute("DROP TRIGGER " + name);
        }
    }
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) SimpleResultSet(org.h2.tools.SimpleResultSet) ResultSet(java.sql.ResultSet)

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