KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > controls > system > jdbc > parser > SqlSubstitutionFragment


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

18
19 package org.apache.beehive.controls.system.jdbc.parser;
20
21 import org.apache.beehive.controls.api.context.ControlBeanContext;
22 import org.apache.beehive.controls.system.jdbc.TypeMappingsFactory;
23
24 import java.lang.reflect.Method JavaDoc;
25
26 /**
27  * Represents a fragement from the SQL annotation's statement member which begins with '{sql:'.
28  * Substitution fragements are unique in that they are fully evaluated BEFORE a PreparedStatement
29  * is generated.
30  * <p/>
31  * Supported 'sql:' escapes are subst and fn. subst is the default mode, and will be used if 'sql: '
32  * is specified.
33  *
34  * The <tt>fn</tt> variant of this construct has a very ridgid syntax at this point. It must conform to:
35  *
36  * <pre>
37  * {sql:fn in(x,{y})}
38  * </pre>
39  *
40  * where the '{y}' could also be some literal term.
41  */

42 public class SqlSubstitutionFragment extends SqlFragmentContainer {
43
44     /**
45      * Constructor for subst or function with no param substitution
46      *
47      * @param child An child which is contained in this fragment.
48      */

49     SqlSubstitutionFragment(SqlFragment child) {
50         super();
51         addChild(child);
52     }
53
54     /**
55      * Constructor for a function which includes a ReflectionFragment
56      *
57      * @param lf A LiteralFragment which contains the text up to the parameter substitution.
58      * @param rf The ReflectionFragment containing the parameter substitution
59      * @param lff A LiteralFragment which contains any text which occures after the parameter substitution.
60      */

61     SqlSubstitutionFragment(LiteralFragment lf, ReflectionFragment rf, LiteralFragment lff) {
62         super();
63         addChild(lf);
64         addChild(rf);
65         addChild(lff);
66     }
67
68     /**
69      * Always true for this fragment type
70      * @return true
71      */

72     boolean isDynamicFragment() { return true; }
73
74     /**
75      * Always false for this fragment type, since all param values are resolved
76      * before the prepared statement is created.
77      * @return false
78      */

79     boolean hasParamValue() { return false; }
80
81     /**
82      * Return the text for a PreparedStatement from this fragment.
83      *
84      * @param context A ControlBeanContext instance
85      * @param m The annotated method
86      * @param args The method parameters
87      * @return A String containing the value of this fragment and its children
88      */

89     String JavaDoc getPreparedStatementText(ControlBeanContext context, Method JavaDoc m, Object JavaDoc[] args) {
90
91         StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
92         for (SqlFragment frag : _children) {
93             if (frag.hasParamValue()) {
94                 Object JavaDoc[] pValues = frag.getParameterValues(context, m, args);
95                 for (Object JavaDoc o : pValues) {
96                     sb.append(processSqlParams(o));
97                 }
98             } else {
99                 sb.append(frag.getPreparedStatementText(context, m, args));
100             }
101         }
102         return sb.toString();
103     }
104
105
106 // ////////////////////////////////////////////// Private Methods //////////////////////////////////////////////
107

108
109     /**
110      * Check for the cases of a null or array type param value. If array type build a string of the array values
111      * seperated by commas.
112      *
113      * @param value
114      * @return
115      */

116     private String JavaDoc processSqlParams(Object JavaDoc value) {
117
118         Object JavaDoc[] arr = null;
119         if (value != null) {
120             arr = TypeMappingsFactory.toObjectArray(value);
121         }
122
123         if (value == null || (arr != null && arr.length == 0)) {
124             return "";
125         } else if (arr != null) {
126             StringBuilder JavaDoc result = new StringBuilder JavaDoc();
127             for (int i = 0; i < arr.length; i++) {
128                 if (i > 0) {
129                     result.append(',');
130                     result.append(arr[i].toString());
131                 } else {
132                     result.append(arr[i].toString());
133                 }
134             }
135             return result.toString();
136         } else {
137             return value.toString();
138         }
139     }
140 }
141
Popular Tags