1 56 package org.objectstyle.cayenne.dba.openbase; 57 58 import org.objectstyle.cayenne.access.trans.QualifierTranslator; 59 import org.objectstyle.cayenne.access.trans.QueryAssembler; 60 import org.objectstyle.cayenne.exp.Expression; 61 import org.objectstyle.cayenne.map.DbAttribute; 62 63 71 public class OpenBaseQualifierTranslator extends QualifierTranslator { 72 73 public OpenBaseQualifierTranslator() { 74 this(null); 75 } 76 77 public OpenBaseQualifierTranslator(QueryAssembler queryAssembler) { 78 super(queryAssembler); 79 } 80 81 public void startNode(Expression node, Expression parentNode) { 82 83 if (node.getOperandCount() == 2) { 84 detectObjectMatch(node); 86 87 if (parenthesisNeeded(node, parentNode)) { 88 qualBuf.append('('); 89 } 90 91 } 96 else { 97 super.startNode(node, parentNode); 98 } 99 } 100 101 public void endNode(Expression node, Expression parentNode) { 102 if (node.getOperandCount() == 2) { 103 if (matchingObject) { 105 appendObjectMatch(); 106 } 107 108 if (parenthesisNeeded(node, parentNode)) 109 qualBuf.append(')'); 110 111 } 116 else { 117 super.endNode(node, parentNode); 118 } 119 } 120 121 protected void appendLiteralDirect( 122 StringBuffer buf, 123 Object val, 124 DbAttribute attr, 125 Expression parentExpression) { 126 127 if (val instanceof String 130 && (parentExpression.getType() == Expression.LIKE 131 || parentExpression.getType() == Expression.NOT_LIKE)) { 132 133 val = caseSensitiveLikePattern((String ) val); 134 } 135 136 super.appendLiteralDirect(buf, val, attr, parentExpression); 137 } 138 139 private String caseSensitiveLikePattern(String pattern) { 140 int len = pattern.length(); 141 StringBuffer buffer = new StringBuffer (len * 3); 142 143 for (int i = 0; i < len; i++) { 144 char c = pattern.charAt(i); 145 if (c == '%' || c == '?') { 146 buffer.append(c); 147 } 148 else { 149 buffer.append("[").append(c).append("]"); 150 } 151 } 152 153 return buffer.toString(); 154 } 155 156 public void finishedChild(Expression node, int childIndex, boolean hasMoreChildren) { 157 if (!hasMoreChildren) { 158 return; 159 } 160 161 166 switch (node.getType()) { 167 168 case Expression.LIKE_IGNORE_CASE : 169 finishedChildNodeAppendExpression(node, " LIKE "); 170 break; 171 case Expression.NOT_LIKE_IGNORE_CASE : 172 finishedChildNodeAppendExpression(node, " NOT LIKE "); 173 break; 174 default : 175 super.finishedChild(node, childIndex, hasMoreChildren); 176 } 177 } 178 179 private void finishedChildNodeAppendExpression(Expression node, String operation) { 180 StringBuffer buf = (matchingObject) ? new StringBuffer () : qualBuf; 181 buf.append(operation); 182 if (matchingObject) { 183 objectMatchTranslator.setOperation(buf.toString()); 184 objectMatchTranslator.setExpression(node); 185 } 186 } 187 } | Popular Tags |