1 19 20 21 package org.apache.cayenne.dba.db2; 22 23 import java.sql.Types ; 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 35 public class DB2QualifierTranslator extends TrimmingQualifierTranslator { 36 37 public DB2QualifierTranslator() { 38 super(); 39 } 40 41 public DB2QualifierTranslator( 42 QueryAssembler queryAssembler, 43 String trimFunction) { 44 super(queryAssembler, trimFunction); 45 } 46 47 protected void appendLiteralDirect( 48 StringBuffer buf, 49 Object 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 78 if (jdbcType == Types.CHAR || jdbcType == Types.LONGVARCHAR) { 81 jdbcType = Types.VARCHAR; 82 83 if (len <= 0) { 85 len = 254; 86 } 87 } 88 89 buf.append(" AS "); 90 String [] 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 |