File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\expression\Expression.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 java.sql.ResultSet;
10 import java.sql.ResultSetMetaData;
11 import java.sql.SQLException;
12 import org.h2.engine.Database;
13 import org.h2.engine.Session;
14 import org.h2.message.DbException;
15 import org.h2.table.Column;
16 import org.h2.table.ColumnResolver;
17 import org.h2.table.TableFilter;
18 import org.h2.util.StringUtils;
19 import org.h2.value.DataType;
20 import org.h2.value.Value;
21 import org.h2.value.ValueArray;
22
23 /**
24 * An expression is a operation, a value, or a function in a query.
25 */
26 public abstract class Expression {
27
28 private boolean addedToFilter;
29
30 /**
31 * Return the resulting value for the current row.
32 *
33 * @param session the session
34 * @return the result
35 */
36 public abstract Value getValue(Session session);
37
38 /**
39 * Return the data type. The data type may not be known before the
40 * optimization phase.
41 *
42 * @return the type
43 */
44 public abstract int getType();
45
46 /**
47 * Map the columns of the resolver to expression columns.
48 *
49 * @param resolver the column resolver
50 * @param level the subquery nesting level
51 */
52 public abstract void mapColumns(ColumnResolver resolver, int level);
53
54 /**
55 * Try to optimize the expression.
56 *
57 * @param session the session
58 * @return the optimized expression
59 */
60 public abstract Expression optimize(Session session);
61
62 /**
63 * Tell the expression columns whether the table filter can return values now.
64 * This is used when optimizing the query.
65 *
66 * @param tableFilter the table filter
67 * @param value true if the table filter can return value
68 */
69 public abstract void setEvaluatable(TableFilter tableFilter, boolean value);
70
71 /**
72 * Get the scale of this expression.
73 *
74 * @return the scale
75 */
76 public abstract int getScale();
77
78 /**
79 * Get the precision of this expression.
80 *
81 * @return the precision
82 */
83 public abstract long getPrecision();
84
85 /**
86 * Get the display size of this expression.
87 *
88 * @return the display size
89 */
90 public abstract int getDisplaySize();
91
92 /**
93 * Get the SQL statement of this expression.
94 * This may not always be the original SQL statement,
95 * specially after optimization.
96 *
97 * @return the SQL statement
98 */
99 public abstract String getSQL();
100
101 /**
102 * Update an aggregate value.
103 * This method is called at statement execution time.
104 * It is usually called once for each row, but if the expression is used multiple
105 * times (for example in the column list, and as part of the HAVING expression)
106 * it is called multiple times - the row counter needs to be used to make sure
107 * the internal state is only updated once.
108 *
109 * @param session the session
110 */
111 public abstract void updateAggregate(Session session);
112
113 /**
114 * Check if this expression and all sub-expressions can fulfill a criteria.
115 * If any part returns false, the result is false.
116 *
117 * @param visitor the visitor
118 * @return if the criteria can be fulfilled
119 */
120 public abstract boolean isEverything(ExpressionVisitor visitor);
121
122 /**
123 * Estimate the cost to process the expression.
124 * Used when optimizing the query, to calculate the query plan
125 * with the lowest estimated cost.
126 *
127 * @return the estimated cost
128 */
129 public abstract int getCost();
130
131 /**
132 * If it is possible, return the negated expression. This is used
133 * to optimize NOT expressions: NOT ID>10 can be converted to
134 * ID&lt;=10. Returns null if negating is not possible.
135 *
136 * @param session the session
137 * @return the negated expression, or null
138 */
139 public Expression getNotIfPossible(Session session) {
140 // by default it is not possible
141 return null;
142 }
143
144 /**
145 * Check if this expression will always return the same value.
146 *
147 * @return if the expression is constant
148 */
149 public boolean isConstant() {
150 return false;
151 }
152
153 /**
154 * Is the value of a parameter set.
155 *
156 * @return true if set
157 */
158 public boolean isValueSet() {
159 return false;
160 }
161
162 /**
163 * Check if this is an auto-increment column.
164 *
165 * @return true if it is an auto-increment column
166 */
167 public boolean isAutoIncrement() {
168 return false;
169 }
170
171 /**
172 * Get the value in form of a boolean expression.
173 * Returns true, false, or null.
174 * In this database, everything can be a condition.
175 *
176 * @param session the session
177 * @return the result
178 */
179 public Boolean getBooleanValue(Session session) {
180 return getValue(session).getBoolean();
181 }
182
183 /**
184 * Create index conditions if possible and attach them to the table filter.
185 *
186 * @param session the session
187 * @param filter the table filter
188 */
189 public void createIndexConditions(Session session, TableFilter filter) {
190 // default is do nothing
191 }
192
193 /**
194 * Get the column name or alias name of this expression.
195 *
196 * @return the column name
197 */
198 public String getColumnName() {
199 return getAlias();
200 }
201
202 /**
203 * Get the schema name, or null
204 *
205 * @return the schema name
206 */
207 public String getSchemaName() {
208 return null;
209 }
210
211 /**
212 * Get the table name, or null
213 *
214 * @return the table name
215 */
216 public String getTableName() {
217 return null;
218 }
219
220 /**
221 * Check whether this expression is a column and can store NULL.
222 *
223 * @return whether NULL is allowed
224 */
225 public int getNullable() {
226 return Column.NULLABLE_UNKNOWN;
227 }
228
229 /**
230 * Get the table alias name or null
231 * if this expression does not represent a column.
232 *
233 * @return the table alias name
234 */
235 public String getTableAlias() {
236 return null;
237 }
238
239 /**
240 * Get the alias name of a column or SQL expression
241 * if it is not an aliased expression.
242 *
243 * @return the alias name
244 */
245 public String getAlias() {
246 return StringUtils.unEnclose(getSQL());
247 }
248
249 /**
250 * Only returns true if the expression is a wildcard.
251 *
252 * @return if this expression is a wildcard
253 */
254 public boolean isWildcard() {
255 return false;
256 }
257
258 /**
259 * Returns the main expression, skipping aliases.
260 *
261 * @return the expression
262 */
263 public Expression getNonAliasExpression() {
264 return this;
265 }
266
267 /**
268 * Add conditions to a table filter if they can be evaluated.
269 *
270 * @param filter the table filter
271 * @param outerJoin if the expression is part of an outer join
272 */
273 public void addFilterConditions(TableFilter filter, boolean outerJoin) {
274 if (!addedToFilter && !outerJoin && isEverything(ExpressionVisitor.EVALUATABLE_VISITOR)) {
275 filter.addFilterCondition(this, false);
276 addedToFilter = true;
277 }
278 }
279
280 /**
281 * Convert this expression to a String.
282 *
283 * @return the string representation
284 */
285 public String toString() {
286 return getSQL();
287 }
288
289 /**
290 * If this expression consists of column expressions it should return them.
291 *
292 * @param session the session
293 * @return array of expression columns if applicable, null otherwise
294 */
295 public Expression[] getExpressionColumns(Session session) {
296 return null;
297 }
298
299 /**
300 * Extracts expression columns from ValueArray
301 *
302 * @param session the current session
303 * @param value the value to extract columns from
304 * @return array of expression columns
305 */
306 static Expression[] getExpressionColumns(Session session, ValueArray value) {
307 Value[] list = value.getList();
308 ExpressionColumn[] expr = new ExpressionColumn[list.length];
309 for (int i = 0, len = list.length; i < len; i++) {
310 Value v = list[i];
311 Column col = new Column("C" + (i + 1), v.getType(), v.getPrecision(), v.getScale(),
312 v.getDisplaySize());
313 expr[i] = new ExpressionColumn(session.getDatabase(), col);
314 }
315 return expr;
316 }
317
318 /**
319 * Extracts expression columns from the given result set.
320 *
321 * @param session the session
322 * @param rs the result set
323 * @return an array of expression columns
324 */
325 public static Expression[] getExpressionColumns(Session session, ResultSet rs) {
326 try {
327 ResultSetMetaData meta = rs.getMetaData();
328 int columnCount = meta.getColumnCount();
329 Expression[] expressions = new Expression[columnCount];
330 Database db = session == null ? null : session.getDatabase();
331 for (int i = 0; i < columnCount; i++) {
332 String name = meta.getColumnLabel(i + 1);
333 int type = DataType.convertSQLTypeToValueType(meta.getColumnType(i + 1));
334 int precision = meta.getPrecision(i + 1);
335 int scale = meta.getScale(i + 1);
336 int displaySize = meta.getColumnDisplaySize(i + 1);
337 Column col = new Column(name, type, precision, scale, displaySize);
338 Expression expr = new ExpressionColumn(db, col);
339 expressions[i] = expr;
340 }
341 return expressions;
342 } catch (SQLException e) {
343 throw DbException.convert(e);
344 }
345 }
346
347 }
348