1 5 package org.h2.index; 6 7 import java.sql.SQLException ; 8 9 import org.h2.engine.Session; 10 import org.h2.expression.Comparison; 11 import org.h2.expression.Expression; 12 import org.h2.expression.ExpressionColumn; 13 import org.h2.expression.ExpressionVisitor; 14 import org.h2.message.Message; 15 import org.h2.table.Column; 16 import org.h2.value.Value; 17 18 19 22 public class IndexCondition { 23 public static final int EQUALITY = 1, START = 2, END = 4, RANGE = START | END, ALWAYS_FALSE = 8; 24 private Column column; 25 private Expression expression; 26 private int compareType; 27 28 public IndexCondition(int compareType, ExpressionColumn column, Expression expression) { 29 this.compareType = compareType; 30 this.column = column == null ? null : column.getColumn(); 31 this.expression = expression; 32 } 33 34 public Value getCurrentValue(Session session) throws SQLException { 35 return expression.getValue(session); 36 } 37 38 public String getSQL() { 39 if(compareType == Comparison.FALSE) { 40 return "FALSE"; 41 } 42 StringBuffer buff = new StringBuffer (); 43 buff.append(column.getSQL()); 44 switch(compareType) { 45 case Comparison.EQUAL: 46 buff.append(" = "); 47 break; 48 case Comparison.BIGGER_EQUAL: 49 buff.append(" >= "); 50 break; 51 case Comparison.BIGGER: 52 buff.append(" > "); 53 break; 54 case Comparison.SMALLER_EQUAL: 55 buff.append(" <= "); 56 break; 57 case Comparison.SMALLER: 58 buff.append(" < "); 59 break; 60 default: 61 throw Message.getInternalError("type="+compareType); 62 } 63 buff.append(expression.getSQL()); 64 return buff.toString(); 65 } 66 67 public int getMask() { 68 switch (compareType) { 69 case Comparison.FALSE: 70 return ALWAYS_FALSE; 71 case Comparison.EQUAL: 72 return EQUALITY; 73 case Comparison.BIGGER_EQUAL: 74 case Comparison.BIGGER: 75 return START; 76 case Comparison.SMALLER_EQUAL: 77 case Comparison.SMALLER: 78 return END; 79 default: 80 throw Message.getInternalError("type=" + compareType); 81 } 82 } 83 84 public boolean isAlwaysFalse() { 85 return compareType == Comparison.FALSE; 86 } 87 88 public boolean isStart() { 89 switch (compareType) { 90 case Comparison.EQUAL: 91 case Comparison.BIGGER_EQUAL: 92 case Comparison.BIGGER: 93 return true; 94 } 95 return false; 96 } 97 98 public boolean isEnd() { 99 switch (compareType) { 100 case Comparison.EQUAL: 101 case Comparison.SMALLER_EQUAL: 102 case Comparison.SMALLER: 103 return true; 104 } 105 return false; 106 } 107 108 public Column getColumn() { 109 return column; 110 } 111 112 public boolean isEvaluatable() { 113 return expression.isEverything(ExpressionVisitor.EVALUATABLE); 114 } 115 116 } 117 | Popular Tags |