Search in sources :

Example 71 with StatementBuilder

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

the class RowImpl method toString.

@Override
public String toString() {
    StatementBuilder buff = new StatementBuilder("( /* key:");
    buff.append(getKey());
    if (version != 0) {
        buff.append(" v:").append(version);
    }
    if (isDeleted()) {
        buff.append(" deleted");
    }
    buff.append(" */ ");
    if (data != null) {
        for (Value v : data) {
            buff.appendExceptFirst(", ");
            buff.append(v == null ? "null" : v.getTraceSQL());
        }
    }
    return buff.append(')').toString();
}
Also used : StatementBuilder(org.h2.util.StatementBuilder) Value(org.h2.value.Value)

Example 72 with StatementBuilder

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

the class SortOrder method getSQL.

/**
 * Create the SQL snippet that describes this sort order.
 * This is the SQL snippet that usually appears after the ORDER BY clause.
 *
 * @param list the expression list
 * @param visible the number of columns in the select list
 * @return the SQL snippet
 */
public String getSQL(Expression[] list, int visible) {
    StatementBuilder buff = new StatementBuilder();
    int i = 0;
    for (int idx : queryColumnIndexes) {
        buff.appendExceptFirst(", ");
        if (idx < visible) {
            buff.append(idx + 1);
        } else {
            buff.append('=').append(StringUtils.unEnclose(list[idx].getSQL()));
        }
        int type = sortTypes[i++];
        if ((type & DESCENDING) != 0) {
            buff.append(" DESC");
        }
        if ((type & NULLS_FIRST) != 0) {
            buff.append(" NULLS FIRST");
        } else if ((type & NULLS_LAST) != 0) {
            buff.append(" NULLS LAST");
        }
    }
    return buff.toString();
}
Also used : StatementBuilder(org.h2.util.StatementBuilder)

Example 73 with StatementBuilder

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

the class UpdatableRow method updateRow.

/**
 * Update a row in the database.
 *
 * @param current the old row
 * @param updateRow the new row
 * @throws SQLException if the row has been deleted
 */
public void updateRow(Value[] current, Value[] updateRow) throws SQLException {
    StatementBuilder buff = new StatementBuilder("UPDATE ");
    appendTableName(buff);
    buff.append(" SET ");
    appendColumnList(buff, true);
    // TODO updatable result set: we could add all current values to the
    // where clause
    // - like this optimistic ('no') locking is possible
    appendKeyCondition(buff);
    PreparedStatement prep = conn.prepareStatement(buff.toString());
    int j = 1;
    for (int i = 0; i < columnCount; i++) {
        Value v = updateRow[i];
        if (v == null) {
            v = current[i];
        }
        v.set(prep, j++);
    }
    setKey(prep, j, current);
    int count = prep.executeUpdate();
    if (count != 1) {
        // the row has been deleted
        throw DbException.get(ErrorCode.NO_DATA_AVAILABLE);
    }
}
Also used : StatementBuilder(org.h2.util.StatementBuilder) Value(org.h2.value.Value) PreparedStatement(java.sql.PreparedStatement)

Example 74 with StatementBuilder

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

the class Command method select.

private Result select(DbInterface db) throws SQLException {
    StatementBuilder buff = new StatementBuilder("SELECT ");
    for (String s : selectList) {
        buff.appendExceptFirst(", ");
        buff.append(s);
    }
    buff.append("  FROM ").append(table.getName()).append(" M").append(' ').append(join);
    if (condition != null) {
        buff.append("  WHERE ").append(condition);
    }
    if (order.trim().length() > 0) {
        buff.append("  ORDER BY ").append(order);
    }
    return db.select(buff.toString());
}
Also used : StatementBuilder(org.h2.util.StatementBuilder)

Example 75 with StatementBuilder

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

the class TableDefinition method delete.

void delete(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("DELETE FROM ");
    buff.append(db.getDialect().getTableName(schemaName, tableName));
    buff.resetCount();
    Object alias = ClassUtils.newObject(obj.getClass());
    Query<Object> query = Query.from(db, alias);
    boolean firstCondition = true;
    for (FieldDefinition field : fields) {
        if (field.isPrimaryKey) {
            Object aliasValue = field.getValue(alias);
            Object value = field.getValue(obj);
            if (!firstCondition) {
                query.addConditionToken(ConditionAndOr.AND);
            }
            firstCondition = false;
            query.addConditionToken(new Condition<>(aliasValue, value, CompareType.EQUAL));
        }
    }
    stat.setSQL(buff.toString());
    query.appendWhere(stat);
    StatementLogger.delete(stat.getSQL());
    stat.executeUpdate();
}
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