001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (http://h2database.com/html/license.html).
004: * Initial Developer: H2 Group
005: */
006: package org.h2.expression;
007:
008: import java.sql.SQLException;
009:
010: import org.h2.engine.Session;
011: import org.h2.table.ColumnResolver;
012: import org.h2.table.TableFilter;
013: import org.h2.value.Value;
014: import org.h2.value.ValueArray;
015:
016: /**
017: * A list of expressions, as in (ID, NAME).
018: * The result of this expression is an array.
019: */
020: public class ExpressionList extends Expression {
021:
022: private Expression[] list;
023:
024: public ExpressionList(Expression[] list) {
025: this .list = list;
026: }
027:
028: public Value getValue(Session session) throws SQLException {
029: Value[] v = new Value[list.length];
030: for (int i = 0; i < list.length; i++) {
031: v[i] = list[i].getValue(session);
032: }
033: return ValueArray.get(v);
034: }
035:
036: public int getType() {
037: return Value.ARRAY;
038: }
039:
040: public void mapColumns(ColumnResolver resolver, int level)
041: throws SQLException {
042: for (int i = 0; i < list.length; i++) {
043: list[i].mapColumns(resolver, level);
044: }
045: }
046:
047: public Expression optimize(Session session) throws SQLException {
048: boolean allConst = true;
049: for (int i = 0; i < list.length; i++) {
050: Expression e = list[i].optimize(session);
051: if (!e.isConstant()) {
052: allConst = false;
053: }
054: list[i] = e;
055: }
056: if (allConst) {
057: return ValueExpression.get(getValue(session));
058: }
059: return this ;
060: }
061:
062: public void setEvaluatable(TableFilter tableFilter, boolean b) {
063: for (int i = 0; i < list.length; i++) {
064: list[i].setEvaluatable(tableFilter, b);
065: }
066: }
067:
068: public int getScale() {
069: return 0;
070: }
071:
072: public long getPrecision() {
073: return Integer.MAX_VALUE;
074: }
075:
076: public int getDisplaySize() {
077: return Integer.MAX_VALUE;
078: }
079:
080: public String getSQL() {
081: StringBuffer buff = new StringBuffer();
082: buff.append('(');
083: for (int i = 0; i < list.length; i++) {
084: if (i > 0) {
085: buff.append(", ");
086: }
087: buff.append(list[i].getSQL());
088: }
089: buff.append(')');
090: return buff.toString();
091: }
092:
093: public void updateAggregate(Session session) throws SQLException {
094: for (int i = 0; i < list.length; i++) {
095: list[i].updateAggregate(session);
096: }
097: }
098:
099: public boolean isEverything(ExpressionVisitor visitor) {
100: for (int i = 0; i < list.length; i++) {
101: if (!list[i].isEverything(visitor)) {
102: return false;
103: }
104: }
105: return true;
106: }
107:
108: public int getCost() {
109: int cost = 1;
110: for (int i = 0; i < list.length; i++) {
111: cost += list[i].getCost();
112: }
113: return cost;
114: }
115:
116: }
|