1 16 19 20 26 27 package org.apache.xalan.lib.sql; 28 29 import java.util.*; 30 import java.sql.*; 31 import org.apache.xpath.objects.*; 32 import org.apache.xalan.extensions.ExpressionContext; 33 import org.apache.xml.utils.QName; 34 import javax.xml.transform.TransformerException ; 35 36 37 38 public class SQLQueryParser 39 { 40 44 private boolean m_InlineVariables = false; 45 46 49 private boolean m_IsCallable = false; 50 51 54 private String m_OrigQuery = null; 55 56 59 private StringBuffer m_ParsedQuery = null; 60 61 64 private Vector m_Parameters = null; 65 66 69 private boolean m_hasOutput = false; 70 71 74 private boolean m_HasParameters; 75 76 public static final int NO_OVERRIDE = 0; 77 public static final int NO_INLINE_PARSER = 1; 78 public static final int INLINE_PARSER = 2; 79 80 86 public SQLQueryParser() 87 { 88 init(); 89 } 90 91 94 private SQLQueryParser(String query) 95 { 96 m_OrigQuery = query; 97 } 98 99 104 private void init() 105 { 106 } 108 109 121 public SQLQueryParser parse(XConnection xconn, String query, int override) 122 { 123 SQLQueryParser parser = new SQLQueryParser(query); 124 125 parser.parse(xconn, override); 128 129 return parser; 130 } 131 132 133 134 146 private void parse(XConnection xconn, int override) 147 { 148 152 m_InlineVariables = "true".equals(xconn.getFeature("inline-variables")); 153 if (override == NO_INLINE_PARSER) m_InlineVariables = false; 154 else if (override == INLINE_PARSER) m_InlineVariables = true; 155 156 if (m_InlineVariables) inlineParser(); 157 158 } 159 160 165 public boolean hasParameters() 166 { 167 return m_HasParameters; 168 } 169 170 180 public boolean isCallable() 181 { 182 return m_IsCallable; 183 } 184 185 186 189 public Vector getParameters() 190 { 191 return m_Parameters; 192 } 193 194 199 public void setParameters(Vector p) 200 { 201 m_HasParameters = true; 202 m_Parameters = p; 203 } 204 205 210 public String getSQLQuery() 211 { 212 if (m_InlineVariables) return m_ParsedQuery.toString(); 213 else return m_OrigQuery; 214 } 215 216 217 225 public void populateStatement(PreparedStatement stmt, ExpressionContext ctx) 226 { 227 230 for ( int indx = 0 ; indx < m_Parameters.size() ; indx++ ) 231 { 232 QueryParameter parm = (QueryParameter) m_Parameters.elementAt(indx); 233 234 try 235 { 236 237 if (m_InlineVariables) 238 { 239 XObject value = (XObject)ctx.getVariableOrParam(new QName(parm.getName())); 240 241 if (value != null) 242 { 243 stmt.setObject( 244 indx + 1, 245 value.object(), 246 parm.getType(), 4); } 248 else 249 { 250 stmt.setNull(indx + 1, parm.getType()); 251 } 252 } 253 else 254 { 255 String value = parm.getValue(); 256 257 if (value != null) 258 { 259 stmt.setObject( 260 indx + 1, 261 value, 262 parm.getType(), 4); } 264 else 265 { 266 stmt.setNull(indx + 1, parm.getType()); 267 } 268 } 269 } 270 catch (Exception tx) 271 { 272 } 274 } 275 276 } 277 278 public void registerOutputParameters(CallableStatement cstmt) throws SQLException 279 { 280 if ( m_IsCallable && m_hasOutput ) 282 { 283 for ( int indx = 0 ; indx < m_Parameters.size() ; indx++ ) 284 { 285 QueryParameter parm = (QueryParameter) m_Parameters.elementAt(indx); 286 if ( parm.isOutput() ) 287 { 288 cstmt.registerOutParameter(indx + 1, parm.getType()); 290 } 291 } 292 } 293 } 294 295 298 protected void inlineParser() 299 { 300 QueryParameter curParm = null; 301 int state = 0; 302 StringBuffer tok = new StringBuffer (); 303 boolean firstword = true; 304 305 if (m_Parameters == null) m_Parameters = new Vector(); 306 307 if (m_ParsedQuery == null) m_ParsedQuery = new StringBuffer (); 308 309 for ( int idx = 0 ; idx < m_OrigQuery.length() ; idx++ ) 310 { 311 char ch = m_OrigQuery.charAt(idx); 312 switch ( state ) 313 { 314 315 case 0: if ( ch == '\'' ) state = 1; 317 else if ( ch == '?' ) state = 4; 318 else if ( firstword && (Character.isLetterOrDigit(ch) || ch == '#') ) 319 { 320 tok.append(ch); 321 state = 3; 322 } 323 m_ParsedQuery.append(ch); 324 break; 325 326 case 1: if ( ch == '\'' ) state = 0; 328 else if ( ch == '\\' ) state = 2; 329 m_ParsedQuery.append(ch); 330 break; 331 332 case 2: state = 1; 334 m_ParsedQuery.append(ch); 335 break; 336 337 case 3: if ( Character.isLetterOrDigit(ch) || ch == '#' || ch == '_' ) tok.append(ch); 339 else 340 { 341 if ( tok.toString().equalsIgnoreCase("call") ) 342 { 343 m_IsCallable = true; 344 if ( curParm != null ) 345 { 346 curParm.setIsOutput(true); 348 } 350 } 351 firstword = false; 352 tok = new StringBuffer (); 353 if ( ch == '\'' ) state = 1; 354 else if ( ch == '?' ) state = 4; 355 else state = 0; 356 } 357 358 m_ParsedQuery.append(ch); 359 break; 360 361 case 4: if ( ch == '[' ) state = 5; 363 break; 364 365 case 5: if ( !Character.isWhitespace(ch) && ch != '=' ) 367 { 368 tok.append(Character.toUpperCase(ch)); 369 } 370 else if ( tok.length() > 0 ) 371 { 372 m_HasParameters = true; 374 375 curParm = new QueryParameter(); 376 377 curParm.setTypeName(tok.toString()); 378 m_Parameters.addElement(curParm); 380 tok = new StringBuffer (); 381 if ( ch == '=' ) state = 7; 382 else state = 6; 383 } 384 break; 385 386 case 6: if ( ch == '=' ) state = 7; 388 break; 389 390 case 7: if ( !Character.isWhitespace(ch) && ch != ']' ) tok.append(ch); 392 else if ( tok.length() > 0 ) 393 { 394 curParm.setName(tok.toString()); 395 tok = new StringBuffer (); 396 if ( ch == ']' ) 397 { 398 state = 0; 400 } 401 else state = 8; 402 } 403 break; 404 405 case 8: if ( !Character.isWhitespace(ch) && ch != ']' ) 407 { 408 tok.append(ch); 409 } 410 else if ( tok.length() > 0 ) 411 { 412 tok.setLength(3); 413 if ( tok.toString().equalsIgnoreCase("OUT") ) 414 { 415 curParm.setIsOutput(true); 416 m_hasOutput = true; 417 } 418 419 tok = new StringBuffer (); 420 if ( ch == ']' ) 421 { 422 state = 0; 423 } 424 } 425 break; 426 } 427 } 428 429 430 if ( m_IsCallable ) 432 { 433 m_ParsedQuery.insert(0, '{'); 434 m_ParsedQuery.append('}'); 435 } 436 437 } 438 439 } 440 441 | Popular Tags |