Search in sources :

Example 41 with StatementBuilder

use of org.h2.util.StatementBuilder in project ignite by apache.

the class GridSqlArray method getSQL.

/**
 * {@inheritDoc}
 */
@Override
public String getSQL() {
    if (size() == 0)
        return "()";
    StatementBuilder buff = new StatementBuilder("(");
    for (int i = 0; i < size(); i++) {
        buff.appendExceptFirst(", ");
        buff.append(child(i).getSQL());
    }
    if (size() == 1)
        buff.append(',');
    return buff.append(')').toString();
}
Also used : StatementBuilder(org.h2.util.StatementBuilder)

Example 42 with StatementBuilder

use of org.h2.util.StatementBuilder in project ignite by apache.

the class GridSqlFunction method getSQL.

/**
 * {@inheritDoc}
 */
@Override
public String getSQL() {
    StatementBuilder buff = new StatementBuilder();
    if (schema != null)
        buff.append(Parser.quoteIdentifier(schema)).append('.');
    // We don't need to quote identifier as long as H2 never does so with function names when generating plan SQL.
    // On the other hand, quoting identifiers that also serve as keywords (like CURRENT_DATE() and CURRENT_DATE)
    // turns CURRENT_DATE() into "CURRENT_DATE"(), which is not good.
    buff.append(name);
    if (type == CASE) {
        buff.append(' ').append(child().getSQL());
        for (int i = 1, len = size() - 1; i < len; i += 2) {
            buff.append(" WHEN ").append(child(i).getSQL());
            buff.append(" THEN ").append(child(i + 1).getSQL());
        }
        if ((size() & 1) == 0)
            buff.append(" ELSE ").append(child(size() - 1).getSQL());
        return buff.append(" END").toString();
    }
    buff.append('(');
    switch(type) {
        case CAST:
        case CONVERT:
            assert size() == 1;
            String castType = resultType().sql();
            assert !F.isEmpty(castType) : castType;
            buff.append(child().getSQL());
            buff.append(type == CAST ? " AS " : ",");
            buff.append(castType);
            break;
        case EXTRACT:
            ValueString v = (ValueString) ((GridSqlConst) child(0)).value();
            buff.append(v.getString()).append(" FROM ").append(child(1).getSQL());
            break;
        case TABLE:
            for (int i = 0; i < size(); i++) {
                buff.appendExceptFirst(", ");
                GridSqlElement e = child(i);
                // id int = ?, name varchar = ('aaa', 'bbb')
                buff.append(Parser.quoteIdentifier(((GridSqlAlias) e).alias())).append(' ').append(e.resultType().sql()).append('=').append(e.child().getSQL());
            }
            break;
        default:
            for (int i = 0; i < size(); i++) {
                buff.appendExceptFirst(", ");
                buff.append(child(i).getSQL());
            }
    }
    return buff.append(')').toString();
}
Also used : StatementBuilder(org.h2.util.StatementBuilder) ValueString(org.h2.value.ValueString) ValueString(org.h2.value.ValueString)

Example 43 with StatementBuilder

use of org.h2.util.StatementBuilder in project ignite by apache.

the class GridSqlInsert method getSQL.

/**
 * {@inheritDoc}
 */
@Override
public String getSQL() {
    char delim = delimeter();
    StatementBuilder buff = new StatementBuilder(explain() ? "EXPLAIN " : "");
    buff.append("INSERT").append(delim).append("INTO ").append(into.getSQL()).append('(');
    for (GridSqlColumn col : cols) {
        buff.appendExceptFirst(",");
        buff.append(delim).append(col.getSQL());
    }
    buff.append(delim).append(')').append(delim);
    if (direct)
        buff.append("DIRECT ");
    if (sorted)
        buff.append("SORTED ");
    if (!rows.isEmpty()) {
        buff.append("VALUES").append(delim);
        StatementBuilder valuesBuff = new StatementBuilder();
        for (GridSqlElement[] row : rows()) {
            valuesBuff.appendExceptFirst("," + delim);
            StatementBuilder rowBuff = new StatementBuilder("(");
            for (GridSqlElement e : row) {
                rowBuff.appendExceptFirst(", ");
                rowBuff.append(e != null ? e.getSQL() : "DEFAULT");
            }
            rowBuff.append(')');
            valuesBuff.append(rowBuff.toString());
        }
        buff.append(valuesBuff.toString());
    } else
        buff.append(delim).append(qry.getSQL());
    return buff.toString();
}
Also used : StatementBuilder(org.h2.util.StatementBuilder)

