KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > dba > postgres > PostgresQualifierTranslator


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.postgres;
21
22 import org.apache.cayenne.access.trans.QueryAssembler;
23 import org.apache.cayenne.access.trans.TrimmingQualifierTranslator;
24 import org.apache.cayenne.exp.Expression;
25
26 /**
27  * Uses Postgres extensions to optimize various translations.
28  *
29  * @author Andrus Adamchik
30  * @since 1.1
31  */

32 public class PostgresQualifierTranslator extends TrimmingQualifierTranslator {
33
34     public PostgresQualifierTranslator(QueryAssembler queryAssembler) {
35         super(queryAssembler, "RTRIM");
36     }
37
38     public void startNode(Expression node, Expression parentNode) {
39
40         if (node.getOperandCount() == 2) {
41             // binary nodes are the only ones that currently require this
42
detectObjectMatch(node);
43
44             if (parenthesisNeeded(node, parentNode)) {
45                 qualBuf.append('(');
46             }
47
48             // super implementation has special handling
49
// of LIKE_IGNORE_CASE and NOT_LIKE_IGNORE_CASE
50
// Postgres uses ILIKE
51
// ...
52
}
53         else {
54             super.startNode(node, parentNode);
55         }
56     }
57
58     public void endNode(Expression node, Expression parentNode) {
59         if (node.getOperandCount() == 2) {
60             // check if we need to use objectMatchTranslator to finish building the expression
61
if (matchingObject) {
62                 appendObjectMatch();
63             }
64
65             if (parenthesisNeeded(node, parentNode))
66                 qualBuf.append(')');
67
68             // super implementation has special handling
69
// of LIKE_IGNORE_CASE and NOT_LIKE_IGNORE_CASE
70
// Postgres uses ILIKE
71
// ...
72
}
73         else {
74             super.endNode(node, parentNode);
75         }
76     }
77
78     public void finishedChild(Expression node, int childIndex, boolean hasMoreChildren) {
79         if (!hasMoreChildren) {
80             return;
81         }
82
83         // use ILIKE
84

85         switch (node.getType()) {
86
87             case Expression.LIKE_IGNORE_CASE :
88                 finishedChildNodeAppendExpression(node, " ILIKE ");
89                 break;
90             case Expression.NOT_LIKE_IGNORE_CASE :
91                 finishedChildNodeAppendExpression(node, " NOT ILIKE ");
92                 break;
93             default :
94                 super.finishedChild(node, childIndex, hasMoreChildren);
95         }
96     }
97
98     private void finishedChildNodeAppendExpression(Expression node, String JavaDoc operation) {
99         StringBuffer JavaDoc buf = (matchingObject) ? new StringBuffer JavaDoc() : qualBuf;
100         buf.append(operation);
101         if (matchingObject) {
102             objectMatchTranslator.setOperation(buf.toString());
103             objectMatchTranslator.setExpression(node);
104         }
105     }
106 }
107
Popular Tags