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