KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > internetcds > jdbc > tds > Procedure


1 //
2
// Copyright 1998 CDS Networks, Inc., Medford Oregon
3
//
4
// All rights reserved.
5
//
6
// Redistribution and use in source and binary forms, with or without
7
// modification, are permitted provided that the following conditions are met:
8
// 1. Redistributions of source code must retain the above copyright
9
// notice, this list of conditions and the following disclaimer.
10
// 2. Redistributions in binary form must reproduce the above copyright
11
// notice, this list of conditions and the following disclaimer in the
12
// documentation and/or other materials provided with the distribution.
13
// 3. All advertising materials mentioning features or use of this software
14
// must display the following acknowledgement:
15
// This product includes software developed by CDS Networks, Inc.
16
// 4. The name of CDS Networks, Inc. may not be used to endorse or promote
17
// products derived from this software without specific prior
18
// written permission.
19
//
20
// THIS SOFTWARE IS PROVIDED BY CDS NETWORKS, INC. ``AS IS'' AND
21
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
// ARE DISCLAIMED. IN NO EVENT SHALL CDS NETWORKS, INC. BE LIABLE
24
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26
// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30
// SUCH DAMAGE.
31
//
32

33
34 package com.internetcds.jdbc.tds;
35
36 import java.sql.*;
37 import java.util.StringTokenizer JavaDoc;
38 import java.util.Vector JavaDoc;
39
40
41 public class Procedure
42 {
43    public static final String JavaDoc cvsVersion = "$Id: Procedure.java,v 1.1 2006/06/23 10:39:30 sinisa Exp $";
44
45
46    ParameterListItem parameterList[] = null;
47    String JavaDoc sqlProcedureName = null;
48    String JavaDoc rawQueryString = null;
49    String JavaDoc procedureString = "";
50    
51    private Tds tds = null;
52    private boolean hasLobs = false;
53
54    private void init(String JavaDoc rawSql_,
55                 String JavaDoc sqlProcedureName_,
56                 ParameterListItem[] parameterList_,
57                 Tds tds_)
58       throws SQLException
59    {
60       int i;
61       
62       
63       rawQueryString = rawSql_;
64       sqlProcedureName = sqlProcedureName_;
65       tds = tds_;
66
67       // need a clone to keep the maxlength information for char data
68
parameterList = new ParameterListItem[parameterList_.length];
69       for (i=0; i<parameterList.length; i++)
70       {
71          parameterList[i] = (ParameterListItem)parameterList_[i].clone();
72       }
73
74
75       // make sure all the parameters are set
76
ParameterUtils.verifyThatParametersAreSet(parameterList);
77       
78       // make sure we have the same number of placeholders as
79
// item in the parameter list.
80
if (parameterList.length !=
81           ParameterUtils.countParameters(rawQueryString))
82       {
83          throw new SQLException("Number of parametrs in sql statement "
84                                 + "does not match the number of parameters "
85                                 + " in parameter list");
86       }
87
88       // Create the procedure text
89
{
90          procedureString = "create proc " + sqlProcedureName;
91       
92          // Create actual to formal parameter mapping
93
ParameterUtils.createParameterMapping(rawQueryString, parameterList,
94                                                tds);
95       
96          // copy back the formal types and check for LOBs
97
for (i=0; i<parameterList.length; i++)
98          {
99             parameterList_[i].formalType = parameterList[i].formalType;
100
101             if (parameterList[i].formalType.equalsIgnoreCase("image") ||
102                 parameterList[i].formalType.equalsIgnoreCase("text") ||
103                 parameterList[i].formalType.equalsIgnoreCase("ntext"))
104             {
105                hasLobs = true;
106             }
107          }
108       
109          // add the formal parameter list to the sql string
110
procedureString = procedureString
111             + createFormalParameterList(parameterList);
112       
113          procedureString = procedureString + " as ";
114             
115          // Go through the raw sql and substitute a parameter name
116
// for each '?' placeholder in the raw sql
117

118          StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(rawQueryString,
119                                                     "'?\\", true);
120          final int normal = 1;
121          final int inString = 2;
122          final int inEscape = 3;
123          int state = normal;
124          String JavaDoc current;
125          int nextParameterIndex = 0;
126       
127          while (st.hasMoreTokens())
128          {
129             current = st.nextToken();
130             switch (state)
131             {
132                case normal:
133                {
134                   if (current.equals("?"))
135                   {
136                      procedureString = procedureString + "@"
137                         + parameterList[nextParameterIndex].formalName;
138                      nextParameterIndex++;
139                   }
140                   else if (current.equals("'"))
141                   {
142                      procedureString = procedureString + current;
143                      state = inString;
144                   }
145                   else
146                   {
147                      procedureString = procedureString + current;
148                   }
149                   break;
150                }
151                case inString:
152                {
153                   if (current.equals("'"))
154                   {
155                      state = normal;
156                   }
157                   else if (current.equals("\\"))
158                   {
159                      state = inEscape;
160                   }
161                   else
162                   {
163                      // nop
164
}
165                   procedureString = procedureString + current;
166                   break;
167                }
168                case inEscape:
169                {
170                   state = inString;
171                   procedureString = procedureString + current;
172                   break;
173                }
174                default:
175                {
176                   throw new SQLException("Internal error. Bad State " + state);
177                }
178             }
179          }
180       }
181    }
182
183    public Procedure(
184       String JavaDoc rawSql_,
185       String JavaDoc sqlProcedureName_,
186       ParameterListItem[] parameterList_,
187       Tds tds_)
188       throws SQLException
189    {
190       init(rawSql_,
191            sqlProcedureName_,
192            parameterList_,
193            tds_);
194    }
195    
196    public String JavaDoc getPreparedSqlString()
197    {
198       return procedureString;
199    }
200
201    public String JavaDoc getProcedureName()
202    {
203       return sqlProcedureName;
204    }
205
206    public ParameterListItem getParameter(int index)
207    {
208       return parameterList[index];
209    }
210
211    private String JavaDoc createFormalParameterList(
212       ParameterListItem[] parameterList)
213    {
214       int i;
215       String JavaDoc result;
216
217
218       if (parameterList.length == 0)
219       {
220          result = "";
221       }
222       else
223       {
224          result = "(";
225          for(i=0; i<parameterList.length; i++)
226          {
227             if (i>0)
228             {
229                result = result + ", ";
230             }
231             result =
232                result + "@" + parameterList[i].formalName + " "
233                + parameterList[i].formalType;
234          }
235          result = result + ")";
236       }
237       return result;
238    }
239
240    /**
241     * This method checks to see if the actual parameters are compatible
242     * with the formal parameters for this procedure.
243     *
244     */

245    public boolean compatibleParameters(
246       ParameterListItem actualParams[])
247       throws SQLException
248    {
249       int i;
250       boolean isOkay = true;
251
252       isOkay = parameterList.length == actualParams.length;
253
254       for(i=0; isOkay && i<actualParams.length; i++)
255       {
256           if ((parameterList[i].formalType.startsWith("char") ||
257                parameterList[i].formalType.startsWith("varchar") ||
258                parameterList[i].formalType.startsWith("text") ||
259                parameterList[i].formalType.startsWith("nchar") ||
260                parameterList[i].formalType.startsWith("nvarchar") ||
261                parameterList[i].formalType.startsWith("ntext"))
262               && (actualParams[i].type == java.sql.Types.CHAR ||
263                   actualParams[i].type == java.sql.Types.VARCHAR ||
264                   actualParams[i].type == java.sql.Types.LONGVARCHAR))
265           {
266              isOkay = parameterList[i].maxLength >= actualParams[i].maxLength;
267           }
268           else
269           {
270                isOkay =
271                    parameterList[i].formalType.equalsIgnoreCase(actualParams[i].formalType);
272           }
273       }
274       return isOkay;
275    }
276
277
278    public ParameterListItem[] getParameterList()
279    {
280       return parameterList;
281    }
282
283    /**
284     * Does the formal parameter list include LOB parameters?
285     */

286    public boolean hasLobParameters() {return hasLobs;}
287 }
288
Popular Tags