Search in sources :

Example 26 with StatementBuilder

use of org.h2.util.StatementBuilder in project h2database by h2database.

the class UpdatableRow method deleteRow.

/**
 * Delete the given row in the database.
 *
 * @param current the row
 * @throws SQLException if this row has already been deleted
 */
public void deleteRow(Value[] current) throws SQLException {
    StatementBuilder buff = new StatementBuilder("DELETE FROM ");
    appendTableName(buff);
    appendKeyCondition(buff);
    PreparedStatement prep = conn.prepareStatement(buff.toString());
    setKey(prep, 1, current);
    int count = prep.executeUpdate();
    if (count != 1) {
        // the row has already been deleted
        throw DbException.get(ErrorCode.NO_DATA_AVAILABLE);
    }
}
Also used : StatementBuilder(org.h2.util.StatementBuilder) PreparedStatement(java.sql.PreparedStatement)

Example 27 with StatementBuilder

use of org.h2.util.StatementBuilder in project h2database by h2database.

the class UpdatableRow method readRow.

/**
 * Re-reads a row from the database and updates the values in the array.
 *
 * @param row the values that contain the key
 * @return the row
 */
public Value[] readRow(Value[] row) throws SQLException {
    StatementBuilder buff = new StatementBuilder("SELECT ");
    appendColumnList(buff, false);
    buff.append(" FROM ");
    appendTableName(buff);
    appendKeyCondition(buff);
    PreparedStatement prep = conn.prepareStatement(buff.toString());
    setKey(prep, 1, row);
    ResultSet rs = prep.executeQuery();
    if (!rs.next()) {
        throw DbException.get(ErrorCode.NO_DATA_AVAILABLE);
    }
    Value[] newRow = new Value[columnCount];
    for (int i = 0; i < columnCount; i++) {
        int type = result.getColumnType(i);
        newRow[i] = DataType.readValue(conn.getSession(), rs, i + 1, type);
    }
    return newRow;
}
Also used : StatementBuilder(org.h2.util.StatementBuilder) ResultSet(java.sql.ResultSet) Value(org.h2.value.Value) PreparedStatement(java.sql.PreparedStatement)

Example 28 with StatementBuilder

use of org.h2.util.StatementBuilder in project h2database by h2database.

the class TableDefinition method insert.

long insert(Db db, Object obj, boolean returnKey) {
    SQLStatement stat = new SQLStatement(db);
    StatementBuilder buff = new StatementBuilder("INSERT INTO ");
    buff.append(db.getDialect().getTableName(schemaName, tableName)).append('(');
    for (FieldDefinition field : fields) {
        buff.appendExceptFirst(", ");
        buff.append(field.columnName);
    }
    buff.append(") VALUES(");
    buff.resetCount();
    for (FieldDefinition field : fields) {
        buff.appendExceptFirst(", ");
        buff.append('?');
        Object value = getValue(obj, field);
        stat.addParameter(value);
    }
    buff.append(')');
    stat.setSQL(buff.toString());
    StatementLogger.insert(stat.getSQL());
    if (returnKey) {
        return stat.executeInsert();
    }
    return stat.executeUpdate();
}
Also used : StatementBuilder(org.h2.util.StatementBuilder)

Example 29 with StatementBuilder

use of org.h2.util.StatementBuilder in project h2database by h2database.

the class TableDefinition method merge.

void merge(Db db, Object obj) {
    if (primaryKeyColumnNames == null || primaryKeyColumnNames.size() == 0) {
        throw new IllegalStateException("No primary key columns defined " + "for table " + obj.getClass() + " - no update possible");
    }
    SQLStatement stat = new SQLStatement(db);
    StatementBuilder buff = new StatementBuilder("MERGE INTO ");
    buff.append(db.getDialect().getTableName(schemaName, tableName)).append(" (");
    buff.resetCount();
    for (FieldDefinition field : fields) {
        buff.appendExceptFirst(", ");
        buff.append(field.columnName);
    }
    buff.append(") KEY(");
    buff.resetCount();
    for (FieldDefinition field : fields) {
        if (field.isPrimaryKey) {
            buff.appendExceptFirst(", ");
            buff.append(field.columnName);
        }
    }
    buff.append(") ");
    buff.resetCount();
    buff.append("VALUES (");
    for (FieldDefinition field : fields) {
        buff.appendExceptFirst(", ");
        buff.append('?');
        Object value = getValue(obj, field);
        stat.addParameter(value);
    }
    buff.append(')');
    stat.setSQL(buff.toString());
    StatementLogger.merge(stat.getSQL());
    stat.executeUpdate();
}
Also used : StatementBuilder(org.h2.util.StatementBuilder)

Example 30 with StatementBuilder

use of org.h2.util.StatementBuilder in project h2database by h2database.

the class ValueArray method getString.

@Override
public String getString() {
    StatementBuilder buff = new StatementBuilder("(");
    for (Value v : values) {
        buff.appendExceptFirst(", ");
        buff.append(v.getString());
    }
    return buff.append(')').toString();
}
Also used : StatementBuilder(org.h2.util.StatementBuilder)

Aggregations

StatementBuilder (org.h2.util.StatementBuilder)82 Value (org.h2.value.Value)16 Column (org.h2.table.Column)15 IndexColumn (org.h2.table.IndexColumn)11 PreparedStatement (java.sql.PreparedStatement)9 Expression (org.h2.expression.Expression)9 ValueString (org.h2.value.ValueString)7 Index (org.h2.index.Index)6 DbException (org.h2.message.DbException)6 ResultSet (java.sql.ResultSet)5 Row (org.h2.result.Row)4 SQLException (java.sql.SQLException)3 ArrayList (java.util.ArrayList)3 Constraint (org.h2.constraint.Constraint)3 Database (org.h2.engine.Database)3 ExpressionColumn (org.h2.expression.ExpressionColumn)3 HashMap (java.util.HashMap)2 SelectOrderBy (org.h2.command.dml.SelectOrderBy)2 ResultInterface (org.h2.result.ResultInterface)2 SearchRow (org.h2.result.SearchRow)2