KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > dba > openbase > OpenBaseQualifierTranslator


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.dba.openbase;
21
22 import org.apache.cayenne.access.trans.QualifierTranslator;
23 import org.apache.cayenne.access.trans.QueryAssembler;
24 import org.apache.cayenne.exp.Expression;
25 import org.apache.cayenne.map.DbAttribute;
26
27 /**
28  * Translates query qualifier to SQL. Used as a helper class by query translators.
29  *
30  * @author <a HREF="mailto:mkienenb@alaska.net">Mike Kienenberger</a>
31  * @author Andrus Adamchik
32  *
33  * @since 1.1
34  */

35 public class OpenBaseQualifierTranslator extends QualifierTranslator {
36
37     public OpenBaseQualifierTranslator() {
38         this(null);
39     }
40
41     public OpenBaseQualifierTranslator(QueryAssembler queryAssembler) {
42         super(queryAssembler);
43     }
44
45     public void startNode(Expression node, Expression parentNode) {
46
47         if (node.getOperandCount() == 2) {
48             // binary nodes are the only ones that currently require this
49
detectObjectMatch(node);
50
51             if (parenthesisNeeded(node, parentNode)) {
52                 qualBuf.append('(');
53             }
54
55             // super implementation has special handling
56
// of LIKE_IGNORE_CASE and NOT_LIKE_IGNORE_CASE
57
// OpenBase is case-insensitive by default
58
// ...
59
}
60         else {
61             super.startNode(node, parentNode);
62         }
63     }
64
65     public void endNode(Expression node, Expression parentNode) {
66         if (node.getOperandCount() == 2) {
67             // check if we need to use objectMatchTranslator to finish building the expression
68
if (matchingObject) {
69                 appendObjectMatch();
70             }
71
72             if (parenthesisNeeded(node, parentNode))
73                 qualBuf.append(')');
74
75             // super implementation has special handling
76
// of LIKE_IGNORE_CASE and NOT_LIKE_IGNORE_CASE
77
// OpenBase is case-insensitive by default
78
// ...
79
}
80         else {
81             super.endNode(node, parentNode);
82         }
83     }
84
85     protected void appendLiteralDirect(
86         StringBuffer JavaDoc buf,
87         Object JavaDoc val,
88         DbAttribute attr,
89         Expression parentExpression) {
90
91         // Special handling of string matching is needed:
92
// Case-sensitive LIKE must be converted to [x][Y][z] format
93
if (val instanceof String JavaDoc
94             && (parentExpression.getType() == Expression.LIKE
95                 || parentExpression.getType() == Expression.NOT_LIKE)) {
96
97             val = caseSensitiveLikePattern((String JavaDoc) val);
98         }
99
100         super.appendLiteralDirect(buf, val, attr, parentExpression);
101     }
102
103     private String JavaDoc caseSensitiveLikePattern(String JavaDoc pattern) {
104         int len = pattern.length();
105         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(len * 3);
106
107         for (int i = 0; i < len; i++) {
108             char c = pattern.charAt(i);
109             if (c == '%' || c == '?') {
110                 buffer.append(c);
111             }
112             else {
113                 buffer.append("[").append(c).append("]");
114             }
115         }
116
117         return buffer.toString();
118     }
119
120     public void finishedChild(Expression node, int childIndex, boolean hasMoreChildren) {
121         if (!hasMoreChildren) {
122             return;
123         }
124
125         // super implementation has special handling
126
// of LIKE_IGNORE_CASE and NOT_LIKE_IGNORE_CASE
127
// OpenBase is case-insensitive by default
128
// ...
129

130         switch (node.getType()) {
131
132             case Expression.LIKE_IGNORE_CASE :
133                 finishedChildNodeAppendExpression(node, " LIKE ");
134                 break;
135             case Expression.NOT_LIKE_IGNORE_CASE :
136                 finishedChildNodeAppendExpression(node, " NOT LIKE ");
137                 break;
138             default :
139                 super.finishedChild(node, childIndex, hasMoreChildren);
140         }
141     }
142
143     private void finishedChildNodeAppendExpression(Expression node, String JavaDoc operation) {
144         StringBuffer JavaDoc buf = (matchingObject) ? new StringBuffer JavaDoc() : qualBuf;
145         buf.append(operation);
146         if (matchingObject) {
147             objectMatchTranslator.setOperation(buf.toString());
148             objectMatchTranslator.setExpression(node);
149         }
150     }
151 }
152
Popular Tags