1 33 34 package com.internetcds.jdbc.tds; 35 36 import java.sql.*; 37 import java.util.StringTokenizer ; 38 import java.util.Vector ; 39 40 41 public class Procedure 42 { 43 public static final String cvsVersion = "$Id: Procedure.java,v 1.1 2006/06/23 10:39:30 sinisa Exp $"; 44 45 46 ParameterListItem parameterList[] = null; 47 String sqlProcedureName = null; 48 String rawQueryString = null; 49 String procedureString = ""; 50 51 private Tds tds = null; 52 private boolean hasLobs = false; 53 54 private void init(String rawSql_, 55 String 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 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 ParameterUtils.verifyThatParametersAreSet(parameterList); 77 78 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 { 90 procedureString = "create proc " + sqlProcedureName; 91 92 ParameterUtils.createParameterMapping(rawQueryString, parameterList, 94 tds); 95 96 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 procedureString = procedureString 111 + createFormalParameterList(parameterList); 112 113 procedureString = procedureString + " as "; 114 115 118 StringTokenizer st = new StringTokenizer (rawQueryString, 119 "'?\\", true); 120 final int normal = 1; 121 final int inString = 2; 122 final int inEscape = 3; 123 int state = normal; 124 String 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 } 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 rawSql_, 185 String 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 getPreparedSqlString() 197 { 198 return procedureString; 199 } 200 201 public String getProcedureName() 202 { 203 return sqlProcedureName; 204 } 205 206 public ParameterListItem getParameter(int index) 207 { 208 return parameterList[index]; 209 } 210 211 private String createFormalParameterList( 212 ParameterListItem[] parameterList) 213 { 214 int i; 215 String 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 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 286 public boolean hasLobParameters() {return hasLobs;} 287 } 288 | Popular Tags |