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.constant.ErrorCode;
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: import org.h2.value.ValueBoolean;
017: import org.h2.value.ValueNull;
018:
019: /**
020: * A parameter of a prepared statement.
021: */
022: public class Parameter extends Expression implements ParameterInterface {
023:
024: private Value value;
025: private int index;
026:
027: public Parameter(int index) {
028: this .index = index;
029: }
030:
031: public String getSQL() {
032: return "?" + (index + 1);
033: }
034:
035: public void setValue(Value v) {
036: this .value = v;
037: }
038:
039: public Value getParamValue() throws SQLException {
040: if (value == null) {
041: // to allow parameters in function tables
042: return ValueNull.INSTANCE;
043: } else {
044: return value;
045: }
046: }
047:
048: public Value getValue(Session session) throws SQLException {
049: return getParamValue();
050: }
051:
052: public int getType() {
053: return value == null ? Value.UNKNOWN : value.getType();
054: }
055:
056: public void mapColumns(ColumnResolver resolver, int level) {
057: // can't map
058: }
059:
060: public void checkMapped() {
061: // always ok
062: }
063:
064: public void checkSet() throws SQLException {
065: if (value == null) {
066: throw Message.getSQLException(
067: ErrorCode.PARAMETER_NOT_SET_1, "#" + (index + 1));
068: }
069: }
070:
071: public Expression optimize(Session session) {
072: return this ;
073: }
074:
075: public boolean isConstant() {
076: return false;
077: }
078:
079: public boolean isValueSet() {
080: return value != null;
081: }
082:
083: public void setEvaluatable(TableFilter tableFilter, boolean b) {
084: // not bound
085: }
086:
087: public int getScale() {
088: return value == null ? 0 : value.getScale();
089: }
090:
091: public long getPrecision() {
092: return value == null ? 0 : value.getPrecision();
093: }
094:
095: public int getDisplaySize() {
096: return value == null ? 0 : value.getDisplaySize();
097: }
098:
099: public void updateAggregate(Session session) {
100: // nothing to do
101: }
102:
103: public boolean isEverything(ExpressionVisitor visitor) {
104: switch (visitor.type) {
105: case ExpressionVisitor.OPTIMIZABLE_MIN_MAX_COUNT_ALL:
106: return true;
107: case ExpressionVisitor.DETERMINISTIC:
108: case ExpressionVisitor.READONLY:
109: return true;
110: case ExpressionVisitor.INDEPENDENT:
111: return value != null;
112: case ExpressionVisitor.EVALUATABLE:
113: // the parameter _will_be_ evaluatable at execute time
114: return true;
115: case ExpressionVisitor.SET_MAX_DATA_MODIFICATION_ID:
116: // it is checked independently if the value is the same as the last time
117: return true;
118: case ExpressionVisitor.NOT_FROM_RESOLVER:
119: return true;
120: case ExpressionVisitor.GET_DEPENDENCIES:
121: return true;
122: default:
123: throw Message.getInternalError("type=" + visitor.type);
124: }
125: }
126:
127: public int getCost() {
128: return 0;
129: }
130:
131: public Expression getNotIfPossible(Session session) {
132: return new Comparison(session, Comparison.EQUAL, this ,
133: ValueExpression.get(ValueBoolean.get(false)));
134: }
135:
136: }
|