Search in sources :

Example 61 with Expression

use of de.neemann.digital.hdl.model2.expression.Expression 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)

Example 62 with Expression

use of de.neemann.digital.hdl.model2.expression.Expression 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 63 with Expression

use of de.neemann.digital.hdl.model2.expression.Expression 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 64 with Expression

use of de.neemann.digital.hdl.model2.expression.Expression 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 65 with Expression

use of de.neemann.digital.hdl.model2.expression.Expression in project h2database by h2database.

the class Query method prepareOrder.

/**
 * Create a {@link SortOrder} object given the list of {@link SelectOrderBy}
 * objects. The expression list is extended if necessary.
 *
 * @param orderList a list of {@link SelectOrderBy} elements
 * @param expressionCount the number of columns in the query
 * @return the {@link SortOrder} object
 */
public SortOrder prepareOrder(ArrayList<SelectOrderBy> orderList, int expressionCount) {
    int size = orderList.size();
    int[] index = new int[size];
    int[] sortType = new int[size];
    for (int i = 0; i < size; i++) {
        SelectOrderBy o = orderList.get(i);
        int idx;
        boolean reverse = false;
        Expression expr = o.columnIndexExpr;
        Value v = expr.getValue(null);
        if (v == ValueNull.INSTANCE) {
            // parameter not yet set - order by first column
            idx = 0;
        } else {
            idx = v.getInt();
            if (idx < 0) {
                reverse = true;
                idx = -idx;
            }
            idx -= 1;
            if (idx < 0 || idx >= expressionCount) {
                throw DbException.get(ErrorCode.ORDER_BY_NOT_IN_RESULT, "" + (idx + 1));
            }
        }
        index[i] = idx;
        boolean desc = o.descending;
        if (reverse) {
            desc = !desc;
        }
        int type = desc ? SortOrder.DESCENDING : SortOrder.ASCENDING;
        if (o.nullsFirst) {
            type += SortOrder.NULLS_FIRST;
        } else if (o.nullsLast) {
            type += SortOrder.NULLS_LAST;
        }
        sortType[i] = type;
    }
    return new SortOrder(session.getDatabase(), index, sortType, orderList);
}
Also used : ValueExpression(org.h2.expression.ValueExpression) Expression(org.h2.expression.Expression) Value(org.h2.value.Value) SortOrder(org.h2.result.SortOrder)

Aggregations

Expression (org.h2.expression.Expression)84 ValueExpression (org.h2.expression.ValueExpression)53 Column (org.h2.table.Column)45 ExpressionColumn (org.h2.expression.ExpressionColumn)38 IndexColumn (org.h2.table.IndexColumn)24 AlterTableAlterColumn (org.h2.command.ddl.AlterTableAlterColumn)21 AlterTableAddConstraint (org.h2.command.ddl.AlterTableAddConstraint)18 ValueString (org.h2.value.ValueString)18 AlterTableRenameColumn (org.h2.command.ddl.AlterTableRenameColumn)17 TableFilter (org.h2.table.TableFilter)15 AlterTableDropConstraint (org.h2.command.ddl.AlterTableDropConstraint)14 AlterTableRenameConstraint (org.h2.command.ddl.AlterTableRenameConstraint)14 Expression (expression.Expression)13 Table (org.h2.table.Table)13 Value (org.h2.value.Value)12 ArrayList (java.util.ArrayList)11 Node (expression.Node)10 RangeTable (org.h2.table.RangeTable)10 Number (expression.Number)9 CreateTable (org.h2.command.ddl.CreateTable)9