Search in sources :

Example 51 with Column

use of org.h2.table.Column in project h2database by h2database.

the class Expression method getExpressionColumns.

/**
 * Extracts expression columns from ValueArray
 *
 * @param session the current session
 * @param value the value to extract columns from
 * @return array of expression columns
 */
static Expression[] getExpressionColumns(Session session, ValueArray value) {
    Value[] list = value.getList();
    ExpressionColumn[] expr = new ExpressionColumn[list.length];
    for (int i = 0, len = list.length; i < len; i++) {
        Value v = list[i];
        Column col = new Column("C" + (i + 1), v.getType(), v.getPrecision(), v.getScale(), v.getDisplaySize());
        expr[i] = new ExpressionColumn(session.getDatabase(), col);
    }
    return expr;
}
Also used : Column(org.h2.table.Column) Value(org.h2.value.Value)

Example 52 with Column

use of org.h2.table.Column in project h2database by h2database.

the class ExpressionColumn method getValue.

@Override
public Value getValue(Session session) {
    Select select = columnResolver.getSelect();
    if (select != null) {
        HashMap<Expression, Object> values = select.getCurrentGroup();
        if (values != null) {
            Value v = (Value) values.get(this);
            if (v != null) {
                return v;
            }
        }
    }
    Value value = columnResolver.getValue(column);
    if (value == null) {
        if (select == null) {
            throw DbException.get(ErrorCode.NULL_NOT_ALLOWED, getSQL());
        } else {
            throw DbException.get(ErrorCode.MUST_GROUP_BY_COLUMN_1, getSQL());
        }
    }
    if (column.getEnumerators() != null && value != ValueNull.INSTANCE) {
        return ValueEnum.get(column.getEnumerators(), value.getInt());
    }
    return value;
}
Also used : Select(org.h2.command.dml.Select) Value(org.h2.value.Value)

Example 53 with Column

use of org.h2.table.Column in project h2database by h2database.

the class ExpressionColumn method updateAggregate.

@Override
public void updateAggregate(Session session) {
    Value now = columnResolver.getValue(column);
    Select select = columnResolver.getSelect();
    if (select == null) {
        throw DbException.get(ErrorCode.MUST_GROUP_BY_COLUMN_1, getSQL());
    }
    HashMap<Expression, Object> values = select.getCurrentGroup();
    if (values == null) {
        // this is a different level (the enclosing query)
        return;
    }
    Value v = (Value) values.get(this);
    if (v == null) {
        values.put(this, now);
    } else {
        if (!database.areEqual(now, v)) {
            throw DbException.get(ErrorCode.MUST_GROUP_BY_COLUMN_1, getSQL());
        }
    }
}
Also used : Value(org.h2.value.Value) Select(org.h2.command.dml.Select)

Example 54 with Column

use of org.h2.table.Column in project h2database by h2database.

the class ExpressionList method getExpressionColumns.

@Override
public Expression[] getExpressionColumns(Session session) {
    ExpressionColumn[] expr = new ExpressionColumn[list.length];
    for (int i = 0; i < list.length; i++) {
        Expression e = list[i];
        Column col = new Column("C" + (i + 1), e.getType(), e.getPrecision(), e.getScale(), e.getDisplaySize());
        expr[i] = new ExpressionColumn(session.getDatabase(), col);
    }
    return expr;
}
Also used : Column(org.h2.table.Column)

Example 55 with Column

use of org.h2.table.Column in project h2database by h2database.

the class Function method getSQL.

@Override
public String getSQL() {
    StatementBuilder buff = new StatementBuilder(info.name);
    if (info.type == CASE) {
        if (args[0] != null) {
            buff.append(" ").append(args[0].getSQL());
        }
        for (int i = 1, len = args.length - 1; i < len; i += 2) {
            buff.append(" WHEN ").append(args[i].getSQL());
            buff.append(" THEN ").append(args[i + 1].getSQL());
        }
        if (args.length % 2 == 0) {
            buff.append(" ELSE ").append(args[args.length - 1].getSQL());
        }
        return buff.append(" END").toString();
    }
    buff.append('(');
    switch(info.type) {
        case CAST:
            {
                buff.append(args[0].getSQL()).append(" AS ").append(new Column(null, dataType, precision, scale, displaySize).getCreateSQL());
                break;
            }
        case CONVERT:
            {
                if (database.getMode().swapConvertFunctionParameters) {
                    buff.append(new Column(null, dataType, precision, scale, displaySize).getCreateSQL()).append(',').append(args[0].getSQL());
                } else {
                    buff.append(args[0].getSQL()).append(',').append(new Column(null, dataType, precision, scale, displaySize).getCreateSQL());
                }
                break;
            }
        case EXTRACT:
            {
                ValueString v = (ValueString) ((ValueExpression) args[0]).getValue(null);
                buff.append(v.getString()).append(" FROM ").append(args[1].getSQL());
                break;
            }
        default:
            {
                for (Expression e : args) {
                    buff.appendExceptFirst(", ");
                    buff.append(e.getSQL());
                }
            }
    }
    return buff.append(')').toString();
}
Also used : Column(org.h2.table.Column) StatementBuilder(org.h2.util.StatementBuilder) ValueString(org.h2.value.ValueString)

Aggregations

Column (org.h2.table.Column)146 Value (org.h2.value.Value)83 IndexColumn (org.h2.table.IndexColumn)79 DbException (org.h2.message.DbException)64 SQLException (java.sql.SQLException)60 Expression (org.h2.expression.Expression)55 ExpressionColumn (org.h2.expression.ExpressionColumn)51 ValueString (org.h2.value.ValueString)34 AlterTableAlterColumn (org.h2.command.ddl.AlterTableAlterColumn)32 ValueExpression (org.h2.expression.ValueExpression)30 ArrayList (java.util.ArrayList)27 PreparedStatement (java.sql.PreparedStatement)26 Index (org.h2.index.Index)26 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)25 Table (org.h2.table.Table)25 AlterTableRenameColumn (org.h2.command.ddl.AlterTableRenameColumn)22 GridH2Table (org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)21 AlterTableAddConstraint (org.h2.command.ddl.AlterTableAddConstraint)21 StatementBuilder (org.h2.util.StatementBuilder)19 Constraint (org.h2.constraint.Constraint)18