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.command.Parser;
011: import org.h2.engine.Session;
012: import org.h2.message.Message;
013: import org.h2.table.ColumnResolver;
014: import org.h2.table.TableFilter;
015: import org.h2.value.Value;
016:
017: /**
018: * A user defined variable, for example: @ID.
019: */
020: public class Variable extends Expression {
021:
022: private final String name;
023: private Value lastValue;
024:
025: public Variable(Session session, String name) {
026: this .name = name;
027: lastValue = session.getVariable(name);
028: }
029:
030: public int getCost() {
031: return 0;
032: }
033:
034: public int getDisplaySize() {
035: return lastValue.getDisplaySize();
036: }
037:
038: public long getPrecision() {
039: return lastValue.getPrecision();
040: }
041:
042: public String getSQL() {
043: return "@" + Parser.quoteIdentifier(name);
044: }
045:
046: public int getScale() {
047: return lastValue.getScale();
048: }
049:
050: public int getType() {
051: return lastValue.getType();
052: }
053:
054: public Value getValue(Session session) throws SQLException {
055: lastValue = session.getVariable(name);
056: return lastValue;
057: }
058:
059: public boolean isEverything(ExpressionVisitor visitor) {
060: switch (visitor.type) {
061: case ExpressionVisitor.OPTIMIZABLE_MIN_MAX_COUNT_ALL:
062: return true;
063: case ExpressionVisitor.DETERMINISTIC:
064: return false;
065: case ExpressionVisitor.READONLY:
066: return true;
067: case ExpressionVisitor.INDEPENDENT:
068: return true;
069: case ExpressionVisitor.EVALUATABLE:
070: // the value will be evaluated at execute time
071: return true;
072: case ExpressionVisitor.SET_MAX_DATA_MODIFICATION_ID:
073: // it is checked independently if the value is the same as the last time
074: return true;
075: case ExpressionVisitor.NOT_FROM_RESOLVER:
076: return true;
077: case ExpressionVisitor.GET_DEPENDENCIES:
078: return true;
079: default:
080: throw Message.getInternalError("type=" + visitor.type);
081: }
082: }
083:
084: public void mapColumns(ColumnResolver resolver, int level)
085: throws SQLException {
086: }
087:
088: public Expression optimize(Session session) throws SQLException {
089: return this ;
090: }
091:
092: public void setEvaluatable(TableFilter tableFilter, boolean value) {
093: }
094:
095: public void updateAggregate(Session session) throws SQLException {
096: }
097:
098: public String getName() {
099: return name;
100: }
101:
102: }
|