/*
* Copyright 2014-2015 the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the “License”);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an “AS IS” BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.wplatform.ddal.command.expression;
import com.wplatform.ddal.dbobject.schema.Sequence;
import com.wplatform.ddal.dbobject.table.ColumnResolver;
import com.wplatform.ddal.dbobject.table.TableFilter;
import com.wplatform.ddal.engine.Session;
import com.wplatform.ddal.message.DbException;
import com.wplatform.ddal.value.Value;
import com.wplatform.ddal.value.ValueInt;
import com.wplatform.ddal.value.ValueLong;
/**
* Wraps a sequence when used in a statement.
*/
public class SequenceValue extends Expression {
private final Sequence sequence;
public SequenceValue(Sequence sequence) {
this.sequence = sequence;
}
@Override
public Value getValue(Session session) {
long value = sequence.getNext(session);
session.setLastIdentity(ValueLong.get(value));
return ValueLong.get(value);
}
@Override
public int getType() {
return Value.LONG;
}
@Override
public void mapColumns(ColumnResolver resolver, int level) {
// nothing to do
}
@Override
public Expression optimize(Session session) {
return this;
}
@Override
public void setEvaluatable(TableFilter tableFilter, boolean b) {
// nothing to do
}
@Override
public int getScale() {
return 0;
}
@Override
public long getPrecision() {
return ValueInt.PRECISION;
}
@Override
public int getDisplaySize() {
return ValueInt.DISPLAY_SIZE;
}
@Override
public String getSQL() {
return "(NEXT VALUE FOR " + sequence.getSQL() +")";
}
@Override
public void updateAggregate(Session session) {
// nothing to do
}
@Override
public boolean isEverything(ExpressionVisitor visitor) {
switch(visitor.getType()) {
case ExpressionVisitor.EVALUATABLE:
case ExpressionVisitor.OPTIMIZABLE_MIN_MAX_COUNT_ALL:
case ExpressionVisitor.NOT_FROM_RESOLVER:
case ExpressionVisitor.GET_COLUMNS:
return true;
case ExpressionVisitor.DETERMINISTIC:
case ExpressionVisitor.READONLY:
case ExpressionVisitor.INDEPENDENT:
case ExpressionVisitor.QUERY_COMPARABLE:
return false;
case ExpressionVisitor.GET_DEPENDENCIES:
visitor.addDependency(sequence);
return true;
default:
throw DbException.throwInternalError("type="+visitor.getType());
}
}
@Override
public int getCost() {
return 1;
}
}