001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.nativequery.optimization;
022:
023: import com.db4o.foundation.*;
024: import com.db4o.instrumentation.core.*;
025: import com.db4o.nativequery.*;
026: import com.db4o.nativequery.expr.*;
027: import com.db4o.nativequery.expr.cmp.*;
028: import com.db4o.nativequery.expr.cmp.operand.*;
029: import com.db4o.query.*;
030:
031: public class SODAQueryBuilder {
032: private static class SODAQueryVisitor implements ExpressionVisitor {
033: private Object _predicate;
034: private Query _query;
035: private Constraint _constraint;
036: private NativeClassFactory _classSource;
037:
038: SODAQueryVisitor(Query query, Object predicate,
039: NativeClassFactory classSource) {
040: _query = query;
041: _predicate = predicate;
042: _classSource = classSource;
043: }
044:
045: public void visit(AndExpression expression) {
046: expression.left().accept(this );
047: Constraint left = _constraint;
048: expression.right().accept(this );
049: left.and(_constraint);
050: _constraint = left;
051: }
052:
053: public void visit(BoolConstExpression expression) {
054: }
055:
056: public void visit(OrExpression expression) {
057: expression.left().accept(this );
058: Constraint left = _constraint;
059: expression.right().accept(this );
060: left.or(_constraint);
061: _constraint = left;
062: }
063:
064: public void visit(ComparisonExpression expression) {
065: Query subQuery = _query;
066: Iterator4 fieldNameIterator = fieldNames(expression.left());
067: while (fieldNameIterator.moveNext()) {
068: subQuery = subQuery.descend((String) fieldNameIterator
069: .current());
070: }
071: ComparisonQueryGeneratingVisitor visitor = new ComparisonQueryGeneratingVisitor(
072: _predicate, _classSource);
073: expression.right().accept(visitor);
074: _constraint = subQuery.constrain(visitor.value());
075: ComparisonOperator op = expression.op();
076: if (op.equals(ComparisonOperator.EQUALS)) {
077: return;
078: }
079: if (op.equals(ComparisonOperator.IDENTITY)) {
080: _constraint.identity();
081: return;
082: }
083: if (op.equals(ComparisonOperator.GREATER)) {
084: _constraint.greater();
085: return;
086: }
087: if (op.equals(ComparisonOperator.SMALLER)) {
088: _constraint.smaller();
089: return;
090: }
091: if (op.equals(ComparisonOperator.CONTAINS)) {
092: _constraint.contains();
093: return;
094: }
095: if (op.equals(ComparisonOperator.STARTSWITH)) {
096: _constraint.startsWith(true);
097: return;
098: }
099: if (op.equals(ComparisonOperator.ENDSWITH)) {
100: _constraint.endsWith(true);
101: return;
102: }
103: throw new RuntimeException("Can't handle constraint: " + op);
104: }
105:
106: public void visit(NotExpression expression) {
107: expression.expr().accept(this );
108: _constraint.not();
109: }
110:
111: private Iterator4 fieldNames(FieldValue fieldValue) {
112: Collection4 coll = new Collection4();
113: ComparisonOperand curOp = fieldValue;
114: while (curOp instanceof FieldValue) {
115: FieldValue curField = (FieldValue) curOp;
116: coll.prepend(curField.fieldName());
117: curOp = curField.parent();
118: }
119: return coll.iterator();
120: }
121: }
122:
123: public void optimizeQuery(Expression expr, Query query,
124: Object predicate, NativeClassFactory classSource) {
125: expr
126: .accept(new SODAQueryVisitor(query, predicate,
127: classSource));
128: }
129: }
|