1 19 20 package org.apache.cayenne.access.trans; 21 22 import java.util.ArrayList ; 23 import java.util.List ; 24 25 import org.apache.cayenne.CayenneRuntimeException; 26 import org.apache.cayenne.exp.Expression; 27 import org.apache.cayenne.query.Ordering; 28 import org.apache.cayenne.query.Query; 29 import org.apache.cayenne.query.SelectQuery; 30 31 37 public class OrderingTranslator extends QueryAssemblerHelper { 38 39 protected List orderByColumnList = new ArrayList (); 40 41 public OrderingTranslator(QueryAssembler queryAssembler) { 42 super(queryAssembler); 43 } 44 45 49 public String doTranslation() { 50 Query q = queryAssembler.getQuery(); 51 52 if (q == null || !(q instanceof SelectQuery)) 54 return null; 55 56 StringBuffer buf = new StringBuffer (); 57 List list = ((SelectQuery) q).getOrderings(); 58 int len = list.size(); 59 60 for (int i = 0; i < len; i++) { 61 if (i > 0) 62 buf.append(", "); 63 64 StringBuffer ordComp = new StringBuffer (); 65 66 Ordering ord = (Ordering) list.get(i); 67 68 if (ord.isCaseInsensitive()) { 72 ordComp.append("UPPER("); 73 74 } 75 76 Expression exp = ord.getSortSpec(); 77 78 if (exp.getType() == Expression.OBJ_PATH) { 79 appendObjPath(ordComp, exp); 80 } else if (exp.getType() == Expression.DB_PATH) { 81 appendDbPath(ordComp, exp); 82 } else { 83 throw new CayenneRuntimeException( 84 "Unsupported ordering expression: " + exp); 85 } 86 87 if (ord.isCaseInsensitive()) { 89 ordComp.append(")"); 90 } 91 92 orderByColumnList.add(ordComp.toString()); 93 94 buf.append(ordComp.toString()); 95 96 if (!ord.isAscending()) { 98 buf.append(" DESC"); 99 } 100 } 101 102 return buf.length() > 0 ? buf.toString() : null; 103 } 104 105 111 public List getOrderByColumnList() { 112 return orderByColumnList; 113 } 114 } 115 | Popular Tags |