KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > jpa > JpaNativeQuery


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.jpa;
21
22 import javax.persistence.Query;
23
24 import org.apache.cayenne.ObjectContext;
25 import org.apache.cayenne.map.DataMap;
26 import org.apache.cayenne.query.SQLTemplate;
27
28 public class JpaNativeQuery extends JpaQuery {
29
30     private static final String JavaDoc POSITIONAL_PARAM_PREFIX = "positional_";
31
32     public JpaNativeQuery(ObjectContext context, String JavaDoc sqlString, Class JavaDoc resultClass) {
33         super(context);
34         setQuery(new SQLTemplate(resultClass, processSQLString(sqlString)));
35     }
36
37     public JpaNativeQuery(ObjectContext context, String JavaDoc sqlString, String JavaDoc dataMapName) {
38         super(context);
39         DataMap map = context.getEntityResolver().getDataMap(dataMapName);
40         setQuery(new SQLTemplate(map, processSQLString(sqlString)));
41     }
42
43     protected String JavaDoc processSQLString(String JavaDoc sqlString) {
44         // named parameters are like ":parametername" and positional parameters
45
// are like "?123". SQLTemplate support "$parametername"
46

47         // TODO: improve convert as ':' could be used in sql. e.x. in
48
// non-parametrized parameter or postgresql cast.
49

50         sqlString = sqlString.replace(':', '$');
51
52         // handle positional parameters like named
53
if (sqlString.indexOf('?') >= 0) {
54             // convert "?123" to "$positional_123"
55
sqlString = sqlString.replaceAll("\\?([0-9]+)", "\\$"
56                     + POSITIONAL_PARAM_PREFIX
57                     + "$1");
58         }
59
60         return sqlString;
61     }
62
63     /**
64      * Bind an argument to a positional parameter.
65      *
66      * @param position
67      * @param value
68      * @return the same query instance
69      * @throws IllegalArgumentException if position does not correspond to positional
70      * parameter of query or argument is of incorrect type
71      */

72     public Query setParameter(int position, Object JavaDoc value) {
73
74         // Positional parameters are designated by the question
75
// mark(?) prefix followed by an integer. For example: ?1.
76
String JavaDoc name = POSITIONAL_PARAM_PREFIX + Integer.toString(position);
77         try {
78             return setParameter(name, value);
79         }
80         catch (IllegalArgumentException JavaDoc e) {
81             throw new IllegalArgumentException JavaDoc("Invalid positional parameter: "
82                     + position, e);
83         }
84     }
85
86 }
87
Popular Tags