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.util.HashSet;
009:
010: import org.h2.engine.DbObject;
011: import org.h2.table.ColumnResolver;
012: import org.h2.table.Table;
013:
014: /**
015: * The visitor pattern is used to iterate through all expressions of a query
016: * to optimize a statement.
017: */
018: public class ExpressionVisitor {
019:
020: /**
021: * Is the value independent on unset parameters or on columns of a higher
022: * level query, or sequence values (that means can it be evaluated right
023: * now)?
024: */
025: public static final int INDEPENDENT = 0;
026:
027: /**
028: * Are all aggregates MIN(column), MAX(column), or COUNT(*)?
029: */
030: public static final int OPTIMIZABLE_MIN_MAX_COUNT_ALL = 1;
031:
032: /**
033: * Does the expression return the same results for the same parameters?
034: */
035: public static final int DETERMINISTIC = 2;
036:
037: /**
038: * Can the expression be evaluated, that means are all columns set to
039: * 'evaluatable'?
040: */
041: public static final int EVALUATABLE = 3;
042:
043: /**
044: * Request to set the latest modification id.
045: */
046: public static final int SET_MAX_DATA_MODIFICATION_ID = 4;
047:
048: /**
049: * Does the expression have no side effects (change the data)?
050: */
051: public static final int READONLY = 5;
052:
053: /**
054: * Does an expression have no relation to the given table filter?
055: */
056: public static final int NOT_FROM_RESOLVER = 6;
057:
058: /**
059: * Request to get the set of dependencies.
060: */
061: public static final int GET_DEPENDENCIES = 7;
062:
063: int queryLevel;
064: public Table table;
065: public int type;
066: private long maxDataModificationId;
067: private ColumnResolver resolver;
068: private HashSet dependencies;
069:
070: public static ExpressionVisitor get(int type) {
071: return new ExpressionVisitor(type);
072: }
073:
074: public long getMaxDataModificationId() {
075: return maxDataModificationId;
076: }
077:
078: public void addDependency(DbObject obj) {
079: dependencies.add(obj);
080: }
081:
082: public HashSet getDependencies() {
083: return dependencies;
084: }
085:
086: public void setDependencies(HashSet dependencies) {
087: this .dependencies = dependencies;
088: }
089:
090: private ExpressionVisitor(int type) {
091: this .type = type;
092: }
093:
094: public void queryLevel(int offset) {
095: queryLevel += offset;
096: }
097:
098: public ColumnResolver getResolver() {
099: return resolver;
100: }
101:
102: public void addDataModificationId(long v) {
103: maxDataModificationId = Math.max(maxDataModificationId, v);
104: }
105:
106: public static ExpressionVisitor getNotFromResolver(
107: ColumnResolver resolver) {
108: ExpressionVisitor v = new ExpressionVisitor(NOT_FROM_RESOLVER);
109: v.resolver = resolver;
110: return v;
111: }
112:
113: }
|