Search in sources :

Example 71 with Delete

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

the class ScanIndex method remove.

@Override
public void remove(Session session, Row row) {
    // in-memory
    if (!database.isMultiVersion() && rowCount == 1) {
        rows = New.arrayList();
        firstFree = -1;
    } else {
        Row free = session.createRow(null, 1);
        free.setKey(firstFree);
        long key = row.getKey();
        if (rows.size() <= key) {
            throw DbException.get(ErrorCode.ROW_NOT_FOUND_WHEN_DELETING_1, rows.size() + ": " + key);
        }
        rows.set((int) key, free);
        firstFree = key;
    }
    if (database.isMultiVersion()) {
        // if storage is null, the delete flag is not yet set
        row.setDeleted(true);
        if (delta == null) {
            delta = new HashSet<>();
        }
        boolean wasAdded = delta.remove(row);
        if (!wasAdded) {
            delta.add(row);
        }
        incrementRowCount(session.getId(), -1);
    }
    rowCount--;
}
Also used : Row(org.h2.result.Row) SearchRow(org.h2.result.SearchRow)

Example 72 with Delete

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

the class JdbcPreparedStatement method clearParameters.

/**
 * Clears all parameters.
 *
 * @throws SQLException if this object is closed or invalid
 */
@Override
public void clearParameters() throws SQLException {
    try {
        debugCodeCall("clearParameters");
        checkClosed();
        ArrayList<? extends ParameterInterface> parameters = command.getParameters();
        for (ParameterInterface param : parameters) {
            // can only delete old temp files if they are not in the batch
            param.setValue(null, batchParameters == null);
        }
    } catch (Exception e) {
        throw logAndConvert(e);
    }
}
Also used : ParameterInterface(org.h2.expression.ParameterInterface) DbException(org.h2.message.DbException) SQLException(java.sql.SQLException)

Example 73 with Delete

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

the class JdbcPreparedStatement method setParameter.

// =============================================================
private void setParameter(int parameterIndex, Value value) {
    checkClosed();
    parameterIndex--;
    ArrayList<? extends ParameterInterface> parameters = command.getParameters();
    if (parameterIndex < 0 || parameterIndex >= parameters.size()) {
        throw DbException.getInvalidValueException("parameterIndex", parameterIndex + 1);
    }
    ParameterInterface param = parameters.get(parameterIndex);
    // can only delete old temp files if they are not in the batch
    param.setValue(value, batchParameters == null);
}
Also used : ParameterInterface(org.h2.expression.ParameterInterface)

Example 74 with Delete

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

the class TriggerObject method getTypeNameList.

public String getTypeNameList() {
    StatementBuilder buff = new StatementBuilder();
    if ((typeMask & Trigger.INSERT) != 0) {
        buff.appendExceptFirst(", ");
        buff.append("INSERT");
    }
    if ((typeMask & Trigger.UPDATE) != 0) {
        buff.appendExceptFirst(", ");
        buff.append("UPDATE");
    }
    if ((typeMask & Trigger.DELETE) != 0) {
        buff.appendExceptFirst(", ");
        buff.append("DELETE");
    }
    if ((typeMask & Trigger.SELECT) != 0) {
        buff.appendExceptFirst(", ");
        buff.append("SELECT");
    }
    if (onRollback) {
        buff.appendExceptFirst(", ");
        buff.append("ROLLBACK");
    }
    return buff.toString();
}
Also used : StatementBuilder(org.h2.util.StatementBuilder)

Example 75 with Delete

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

the class TestCluster method testCreateClusterAtRuntime.

