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