KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > nativequery > optimization > SODAQueryBuilder


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.nativequery.optimization;
22
23 import com.db4o.foundation.*;
24 import com.db4o.nativequery.expr.*;
25 import com.db4o.nativequery.expr.cmp.*;
26 import com.db4o.query.*;
27
28 public class SODAQueryBuilder {
29     private static class SODAQueryVisitor implements ExpressionVisitor {
30         private Object JavaDoc _predicate;
31         private Query _query;
32         private Constraint _constraint;
33
34         SODAQueryVisitor(Query query, Object JavaDoc predicate) {
35             _query=query;
36             _predicate = predicate;
37         }
38         
39         public void visit(AndExpression expression) {
40             expression.left().accept(this);
41             Constraint left=_constraint;
42             expression.right().accept(this);
43             left.and(_constraint);
44             _constraint=left;
45         }
46
47         public void visit(BoolConstExpression expression) {
48         }
49
50         public void visit(OrExpression expression) {
51             expression.left().accept(this);
52             Constraint left=_constraint;
53             expression.right().accept(this);
54             left.or(_constraint);
55             _constraint=left;
56         }
57
58         public void visit(ComparisonExpression expression) {
59             Query subQuery=_query;
60             Iterator4 fieldNameIterator = fieldNames(expression.left());
61             while(fieldNameIterator.moveNext()) {
62                 subQuery=subQuery.descend((String JavaDoc)fieldNameIterator.current());
63             }
64             ComparisonQueryGeneratingVisitor visitor = new ComparisonQueryGeneratingVisitor(_predicate);
65             expression.right().accept(visitor);
66             _constraint=subQuery.constrain(visitor.value());
67             if(!expression.op().equals(ComparisonOperator.EQUALS)) {
68                 if(expression.op().equals(ComparisonOperator.GREATER)) {
69                     _constraint.greater();
70                 }
71                 else if(expression.op().equals(ComparisonOperator.SMALLER)) {
72                     _constraint.smaller();
73                 }
74                 else if(expression.op().equals(ComparisonOperator.CONTAINS)) {
75                     _constraint.contains();
76                 }
77                 else if(expression.op().equals(ComparisonOperator.STARTSWITH)) {
78                     _constraint.startsWith(true);
79                 }
80                 else if(expression.op().equals(ComparisonOperator.ENDSWITH)) {
81                     _constraint.endsWith(true);
82                 }
83                 else {
84                     throw new RuntimeException JavaDoc("Can't handle constraint: "+expression.op());
85                 }
86             }
87         }
88
89         public void visit(NotExpression expression) {
90             expression.expr().accept(this);
91             _constraint.not();
92         }
93
94         private Iterator4 fieldNames(FieldValue fieldValue) {
95             Collection4 coll=new Collection4();
96             ComparisonOperand curOp=fieldValue;
97             while(curOp instanceof FieldValue) {
98                 FieldValue curField=(FieldValue)curOp;
99                 coll.prepend(curField.fieldName());
100                 curOp=curField.parent();
101             }
102             return coll.iterator();
103         }
104     }
105         
106     public void optimizeQuery(Expression expr, Query query, Object JavaDoc predicate) {
107         expr.accept(new SODAQueryVisitor(query, predicate));
108     }
109 }
110
Popular Tags