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.index.IndexCondition;
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: * An expression representing a constant value.
021: */
022: public class ValueExpression extends Expression {
023: private Value value;
024:
025: public static final ValueExpression NULL = new ValueExpression(
026: ValueNull.INSTANCE);
027: public static final ValueExpression DEFAULT = new ValueExpression(
028: ValueNull.INSTANCE);
029:
030: public static ValueExpression get(Value v) {
031: if (v == ValueNull.INSTANCE) {
032: return ValueExpression.NULL;
033: }
034: return new ValueExpression(v);
035: }
036:
037: private ValueExpression(Value value) {
038: this .value = value;
039: }
040:
041: public Value getValue(Session session) {
042: return value;
043: }
044:
045: public int getType() {
046: return value.getType();
047: }
048:
049: public void createIndexConditions(Session session,
050: TableFilter filter) {
051: if (value.getType() == Value.BOOLEAN) {
052: boolean v = ((ValueBoolean) value).getBoolean()
053: .booleanValue();
054: if (!v) {
055: filter.addIndexCondition(new IndexCondition(
056: Comparison.FALSE, null, this ));
057: }
058: }
059: }
060:
061: public Expression getNotIfPossible(Session session) {
062: return new Comparison(session, Comparison.EQUAL, this ,
063: ValueExpression.get(ValueBoolean.get(false)));
064: }
065:
066: public void mapColumns(ColumnResolver resolver, int level)
067: throws SQLException {
068: }
069:
070: public Expression optimize(Session session) throws SQLException {
071: return this ;
072: }
073:
074: public boolean isConstant() {
075: return true;
076: }
077:
078: public boolean isValueSet() {
079: return true;
080: }
081:
082: public void setEvaluatable(TableFilter tableFilter, boolean b) {
083: }
084:
085: public int getScale() {
086: return value.getScale();
087: }
088:
089: public long getPrecision() {
090: return value.getPrecision();
091: }
092:
093: public int getDisplaySize() {
094: return value.getDisplaySize();
095: }
096:
097: public String getSQL() {
098: if (this == DEFAULT) {
099: return "DEFAULT";
100: } else {
101: return value.getSQL();
102: }
103: }
104:
105: public void updateAggregate(Session session) throws SQLException {
106: }
107:
108: public boolean isEverything(ExpressionVisitor visitor) {
109: switch (visitor.type) {
110: case ExpressionVisitor.OPTIMIZABLE_MIN_MAX_COUNT_ALL:
111: return true;
112: case ExpressionVisitor.DETERMINISTIC:
113: case ExpressionVisitor.READONLY:
114: return true;
115: case ExpressionVisitor.INDEPENDENT:
116: return true;
117: case ExpressionVisitor.EVALUATABLE:
118: return true;
119: case ExpressionVisitor.SET_MAX_DATA_MODIFICATION_ID:
120: return true;
121: case ExpressionVisitor.NOT_FROM_RESOLVER:
122: return true;
123: case ExpressionVisitor.GET_DEPENDENCIES:
124: return true;
125: default:
126: throw Message.getInternalError("type=" + visitor.type);
127: }
128: }
129:
130: public int getCost() {
131: return 0;
132: }
133:
134: }
|