1 /***
2 * License Agreement.
3 *
4 * JSPA (POJO-SP)
5 *
6 * Copyright (C) 2009 HRX Pty Ltd
7 *
8 * This file is part of JSPA.
9 *
10 * JSPA is free software: you can redistribute it and/or modify it under the
11 * terms of the GNU Lesser General Public License version 3 as published by the
12 * Free Software Foundation.
13 *
14 * JSPA is distributed in the hope that it will be useful, but WITHOUT ANY
15 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 * A PARTICULAR PURPOSE. See the Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with JSPA. If not, see <http://www.gnu.org/licenses/>.
20 */
21 package com.hrx.rasp.util.dao;
22
23 import org.apache.log4j.Logger;
24
25 /***
26 * @author dan.stoica <dan.stoica@acslink.net.au>
27 *
28 */
29 /***
30 * the CallBuilder class is responsible for building the pl/sql commands to be
31 * sent to the db
32 *
33 */
34 public final class ExpressionBuilder
35 {
36
37 private final static Logger log = Logger.getLogger(ExpressionBuilder.class);
38
39 /***
40 * Description for getFind method
41 *
42 * @param storedProc
43 * the name of the stored procedure
44 * @param argx
45 * the number of arguments for the find operation
46 * @return java.lang.String
47 */
48 public static String getFind(String storedProc, int argc)
49 {
50
51
52
53
54
55 String call = buildCall(storedProc, false, 2, argc);
56
57 return call;
58 }
59
60 /***
61 * Description for getSelect method
62 *
63 * @param storedProc
64 * the name of the stored procedure
65 * @param argc
66 * the number of arguments
67 * @return java.lang.String
68 */
69 public static String getSelect(String _storedProc, int argc)
70 {
71
72
73
74
75
76 String call = buildCall(_storedProc, true, 2, argc);
77
78 return call;
79 }
80
81 /***
82 * Description for getInsert method
83 *
84 * @param _storedProc
85 * the stored procedure to be called
86 * @param inParameters,
87 * used as IN arguments for the insert statement
88 * @param pkParameters,
89 * used as OUT arguments for the insert statement
90 * @return String
91 */
92 public static String getInsert(final String storedProc, int params)
93 {
94
95
96
97
98
99 return buildCall(storedProc, false, 2, params);
100 }
101
102 /***
103 * Description for getUpdate method
104 *
105 * @param storedProc
106 * String
107 * @param inParameters
108 * Object[]
109 * @return java.lang.String
110 */
111 public static String getUpdate(final String storedProc, int inParameters)
112 {
113
114
115
116
117
118 return buildCall(storedProc, false, 2, inParameters);
119 }
120
121 public static String getFunctionCall(final String function, int argc)
122 {
123
124
125
126
127
128
129
130 return buildCall(function, argc - 1, true);
131 }
132
133 public static String getStoredProcedureCall(String storedProc, int argc)
134 {
135
136
137
138
139
140
141 return buildCall(storedProc, argc, false);
142 }
143
144 /***
145 * Description for buildCall method
146 *
147 * @param storedProc
148 * The stored proc name
149 * @param parametersIN
150 * The IN arguments of the stored proc
151 * @param parametersOUT
152 * The OUT arguments of the stored proc
153 * @param isFunction
154 * Is the call a function or a procedure
155 * @return java.lang.String
156 */
157 protected static String buildCall(String storedProc, boolean isFunction, int... args)
158 {
159 int count = 0;
160 for (int arg : args)
161 {
162 count = count + Math.max(0, arg);
163 }
164 return buildCall(storedProc, count, isFunction);
165 }
166
167 /***
168 * Description for buildCall method
169 *
170 * @param storedProc
171 * The stored proc name
172 * @param args
173 * The number of arguments of the stored proc
174 * @param isFunction
175 * Is the call a function or a procedure
176 * @return java.lang.String
177 */
178 protected static String buildCall(String storedProc, int argc, boolean isFunction)
179 {
180 StringBuffer buffer = new StringBuffer();
181
182 buffer.append("{");
183
184 if (isFunction) buffer.append("? = ");
185
186 buffer.append("call ");
187 buffer.append(storedProc);
188 buffer.append("(");
189
190 if (argc > 0)
191 {
192 for (int i = 0; i < argc; ++i)
193 {
194 buffer.append("?");
195
196 if (i < argc - 1) buffer.append(", ");
197 }
198 }
199
200 buffer.append(")}");
201
202 String call = buffer.toString();
203
204 if (log.isDebugEnabled())
205 {
206 log.debug("DAO: JDBC Statement is: " + call);
207 }
208 return call;
209 }
210 }