001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (http://h2database.com/html/license.html).
004: * Initial Developer: H2 Group
005: */
006: package org.h2.expression;
007:
008: import java.sql.SQLException;
009:
010: import org.h2.engine.Session;
011: import org.h2.message.Message;
012: import org.h2.schema.Sequence;
013: import org.h2.table.ColumnResolver;
014: import org.h2.table.TableFilter;
015: import org.h2.value.Value;
016: import org.h2.value.ValueInt;
017: import org.h2.value.ValueLong;
018:
019: /**
020: * Wraps a sequence when used in a statement.
021: */
022: public class SequenceValue extends Expression {
023:
024: private Sequence sequence;
025:
026: public SequenceValue(Sequence sequence) {
027: this .sequence = sequence;
028: }
029:
030: public Value getValue(Session session) throws SQLException {
031: long value = sequence.getNext();
032: session.setLastIdentity(ValueLong.get(value));
033: return ValueLong.get(value);
034: }
035:
036: public int getType() {
037: return Value.LONG;
038: }
039:
040: public void mapColumns(ColumnResolver resolver, int level) {
041: // nothing to do
042: }
043:
044: public void checkMapped() {
045: // nothing to do
046: }
047:
048: public Expression optimize(Session session) {
049: return this ;
050: }
051:
052: public void setEvaluatable(TableFilter tableFilter, boolean b) {
053: // nothing to do
054: }
055:
056: public int getScale() {
057: return 0;
058: }
059:
060: public long getPrecision() {
061: return ValueInt.PRECISION;
062: }
063:
064: public int getDisplaySize() {
065: return ValueInt.DISPLAY_SIZE;
066: }
067:
068: public String getSQL() {
069: return "(NEXT VALUE FOR " + sequence.getSQL() + ")";
070: }
071:
072: public void updateAggregate(Session session) {
073: // nothing to do
074: }
075:
076: public boolean isEverything(ExpressionVisitor visitor) {
077: switch (visitor.type) {
078: case ExpressionVisitor.OPTIMIZABLE_MIN_MAX_COUNT_ALL:
079: return true;
080: case ExpressionVisitor.DETERMINISTIC:
081: case ExpressionVisitor.READONLY:
082: return false;
083: case ExpressionVisitor.INDEPENDENT:
084: return false;
085: case ExpressionVisitor.EVALUATABLE:
086: return true;
087: case ExpressionVisitor.SET_MAX_DATA_MODIFICATION_ID:
088: visitor.addDataModificationId(sequence.getModificationId());
089: return true;
090: case ExpressionVisitor.NOT_FROM_RESOLVER:
091: return true;
092: case ExpressionVisitor.GET_DEPENDENCIES:
093: visitor.addDependency(sequence);
094: return true;
095: default:
096: throw Message.getInternalError("type=" + visitor.type);
097: }
098: }
099:
100: public int getCost() {
101: return 1;
102: }
103:
104: }
|