KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > access > trans > OrderingTranslator


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.access.trans;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
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 /**
32  * Translates query ordering to SQL.
33  *
34  * @author Andrus Adamchik
35  * @author Craig Miskell
36  */

37 public class OrderingTranslator extends QueryAssemblerHelper {
38
39     protected List JavaDoc orderByColumnList = new ArrayList JavaDoc();
40     
41     public OrderingTranslator(QueryAssembler queryAssembler) {
42         super(queryAssembler);
43     }
44
45     /** Translates query Ordering list to SQL ORDER BY clause.
46      * Ordering list is obtained from <code>queryAssembler</code>'s query object.
47      * In a process of building of ORDER BY clause, <code>queryAssembler</code>
48      * is notified when a join needs to be added. */

49     public String JavaDoc doTranslation() {
50         Query q = queryAssembler.getQuery();
51
52         // only select queries can have ordering...
53
if (q == null || !(q instanceof SelectQuery))
54             return null;
55
56         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
57         List JavaDoc 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 JavaDoc ordComp = new StringBuffer JavaDoc();
65
66             Ordering ord = (Ordering) list.get(i);
67
68             //UPPER is (I think) part of the SQL99 standard, and I'm not convinced it's universally available
69
// - should the syntax used here be defined by the Db specific adaptor perhaps, or at least
70
// possibly specified by the db adaptor (a DB specific OrderingTranslator hook)?
71
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             //Close UPPER() modifier
88
if (ord.isCaseInsensitive()) {
89                 ordComp.append(")");
90             }
91
92             orderByColumnList.add(ordComp.toString());
93             
94             buf.append(ordComp.toString());
95
96             // "ASC" is a noop, omit it from the query
97
if (!ord.isAscending()) {
98                 buf.append(" DESC");
99             }
100         }
101
102         return buf.length() > 0 ? buf.toString() : null;
103     }
104
105     /**
106      * Returns the column expressions (not Expressions) used in
107      * the order by clause. E.g., in the case of an case-insensitive
108      * order by, an element of the list would be
109      * <code>UPPER(&lt;column reference&gt;)</code>
110      */

111     public List JavaDoc getOrderByColumnList() {
112         return orderByColumnList;
113     }
114 }
115
Popular Tags