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.openjpa.kernel.jpql;
20
21 import org.apache.openjpa.kernel.ExpressionStoreQuery;
22 import org.apache.openjpa.kernel.exps.ExpressionFactory;
23 import org.apache.openjpa.kernel.exps.ExpressionParser;
24 import org.apache.openjpa.kernel.exps.QueryExpressions;
25 import org.apache.openjpa.kernel.exps.Value;
26 import org.apache.openjpa.lib.util.Localizer;
27 import org.apache.openjpa.meta.ClassMetaData;
28 import org.apache.openjpa.util.OpenJPAException;
29 import org.apache.openjpa.util.UserException;
30
31 /**
32 * Parser for JPQL queries.
33 *
34 * @author Marc Prud'hommeaux
35 * @nojavadoc
36 */
37 public class JPQLParser
38 implements ExpressionParser {
39
40 private static final Localizer _loc =
41 Localizer.forPackage(JPQLParser.class);
42 public static final String LANG_JPQL = "javax.persistence.JPQL";
43
44 public Object parse(String ql, ExpressionStoreQuery query) {
45 if (query.getContext().getParameterDeclaration() != null)
46 throw new UserException(_loc.get("param-decs-invalid"));
47
48 return new JPQLExpressionBuilder.ParsedJPQL(ql);
49 }
50
51 public void populate(Object parsed, ExpressionStoreQuery query) {
52 if (!(parsed instanceof JPQLExpressionBuilder.ParsedJPQL))
53 throw new ClassCastException(parsed == null ? null + ""
54 : parsed.getClass().getName());
55
56 ((JPQLExpressionBuilder.ParsedJPQL) parsed).populate(query);
57 }
58
59 public QueryExpressions eval(Object parsed, ExpressionStoreQuery query,
60 ExpressionFactory factory, ClassMetaData candidate) {
61 try {
62 return new JPQLExpressionBuilder(factory, query, parsed).
63 getQueryExpressions();
64 } catch (OpenJPAException ke) {
65 throw ke;
66 } catch (Exception e) {
67 throw new UserException(_loc.get("bad-jpql", parsed), e);
68 }
69 }
70
71 public Value[] eval(String[] vals, ExpressionStoreQuery query,
72 ExpressionFactory factory, ClassMetaData candidate) {
73 return null;
74 }
75
76 public String getLanguage() {
77 return JPQLParser.LANG_JPQL;
78 }
79 }