Search in sources :

Example 21 with Replace

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

the class TestView method testViewAlterAndCommandCache.

/**
 * Make sure that when we change a view, that change in reflected in other
 * sessions command cache.
 */
private void testViewAlterAndCommandCache() throws SQLException {
    deleteDb("view");
    Connection conn = getConnection("view");
    Statement stat = conn.createStatement();
    stat.execute("create table t0(id int primary key)");
    stat.execute("create table t1(id int primary key)");
    stat.execute("insert into t0 values(0)");
    stat.execute("insert into t1 values(1)");
    stat.execute("create view v1 as select * from t0");
    ResultSet rs = stat.executeQuery("select * from v1");
    assertTrue(rs.next());
    assertEquals(0, rs.getInt(1));
    stat.execute("create or replace view v1 as select * from t1");
    rs = stat.executeQuery("select * from v1");
    assertTrue(rs.next());
    assertEquals(1, rs.getInt(1));
    conn.close();
    deleteDb("view");
}
Also used : Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) ResultSet(java.sql.ResultSet)

Example 22 with Replace

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

the class TestLob method testRemovedAfterTimeout.

private void testRemovedAfterTimeout() throws Exception {
    if (config.lazy) {
        return;
    }
    deleteDb("lob");
    final String url = getURL("lob;lob_timeout=50", true);
    Connection conn = getConnection(url);
    Statement stat = conn.createStatement();
    stat.execute("create table test(id int primary key, data clob)");
    PreparedStatement prep = conn.prepareStatement("insert into test values(?, ?)");
    prep.setInt(1, 1);
    prep.setString(2, "aaa" + new String(new char[1024 * 16]).replace((char) 0, 'x'));
    prep.execute();
    prep.setInt(1, 2);
    prep.setString(2, "bbb" + new String(new char[1024 * 16]).replace((char) 0, 'x'));
    prep.execute();
    ResultSet rs = stat.executeQuery("select * from test order by id");
    rs.next();
    Clob c1 = rs.getClob(2);
    assertEquals("aaa", c1.getSubString(1, 3));
    rs.next();
    assertEquals("aaa", c1.getSubString(1, 3));
    rs.close();
    assertEquals("aaa", c1.getSubString(1, 3));
    stat.execute("delete from test");
    c1.getSubString(1, 3);
    // wait until it times out
    Thread.sleep(100);
    // start a new transaction, to be sure
    stat.execute("delete from test");
    assertThrows(SQLException.class, c1).getSubString(1, 3);
    conn.close();
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Clob(java.sql.Clob)

Example 23 with Replace

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

the class Replace method getPlanSQL.

@Override
public String getPlanSQL() {
    StatementBuilder buff = new StatementBuilder("REPLACE INTO ");
    buff.append(table.getSQL()).append('(');
    for (Column c : columns) {
        buff.appendExceptFirst(", ");
        buff.append(c.getSQL());
    }
    buff.append(')');
    buff.append('\n');
    if (!list.isEmpty()) {
        buff.append("VALUES ");
        int row = 0;
        for (Expression[] expr : list) {
            if (row++ > 0) {
                buff.append(", ");
            }
            buff.append('(');
            buff.resetCount();
            for (Expression e : expr) {
                buff.appendExceptFirst(", ");
                if (e == null) {
                    buff.append("DEFAULT");
                } else {
                    buff.append(e.getSQL());
                }
            }
            buff.append(')');
        }
    } else {
        buff.append(query.getPlanSQL());
    }
    return buff.toString();
}
Also used : Column(org.h2.table.Column) Expression(org.h2.expression.Expression) StatementBuilder(org.h2.util.StatementBuilder)

Example 24 with Replace

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

the class CreateView method update.

@Override
public int update() {
    session.commit(true);
    session.getUser().checkAdmin();
    Database db = session.getDatabase();
    TableView view = null;
    Table old = getSchema().findTableOrView(session, viewName);
    if (old != null) {
        if (ifNotExists) {
            return 0;
        }
        if (!orReplace || TableType.VIEW != old.getTableType()) {
            throw DbException.get(ErrorCode.VIEW_ALREADY_EXISTS_1, viewName);
        }
        view = (TableView) old;
    }
    int id = getObjectId();
    String querySQL;
    if (select == null) {
        querySQL = selectSQL;
    } else {
        ArrayList<Parameter> params = select.getParameters();
        if (params != null && !params.isEmpty()) {
            throw DbException.getUnsupportedException("parameters in views");
        }
        querySQL = select.getPlanSQL();
    }
    Column[] columnTemplatesAsUnknowns = null;
    Column[] columnTemplatesAsStrings = null;
    if (columnNames != null) {
        columnTemplatesAsUnknowns = new Column[columnNames.length];
        columnTemplatesAsStrings = new Column[columnNames.length];
        for (int i = 0; i < columnNames.length; ++i) {
            // non table expressions are fine to use unknown column type
            columnTemplatesAsUnknowns[i] = new Column(columnNames[i], Value.UNKNOWN);
            // table expressions can't have unknown types - so we use string instead
            columnTemplatesAsStrings[i] = new Column(columnNames[i], Value.STRING);
        }
    }
    if (view == null) {
        if (isTableExpression) {
            view = TableView.createTableViewMaybeRecursive(getSchema(), id, viewName, querySQL, null, columnTemplatesAsStrings, session, false, /* literalsChecked */
            isTableExpression, true, /* isPersistent */
            db);
        } else {
            view = new TableView(getSchema(), id, viewName, querySQL, null, columnTemplatesAsUnknowns, session, false, /* allow recursive */
            false, /* literalsChecked */
            isTableExpression, true);
        }
    } else {
        // TODO support isTableExpression in replace function...
        view.replace(querySQL, columnTemplatesAsUnknowns, session, false, force, false);
        view.setModified();
    }
    if (comment != null) {
        view.setComment(comment);
    }
    if (old == null) {
        db.addSchemaObject(session, view);
        db.unlockMeta(session);
    } else {
        db.updateMeta(session, view);
    }
    return 0;
}
Also used : Table(org.h2.table.Table) Column(org.h2.table.Column) Database(org.h2.engine.Database) Parameter(org.h2.expression.Parameter) TableView(org.h2.table.TableView)

Example 25 with Replace

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

the class TestLob method testLobCleanupSessionTemporaries.

private void testLobCleanupSessionTemporaries() throws SQLException {
    if (config.mvStore) {
        return;
    }
    deleteDb("lob");
    Connection conn = getConnection("lob");
    Statement stat = conn.createStatement();
    stat.execute("create table test(data clob)");
    ResultSet rs = stat.executeQuery("select count(*) " + "from INFORMATION_SCHEMA.LOBS");
    assertTrue(rs.next());
    assertEquals(0, rs.getInt(1));
    rs.close();
    PreparedStatement prep = conn.prepareStatement("INSERT INTO test(data) VALUES(?)");
    String name = new String(new char[200]).replace((char) 0, 'x');
    prep.setString(1, name);
    prep.execute();
    prep.close();
    rs = stat.executeQuery("select count(*) from INFORMATION_SCHEMA.LOBS");
    assertTrue(rs.next());
    assertEquals(1, rs.getInt(1));
    rs.close();
    conn.close();
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

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