Search in sources :

Example 51 with StatementBuilder

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

the class ConditionInConstantSet method getSQL.

@Override
public String getSQL() {
    StatementBuilder buff = new StatementBuilder("(");
    buff.append(left.getSQL()).append(" IN(");
    for (Expression e : valueList) {
        buff.appendExceptFirst(", ");
        buff.append(e.getSQL());
    }
    return buff.append("))").toString();
}
Also used : StatementBuilder(org.h2.util.StatementBuilder)

Example 52 with StatementBuilder

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

the class Merge method prepare.

@Override
public void prepare() {
    if (columns == null) {
        if (!valuesExpressionList.isEmpty() && valuesExpressionList.get(0).length == 0) {
            // special case where table is used as a sequence
            columns = new Column[0];
        } else {
            columns = targetTable.getColumns();
        }
    }
    if (!valuesExpressionList.isEmpty()) {
        for (Expression[] expr : valuesExpressionList) {
            if (expr.length != columns.length) {
                throw DbException.get(ErrorCode.COLUMN_COUNT_DOES_NOT_MATCH);
            }
            for (int i = 0; i < expr.length; i++) {
                Expression e = expr[i];
                if (e != null) {
                    expr[i] = e.optimize(session);
                }
            }
        }
    } else {
        query.prepare();
        if (query.getColumnCount() != columns.length) {
            throw DbException.get(ErrorCode.COLUMN_COUNT_DOES_NOT_MATCH);
        }
    }
    if (keys == null) {
        Index idx = targetTable.getPrimaryKey();
        if (idx == null) {
            throw DbException.get(ErrorCode.CONSTRAINT_NOT_FOUND_1, "PRIMARY KEY");
        }
        keys = idx.getColumns();
    }
    StatementBuilder buff = new StatementBuilder("UPDATE ");
    buff.append(targetTable.getSQL()).append(" SET ");
    for (Column c : columns) {
        buff.appendExceptFirst(", ");
        buff.append(c.getSQL()).append("=?");
    }
    buff.append(" WHERE ");
    buff.resetCount();
    for (Column c : keys) {
        buff.appendExceptFirst(" AND ");
        buff.append(c.getSQL()).append("=?");
    }
    String sql = buff.toString();
    update = session.prepare(sql);
}
Also used : Expression(org.h2.expression.Expression) Column(org.h2.table.Column) StatementBuilder(org.h2.util.StatementBuilder) Index(org.h2.index.Index)

Example 53 with StatementBuilder

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

the class ConstraintReferential method getShortDescription.

/**
 * Get a short description of the constraint. This includes the constraint
 * name (if set), and the constraint expression.
 *
 * @param searchIndex the index, or null
 * @param check the row, or null
 * @return the description
 */
private String getShortDescription(Index searchIndex, SearchRow check) {
    StatementBuilder buff = new StatementBuilder(getName());
    buff.append(": ").append(table.getSQL()).append(" FOREIGN KEY(");
    for (IndexColumn c : columns) {
        buff.appendExceptFirst(", ");
        buff.append(c.getSQL());
    }
    buff.append(") REFERENCES ").append(refTable.getSQL()).append('(');
    buff.resetCount();
    for (IndexColumn r : refColumns) {
        buff.appendExceptFirst(", ");
        buff.append(r.getSQL());
    }
    buff.append(')');
    if (searchIndex != null && check != null) {
        buff.append(" (");
        buff.resetCount();
        Column[] cols = searchIndex.getColumns();
        int len = Math.min(columns.length, cols.length);
        for (int i = 0; i < len; i++) {
            int idx = cols[i].getColumnId();
            Value c = check.getValue(idx);
            buff.appendExceptFirst(", ");
            buff.append(c == null ? "" : c.toString());
        }
        buff.append(')');
    }
    return buff.toString();
}
Also used : Column(org.h2.table.Column) IndexColumn(org.h2.table.IndexColumn) StatementBuilder(org.h2.util.StatementBuilder) Value(org.h2.value.Value) IndexColumn(org.h2.table.IndexColumn)

Example 54 with StatementBuilder

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

the class ConstraintReferential method buildDeleteSQL.

private void buildDeleteSQL() {
    if (deleteAction == ConstraintActionType.RESTRICT) {
        return;
    }
    StatementBuilder buff = new StatementBuilder();
    if (deleteAction == ConstraintActionType.CASCADE) {
        buff.append("DELETE FROM ").append(table.getSQL());
    } else {
        appendUpdate(buff);
    }
    appendWhere(buff);
    deleteSQL = buff.toString();
}
Also used : StatementBuilder(org.h2.util.StatementBuilder)

Example 55 with StatementBuilder

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

the class ConstraintReferential method appendWhere.

private void appendWhere(StatementBuilder buff) {
    buff.append(" WHERE ");
    buff.resetCount();
    for (IndexColumn c : columns) {
        buff.appendExceptFirst(" AND ");
        buff.append(Parser.quoteIdentifier(c.column.getName())).append("=?");
    }
}
Also used : IndexColumn(org.h2.table.IndexColumn)

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