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");
}
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();
}
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();
}
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;
}
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();
}
Aggregations