1 | /* |
2 | * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0, |
3 | * and the EPL 1.0 (http://h2database.com/html/license.html). |
4 | * Initial Developer: H2 Group |
5 | */ |
6 | package org.h2.expression; |
7 | |
8 | import org.h2.command.Parser; |
9 | import org.h2.engine.Session; |
10 | import org.h2.message.DbException; |
11 | import org.h2.table.ColumnResolver; |
12 | import org.h2.table.TableFilter; |
13 | import org.h2.value.Value; |
14 | |
15 | /** |
16 | * A user-defined variable, for example: @ID. |
17 | */ |
18 | public class Variable extends Expression { |
19 | |
20 | private final String name; |
21 | private Value lastValue; |
22 | |
23 | public Variable(Session session, String name) { |
24 | this.name = name; |
25 | lastValue = session.getVariable(name); |
26 | } |
27 | |
28 | @Override |
29 | public int getCost() { |
30 | return 0; |
31 | } |
32 | |
33 | @Override |
34 | public int getDisplaySize() { |
35 | return lastValue.getDisplaySize(); |
36 | } |
37 | |
38 | @Override |
39 | public long getPrecision() { |
40 | return lastValue.getPrecision(); |
41 | } |
42 | |
43 | @Override |
44 | public String getSQL() { |
45 | return "@" + Parser.quoteIdentifier(name); |
46 | } |
47 | |
48 | @Override |
49 | public int getScale() { |
50 | return lastValue.getScale(); |
51 | } |
52 | |
53 | @Override |
54 | public int getType() { |
55 | return lastValue.getType(); |
56 | } |
57 | |
58 | @Override |
59 | public Value getValue(Session session) { |
60 | lastValue = session.getVariable(name); |
61 | return lastValue; |
62 | } |
63 | |
64 | @Override |
65 | public boolean isEverything(ExpressionVisitor visitor) { |
66 | switch(visitor.getType()) { |
67 | case ExpressionVisitor.EVALUATABLE: |
68 | // the value will be evaluated at execute time |
69 | case ExpressionVisitor.SET_MAX_DATA_MODIFICATION_ID: |
70 | // it is checked independently if the value is the same as the last |
71 | // time |
72 | case ExpressionVisitor.OPTIMIZABLE_MIN_MAX_COUNT_ALL: |
73 | case ExpressionVisitor.READONLY: |
74 | case ExpressionVisitor.INDEPENDENT: |
75 | case ExpressionVisitor.NOT_FROM_RESOLVER: |
76 | case ExpressionVisitor.QUERY_COMPARABLE: |
77 | case ExpressionVisitor.GET_DEPENDENCIES: |
78 | case ExpressionVisitor.GET_COLUMNS: |
79 | return true; |
80 | case ExpressionVisitor.DETERMINISTIC: |
81 | return false; |
82 | default: |
83 | throw DbException.throwInternalError("type="+visitor.getType()); |
84 | } |
85 | } |
86 | |
87 | @Override |
88 | public void mapColumns(ColumnResolver resolver, int level) { |
89 | // nothing to do |
90 | } |
91 | |
92 | @Override |
93 | public Expression optimize(Session session) { |
94 | return this; |
95 | } |
96 | |
97 | @Override |
98 | public void setEvaluatable(TableFilter tableFilter, boolean value) { |
99 | // nothing to do |
100 | } |
101 | |
102 | @Override |
103 | public void updateAggregate(Session session) { |
104 | // nothing to do |
105 | } |
106 | |
107 | public String getName() { |
108 | return name; |
109 | } |
110 | |
111 | } |