1 33 34 package com.internetcds.jdbc.tds; 35 36 import java.sql.*; 37 import java.util.StringTokenizer ; 38 40 public class ParameterUtils 41 { 42 public static final String cvsVersion = "$Id: ParameterUtils.java,v 1.1 2006/06/23 10:39:04 sinisa Exp $"; 43 44 45 51 public static int countParameters(String sql) 52 throws java.sql.SQLException 53 { 54 58 int result = 0; 59 60 if (sql == null) 61 { 62 throw new SQLException("No statement"); 63 } 64 else 65 { 66 StringTokenizer st = new StringTokenizer (sql, "'?\\", true); 67 final int normal = 1; 68 final int inString = 2; 69 final int inEscape = 3; 70 int state = normal; 71 String 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 } 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 } 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 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 163 public static void createParameterMapping( 164 String rawQueryString, 165 ParameterListItem[] parameterList, 166 Tds tds) 167 throws SQLException 168 { 169 170 int i; 171 String 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 value = (String )parameterList[i].value; 192 if (value == null) 193 { 194 parameterList[i].formalType = "varchar(255)"; 196 parameterList[i].maxLength = 255; 197 198 } 199 else if (tdsVer == Tds.TDS70) 200 { 201 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 case java.sql.Types.DECIMAL: 273 { 274 parameterList[i].formalType = "real"; 275 break; 276 } 277 case java.sql.Types.DOUBLE: 278 { 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 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.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 |