KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > dba > db2 > DB2QualifierTranslator


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
21 package org.apache.cayenne.dba.db2;
22
23 import java.sql.Types JavaDoc;
24
25 import org.apache.cayenne.CayenneRuntimeException;
26 import org.apache.cayenne.access.trans.QueryAssembler;
27 import org.apache.cayenne.access.trans.TrimmingQualifierTranslator;
28 import org.apache.cayenne.dba.TypesMapping;
29 import org.apache.cayenne.exp.Expression;
30 import org.apache.cayenne.map.DbAttribute;
31
32 /**
33  * @author Andrus Adamchik
34  */

35 public class DB2QualifierTranslator extends TrimmingQualifierTranslator {
36
37     public DB2QualifierTranslator() {
38         super();
39     }
40
41     public DB2QualifierTranslator(
42         QueryAssembler queryAssembler,
43         String JavaDoc trimFunction) {
44         super(queryAssembler, trimFunction);
45     }
46
47     protected void appendLiteralDirect(
48         StringBuffer JavaDoc buf,
49         Object JavaDoc val,
50         DbAttribute attr,
51         Expression parentExpression) {
52
53         boolean castNeeded = false;
54
55         if (parentExpression != null) {
56             int type = parentExpression.getType();
57
58             castNeeded =
59                 attr != null
60                     && (type == Expression.LIKE
61                         || type == Expression.LIKE_IGNORE_CASE
62                         || type == Expression.NOT_LIKE
63                         || type == Expression.NOT_LIKE_IGNORE_CASE);
64         }
65
66         if (castNeeded) {
67             buf.append("CAST (");
68         }
69
70         super.appendLiteralDirect(buf, val, attr, parentExpression);
71
72         if (castNeeded) {
73             int jdbcType = attr.getType();
74             int len = attr.getMaxLength();
75
76             // determine CAST type
77

78             // LIKE on CHAR may produce unpredictible results
79
// LIKE on LONVARCHAR doesn't seem to be supported
80
if (jdbcType == Types.CHAR || jdbcType == Types.LONGVARCHAR) {
81                 jdbcType = Types.VARCHAR;
82
83                 // length is required for VARCHAR
84
if (len <= 0) {
85                     len = 254;
86                 }
87             }
88
89             buf.append(" AS ");
90             String JavaDoc[] types =
91                 getQueryAssembler().getAdapter().externalTypesForJdbcType(
92                     jdbcType);
93
94             if (types == null || types.length == 0) {
95                 throw new CayenneRuntimeException(
96                     "Can't find database type for JDBC type '"
97                         + TypesMapping.getSqlNameByType(jdbcType));
98             }
99
100             buf.append(types[0]);
101             if (len > 0 && TypesMapping.supportsLength(jdbcType)) {
102                 buf.append("(").append(len).append(")");
103             }
104
105             buf.append(")");
106         }
107     }
108 }
109
Popular Tags