Example 44 with StatementBuilder

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

the class Insert method handleOnDuplicate.

/**
 * @param de duplicate key exception
 * @return {@code true} if row was updated, {@code false} if row was ignored
 */
private boolean handleOnDuplicate(DbException de) {
    if (de.getErrorCode() != ErrorCode.DUPLICATE_KEY_1) {
        throw de;
    }
    if (duplicateKeyAssignmentMap == null || duplicateKeyAssignmentMap.isEmpty()) {
        if (ignore) {
            return false;
        }
        throw de;
    }
    ArrayList<String> variableNames = new ArrayList<>(duplicateKeyAssignmentMap.size());
    Expression[] row = list.get(getCurrentRowNumber() - 1);
    for (int i = 0; i < columns.length; i++) {
        String key = table.getSchema().getName() + "." + table.getName() + "." + columns[i].getName();
        variableNames.add(key);
        session.setVariable(key, row[i].getValue(session));
    }
    StatementBuilder buff = new StatementBuilder("UPDATE ");
    buff.append(table.getSQL()).append(" SET ");
    for (Column column : duplicateKeyAssignmentMap.keySet()) {
        buff.appendExceptFirst(", ");
        Expression ex = duplicateKeyAssignmentMap.get(column);
        buff.append(column.getSQL()).append("=").append(ex.getSQL());
    }
    buff.append(" WHERE ");
    Index foundIndex = (Index) de.getSource();
    if (foundIndex == null) {
        throw DbException.getUnsupportedException("Unable to apply ON DUPLICATE KEY UPDATE, no index found!");
    }
    buff.append(prepareUpdateCondition(foundIndex).getSQL());
    String sql = buff.toString();
    Update command = (Update) session.prepare(sql);
    command.setUpdateToCurrentValuesReturnsZero(true);
    for (Parameter param : command.getParameters()) {
        Parameter insertParam = parameters.get(param.getIndex());
        param.setValue(insertParam.getValue(session));
    }
    boolean result = command.update() > 0;
    for (String variableName : variableNames) {
        session.setVariable(variableName, ValueNull.INSTANCE);
    }
    return result;
}
Also used : Expression(org.h2.expression.Expression) Column(org.h2.table.Column) ExpressionColumn(org.h2.expression.ExpressionColumn) StatementBuilder(org.h2.util.StatementBuilder) ArrayList(java.util.ArrayList) Parameter(org.h2.expression.Parameter) MVPrimaryIndex(org.h2.mvstore.db.MVPrimaryIndex) Index(org.h2.index.Index)

Example 45 with StatementBuilder

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

the class Insert method getPlanSQL.

@Override
public String getPlanSQL() {
    StatementBuilder buff = new StatementBuilder("INSERT INTO ");
    buff.append(table.getSQL()).append('(');
    for (Column c : columns) {
        buff.appendExceptFirst(", ");
        buff.append(c.getSQL());
    }
    buff.append(")\n");
    if (insertFromSelect) {
        buff.append("DIRECT ");
    }
    if (sortedInsertMode) {
        buff.append("SORTED ");
    }
    if (!list.isEmpty()) {
        buff.append("VALUES ");
        int row = 0;
        if (list.size() > 1) {
            buff.append('\n');
        }
        for (Expression[] expr : list) {
            if (row++ > 0) {
                buff.append(",\n");
            }
            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) ExpressionColumn(org.h2.expression.ExpressionColumn) 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