File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\expression\Parameter.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.constant.ErrorCode;
10 import org.h2.engine.Session;
11 import org.h2.message.DbException;
12 import org.h2.table.Column;
13 import org.h2.table.ColumnResolver;
14 import org.h2.table.TableFilter;
15 import org.h2.value.Value;
16 import org.h2.value.ValueBoolean;
17 import org.h2.value.ValueNull;
18
19 /**
20 * A parameter of a prepared statement.
21 */
22 public class Parameter extends Expression implements ParameterInterface {
23
24 private Value value;
25 private Column column;
26 private final int index;
27
28 public Parameter(int index) {
29 this.index = index;
30 }
31
32 public String getSQL() {
33 return "?" + (index + 1);
34 }
35
36 public void setValue(Value v, boolean closeOld) {
37 // don't need to close the old value as temporary files are anyway removed
38 this.value = v;
39 }
40
41 public void setValue(Value v) {
42 this.value = v;
43 }
44
45 public Value getParamValue() {
46 if (value == null) {
47 // to allow parameters in function tables
48 return ValueNull.INSTANCE;
49 }
50 return value;
51 }
52
53 public Value getValue(Session session) {
54 return getParamValue();
55 }
56
57 public int getType() {
58 if (value != null) {
59 return value.getType();
60 }
61 if (column != null) {
62 return column.getType();
63 }
64 return Value.UNKNOWN;
65 }
66
67 public void mapColumns(ColumnResolver resolver, int level) {
68 // can't map
69 }
70
71 public void checkSet() {
72 if (value == null) {
73 throw DbException.get(ErrorCode.PARAMETER_NOT_SET_1, "#" + (index + 1));
74 }
75 }
76
77 public Expression optimize(Session session) {
78 return this;
79 }
80
81 public boolean isConstant() {
82 return false;
83 }
84
85 public boolean isValueSet() {
86 return value != null;
87 }
88
89 public void setEvaluatable(TableFilter tableFilter, boolean b) {
90 // not bound
91 }
92
93 public int getScale() {
94 if (value != null) {
95 return value.getScale();
96 }
97 if (column != null) {
98 return column.getScale();
99 }
100 return 0;
101 }
102
103 public long getPrecision() {
104 if (value != null) {
105 return value.getPrecision();
106 }
107 if (column != null) {
108 return column.getPrecision();
109 }
110 return 0;
111 }
112
113 public int getDisplaySize() {
114 if (value != null) {
115 return value.getDisplaySize();
116 }
117 if (column != null) {
118 return column.getDisplaySize();
119 }
120 return 0;
121 }
122
123 public void updateAggregate(Session session) {
124 // nothing to do
125 }
126
127 public boolean isEverything(ExpressionVisitor visitor) {
128 switch(visitor.getType()) {
129 case ExpressionVisitor.EVALUATABLE:
130 // the parameter _will_be_ evaluatable at execute time
131 case ExpressionVisitor.SET_MAX_DATA_MODIFICATION_ID:
132 // it is checked independently if the value is the same as the last time
133 case ExpressionVisitor.NOT_FROM_RESOLVER:
134 case ExpressionVisitor.QUERY_COMPARABLE:
135 case ExpressionVisitor.GET_DEPENDENCIES:
136 case ExpressionVisitor.OPTIMIZABLE_MIN_MAX_COUNT_ALL:
137 case ExpressionVisitor.DETERMINISTIC:
138 case ExpressionVisitor.READONLY:
139 case ExpressionVisitor.GET_COLUMNS:
140 return true;
141 case ExpressionVisitor.INDEPENDENT:
142 return value != null;
143 default:
144 throw DbException.throwInternalError("type="+visitor.getType());
145 }
146 }
147
148 public int getCost() {
149 return 0;
150 }
151
152 public Expression getNotIfPossible(Session session) {
153 return new Comparison(session, Comparison.EQUAL, this, ValueExpression.get(ValueBoolean.get(false)));
154 }
155
156 public void setColumn(Column column) {
157 this.column = column;
158 }
159
160 public int getIndex() {
161 return index;
162 }
163
164 }
165