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.engine.Session; |
9 | import org.h2.message.DbException; |
10 | import org.h2.schema.Sequence; |
11 | import org.h2.table.ColumnResolver; |
12 | import org.h2.table.TableFilter; |
13 | import org.h2.value.Value; |
14 | import org.h2.value.ValueInt; |
15 | import org.h2.value.ValueLong; |
16 | |
17 | /** |
18 | * Wraps a sequence when used in a statement. |
19 | */ |
20 | public class SequenceValue extends Expression { |
21 | |
22 | private final Sequence sequence; |
23 | |
24 | public SequenceValue(Sequence sequence) { |
25 | this.sequence = sequence; |
26 | } |
27 | |
28 | @Override |
29 | public Value getValue(Session session) { |
30 | long value = sequence.getNext(session); |
31 | session.setLastIdentity(ValueLong.get(value)); |
32 | return ValueLong.get(value); |
33 | } |
34 | |
35 | @Override |
36 | public int getType() { |
37 | return Value.LONG; |
38 | } |
39 | |
40 | @Override |
41 | public void mapColumns(ColumnResolver resolver, int level) { |
42 | // nothing to do |
43 | } |
44 | |
45 | @Override |
46 | public Expression optimize(Session session) { |
47 | return this; |
48 | } |
49 | |
50 | @Override |
51 | public void setEvaluatable(TableFilter tableFilter, boolean b) { |
52 | // nothing to do |
53 | } |
54 | |
55 | @Override |
56 | public int getScale() { |
57 | return 0; |
58 | } |
59 | |
60 | @Override |
61 | public long getPrecision() { |
62 | return ValueInt.PRECISION; |
63 | } |
64 | |
65 | @Override |
66 | public int getDisplaySize() { |
67 | return ValueInt.DISPLAY_SIZE; |
68 | } |
69 | |
70 | @Override |
71 | public String getSQL() { |
72 | return "(NEXT VALUE FOR " + sequence.getSQL() +")"; |
73 | } |
74 | |
75 | @Override |
76 | public void updateAggregate(Session session) { |
77 | // nothing to do |
78 | } |
79 | |
80 | @Override |
81 | public boolean isEverything(ExpressionVisitor visitor) { |
82 | switch(visitor.getType()) { |
83 | case ExpressionVisitor.EVALUATABLE: |
84 | case ExpressionVisitor.OPTIMIZABLE_MIN_MAX_COUNT_ALL: |
85 | case ExpressionVisitor.NOT_FROM_RESOLVER: |
86 | case ExpressionVisitor.GET_COLUMNS: |
87 | return true; |
88 | case ExpressionVisitor.DETERMINISTIC: |
89 | case ExpressionVisitor.READONLY: |
90 | case ExpressionVisitor.INDEPENDENT: |
91 | case ExpressionVisitor.QUERY_COMPARABLE: |
92 | return false; |
93 | case ExpressionVisitor.SET_MAX_DATA_MODIFICATION_ID: |
94 | visitor.addDataModificationId(sequence.getModificationId()); |
95 | return true; |
96 | case ExpressionVisitor.GET_DEPENDENCIES: |
97 | visitor.addDependency(sequence); |
98 | return true; |
99 | default: |
100 | throw DbException.throwInternalError("type="+visitor.getType()); |
101 | } |
102 | } |
103 | |
104 | @Override |
105 | public int getCost() { |
106 | return 1; |
107 | } |
108 | |
109 | } |