KickJava   Java API By Example, From Geeks To Geeks.

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


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;
39

40 public class ParameterUtils
41 {
42    public static final String JavaDoc cvsVersion = "$Id: ParameterUtils.java,v 1.1 2006/06/23 10:39:04 sinisa Exp $";
43
44
45    /**
46     * Count the number of parameters in a prepared sql statement.
47     *
48     * @return number of parameter placeholders in sql statement.
49     * @exception SQLException thrown if there is a problem parsing statment.
50     */

51    public static int countParameters(String JavaDoc sql)
52       throws java.sql.SQLException JavaDoc
53    {
54       //
55
// This is method is implemented as a very simple finite state machine.
56
//
57

58       int result = 0;
59
60       if (sql == null)
61       {
62          throw new SQLException("No statement");
63       }
64       else
65       {
66          StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(sql, "'?\\", true);
67          final int normal = 1;
68          final int inString = 2;
69          final int inEscape = 3;
70          int state = normal;
71          String JavaDoc current;
72
73          while (st.hasMoreTokens())
74          {
75             current = st.nextToken();
76             switch (state)
77             {
78                case normal:
79                {
80                   if (current.equals("?"))
81                   {
82                      result++;
83                   }
84                   else if (current.equals("'"))
85                   {
86                      state = inString;
87                   }
88                   else
89                   {
90                      // nop
91
}
92                   break;
93                }
94                case inString:
95                {
96                   if (current.equals("'"))
97                   {
98                      state = normal;
99                   }
100                   else if (current.equals("\\"))
101                   {
102                      state = inEscape;
103                   }
104                   else
105                   {
106                      // nop
107
}
108                   break;
109                }
110                case inEscape:
111                {
112                   state = inString;
113                   break;
114                }
115                default:
116                {
117                   throw new SQLException("Internal error. Bad State " + state);
118                }
119             }
120          }
121       }
122
123       return result;
124    }
125
126    /**
127     * check that all items in parameterList have been given a value
128     *
129     * @exception SQLException thrown if one or more parameters aren't set
130     */

131    public static void verifyThatParametersAreSet(
132       ParameterListItem[] parameterList)
133       throws SQLException
134    {
135       
136       int i;
137       boolean okay = true;
138
139       for(i=0; okay && i<parameterList.length; i++)
140       {
141          okay = okay && parameterList[i].isSet;
142          if (!okay)
143          {
144             throw new SQLException("parameter #" + (i+1)
145                                    + " has not been set");
146          }
147       }
148       
149    }
150
151    /**
152     * create the formal parameters for a parameter list.
153     *
154     * This method takes a sql string and a parameter list containing
155     * the actual parameters and creates the formal parameters for
156     * a stored procedure. The formal parameters will later be used
157     * when the stored procedure is submitted for creation on the server.
158     *
159     * @param rawQueryString (in-only)
160     * @param parameterList (update)
161     *
162     */

163    public static void createParameterMapping(
164       String JavaDoc rawQueryString,
165       ParameterListItem[] parameterList,
166       Tds tds)
167       throws SQLException
168    {
169       
170       int i;
171       String JavaDoc nextFormal;
172       int nextParameterNumber = 0;
173       int tdsVer = tds.getTdsVer();
174       EncodingHelper encoder = tds.getEncoder();
175
176       for(i=0; i<parameterList.length; i++)
177       {
178          do
179          {
180             nextParameterNumber++;
181             nextFormal = "P" + nextParameterNumber;
182          } while (-1 != rawQueryString.indexOf(nextFormal));
183
184          parameterList[i].formalName = nextFormal;
185
186          switch (parameterList[i].type)
187          {
188             case java.sql.Types.VARCHAR:
189             case java.sql.Types.CHAR:
190             {
191                String JavaDoc value = (String JavaDoc)parameterList[i].value;
192                if (value == null)
193                {
194                    // use the smalles case possible for nulls
195
parameterList[i].formalType = "varchar(255)";
196                    parameterList[i].maxLength = 255;
197
198                }
199                else if (tdsVer == Tds.TDS70)
200                {
201                    /*
202                     * SQL Server 7 can handle Unicode so use it wherever
203                     * possible
204                     */

205
206                    if (value.length() < 4001)
207                    {
208                        parameterList[i].formalType = "nvarchar(4000)";
209                        parameterList[i].maxLength = 4000;
210                    }
211                    else if (value.length() < 8001
212                               && !encoder.isDBCS()
213                               && encoder.canBeConverted(value))
214                    {
215                        parameterList[i].formalType = "varchar(8000)";
216                        parameterList[i].maxLength = 8000;
217                    }
218                    else
219                    {
220                        parameterList[i].formalType = "ntext";
221                        parameterList[i].maxLength = Integer.MAX_VALUE;
222                    }
223                }
224                else
225                {
226                    int len = value.length();
227                    if (encoder.isDBCS() && len > 127 && len < 256)
228                    {
229                        len = encoder.getBytes(value).length;
230                    }
231                    
232                    if (len < 256)
233                    {
234                        parameterList[i].formalType = "varchar(255)";
235                        parameterList[i].maxLength = 255;
236                    }
237                    else
238                    {
239                        parameterList[i].formalType = "text";
240                        parameterList[i].maxLength = Integer.MAX_VALUE;
241                    }
242                }
243                break;
244             }
245             case java.sql.Types.LONGVARCHAR:
246             {
247                if (tdsVer == Tds.TDS70)
248                {
249                    parameterList[i].formalType = "ntext";
250                }
251                else
252                {
253                    parameterList[i].formalType = "text";
254                }
255                parameterList[i].maxLength = Integer.MAX_VALUE;
256                break;
257             }
258             case java.sql.Types.INTEGER:
259             case java.sql.Types.SMALLINT:
260             case java.sql.Types.BIGINT:
261             {
262                parameterList[i].formalType = "integer";
263                break;
264             }
265             case java.sql.Types.REAL:
266             {
267                parameterList[i].formalType = "real";
268                break;
269             }
270 //sinisa
271
//implement DECIMAL as formal type real for procedure parameters
272
case java.sql.Types.DECIMAL:
273             {
274                parameterList[i].formalType = "real";
275                break;
276             }
277             case java.sql.Types.DOUBLE:
278 // case java.sql.Types.FLOAT:
279
// case java.sql.Types.DECIMAL:
280
{
281                parameterList[i].formalType = "float";
282                break;
283             }
284             case java.sql.Types.TIMESTAMP:
285             case java.sql.Types.DATE:
286             case java.sql.Types.TIME:
287             {
288                parameterList[i].formalType = "datetime";
289                break;
290             }
291             case java.sql.Types.LONGVARBINARY:
292             case java.sql.Types.VARBINARY:
293             {
294                parameterList[i].formalType = "image";
295                break;
296             }
297             //Dusan
298
case java.sql.Types.BIT:
299             {
300                parameterList[i].formalType = "bit";
301                break;
302             }
303             case java.sql.Types.BINARY:
304             //case java.sql.Types.BIT:
305
//Sinisa
306
//java.sql.Types.DECIMAL implemented
307
//case java.sql.Types.DECIMAL:
308
case java.sql.Types.FLOAT:
309             case java.sql.Types.NULL:
310             case java.sql.Types.NUMERIC:
311             case java.sql.Types.OTHER:
312             case java.sql.Types.TINYINT:
313             {
314                throw new SQLException("Not implemented (type is ("
315                                       + TdsUtil.javaSqlTypeToString(parameterList[i].type) + ")");
316             }
317             default:
318             {
319                throw new SQLException("Internal error. Unrecognized type "
320                                       + parameterList[i].type);
321             }
322          }
323       }
324    }
325    
326 }
327
328
Popular Tags