File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\expression\Variable.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.command.Parser;
10 import org.h2.engine.Session;
11 import org.h2.message.DbException;
12 import org.h2.table.ColumnResolver;
13 import org.h2.table.TableFilter;
14 import org.h2.value.Value;
15
16 /**
17 * A user-defined variable, for example: @ID.
18 */
19 public class Variable extends Expression {
20
21 private final String name;
22 private Value lastValue;
23
24 public Variable(Session session, String name) {
25 this.name = name;
26 lastValue = session.getVariable(name);
27 }
28
29 public int getCost() {
30 return 0;
31 }
32
33 public int getDisplaySize() {
34 return lastValue.getDisplaySize();
35 }
36
37 public long getPrecision() {
38 return lastValue.getPrecision();
39 }
40
41 public String getSQL() {
42 return "@" + Parser.quoteIdentifier(name);
43 }
44
45 public int getScale() {
46 return lastValue.getScale();
47 }
48
49 public int getType() {
50 return lastValue.getType();
51 }
52
53 public Value getValue(Session session) {
54 lastValue = session.getVariable(name);
55 return lastValue;
56 }
57
58 public boolean isEverything(ExpressionVisitor visitor) {
59 switch(visitor.getType()) {
60 case ExpressionVisitor.EVALUATABLE:
61 // the value will be evaluated at execute time
62 case ExpressionVisitor.SET_MAX_DATA_MODIFICATION_ID:
63 // it is checked independently if the value is the same as the last time
64 case ExpressionVisitor.OPTIMIZABLE_MIN_MAX_COUNT_ALL:
65 case ExpressionVisitor.READONLY:
66 case ExpressionVisitor.INDEPENDENT:
67 case ExpressionVisitor.NOT_FROM_RESOLVER:
68 case ExpressionVisitor.QUERY_COMPARABLE:
69 case ExpressionVisitor.GET_DEPENDENCIES:
70 case ExpressionVisitor.GET_COLUMNS:
71 return true;
72 case ExpressionVisitor.DETERMINISTIC:
73 return false;
74 default:
75 throw DbException.throwInternalError("type="+visitor.getType());
76 }
77 }
78
79 public void mapColumns(ColumnResolver resolver, int level) {
80 // nothing to do
81 }
82
83 public Expression optimize(Session session) {
84 return this;
85 }
86
87 public void setEvaluatable(TableFilter tableFilter, boolean value) {
88 // nothing to do
89 }
90
91 public void updateAggregate(Session session) {
92 // nothing to do
93 }
94
95 public String getName() {
96 return name;
97 }
98
99 }
100