Search in sources :

Example 46 with StatementBuilder

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

the class MergeUsing method getPlanSQL.

// Use the regular merge syntax as our plan SQL
@Override
public String getPlanSQL() {
    StatementBuilder buff = new StatementBuilder("MERGE INTO ");
    buff.append(targetTable.getSQL()).append('(');
    for (Column c : columns) {
        buff.appendExceptFirst(", ");
        buff.append(c.getSQL());
    }
    buff.append(')');
    if (keys != null) {
        buff.append(" KEY(");
        buff.resetCount();
        for (Column c : keys) {
            buff.appendExceptFirst(", ");
            buff.append(c.getSQL());
        }
        buff.append(')');
    }
    buff.append('\n');
    if (!valuesExpressionList.isEmpty()) {
        buff.append("VALUES ");
        int row = 0;
        for (Expression[] expr : valuesExpressionList) {
            if (row++ > 0) {
                buff.append(", ");
            }
            buff.append('(');
            buff.resetCount();
            for (Expression e : expr) {
                buff.appendExceptFirst(", ");
                if (e == null) {
                    buff.append("DEFAULT");
                } else {
                    buff.append(e.getSQL());
                }
            }
            buff.append(')');
        }
    } else {
        buff.append(query.getPlanSQL());
    }
    return buff.toString();
}
Also used : Column(org.h2.table.Column) Expression(org.h2.expression.Expression) StatementBuilder(org.h2.util.StatementBuilder)

Example 47 with StatementBuilder

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

the class Replace method getPlanSQL.

@Override
public String getPlanSQL() {
    StatementBuilder buff = new StatementBuilder("REPLACE INTO ");
    buff.append(table.getSQL()).append('(');
    for (Column c : columns) {
        buff.appendExceptFirst(", ");
        buff.append(c.getSQL());
    }
    buff.append(')');
    buff.append('\n');
    if (!list.isEmpty()) {
        buff.append("VALUES ");
        int row = 0;
        for (Expression[] expr : list) {
            if (row++ > 0) {
                buff.append(", ");
            }
            buff.append('(');
            buff.resetCount();
            for (Expression e : expr) {
                buff.appendExceptFirst(", ");
                if (e == null) {
                    buff.append("DEFAULT");
                } else {
                    buff.append(e.getSQL());
                }
            }
            buff.append(')');
        }
    } else {
        buff.append(query.getPlanSQL());
    }
    return buff.toString();
}
Also used : Column(org.h2.table.Column) Expression(org.h2.expression.Expression) StatementBuilder(org.h2.util.StatementBuilder)

Example 48 with StatementBuilder

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

the class Replace method prepare.

@Override
public void prepare() {
    if (columns == null) {
        if (!list.isEmpty() && list.get(0).length == 0) {
            // special case where table is used as a sequence
            columns = new Column[0];
        } else {
            columns = table.getColumns();
        }
    }
    if (!list.isEmpty()) {
        for (Expression[] expr : list) {
            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 = table.getPrimaryKey();
        if (idx == null) {
            throw DbException.get(ErrorCode.CONSTRAINT_NOT_FOUND_1, "PRIMARY KEY");
        }
        keys = idx.getColumns();
    }
    // INSERT
    for (Column key : keys) {
        boolean found = false;
        for (Column column : columns) {
            if (column.getColumnId() == key.getColumnId()) {
                found = true;
                break;
            }
        }
        if (!found) {
            return;
        }
    }
    StatementBuilder buff = new StatementBuilder("UPDATE ");
    buff.append(table.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 49 with StatementBuilder

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

the class ScriptCommand method generateInsertValues.

private int generateInsertValues(int count, Table table) throws IOException {
    PlanItem plan = table.getBestPlanItem(session, null, null, -1, null, null);
    Index index = plan.getIndex();
    Cursor cursor = index.find(session, null, null);
    Column[] columns = table.getColumns();
    StatementBuilder buff = new StatementBuilder("INSERT INTO ");
    buff.append(table.getSQL()).append('(');
    for (Column col : columns) {
        buff.appendExceptFirst(", ");
        buff.append(Parser.quoteIdentifier(col.getName()));
    }
    buff.append(") VALUES");
    if (!simple) {
        buff.append('\n');
    }
    buff.append('(');
    String ins = buff.toString();
    buff = null;
    while (cursor.next()) {
        Row row = cursor.get();
        if (buff == null) {
            buff = new StatementBuilder(ins);
        } else {
            buff.append(",\n(");
        }
        for (int j = 0; j < row.getColumnCount(); j++) {
            if (j > 0) {
                buff.append(", ");
            }
            Value v = row.getValue(j);
            if (v.getPrecision() > lobBlockSize) {
                int id;
                if (v.getType() == Value.CLOB) {
                    id = writeLobStream(v);
                    buff.append("SYSTEM_COMBINE_CLOB(").append(id).append(')');
                } else if (v.getType() == Value.BLOB) {
                    id = writeLobStream(v);
                    buff.append("SYSTEM_COMBINE_BLOB(").append(id).append(')');
                } else {
                    buff.append(v.getSQL());
                }
            } else {
                buff.append(v.getSQL());
            }
        }
        buff.append(')');
        count++;
        if ((count & 127) == 0) {
            checkCanceled();
        }
        if (simple || buff.length() > Constants.IO_BUFFER_SIZE) {
            add(buff.toString(), true);
            buff = null;
        }
    }
    if (buff != null) {
        add(buff.toString(), true);
    }
    return count;
}
Also used : Column(org.h2.table.Column) ExpressionColumn(org.h2.expression.ExpressionColumn) StatementBuilder(org.h2.util.StatementBuilder) Value(org.h2.value.Value) PlanItem(org.h2.table.PlanItem) Index(org.h2.index.Index) ValueString(org.h2.value.ValueString) Row(org.h2.result.Row) Cursor(org.h2.index.Cursor) Constraint(org.h2.constraint.Constraint)

Example 50 with StatementBuilder

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

the class Update method getPlanSQL.

@Override
public String getPlanSQL() {
    StatementBuilder buff = new StatementBuilder("UPDATE ");
    buff.append(targetTableFilter.getPlanSQL(false)).append("\nSET\n    ");
    for (Column c : columns) {
        Expression e = expressionMap.get(c);
        buff.appendExceptFirst(",\n    ");
        buff.append(c.getName()).append(" = ").append(e.getSQL());
    }
    if (condition != null) {
        buff.append("\nWHERE ").append(StringUtils.unEnclose(condition.getSQL()));
    }
    if (limitExpr != null) {
        buff.append("\nLIMIT ").append(StringUtils.unEnclose(limitExpr.getSQL()));
    }
    return buff.toString();
}
Also used : Column(org.h2.table.Column) ValueExpression(org.h2.expression.ValueExpression) Expression(org.h2.expression.Expression) 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