Search in sources :

Example 16 with Replace

use of org.h2.command.dml.Replace in project h2database by h2database.

the class TestMVStore method testTruncateFile.

private void testTruncateFile() {
    String fileName = getBaseDir() + "/" + getTestName();
    FileUtils.delete(fileName);
    MVStore s;
    MVMap<Integer, String> m;
    s = openStore(fileName);
    m = s.openMap("data");
    String data = new String(new char[10000]).replace((char) 0, 'x');
    for (int i = 1; i < 10; i++) {
        m.put(i, data);
        s.commit();
    }
    s.close();
    long len = FileUtils.size(fileName);
    s = openStore(fileName);
    s.setRetentionTime(0);
    // remove 75%
    m = s.openMap("data");
    for (int i = 0; i < 10; i++) {
        if (i % 4 != 0) {
            sleep(2);
            m.remove(i);
            s.commit();
        }
    }
    assertTrue(s.compact(100, 50 * 1024));
    s.close();
    long len2 = FileUtils.size(fileName);
    assertTrue("len2: " + len2 + " len: " + len, len2 < len);
}
Also used : MVStore(org.h2.mvstore.MVStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 17 with Replace

use of org.h2.command.dml.Replace in project h2database by h2database.

the class TableView method getCreateSQL.

private String getCreateSQL(boolean orReplace, boolean force, String quotedName) {
    StatementBuilder buff = new StatementBuilder("CREATE ");
    if (orReplace) {
        buff.append("OR REPLACE ");
    }
    if (force) {
        buff.append("FORCE ");
    }
    buff.append("VIEW ");
    if (isTableExpression) {
        buff.append("TABLE_EXPRESSION ");
    }
    buff.append(quotedName);
    if (comment != null) {
        buff.append(" COMMENT ").append(StringUtils.quoteStringSQL(comment));
    }
    if (columns != null && columns.length > 0) {
        buff.append('(');
        for (Column c : columns) {
            buff.appendExceptFirst(", ");
            buff.append(c.getSQL());
        }
        buff.append(')');
    } else if (columnTemplates != null) {
        buff.append('(');
        for (Column c : columnTemplates) {
            buff.appendExceptFirst(", ");
            buff.append(c.getName());
        }
        buff.append(')');
    }
    return buff.append(" AS\n").append(querySQL).toString();
}
Also used : ExpressionColumn(org.h2.expression.ExpressionColumn) StatementBuilder(org.h2.util.StatementBuilder)

Example 18 with Replace

use of org.h2.command.dml.Replace in project h2database by h2database.

the class TableView method replace.

/**
 * Try to replace the SQL statement of the view and re-compile this and all
 * dependent views.
 *
 * @param querySQL the SQL statement
 * @param newColumnTemplates the columns
 * @param session the session
 * @param recursive whether this is a recursive view
 * @param force if errors should be ignored
 * @param literalsChecked if literals have been checked
 */
public void replace(String querySQL, Column[] newColumnTemplates, Session session, boolean recursive, boolean force, boolean literalsChecked) {
    String oldQuerySQL = this.querySQL;
    Column[] oldColumnTemplates = this.columnTemplates;
    boolean oldRecursive = this.allowRecursive;
    init(querySQL, null, newColumnTemplates == null ? this.columnTemplates : newColumnTemplates, session, recursive, literalsChecked, isTableExpression, isPersistent);
    DbException e = recompile(session, force, true);
    if (e != null) {
        init(oldQuerySQL, null, oldColumnTemplates, session, oldRecursive, literalsChecked, isTableExpression, isPersistent);
        recompile(session, true, false);
        throw e;
    }
}
Also used : ExpressionColumn(org.h2.expression.ExpressionColumn) DbException(org.h2.message.DbException)

Example 19 with Replace

use of org.h2.command.dml.Replace in project h2database by h2database.

the class JoinBatch method fetchCurrent.

@SuppressWarnings("unchecked")
private void fetchCurrent(final int jfId) {
    assert current.prev == null || current.prev.isRow(jfId) : "prev must be already fetched";
    assert jfId == 0 || current.isRow(jfId - 1) : "left must be already fetched";
    assert !current.isRow(jfId) : "double fetching";
    Object x = current.row(jfId);
    assert x != null : "x null";
    // in case of outer join we don't have any future around empty cursor
    boolean newCursor = x == EMPTY_CURSOR;
    if (newCursor) {
        if (jfId == 0) {
            // the top cursor is new and empty, then the whole select will
            // not produce any rows
            current.drop();
            return;
        }
    } else if (current.isFuture(jfId)) {
        // get cursor from a future
        x = get((Future<Cursor>) x);
        current.updateRow(jfId, x, JoinRow.S_FUTURE, JoinRow.S_CURSOR);
        newCursor = true;
    }
    final JoinFilter jf = filters[jfId];
    Cursor c = (Cursor) x;
    assert c != null;
    JoinFilter join = jf.join;
    while (true) {
        if (c == null || !c.next()) {
            if (newCursor && jf.isOuterJoin()) {
                // replace cursor with null-row
                current.updateRow(jfId, jf.getNullRow(), JoinRow.S_CURSOR, JoinRow.S_ROW);
                c = null;
                newCursor = false;
            } else {
                // cursor is done, drop it
                current.drop();
                return;
            }
        }
        if (!jf.isOk(c == null)) {
            // try another row from the cursor
            continue;
        }
        boolean joinEmpty = false;
        if (join != null && !join.collectSearchRows()) {
            if (join.isOuterJoin()) {
                joinEmpty = true;
            } else {
                // join will fail, try next row in the cursor
                continue;
            }
        }
        if (c != null) {
            current = current.copyBehind(jfId);
            // update jf, set current row from cursor
            current.updateRow(jfId, c.get(), JoinRow.S_CURSOR, JoinRow.S_ROW);
        }
        if (joinEmpty) {
            // update jf.join, set an empty cursor
            current.updateRow(join.id, EMPTY_CURSOR, JoinRow.S_NULL, JoinRow.S_CURSOR);
        }
        return;
    }
}
Also used : Cursor(org.h2.index.Cursor) IndexCursor(org.h2.index.IndexCursor) ViewCursor(org.h2.index.ViewCursor)

Example 20 with Replace

use of org.h2.command.dml.Replace in project h2database by h2database.

the class BnfSyntax method getLink.

/**
 * Get the HTML link to the given token.
 *
 * @param bnf the BNF
 * @param token the token
 * @return the HTML link
 */
String getLink(Bnf bnf, String token) {
    RuleHead found = null;
    String key = Bnf.getRuleMapKey(token);
    for (int i = 0; i < token.length(); i++) {
        String test = StringUtils.toLowerEnglish(key.substring(i));
        RuleHead r = bnf.getRuleHead(test);
        if (r != null) {
            found = r;
            break;
        }
    }
    if (found == null) {
        return token;
    }
    String page = "grammar.html";
    if (found.getSection().startsWith("Data Types")) {
        page = "datatypes.html";
    } else if (found.getSection().startsWith("Functions")) {
        page = "functions.html";
    } else if (token.equals("@func@")) {
        return "<a href=\"functions.html\">Function</a>";
    } else if (found.getRule() instanceof RuleFixed) {
        found.getRule().accept(this);
        return html;
    }
    String link = found.getTopic().toLowerCase().replace(' ', '_');
    link = page + "#" + StringUtils.urlEncode(link);
    return "<a href=\"" + link + "\">" + token + "</a>";
}
Also used : RuleFixed(org.h2.bnf.RuleFixed) RuleHead(org.h2.bnf.RuleHead)

Aggregations

Connection (java.sql.Connection)7 ResultSet (java.sql.ResultSet)7 PreparedStatement (java.sql.PreparedStatement)6 Statement (java.sql.Statement)6 Expression (org.h2.expression.Expression)4 JdbcConnection (org.h2.jdbc.JdbcConnection)4 MVStore (org.h2.mvstore.MVStore)4 Column (org.h2.table.Column)4 Value (org.h2.value.Value)4 ValueString (org.h2.value.ValueString)4 BigDecimal (java.math.BigDecimal)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 ExpressionColumn (org.h2.expression.ExpressionColumn)3 SQLException (java.sql.SQLException)2 LinkedHashMap (java.util.LinkedHashMap)2 RuleFixed (org.h2.bnf.RuleFixed)2 RuleHead (org.h2.bnf.RuleHead)2 ValueExpression (org.h2.expression.ValueExpression)2 JdbcSQLException (org.h2.jdbc.JdbcSQLException)2 DbException (org.h2.message.DbException)2