KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > access > jdbc > EJBQLOrderByTranslator


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 package org.apache.cayenne.access.jdbc;
20
21 import org.apache.cayenne.ejbql.EJBQLBaseVisitor;
22 import org.apache.cayenne.ejbql.EJBQLException;
23 import org.apache.cayenne.ejbql.EJBQLExpression;
24 import org.apache.cayenne.ejbql.EJBQLExpressionVisitor;
25 import org.apache.cayenne.ejbql.parser.EJBQLPath;
26
27 /**
28  * @since 3.0
29  * @author Andrus Adamchik
30  */

31 class EJBQLOrderByTranslator extends EJBQLBaseVisitor {
32
33     private EJBQLTranslationContext context;
34     private int itemCount;
35
36     EJBQLOrderByTranslator(EJBQLTranslationContext context) {
37         this.context = context;
38     }
39
40     public boolean visitOrderByItem(EJBQLExpression expression) {
41         if (itemCount++ > 0) {
42             context.append(',');
43         }
44
45         return true;
46     }
47
48     public boolean visitDescending(EJBQLExpression expression) {
49         context.append(" DESC");
50         return true;
51     }
52
53     public boolean visitPath(EJBQLPath expression, int finishedChildIndex) {
54
55         EJBQLExpressionVisitor childVisitor = new EJBQLPathTranslator(context) {
56
57             protected void appendMultiColumnPath(EJBQLMultiColumnOperand operand) {
58                 throw new EJBQLException("Can't order on multi-column paths or objects");
59             }
60         };
61         expression.visit(childVisitor);
62         return false;
63     }
64 }
65
Popular Tags