1 19 20 package org.apache.cayenne.dba.openbase; 21 22 import org.apache.cayenne.access.trans.QualifierTranslator; 23 import org.apache.cayenne.access.trans.QueryAssembler; 24 import org.apache.cayenne.exp.Expression; 25 import org.apache.cayenne.map.DbAttribute; 26 27 35 public class OpenBaseQualifierTranslator extends QualifierTranslator { 36 37 public OpenBaseQualifierTranslator() { 38 this(null); 39 } 40 41 public OpenBaseQualifierTranslator(QueryAssembler queryAssembler) { 42 super(queryAssembler); 43 } 44 45 public void startNode(Expression node, Expression parentNode) { 46 47 if (node.getOperandCount() == 2) { 48 detectObjectMatch(node); 50 51 if (parenthesisNeeded(node, parentNode)) { 52 qualBuf.append('('); 53 } 54 55 } 60 else { 61 super.startNode(node, parentNode); 62 } 63 } 64 65 public void endNode(Expression node, Expression parentNode) { 66 if (node.getOperandCount() == 2) { 67 if (matchingObject) { 69 appendObjectMatch(); 70 } 71 72 if (parenthesisNeeded(node, parentNode)) 73 qualBuf.append(')'); 74 75 } 80 else { 81 super.endNode(node, parentNode); 82 } 83 } 84 85 protected void appendLiteralDirect( 86 StringBuffer buf, 87 Object val, 88 DbAttribute attr, 89 Expression parentExpression) { 90 91 if (val instanceof String 94 && (parentExpression.getType() == Expression.LIKE 95 || parentExpression.getType() == Expression.NOT_LIKE)) { 96 97 val = caseSensitiveLikePattern((String ) val); 98 } 99 100 super.appendLiteralDirect(buf, val, attr, parentExpression); 101 } 102 103 private String caseSensitiveLikePattern(String pattern) { 104 int len = pattern.length(); 105 StringBuffer buffer = new StringBuffer (len * 3); 106 107 for (int i = 0; i < len; i++) { 108 char c = pattern.charAt(i); 109 if (c == '%' || c == '?') { 110 buffer.append(c); 111 } 112 else { 113 buffer.append("[").append(c).append("]"); 114 } 115 } 116 117 return buffer.toString(); 118 } 119 120 public void finishedChild(Expression node, int childIndex, boolean hasMoreChildren) { 121 if (!hasMoreChildren) { 122 return; 123 } 124 125 130 switch (node.getType()) { 131 132 case Expression.LIKE_IGNORE_CASE : 133 finishedChildNodeAppendExpression(node, " LIKE "); 134 break; 135 case Expression.NOT_LIKE_IGNORE_CASE : 136 finishedChildNodeAppendExpression(node, " NOT LIKE "); 137 break; 138 default : 139 super.finishedChild(node, childIndex, hasMoreChildren); 140 } 141 } 142 143 private void finishedChildNodeAppendExpression(Expression node, String operation) { 144 StringBuffer buf = (matchingObject) ? new StringBuffer () : qualBuf; 145 buf.append(operation); 146 if (matchingObject) { 147 objectMatchTranslator.setOperation(buf.toString()); 148 objectMatchTranslator.setExpression(node); 149 } 150 } 151 } 152 | Popular Tags |