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.index;
007:
008: import java.sql.SQLException;
009:
010: import org.h2.engine.Session;
011: import org.h2.expression.Comparison;
012: import org.h2.expression.Expression;
013: import org.h2.expression.ExpressionColumn;
014: import org.h2.expression.ExpressionVisitor;
015: import org.h2.message.Message;
016: import org.h2.table.Column;
017: import org.h2.value.Value;
018:
019: /**
020: * A index condition object is made for each condition that can potentially use
021: * an index. This class does not extend expression, but in general there is one
022: * expression that maps to each index condition.
023: */
024: public class IndexCondition {
025: public static final int EQUALITY = 1, START = 2, END = 4,
026: RANGE = START | END, ALWAYS_FALSE = 8;
027: private Column column;
028: private Expression expression;
029: private int compareType;
030:
031: public IndexCondition(int compareType, ExpressionColumn column,
032: Expression expression) {
033: this .compareType = compareType;
034: this .column = column == null ? null : column.getColumn();
035: this .expression = expression;
036: }
037:
038: public Value getCurrentValue(Session session) throws SQLException {
039: return expression.getValue(session);
040: }
041:
042: public String getSQL() {
043: if (compareType == Comparison.FALSE) {
044: return "FALSE";
045: }
046: StringBuffer buff = new StringBuffer();
047: buff.append(column.getSQL());
048: switch (compareType) {
049: case Comparison.EQUAL:
050: buff.append(" = ");
051: break;
052: case Comparison.BIGGER_EQUAL:
053: buff.append(" >= ");
054: break;
055: case Comparison.BIGGER:
056: buff.append(" > ");
057: break;
058: case Comparison.SMALLER_EQUAL:
059: buff.append(" <= ");
060: break;
061: case Comparison.SMALLER:
062: buff.append(" < ");
063: break;
064: default:
065: throw Message.getInternalError("type=" + compareType);
066: }
067: buff.append(expression.getSQL());
068: return buff.toString();
069: }
070:
071: public int getMask() {
072: switch (compareType) {
073: case Comparison.FALSE:
074: return ALWAYS_FALSE;
075: case Comparison.EQUAL:
076: return EQUALITY;
077: case Comparison.BIGGER_EQUAL:
078: case Comparison.BIGGER:
079: return START;
080: case Comparison.SMALLER_EQUAL:
081: case Comparison.SMALLER:
082: return END;
083: default:
084: throw Message.getInternalError("type=" + compareType);
085: }
086: }
087:
088: public boolean isAlwaysFalse() {
089: return compareType == Comparison.FALSE;
090: }
091:
092: public boolean isStart() {
093: switch (compareType) {
094: case Comparison.EQUAL:
095: case Comparison.BIGGER_EQUAL:
096: case Comparison.BIGGER:
097: return true;
098: default:
099: return false;
100: }
101: }
102:
103: public boolean isEnd() {
104: switch (compareType) {
105: case Comparison.EQUAL:
106: case Comparison.SMALLER_EQUAL:
107: case Comparison.SMALLER:
108: return true;
109: default:
110: return false;
111: }
112: }
113:
114: public Column getColumn() {
115: return column;
116: }
117:
118: public boolean isEvaluatable() {
119: return expression.isEverything(ExpressionVisitor.EVALUATABLE);
120: }
121:
122: public Expression getExpression() {
123: return expression;
124: }
125:
126: }
|