private void testCreateClusterAtRuntime() throws SQLException {
    if (config.memory || config.networked || config.cipher != null) {
        return;
    }
    deleteFiles();
    org.h2.Driver.load();
    String user = getUser(), password = getPassword();
    Connection conn;
    Statement stat;
    int len = 10;
    // initialize the database
    Server n1 = org.h2.tools.Server.createTcpServer("-baseDir", getBaseDir() + "/node1").start();
    int port1 = n1.getPort();
    String url1 = getURL("jdbc:h2:tcp://localhost:" + port1 + "/test", false);
    conn = getConnection(url1, user, password);
    stat = conn.createStatement();
    stat.execute("create table test(id int primary key, name varchar) as " + "select x, 'Data' || x from system_range(0, " + (len - 1) + ")");
    stat.execute("create user test password 'test'");
    stat.execute("grant all on test to test");
    // start the second server
    Server n2 = org.h2.tools.Server.createTcpServer("-baseDir", getBaseDir() + "/node2").start();
    int port2 = n2.getPort();
    String url2 = getURL("jdbc:h2:tcp://localhost:" + port2 + "/test", false);
    // copy the database and initialize the cluster
    String serverList = "localhost:" + port1 + ",localhost:" + port2;
    CreateCluster.main("-urlSource", url1, "-urlTarget", url2, "-user", user, "-password", password, "-serverList", serverList);
    // check the original connection is closed
    assertThrows(ErrorCode.CONNECTION_BROKEN_1, stat).execute("select * from test");
    JdbcUtils.closeSilently(conn);
    // test the cluster connection
    String urlCluster = getURL("jdbc:h2:tcp://" + serverList + "/test", false);
    Connection connApp = getConnection(urlCluster + ";AUTO_RECONNECT=TRUE", user, password);
    check(connApp, len, "'" + serverList + "'");
    // delete the rows, but don't commit
    connApp.setAutoCommit(false);
    connApp.createStatement().execute("delete from test");
    // stop server 2, and test if only one server is available
    n2.stop();
    // rollback the transaction
    connApp.createStatement().executeQuery("select count(*) from test");
    connApp.rollback();
    check(connApp, len, "''");
    connApp.setAutoCommit(true);
    // re-create the cluster
    n2 = org.h2.tools.Server.createTcpServer("-tcpPort", "" + port2, "-baseDir", getBaseDir() + "/node2").start();
    CreateCluster.main("-urlSource", url1, "-urlTarget", url2, "-user", user, "-password", password, "-serverList", serverList);
    // test the cluster connection
    check(connApp, len, "'" + serverList + "'");
    connApp.close();
    // test a non-admin user
    String user2 = "test", password2 = getPassword("test");
    connApp = getConnection(urlCluster, user2, password2);
    check(connApp, len, "'" + serverList + "'");
    connApp.close();
    n1.stop();
    // test non-admin cluster connection if only one server runs
    Connection connApp2 = getConnection(urlCluster + ";AUTO_RECONNECT=TRUE", user2, password2);
    check(connApp2, len, "''");
    connApp2.close();
    // test non-admin cluster connection if only one server runs
    connApp2 = getConnection(urlCluster + ";AUTO_RECONNECT=TRUE", user2, password2);
    check(connApp2, len, "''");
    connApp2.close();
    n2.stop();
    deleteFiles();
}
Also used : Server(org.h2.tools.Server) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection)

Aggregations

Connection (java.sql.Connection)40 PreparedStatement (java.sql.PreparedStatement)39 Statement (java.sql.Statement)38 ResultSet (java.sql.ResultSet)36 JdbcConnection (org.h2.jdbc.JdbcConnection)25 SQLException (java.sql.SQLException)17 SimpleResultSet (org.h2.tools.SimpleResultSet)14 Savepoint (java.sql.Savepoint)13 StatementBuilder (org.h2.util.StatementBuilder)9 DbException (org.h2.message.DbException)8 Column (org.h2.table.Column)8 ValueString (org.h2.value.ValueString)7 Random (java.util.Random)6 Expression (org.h2.expression.Expression)5 ValueExpression (org.h2.expression.ValueExpression)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ArrayList (java.util.ArrayList)4 GridH2Table (org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)4 AlterTableAddConstraint (org.h2.command.ddl.AlterTableAddConstraint)4 AlterTableDropConstraint (org.h2.command.ddl.AlterTableDropConstraint)4