1 56 57 package org.objectstyle.cayenne.dba.db2; 58 59 import java.sql.Types ; 60 61 import org.objectstyle.cayenne.CayenneRuntimeException; 62 import org.objectstyle.cayenne.access.trans.QueryAssembler; 63 import org.objectstyle.cayenne.access.trans.TrimmingQualifierTranslator; 64 import org.objectstyle.cayenne.dba.TypesMapping; 65 import org.objectstyle.cayenne.exp.Expression; 66 import org.objectstyle.cayenne.map.DbAttribute; 67 68 71 public class DB2QualifierTranslator extends TrimmingQualifierTranslator { 72 73 public DB2QualifierTranslator() { 74 super(); 75 } 76 77 public DB2QualifierTranslator( 78 QueryAssembler queryAssembler, 79 String trimFunction) { 80 super(queryAssembler, trimFunction); 81 } 82 83 protected void appendLiteralDirect( 84 StringBuffer buf, 85 Object val, 86 DbAttribute attr, 87 Expression parentExpression) { 88 89 boolean castNeeded = false; 90 91 if (parentExpression != null) { 92 int type = parentExpression.getType(); 93 94 castNeeded = 95 attr != null 96 && (type == Expression.LIKE 97 || type == Expression.LIKE_IGNORE_CASE 98 || type == Expression.NOT_LIKE 99 || type == Expression.NOT_LIKE_IGNORE_CASE); 100 } 101 102 if (castNeeded) { 103 buf.append("CAST ("); 104 } 105 106 super.appendLiteralDirect(buf, val, attr, parentExpression); 107 108 if (castNeeded) { 109 int jdbcType = attr.getType(); 110 int len = attr.getMaxLength(); 111 112 114 if (jdbcType == Types.CHAR || jdbcType == Types.LONGVARCHAR) { 117 jdbcType = Types.VARCHAR; 118 119 if (len <= 0) { 121 len = 254; 122 } 123 } 124 125 buf.append(" AS "); 126 String [] types = 127 getQueryAssembler().getAdapter().externalTypesForJdbcType( 128 jdbcType); 129 130 if (types == null || types.length == 0) { 131 throw new CayenneRuntimeException( 132 "Can't find database type for JDBC type '" 133 + TypesMapping.getSqlNameByType(jdbcType)); 134 } 135 136 buf.append(types[0]); 137 if (len > 0 && TypesMapping.supportsLength(jdbcType)) { 138 buf.append("(").append(len).append(")"); 139 } 140 141 buf.append(")"); 142 } 143 } 144 } 145 | Popular Tags |