1 56 package org.objectstyle.cayenne.access.trans; 57 58 import java.util.ArrayList ; 59 import java.util.List ; 60 61 import org.objectstyle.cayenne.CayenneRuntimeException; 62 import org.objectstyle.cayenne.exp.Expression; 63 import org.objectstyle.cayenne.query.Ordering; 64 import org.objectstyle.cayenne.query.Query; 65 import org.objectstyle.cayenne.query.SelectQuery; 66 67 73 public class OrderingTranslator extends QueryAssemblerHelper { 74 75 protected List orderByColumnList = new ArrayList (); 76 77 public OrderingTranslator(QueryAssembler queryAssembler) { 78 super(queryAssembler); 79 } 80 81 85 public String doTranslation() { 86 Query q = queryAssembler.getQuery(); 87 88 if (q == null || !(q instanceof SelectQuery)) 90 return null; 91 92 StringBuffer buf = new StringBuffer (); 93 List list = ((SelectQuery) q).getOrderings(); 94 int len = list.size(); 95 96 for (int i = 0; i < len; i++) { 97 if (i > 0) 98 buf.append(", "); 99 100 StringBuffer ordComp = new StringBuffer (); 101 102 Ordering ord = (Ordering) list.get(i); 103 104 if (ord.isCaseInsensitive()) { 108 ordComp.append("UPPER("); 109 110 } 111 112 Expression exp = ord.getSortSpec(); 113 114 if (exp.getType() == Expression.OBJ_PATH) { 115 appendObjPath(ordComp, exp); 116 } else if (exp.getType() == Expression.DB_PATH) { 117 appendDbPath(ordComp, exp); 118 } else { 119 throw new CayenneRuntimeException( 120 "Unsupported ordering expression: " + exp); 121 } 122 123 if (ord.isCaseInsensitive()) { 125 ordComp.append(")"); 126 } 127 128 orderByColumnList.add(ordComp.toString()); 129 130 buf.append(ordComp.toString()); 131 132 if (!ord.isAscending()) { 134 buf.append(" DESC"); 135 } 136 } 137 138 return buf.length() > 0 ? buf.toString() : null; 139 } 140 141 147 public List getOrderByColumnList() { 148 return orderByColumnList; 149 } 150 } 151 | Popular Tags |