File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\expression\ExpressionList.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.expression;
8
9 import org.h2.engine.Session;
10 import org.h2.table.Column;
11 import org.h2.table.ColumnResolver;
12 import org.h2.table.TableFilter;
13 import org.h2.util.StatementBuilder;
14 import org.h2.value.Value;
15 import org.h2.value.ValueArray;
16
17 /**
18 * A list of expressions, as in (ID, NAME).
19 * The result of this expression is an array.
20 */
21 public class ExpressionList extends Expression {
22
23 private final Expression[] list;
24
25 public ExpressionList(Expression[] list) {
26 this.list = list;
27 }
28
29 public Value getValue(Session session) {
30 Value[] v = new Value[list.length];
31 for (int i = 0; i < list.length; i++) {
32 v[i] = list[i].getValue(session);
33 }
34 return ValueArray.get(v);
35 }
36
37 public int getType() {
38 return Value.ARRAY;
39 }
40
41 public void mapColumns(ColumnResolver resolver, int level) {
42 for (Expression e : list) {
43 e.mapColumns(resolver, level);
44 }
45 }
46
47 public Expression optimize(Session session) {
48 boolean allConst = true;
49 for (int i = 0; i < list.length; i++) {
50 Expression e = list[i].optimize(session);
51 if (!e.isConstant()) {
52 allConst = false;
53 }
54 list[i] = e;
55 }
56 if (allConst) {
57 return ValueExpression.get(getValue(session));
58 }
59 return this;
60 }
61
62 public void setEvaluatable(TableFilter tableFilter, boolean b) {
63 for (Expression e : list) {
64 e.setEvaluatable(tableFilter, b);
65 }
66 }
67
68 public int getScale() {
69 return 0;
70 }
71
72 public long getPrecision() {
73 return Integer.MAX_VALUE;
74 }
75
76 public int getDisplaySize() {
77 return Integer.MAX_VALUE;
78 }
79
80 public String getSQL() {
81 StatementBuilder buff = new StatementBuilder("(");
82 for (Expression e: list) {
83 buff.appendExceptFirst(", ");
84 buff.append(e.getSQL());
85 }
86 if (list.length == 1) {
87 buff.append(',');
88 }
89 return buff.append(')').toString();
90 }
91
92 public void updateAggregate(Session session) {
93 for (Expression e : list) {
94 e.updateAggregate(session);
95 }
96 }
97
98 public boolean isEverything(ExpressionVisitor visitor) {
99 for (Expression e : list) {
100 if (!e.isEverything(visitor)) {
101 return false;
102 }
103 }
104 return true;
105 }
106
107 public int getCost() {
108 int cost = 1;
109 for (Expression e : list) {
110 cost += e.getCost();
111 }
112 return cost;
113 }
114
115 public Expression[] getExpressionColumns(Session session) {
116 ExpressionColumn[] expr = new ExpressionColumn[list.length];
117 for (int i = 0; i < list.length; i++) {
118 Expression e = list[i];
119 Column col = new Column("C" + (i + 1), e.getType(), e.getPrecision(), e.getScale(),
120 e.getDisplaySize());
121 expr[i] = new ExpressionColumn(session.getDatabase(), col);
122 }
123 return expr;
124 }
125
126 }
127