1 12 package org.openbravo.data; 13 14 import java.util.StringTokenizer ; 15 import java.util.Vector ; 16 import java.util.Enumeration ; 17 import java.util.Stack ; 18 import java.util.Properties ; 19 import java.sql.*; 20 21 import java.io.PrintWriter ; 22 import java.io.IOException ; 23 import java.io.FileReader ; 24 import java.io.FileWriter ; 25 import java.io.File ; 26 import java.io.FileInputStream ; 27 import java.io.OutputStreamWriter ; 28 import java.io.FileOutputStream ; 29 30 import org.xml.sax.Attributes ; 31 import org.xml.sax.helpers.DefaultHandler ; 32 import org.xml.sax.InputSource ; 33 import org.xml.sax.XMLReader ; 34 import org.xml.sax.SAXException ; 35 import org.apache.xerces.parsers.*; 36 37 import org.apache.log4j.Logger; 38 import org.apache.log4j.PropertyConfigurator; 39 40 41 45 public class Sqlc extends DefaultHandler { 46 public static final String VERSION = "V1.O00-1"; 47 String sqlcName; 48 String sqlcPackage = null; 49 Stack <String > stcElement; String strElement; 51 String strDriver; 52 String strURL; 53 String strDBUser; 54 String strDBPassword; 55 String javaDateFormat; 56 static String javaFileName; 57 Connection connection; 58 Sql sql; 59 String strComments; 60 PreparedStatement preparedStatement; 61 ResultSet result; 62 ResultSetMetaData rsmd; 63 int numCols; 64 boolean first = true; 65 OutputStreamWriter out; 66 PrintWriter printWriterTxt; 67 Parameter parameterSql; 68 boolean error; 69 boolean writeTxtFiles = false; 70 StringBuffer buffer; 71 72 static Logger log4j = Logger.getLogger(Sqlc.class); 74 public Sqlc () { 75 init(); 76 } 77 78 public void init() { 79 sql = new Sql(); 80 stcElement = new Stack <String >(); 81 first = true; 82 sqlcPackage = null; 83 } 84 85 public static void main(String argv[]) throws Exception { 86 PropertyConfigurator.configure("log4j.lcf"); 87 String dirIni; 88 String dirFin; 89 boolean boolFilter; 90 String strFilter; 91 DirFilter dirFilter = null; 92 93 if (argv.length < 1) { 94 log4j.error("Usage: java org.openbravo.data.Sqlc connection.xml [fileTermination [sourceDir destinyDir [write_txt_files]]]"); 95 return; 96 } 97 98 Sqlc sqlc = new Sqlc(); 99 XMLReader parser; 100 parser = new SAXParser(); 101 if (argv.length <= 4) sqlc.writeTxtFiles = false; 102 else sqlc.writeTxtFiles = argv[4].equalsIgnoreCase("true"); 103 parser.setContentHandler(sqlc); 104 String strFileConnection = argv[0]; 105 sqlc.readProperties(strFileConnection.replace("/dbCon5.xml","/Openbravo.properties")); 106 107 if(argv.length <= 2) 109 dirIni = "."; 110 else 111 dirIni = argv[2]; 112 113 if(argv.length <= 3) 114 dirFin = dirIni; 115 else 116 dirFin = argv[3]; 117 118 boolFilter = true; if(argv.length <= 1) 122 strFilter = ".xml"; 123 else 124 strFilter = argv[1]; 125 dirFilter = new DirFilter(strFilter); 126 log4j.info("directory source: " + dirIni); 127 log4j.info("directory destiny: " + dirFin); 128 log4j.info("file termination: " + strFilter); 129 log4j.info("file connection: " + strFileConnection); 130 log4j.info("Write TXT Files: " + sqlc.writeTxtFiles); 131 try { 132 parser.parse(new InputSource (new FileReader (new File (strFileConnection)))); 134 try { 135 sqlc.connect(); 136 } catch(ClassNotFoundException e){ 137 log4j.error("Class " + sqlc.strDriver + " not found: " + e); 138 return; 139 } catch(SQLException e){ 140 log4j.error("Error in URL: "+ sqlc.strURL + " of connection: " + e); 141 return; 142 } 143 144 } catch (IOException e) { 145 e.printStackTrace(); 146 } catch (SAXException e) { 147 e.printStackTrace(); 148 } catch (Exception e) { 149 e.printStackTrace(); 150 } 151 152 File path = new File (dirIni); 153 if (!path.exists()) { 154 log4j.error("Directory does not exist: " + dirIni); 155 return; 156 } 157 File fileFin = new File (dirFin); 158 if (!fileFin.exists()) { 159 log4j.error("Directory does not exist: " + dirFin); 160 return; 161 } 162 listDir(path, boolFilter, dirFilter, sqlc, parser, strFilter, fileFin); 163 164 sqlc.closeConnection(); 165 } 166 167 public static void listDir(File file, boolean boolFilter, DirFilter dirFilter, 169 Sqlc sqlc, XMLReader parser, String strFilter, File fileFin) { 170 File [] list; 171 if(boolFilter) 172 list = file.listFiles(dirFilter); 173 else 174 list = file.listFiles(); 175 for(int i = 0; i < list.length; i++) { 176 File fileItem = list[i]; 177 if (fileItem.isDirectory()) { 178 listDir(fileItem, boolFilter, dirFilter, sqlc, parser, strFilter, fileFin); 180 } else { 181 try { 182 if (log4j.isDebugEnabled()) log4j.debug(list[i] + " Parent: " + fileItem.getParent() + " getName() " + fileItem.getName() + " canonical: " + fileItem.getCanonicalPath()); 183 parseSqlFile(list[i], sqlc, parser, strFilter, fileFin); 184 } catch (IOException e) { 185 log4j.error("IOException: " + e); 186 } 187 } 188 } 189 } 190 191 194 195 private static void parseSqlFile(File fileParsing, Sqlc sqlc, XMLReader parser, String strFilter, File fileFin) { 196 String strFileName = fileParsing.getName(); 197 if (log4j.isDebugEnabled()) log4j.debug("Parsing of " + strFileName); 198 sqlc.init(); 199 if (log4j.isDebugEnabled()) log4j.debug("new Sql"); 200 int pos = strFileName.indexOf(strFilter); 201 if (pos == -1) { 202 log4j.error("File " + strFileName + " don't have termination " + strFilter); 203 return; 204 } 205 String strFileWithoutTermination = strFileName.substring(0, pos); 206 if (log4j.isDebugEnabled()) log4j.debug("File without termination: " + strFileWithoutTermination); 207 if (strFileWithoutTermination.equalsIgnoreCase("SQLC")) { 208 log4j.error("Name Sqlc not allowed for a file"); 209 return; 210 } 211 try { 212 File dirJava = new File (fileFin, fileParsing.getParent()); 213 dirJava.mkdirs(); 214 javaFileName = TransformaNombreFichero(strFileWithoutTermination); 215 File fileJava = new File (dirJava, javaFileName + ".java"); 216 File fileTxt = null; 217 if (sqlc.writeTxtFiles) fileTxt = new File (fileParsing.getParent(), strFileWithoutTermination + ".txt"); 218 if ((!fileJava.exists())|| (fileParsing.lastModified() > fileJava.lastModified())) { 219 if (log4j.isDebugEnabled()) log4j.debug(" time file parsed: " + fileParsing.lastModified() + 220 " time file java: " + fileParsing.lastModified()); 221 FileOutputStream resultsFile = new FileOutputStream (fileJava); 222 sqlc.out = new OutputStreamWriter (resultsFile, "UTF-8"); 223 225 FileWriter resultsFileTxt = null; 226 if (sqlc.writeTxtFiles) { 227 resultsFileTxt = new FileWriter (fileTxt); 228 sqlc.printWriterTxt = new PrintWriter (resultsFileTxt); 229 } 230 log4j.info("File: " + fileParsing + " \tprocessed"); 231 java.util.Date date = new java.util.Date (); if (log4j.isDebugEnabled()) log4j.debug("Time: " + date.getTime()); 233 234 sqlc.error = false; 235 try { 236 parser.parse(new InputSource (new FileReader (fileParsing))); 237 } catch (IOException e) { 238 e.printStackTrace(); 239 } catch (SAXException e) { 240 e.printStackTrace(); 241 } catch (Exception e) { 242 e.printStackTrace(); 243 } 244 if (!sqlc.first) { 245 sqlc.printEndClass(); 246 } 247 sqlc.out.flush(); 248 resultsFile.close(); 249 if (resultsFileTxt!=null) resultsFileTxt.close(); 250 if (sqlc.error) { 251 fileJava.delete(); 252 fileTxt.delete(); 253 } 254 } else { 255 if (log4j.isDebugEnabled()) log4j.debug("File: " + fileParsing + " \tskipped"); 256 } 257 } catch(IOException e) { 258 e.printStackTrace(); 259 log4j.error("Problem at close of the file"); 260 } 261 } 262 263 private void pushElement(String name) { 264 stcElement.push(name); 265 strElement = name; 266 } 267 268 private void popElement() { 269 strElement = (String )stcElement.pop(); 270 if (!stcElement.isEmpty()) strElement = (String )stcElement.peek(); 271 } 272 273 public void startElement(java.lang.String uri, 274 java.lang.String name, 275 java.lang.String qName, 276 Attributes amap) { readBuffer(); 278 pushElement(qName); 279 if (log4j.isDebugEnabled()) log4j.debug("Configuration: startElement is called: element name=" + name); 280 if (log4j.isDebugEnabled()) log4j.debug("Configuration: startElement is called: element qName=" + qName); 281 if (name.trim().equalsIgnoreCase("CONNECTION")) { 282 int size = amap.getLength(); 283 for (int i = 0; i < size; i++) { 284 if (amap.getQName(i).equals("driver")) { 285 strDriver = amap.getValue(i); 286 } else if (amap.getQName(i).equals("URL")) { 287 strURL = amap.getValue(i); 288 } else if (amap.getQName(i).equals("DBUser")) { 289 strDBUser = amap.getValue(i); 290 } else if (amap.getQName(i).equals("DBPassword")) { 291 strDBPassword = amap.getValue(i); 292 } 293 } 294 } else if (name.equals("SqlMethod")) { 295 sql.sqlStatic = "true"; 296 sql.sqlConnection = "false"; 297 String sqlPackage = null; 298 int size = amap.getLength(); 299 for (int i = 0; i < size; i++) { 300 if (amap.getQName(i).equals("name")) { 301 sql.sqlName = amap.getValue(i); 302 } else if (amap.getQName(i).equals("return")) { 303 sql.sqlReturn = amap.getValue(i).trim(); 304 if (sql.sqlReturn.equalsIgnoreCase("STRING") || 305 sql.sqlReturn.equalsIgnoreCase("BOOLEAN") || 306 sql.sqlReturn.equalsIgnoreCase("DATE") || 307 sql.sqlReturn.equalsIgnoreCase("SINGLE") || 308 sql.sqlReturn.equalsIgnoreCase("MULTIPLE")) { 309 sql.executeType = "executeQuery"; 310 } else if (sql.sqlReturn.equalsIgnoreCase("ROWCOUNT") || 311 sql.sqlReturn.equalsIgnoreCase("SEQUENCE")) { 312 sql.executeType = "executeUpdate"; 313 } else if (sql.sqlReturn.equalsIgnoreCase("OBJECT")) { 314 sql.executeType = "execute"; 315 } else { 316 log4j.error("return not permited"); 317 } 318 } else if (amap.getQName(i).equals("default")) { 319 sql.sqlDefaultReturn = amap.getValue(i); 320 } else if (amap.getQName(i).equals("static")) { 321 sql.sqlStatic = amap.getValue(i); 322 } else if (amap.getQName(i).equals("connection")) { sql.sqlConnection = amap.getValue(i); 324 } else if (amap.getQName(i).equals("type")) { 325 sql.sqlType = amap.getValue(i); 326 } else if (amap.getQName(i).equals("object")) { 327 sql.sqlObject = amap.getValue(i); 328 } else if (amap.getQName(i).equals("package")) { 329 sqlPackage = amap.getValue(i); 330 } else if (amap.getQName(i).equals("import")) { 331 sql.sqlImport = amap.getValue(i); 332 } 333 } 334 if (sqlPackage != null) sql.sqlClass = sqlPackage + "." + sql.sqlObject; 335 else sql.sqlClass = sql.sqlObject; 336 } else if (name.equals("SqlClass")) { 337 int size = amap.getLength(); 338 for (int i = 0; i < size; i++) { 339 if (amap.getQName(i).equals("name")) { 340 sqlcName = amap.getValue(i); 341 } else if (amap.getQName(i).equals("package")) { 342 sqlcPackage = amap.getValue(i); 343 } 344 } 345 } else if (name.equals("Parameter")) { 346 String strName = null; 347 String strDefault = null; 348 String strInOut = "in"; 349 String strOptional = null; 350 String strAfter = null; 351 String strText = null; 352 int size = amap.getLength(); 353 for (int i = 0; i < size; i++) { 354 if (amap.getQName(i).equals("name")) { 355 strName = amap.getValue(i); 356 } else if (amap.getQName(i).equals("default")) { 357 strDefault = amap.getValue(i); 358 } else if (amap.getQName(i).equals("type")) { 359 strInOut = amap.getValue(i); 360 } else if (amap.getQName(i).equals("optional")) { 361 strOptional = amap.getValue(i); 362 } else if (amap.getQName(i).equals("after")) { 363 strAfter = amap.getValue(i); 364 } else if (amap.getQName(i).equals("text")) { 365 strText = amap.getValue(i); 366 } 367 } 368 if (log4j.isDebugEnabled()) log4j.debug("Configuration: call to addParameter "); 369 parameterSql = sql.addParameter(false, strName, strDefault, strInOut, strOptional, strAfter, strText); 370 } else if (name.equals("Field")) { 371 FieldAdded field = null; 372 int size = amap.getLength(); 373 for (int i = 0; i < size; i++) { 374 if (amap.getQName(i).equals("name")) { 375 field = new FieldAdded(amap.getValue(i)); 376 sql.vecFieldAdded.addElement(field); 377 } else if (amap.getQName(i).equals("value")) { 378 field.strValue = amap.getValue(i); 379 } 380 } 381 } else if (name.equals("Sequence")) { 382 int size = amap.getLength(); 383 for (int i = 0; i < size; i++) { 384 if (amap.getQName(i).equals("name")) { 385 sql.strSequenceName = amap.getValue(i); 386 } 387 } 388 parameterSql = sql.addParameter(true, sql.strSequenceName, null, null, null, null, null); 389 } 390 } 391 392 public void endElement(java.lang.String uri, 393 java.lang.String name, 394 java.lang.String qName) { readBuffer(); 396 if (log4j.isDebugEnabled()) log4j.debug("Configuration: call to endElement: " + name); 397 if (log4j.isDebugEnabled()) log4j.debug("(before pop) Element: " + strElement); 398 popElement(); 399 if (log4j.isDebugEnabled()) log4j.debug("(after pop) Element: " + strElement); 400 if (name.equals("SqlMethod")) { 401 if (sql.sqlType.equals("constant")) { 402 try { 403 printFunctionConstant(); 404 } catch (IOException ex) { 405 ex.printStackTrace(); 406 } 407 } else { 408 query(); 409 if (first) { 410 first = false; 411 try { 412 printInitClass(); 413 } catch (IOException ex) { 414 ex.printStackTrace(); 415 } 416 } 417 try { 418 printFunctionSql(); 419 } catch (IOException ex) { 420 ex.printStackTrace(); 421 } 422 } 423 sql = new Sql(); 424 } 425 } 426 427 public void characters(char[] ch, int start, int lengthc) throws SAXException { 428 if (buffer == null) buffer = new StringBuffer (); 429 buffer.append(ch, start, lengthc); 430 } 431 public void readBuffer() { 432 if (buffer != null) { 433 String strBuffer = buffer.toString(); 434 if (log4j.isDebugEnabled()) log4j.debug("Configuration("+strElement+"): characters are called: " + strBuffer); 435 if (strElement.equals("Sql")) { 436 sql.strSQL = strBuffer; 437 } else if (strElement.equals("SqlClassComment")) { 438 strComments = strBuffer; 439 } else if (strElement.equals("SqlMethodComment")) { 440 sql.strSqlComments = strBuffer; 441 } else if (strElement.equals("Parameter")) { 442 parameterSql.strText = strBuffer.replaceAll("\\x0D"," ").replaceAll("\\x0A"," "); 443 } 444 buffer = null; 445 } 446 } 447 448 public void connect() throws ClassNotFoundException , SQLException { 449 log4j.info("Loading driver: " + strDriver); 450 Class.forName(strDriver); 451 log4j.info("Driver loaded"); 452 if (strDBUser==null || strDBUser.equals("")) connection=DriverManager.getConnection(strURL); 453 else connection=DriverManager.getConnection(strURL, strDBUser, strDBPassword); 454 log4j.info("connect made"); 455 } 456 457 public void closeConnection() { 458 try { 459 connection.close(); 460 } catch(SQLException e){ 461 log4j.error("SQL error in closeConnection: " + e); 462 } 463 } 464 465 public void query(){ 466 try { 467 if (preparedStatement!=null) preparedStatement.close(); 468 preparedStatement = connection.prepareStatement(sql.strSQL); 469 if (log4j.isDebugEnabled()) log4j.debug("Prepared statement: " + sql.strSQL); 470 476 int i = 1; 477 for (Enumeration e = sql.vecParameter.elements() ; e.hasMoreElements() ;) { 478 Parameter parameter = (Parameter)e.nextElement(); 479 if (!parameter.boolOptional) { 480 if (parameter.type == java.sql.Types.INTEGER) { 481 if (parameter.strDefault == null) { 482 if (log4j.isDebugEnabled()) log4j.debug("setInt: " + i + " value: 0"); 483 preparedStatement.setInt(i, 0); 484 } else { 485 if (log4j.isDebugEnabled()) log4j.debug("setInt: " + i + " value: " + parameter.strDefault); 486 preparedStatement.setInt(i, Integer.parseInt(parameter.strDefault)); 487 } 488 preparedStatement.setInt(i, Integer.parseInt(parameter.strDefault)); 489 } else if (parameter.type == java.sql.Types.VARCHAR) { 490 if (parameter.strDefault == null) { 491 if (log4j.isDebugEnabled()) log4j.debug("setString: " + i + " value: null"); 492 preparedStatement.setNull(i, java.sql.Types.VARCHAR); 493 } else { 494 if (log4j.isDebugEnabled()) log4j.debug("setString: " + i + " value: " + parameter.strDefault); 495 preparedStatement.setString(i, parameter.strDefault); 496 } 497 } 498 i++; 499 } 500 } 501 if (sql.executeType.equals("executeQuery")) { 504 result = preparedStatement.executeQuery(); 505 if (log4j.isDebugEnabled()) log4j.debug("query done"); 506 rsmd = result.getMetaData (); 507 numCols = rsmd.getColumnCount (); 509 if (log4j.isDebugEnabled()) log4j.debug("number of columns: " + numCols); 510 } } catch(SQLException e){ 513 error = true; 514 log4j.error("SQL error in query: " + sql.strSQL + "Exception:"+ e); 515 e.printStackTrace(); 516 } catch(Exception e){ 517 error = true; 518 log4j.error("Error in query. Exception:"+ e); 519 e.printStackTrace(); 520 } 521 } 522 523 public void printInitClass() throws IOException { 524 if (this.writeTxtFiles) printTxtFile(); 525 out.write("//Sqlc generated " + VERSION + "\n"); if (sqlcPackage != null) { 527 out.write("package " + sqlcPackage + ";\n"); 528 } 529 out.write("\n"); 530 out.write("import java.sql.*;\n"); 531 out.write("import java.util.*;\n"); 532 out.write("\n"); 533 out.write("import org.apache.log4j.Logger;\n"); 534 out.write("\n"); 535 out.write("import javax.servlet.ServletException;\n"); 536 out.write("\n"); 537 out.write("import org.openbravo.data.FieldProvider;\n"); 538 out.write("import org.openbravo.database.ConnectionProvider;\n"); 539 out.write("import org.openbravo.data.UtilSql;\n"); 540 out.write("import org.openbravo.database.RDBMSIndependent;\n"); 541 out.write("import org.openbravo.exception.*;\n"); 542 if (sql.sqlImport != null) { 543 out.write("import " + sql.sqlImport + ";\n"); 544 } 545 out.write("\n"); 546 547 String [] strCommentsVector = stringToVector(strComments, false); 548 for (int i=0; i<strCommentsVector.length; i++) { 549 if (i == 0) { 550 out.write("/**\n" + strCommentsVector[i] + "\n"); 551 } else { 552 out.write(" *" + strCommentsVector[i] + "\n"); 553 } 554 if (i == strCommentsVector.length - 1) { 555 out.write(" */\n"); 556 } 557 } 558 if (!javaFileName.equals(sqlcName)) throw new IOException ("File name for xsql class " + javaFileName + " is different than the class name defined inside the file: " + sqlcName); 559 out.write("public class " + sqlcName + " implements FieldProvider {\n"); 560 out.write("static Logger log4j = Logger.getLogger("+sqlcName+".class);\n"); 561 try { 562 if (log4j.isDebugEnabled()) log4j.debug("Number of columns: "+ numCols); 564 out.write(" private String InitRecordNumber=\"0\";\n"); 565 for (int i=1; i<=numCols; i++) { 566 out.write(" public String "); 567 out.write(TransformaNombreColumna(rsmd.getColumnLabel(i))); 568 out.write(";\n"); 569 } 570 for (Enumeration e = sql.vecFieldAdded.elements() ; e.hasMoreElements() ;) { 571 FieldAdded fieldAdded = (FieldAdded)e.nextElement(); 572 out.write(" public String "); 573 out.write(fieldAdded.strName); 574 out.write(";\n"); 575 } 576 } catch(SQLException e){ 577 log4j.error("SQL Exception error:"+ e); 578 } 579 out.write("\n"); 580 out.write(" public String getInitRecordNumber() {\n"); 581 out.write(" return InitRecordNumber;\n"); 582 out.write(" }\n"); 583 out.write("\n"); 585 out.write(" public String getField(String fieldName) {\n"); 586 try { 587 if (log4j.isDebugEnabled()) log4j.debug("Number of columns in getField: "+ numCols); 589 for (int i=1; i<=numCols; i++) { 590 String columnLabel = rsmd.getColumnLabel(i); 591 String transformedColumnLabel = TransformaNombreColumna(columnLabel); 592 if (i == 1) { 593 out.write(" if "); 594 } else { 595 out.write(" else if "); 596 } 597 out.write("(fieldName.equalsIgnoreCase(\""); 598 out.write(columnLabel); 599 if (!columnLabel.equalsIgnoreCase(transformedColumnLabel)) 600 out.write("\") || fieldName.equals(\"" + transformedColumnLabel); 601 out.write("\"))\n"); 602 out.write(" return " + transformedColumnLabel + ";\n"); 603 } 604 for (Enumeration e = sql.vecFieldAdded.elements() ; e.hasMoreElements() ;) { 605 FieldAdded fieldAdded = (FieldAdded)e.nextElement(); 606 out.write(" else if "); 607 out.write("(fieldName.equals(\""); 608 out.write(fieldAdded.strName + "\"))\n"); 609 out.write(" return " + fieldAdded.strName + ";\n"); 610 } 611 } catch(SQLException e){ 612 log4j.error("SQL Exception error:"+ e); 613 } 614 out.write(" else {\n"); 615 out.write(" log4j.debug(\"Field does not exist: \" + fieldName);\n"); 616 out.write(" return null;\n"); 617 out.write(" }\n"); 618 out.write(" }\n"); 619 } 620 621 void printTxtFile() { 622 try { 623 if (log4j.isDebugEnabled()) log4j.debug("Printing txt File: "+ numCols); 625 for (int i=1; i<=numCols; i++) { 626 printWriterTxt.print(rsmd.getColumnLabel(i)); 627 if (i == numCols) { 628 printWriterTxt.println(""); 629 } else { 630 printWriterTxt.print(", "); 631 } 632 } 633 printWriterTxt.println(""); 634 for (int i=1; i<=numCols; i++) { 635 printWriterTxt.println("<FIELD id=\"" + 636 TransformaNombreColumna("field_" + rsmd.getColumnLabel(i)) + "\">" + 637 TransformaNombreColumna(rsmd.getColumnLabel(i)) + "</FIELD>"); 638 } 639 printWriterTxt.println(""); 640 for (int i=1; i<=numCols; i++) { 641 printWriterTxt.println(" <Parameter name=\"" + 642 TransformaNombreColumna(rsmd.getColumnLabel(i)) + "\"/>"); 643 } 644 printWriterTxt.println(""); 645 printWriterTxt.println(" " + sqlcName + " getEditVariables(VariablesSapp vars) {"); 646 printWriterTxt.println(" " + sqlcName + " data = new " + sqlcName + "();"); 647 for (int i=1; i<=numCols; i++) { 648 printWriterTxt.println(" data." + 649 TransformaNombreColumna(rsmd.getColumnLabel(i)) + 650 " = vars.getStringParameter(\"" + 651 TransformaNombreColumna("inp_" + rsmd.getColumnLabel(i)) + "\");"); 652 } 653 printWriterTxt.println(" data.a1Usuario = vars.getUser();"); 654 printWriterTxt.println(" data.a1Rol = vars.getRole();"); 655 printWriterTxt.println(" return data;"); 656 printWriterTxt.println(" }"); 657 } catch(SQLException e){ 658 log4j.error("SQL Exception error:"+ e); 659 } 660 } 661 662 public void printFunctionConstant() throws IOException { 663 printHeadFunctionSql(false, false, false); 664 out.write(" " + sqlcName + " object" + sqlcName + "[] = new " + sqlcName + "[1];\n"); 665 out.write(" object" + sqlcName + "[0] = new " + sqlcName + "();\n"); 666 try { 667 for (int i=1; i<=numCols; i++) { 668 String strNameLabel = rsmd.getColumnLabel(i); 669 out.write(" object" + sqlcName + "[0]." + 670 TransformaNombreColumna(strNameLabel) + " = "); 671 boolean printedParameter = false; 672 for (Enumeration e = sql.vecParameter.elements() ; e.hasMoreElements() ;) { 673 Parameter parameter = (Parameter)e.nextElement(); 674 if (parameter.strName.equals(TransformaNombreColumna(strNameLabel)) && !printedParameter) { 675 out.write(TransformaNombreColumna(strNameLabel) + ";\n"); 676 printedParameter = true; 677 } 678 } 679 if (!printedParameter) { 680 out.write("\"\";\n"); 681 } 682 } 683 } catch(SQLException e){ 684 log4j.error("SQL Exception error:"+ e); 685 } 686 out.write(" return object" + sqlcName + ";\n"); 687 out.write(" }\n"); 688 } 689 690 public void printSQLBody() throws IOException { 691 int posSQL = 0; 693 out.write(" String strSql = \"\";\n"); 694 for (Enumeration e = sql.vecParameter.elements() ; e.hasMoreElements() ;) { 695 Parameter parameter = (Parameter)e.nextElement(); 696 if (parameter.boolOptional){ 697 if (parameter.strAfter == null) { 698 parameter.strAfter = "WHERE"; 699 parameter.strText = parameter.strName + " = ? AND"; 700 } 701 if (parameter.strName != null && !parameter.strInOut.equals("out")) { 703 int posFinalAfter = posFinal(sql.strSQL, parameter.strAfter); 704 if (posFinalAfter != -1) { 705 imprimirSubstring(sql.strSQL, posSQL, posFinalAfter, out); 706 posSQL = posFinalAfter + (parameter.strInOut.equals("replace")?parameter.strText.length():0); 707 if (parameter.strInOut.equals("none")) { 708 out.write(" strSql = strSql + ((" + 709 parameter.strName + ".equals(\"" + parameter.strName + "\"))?\" " + 710 parameter.strText + " \":\"\");\n"); 711 } else if (parameter.strInOut.equals("argument")) { 712 out.write(" strSql = strSql + ((" + parameter.strName + "==null || " + 713 parameter.strName + ".equals(\"\"))?\"\":\" " + 714 parameter.strText + "\" + " + parameter.strName + ");\n"); 715 } else if (parameter.strInOut.equals("replace")) { 716 out.write(" strSql = strSql + ((" + parameter.strName + "==null || " + 717 parameter.strName + ".equals(\"\"))?\"\":" + parameter.strName + ");\n"); 718 } else { 719 out.write(" strSql = strSql + ((" + parameter.strName + "==null || " + 720 parameter.strName + ".equals(\"\"))?\"\":\" " + 721 parameter.strText + " \");\n"); 722 } 723 } else if (parameter.strInOut.equals("replace")) { 724 posFinalAfter=0; 725 out.write(" strSql = strSql + ((" + parameter.strName + "==null || " + 726 parameter.strName + ".equals(\"\"))?\"\":" + parameter.strName + ");\n"); 727 } 728 } 729 } 730 } 731 imprimirSubstring(sql.strSQL, posSQL, sql.strSQL.length(), out); 732 733 out.write("\n"); 734 735 if (sql.executeType.equals("executeQuery")) { 736 out.write(" ResultSet result;\n"); 737 } 738 if (sql.sqlReturn.equalsIgnoreCase("MULTIPLE")) { 739 out.write(" Vector<Object> vector = new Vector<Object>(0);\n"); 740 } else if (sql.sqlReturn.equalsIgnoreCase("SINGLE")) { 741 out.write(" " + sqlcName + " object" + sqlcName + " = new " + sqlcName + "();\n"); 742 } else if (sql.sqlReturn.equalsIgnoreCase("STRING")) { 743 out.write(" String strReturn = "); 744 if (sql.sqlDefaultReturn != null) { 745 out.write("\"" + sql.sqlDefaultReturn + "\";\n"); 746 } else { 747 out.write("null;\n"); } 749 } else if (sql.sqlReturn.equalsIgnoreCase("BOOLEAN")) { 750 out.write(" boolean boolReturn = "); 751 if (sql.sqlDefaultReturn != null) { 752 out.write(sql.sqlDefaultReturn + ";\n"); 753 } else { 754 out.write("false;\n"); } 756 } else if (sql.sqlReturn.equalsIgnoreCase("DATE")) { 757 out.write(" String dateReturn = "); 758 if (sql.sqlDefaultReturn != null) { 759 out.write(sql.sqlDefaultReturn + ";\n"); 760 } else { 761 out.write("null;\n"); } 763 } else if (sql.sqlReturn.equalsIgnoreCase("OBJECT")) { 764 out.write(" " + sql.sqlClass + " object" + sql.sqlObject + " = new " + sql.sqlClass + "();\n"); 766 } 767 if (sql.executeType.equals("executeUpdate")) { 768 out.write(" int updateCount = 0;\n"); 769 } 770 771 if (sql.sqlType.equals("preparedStatement")) { 772 out.write(" PreparedStatement st = null;\n"); 773 } else if (sql.sqlType.equals("statement")) { 774 out.write(" Statement st = null;\n"); 775 } else if (sql.sqlType.equals("callableStatement")) { 776 out.write(" CallableStatement st = null;\n"); 777 out.write(" if (connectionProvider.getRDBMS().equalsIgnoreCase(\"ORACLE\")) {\n"); 778 } 779 out.write("\n"); 780 } 781 782 public void printSQLParameters() throws IOException { 783 out.write(" int iParameter = 0;\n"); 784 out.write(" try {\n"); 785 if (sql.sqlType.equals("preparedStatement")) { 786 out.write(" st = connectionProvider.getPreparedStatement("); 787 if (sql.sqlConnection.equals("true")) out.write("conn, "); 788 out.write("strSql);\n"); 789 } else if (sql.sqlType.equals("statement")) { 790 out.write(" st = connectionProvider.getStatement("); 791 if (sql.sqlConnection.equals("true")) out.write("conn"); 792 out.write(");\n"); 793 } else if (sql.sqlType.equals("callableStatement")) { 794 out.write(" st = connectionProvider.getCallableStatement("); 795 if (sql.sqlConnection.equals("true")) out.write("conn, "); 796 out.write("strSql);\n"); 797 } 798 for (Enumeration e = sql.vecParameter.elements() ; e.hasMoreElements() ;) { 800 Parameter parameter = (Parameter)e.nextElement(); 801 if (parameter.boolSequence){ 802 out.write(" iParameter++; st.setLong(iParameter, keySequence);\n"); 803 } else if (parameter.strName != null) { 804 if (parameter.strInOut.equals("out") || parameter.strInOut.equals("inOut")) { 805 out.write(" int iParameter" + parameter.strName + " = iParameter + 1;\n"); 806 } 807 if (parameter.boolOptional) { 808 out.write(" if (" + parameter.strName + " != null && !(" + parameter.strName + ".equals(\"\"))) {\n"); 809 out.write(" "); 810 } 811 if (parameter.strInOut.equals("in") || parameter.strInOut.equals("inOut")) { 812 out.write(" iParameter++; UtilSql.setValue(st, iParameter, " + parameter.type + ", "); 813 if (parameter.strDefault == null) { 814 out.write("null, "); 815 } else { 816 out.write("\"" + parameter.strDefault + "\", "); 817 } 818 out.write(parameter.strName + ");\n"); 819 } 820 if (parameter.strInOut.equals("out")) { 821 out.write(" iParameter++; st.registerOutParameter(iParameter, " + parameter.type+ ");\n"); 822 } else if (parameter.strInOut.equals("inOut")) { out.write(" st.registerOutParameter(iParameter, " + parameter.type+ ");\n"); 824 } 825 if (parameter.boolOptional) { 826 out.write(" }\n"); 827 } 828 } 829 } 830 831 out.write("\n"); 832 } 833 834 public void printKeySequenceSql() throws IOException { 835 printHeadFunctionSql(true, false, true); 836 out.write(" boolean existsKey = false;\n"); 837 printSQLBody(); 838 printSQLParameters(); 839 840 out.write(" result = st." + sql.executeType + "("); 841 if (sql.sqlType.equals("statement")) out.write("strSql"); 842 out.write(");\n"); 843 out.write(" long countRecord = 0;\n"); 844 out.write(" long initRecord = 0;\n"); 845 out.write(" boolean searchComplete = false;\n"); 846 out.write(" while(result.next() && !searchComplete) {\n"); 847 out.write(" countRecord++;\n"); 848 out.write(" " + sqlcName + " object" + sqlcName + " = new " + sqlcName + "();\n"); 849 try { 850 for (int i=1; i<=numCols; i++) { 851 if (log4j.isDebugEnabled()) log4j.debug("Columna: " + rsmd.getColumnName(i) + " tipo: " + rsmd.getColumnType(i)); 852 if (rsmd.getColumnType(i) == java.sql.Types.TIMESTAMP || rsmd.getColumnType(i) == 91 ) { 853 out.write(" object" + sqlcName + "." + TransformaNombreColumna(rsmd.getColumnLabel(i)) + " = UtilSql.getDateValue(result, \"" + rsmd.getColumnLabel(i) +"\", \""+javaDateFormat+"\");\n"); 854 } else if (rsmd.getColumnType(i) == java.sql.Types.BLOB) { 855 out.write(" object" + sqlcName + "." + TransformaNombreColumna(rsmd.getColumnLabel(i)) + " = UtilSql.getBlobValue(result, \"" + rsmd.getColumnLabel(i) +"\");\n"); 856 } else { 857 out.write(" object" + sqlcName + "." + TransformaNombreColumna(rsmd.getColumnLabel(i)) + " = UtilSql.getValue(result, \"" + rsmd.getColumnLabel(i) +"\");\n"); 858 } 859 } 860 for (Enumeration e = sql.vecFieldAdded.elements() ; e.hasMoreElements() ;) { 861 FieldAdded fieldAdded = (FieldAdded)e.nextElement(); 862 if (fieldAdded.strValue.equals("count")) 863 out.write(" object" + sqlcName + "." + fieldAdded.strName + " = Long.toString(countRecord);\n"); 864 else if (fieldAdded.strValue.equals("void")) 865 out.write(" object" + sqlcName + "." + fieldAdded.strName + " = \"\";\n"); 866 } 867 out.write(" object" + sqlcName + ".InitRecordNumber = Long.toString(initRecord);\n"); 868 } catch(SQLException e){ 869 log4j.error("SQL Exception error:"+ e); 870 e.printStackTrace(); 871 } 872 out.write(" if (!existsKey) existsKey = (object" + sqlcName + ".getField(keyName).equalsIgnoreCase(keyValue));\n"); 873 out.write(" vector.addElement(object" + sqlcName + ");\n"); 874 out.write(" if (countRecord == numberRegisters) {\n"); 875 out.write(" if (existsKey) searchComplete=true;\n"); 876 out.write(" else {\n"); 877 out.write(" countRecord = 0;\n"); 878 out.write(" initRecord += numberRegisters;\n"); 879 out.write(" vector.clear();\n"); 880 out.write(" }\n"); 881 out.write(" }\n"); 882 out.write(" }\n"); 883 884 885 out.write(" result.close();\n"); 886 out.write(" } catch(SQLException e){\n"); 887 out.write(" log4j.error(\"SQL error in query: \" + strSql + \"Exception:\"+ e);\n"); 888 out.write(" throw new ServletException(\"@CODE=\" + Integer.toString(e.getErrorCode()) + \"@\" + e.getMessage());\n"); 889 out.write(" } catch(Exception ex){\n"); 890 out.write(" log4j.error(\"Exception in query: \" + strSql + \"Exception:\"+ ex);\n"); 891 out.write(" throw new ServletException(\"@CODE=@\" + ex.getMessage());\n"); 892 out.write(" } finally {\n"); 893 out.write(" try {\n"); 894 if (!sql.sqlConnection.equals("true")) { 895 if (sql.sqlType.equals("statement")) out.write(" connectionProvider.releaseStatement(st);\n"); 896 else out.write(" connectionProvider.releasePreparedStatement(st);\n"); 897 } else { 898 if (sql.sqlType.equals("statement")) out.write(" connectionProvider.releaseTransactionalStatement(st);\n"); 899 else if (sql.sqlType.equalsIgnoreCase("preparedstatement")) out.write(" connectionProvider.releaseTransactionalPreparedStatement(st);\n"); 900 } 901 out.write(" } catch(Exception ignore){\n"); 902 out.write(" ignore.printStackTrace();\n"); 903 out.write(" }\n"); 904 out.write(" }\n"); 905 if (sql.sqlType.equals("callableStatement")) out.write(" }\n"); 906 907 out.write(" if (existsKey) {\n"); 908 out.write(" " + sqlcName + " object" + sqlcName + "[] = new " + sqlcName + "[vector.size()];\n"); 909 out.write(" vector.copyInto(object" + sqlcName + ");\n"); 910 out.write(" return(object" + sqlcName + ");\n"); 911 out.write(" }\n"); 912 out.write(" return(new " + sqlcName + "[0]);\n"); 913 out.write(" }\n"); 914 } 915 916 public void printFunctionSql() throws IOException { 917 boolean boolSequence = false; 918 if (sql.sqlReturn.equalsIgnoreCase("MULTIPLE")) { printHeadFunctionSql(true, false, false); 921 out.write(" return "); 922 printCallFunctionSql(true); 923 out.write(";\n"); 924 out.write(" }\n"); 925 boolSequence = true; 926 printKeySequenceSql(); 927 } 928 929 printHeadFunctionSql(true, boolSequence, false); 930 if (sql.strSequenceName != null) { 932 out.write(" long keySequence = 0;\n"); 933 out.write(" String strSql1 = \"SELECT " + sql.strSequenceName); 934 out.write(".NEXTVAL AS KEY_" + sql.strSequenceName.replace(".", "_") + " FROM DUAL\";\n"); 935 out.write(" PreparedStatement psl = null;\n"); 936 out.write(" try {\n"); 937 out.write(" psl = connectionProvider.getPreparedStatement("); 938 if (sql.sqlConnection.equals("true")) out.write("conn, "); 939 out.write("strSql1);\n"); 940 out.write(" ResultSet resultKey;\n"); 941 out.write(" resultKey = psl.executeQuery();\n"); 942 out.write(" if(resultKey.next()){\n"); 943 out.write(" keySequence = resultKey.getLong(\"KEY_" + sql.strSequenceName.replace(".", "_") + "\");\n"); 944 out.write(" }\n"); 945 out.write(" resultKey.close();\n"); 946 out.write(" } catch(SQLException e){\n"); 947 out.write(" log4j.error(\"SQL error in query: \" + strSql1 + \"Exception:\"+ e);\n"); 948 out.write(" throw new ServletException(\"@CODE=\" + Integer.toString(e.getErrorCode()) + \"@\" + e.getMessage());\n"); 949 out.write(" } catch(NoConnectionAvailableException ec){\n"); 950 out.write(" log4j.error(\"Connection error in query: \" + strSql1 + \"Exception:\"+ ec);\n"); 951 out.write(" throw new ServletException(\"@CODE=NoConnectionAvailable\");\n"); 952 out.write(" } catch(PoolNotFoundException ep){\n"); 953 out.write(" log4j.error(\"Pool error in query: \" + strSql1 + \"Exception:\"+ ep);\n"); 954 out.write(" throw new ServletException(\"@CODE=NoConnectionAvailable\");\n"); 955 out.write(" } catch(Exception ex){\n"); 956 out.write(" log4j.error(\"Exception in query: \" + strSql1 + \"Exception:\"+ ex);\n"); 957 out.write(" throw new ServletException(\"@CODE=@\" + ex.getMessage());\n"); 958 out.write(" } finally {\n"); 959 out.write(" try {\n"); 960 if (!sql.sqlConnection.equals("true")) { 961 if (sql.sqlType.equals("statement")) out.write(" connectionProvider.releaseStatement(psl);\n"); 962 else out.write(" connectionProvider.releasePreparedStatement(psl);\n"); 963 } else { 964 if (sql.sqlType.equals("statement")) out.write(" connectionProvider.releaseTransactionalStatement(psl);\n"); 965 else if (sql.sqlType.equalsIgnoreCase("preparedstatement")) out.write(" connectionProvider.releaseTransactionalPreparedStatement(psl);\n"); 966 } 967 out.write(" } catch(Exception ignore){\n"); 968 out.write(" ignore.printStackTrace();\n"); 969 out.write(" }\n"); 970 out.write(" }\n"); 971 out.write("\n"); 972 } 973 974 printSQLBody(); 975 printSQLParameters(); 976 977 if (sql.executeType.equals("executeQuery")) { 978 out.write(" result = st." + sql.executeType + "("); 979 if (sql.sqlType.equals("statement")) out.write("strSql"); 980 out.write(");\n"); 981 if (sql.sqlReturn.equalsIgnoreCase("MULTIPLE")) { 982 out.write(" long countRecord = 0;\n"); 983 out.write(" long countRecordSkip = 1;\n"); 985 out.write(" boolean continueResult = true;\n"); 986 out.write(" while(countRecordSkip < firstRegister && continueResult) {\n"); 987 out.write(" continueResult = result.next();\n"); 988 out.write(" countRecordSkip++;\n"); 989 out.write(" }\n"); 990 out.write(" while(continueResult && result.next()) {\n"); 992 out.write(" countRecord++;\n"); 993 out.write(" " + sqlcName + " object" + sqlcName + " = new " + sqlcName + "();\n"); 994 } else { 995 out.write(" if(result.next()) {\n"); 996 } 997 try { 998 if (sql.sqlReturn.equalsIgnoreCase("STRING")) { 999 out.write(" strReturn = UtilSql.getValue(result, \"" + rsmd.getColumnLabel(1) +"\");\n"); 1000 } else if (sql.sqlReturn.equalsIgnoreCase("BOOLEAN")) { 1001 out.write(" boolReturn = result.getBoolean(\"" + rsmd.getColumnLabel(1) +"\");\n"); 1002 } else if (sql.sqlReturn.equalsIgnoreCase("DATE")) { 1003 out.write(" dateReturn = UtilSql.getDateValue(result, \"" + rsmd.getColumnLabel(1) +"\", \""+javaDateFormat+"\");\n"); 1004 } else { 1005 for (int i=1; i<=numCols; i++) { 1006 if (log4j.isDebugEnabled()) log4j.debug("Columna: " + rsmd.getColumnName(i) + " tipo: " + rsmd.getColumnType(i)); 1007 if (rsmd.getColumnType(i) == java.sql.Types.TIMESTAMP || rsmd.getColumnType(i) == 91 ) { 1008 out.write(" object" + sqlcName + "." + TransformaNombreColumna(rsmd.getColumnLabel(i)) + " = UtilSql.getDateValue(result, \"" + rsmd.getColumnLabel(i) +"\", \""+javaDateFormat+"\");\n"); 1009 } else if (rsmd.getColumnType(i) == java.sql.Types.BLOB) { 1010 out.write(" object" + sqlcName + "." + TransformaNombreColumna(rsmd.getColumnLabel(i)) + " = UtilSql.getBlobValue(result, \"" + rsmd.getColumnLabel(i) +"\");\n"); 1011 } else { 1012 out.write(" object" + sqlcName + "." + TransformaNombreColumna(rsmd.getColumnLabel(i)) + " = UtilSql.getValue(result, \"" + rsmd.getColumnLabel(i) +"\");\n"); 1013 } 1014 } 1015 for (Enumeration e = sql.vecFieldAdded.elements() ; e.hasMoreElements() ;) { 1016 FieldAdded fieldAdded = (FieldAdded)e.nextElement(); 1017 if (fieldAdded.strValue.equals("count")) 1018 out.write(" object" + sqlcName + "." + fieldAdded.strName + " = Long.toString(countRecord);\n"); 1019 else if (fieldAdded.strValue.equals("void")) 1020 out.write(" object" + sqlcName + "." + fieldAdded.strName + " = \"\";\n"); 1021 } 1022 out.write(" object" + sqlcName + ".InitRecordNumber = Integer.toString(firstRegister);\n"); 1023 } 1024 } catch(SQLException e){ 1025 log4j.error("SQL Exception error:"+ e); 1026 e.printStackTrace(); 1027 } 1028 if (sql.sqlReturn.equalsIgnoreCase("MULTIPLE")) { 1029 out.write(" vector.addElement(object" + sqlcName + ");\n"); 1030 out.write(" if (countRecord >= numberRegisters && numberRegisters != 0) {\n"); 1032 out.write(" continueResult = false;\n"); 1033 out.write(" }\n"); 1034 } 1036 out.write(" }\n"); 1037 out.write(" result.close();\n"); 1038 } else if (sql.executeType.equals("executeUpdate")) { 1039 out.write(" updateCount = st." + sql.executeType + "("); 1040 if (sql.sqlType.equals("statement")) out.write("strSql"); 1041 out.write(");\n"); 1042 } else if (sql.executeType.equals("execute")) { 1043 out.write(" st." + sql.executeType + "();\n"); 1044 if (sql.sqlReturn.equalsIgnoreCase("OBJECT")) { 1045 for (Enumeration e = sql.vecParameter.elements() ; e.hasMoreElements() ;) { 1046 Parameter parameter = (Parameter)e.nextElement(); 1047 if (!parameter.boolSequence && parameter.strName != null) { 1048 if (parameter.strInOut.equals("out") || parameter.strInOut.equals("inOut")) { 1049 out.write(" object" + sql.sqlObject + "." + parameter.strName); 1050 out.write("= UtilSql.getStringCallableStatement(st, iParameter" + parameter.strName + ");\n"); 1053 } 1054 } 1055 } 1056 } 1057 } 1058 out.write(" } catch(SQLException e){\n"); 1059 out.write(" log4j.error(\"SQL error in query: \" + strSql + \"Exception:\"+ e);\n"); 1060 out.write(" throw new ServletException(\"@CODE=\" + Integer.toString(e.getErrorCode()) + \"@\" + e.getMessage());\n"); 1061 out.write(" } catch(Exception ex){\n"); 1062 out.write(" log4j.error(\"Exception in query: \" + strSql + \"Exception:\"+ ex);\n"); 1063 out.write(" throw new ServletException(\"@CODE=@\" + ex.getMessage());\n"); 1064 out.write(" } finally {\n"); 1065 out.write(" try {\n"); 1066 if (!sql.sqlConnection.equals("true")) { 1067 if (sql.sqlType.equals("statement")) out.write(" connectionProvider.releaseStatement(st);\n"); 1068 else out.write(" connectionProvider.releasePreparedStatement(st);\n"); 1069 } else { 1070 if (sql.sqlType.equals("statement")) out.write(" connectionProvider.releaseTransactionalStatement(st);\n"); 1071 else if (sql.sqlType.equalsIgnoreCase("preparedstatement")) out.write(" connectionProvider.releaseTransactionalPreparedStatement(st);\n"); 1072 } 1073 out.write(" } catch(Exception ignore){\n"); 1074 out.write(" ignore.printStackTrace();\n"); 1075 out.write(" }\n"); 1076 out.write(" }\n"); 1077 if (sql.sqlType.equals("callableStatement")) { 1078 out.write(" }\n"); 1079 out.write(" else {\n"); 1080 out.write(" Vector<String> parametersData = new Vector<String>();\n"); 1081 out.write(" Vector<String> parametersTypes = new Vector<String>();\n"); 1082 int outParams = 0; 1083 String outParamName = ""; 1084 StringBuffer paramsReceipt = new StringBuffer (); 1085 for (Enumeration eparams = sql.vecParameter.elements() ; eparams.hasMoreElements() ;) { 1086 Parameter parameter = (Parameter)eparams.nextElement(); 1087 if (!parameter.strInOut.equals("none") && !parameter.strInOut.equals("argument") && !parameter.strInOut.equals("replace")) { 1088 out.write(" parametersData.addElement(" + (parameter.strInOut.equalsIgnoreCase("out")?"\"" + parameter.strName + "\"":parameter.strName) + ");\n"); 1089 out.write(" parametersTypes.addElement(\"" + parameter.strInOut + "\");\n"); 1090 if (parameter.strInOut.equals("out")) { 1091 outParamName = parameter.strName; 1092 paramsReceipt.append(" object").append(sql.sqlObject).append(".").append(outParamName).append(" = (String) vecTotal.elementAt(").append(outParams).append(");\n"); 1093 outParams++; 1094 } 1095 } 1096 } 1097 out.write(" Vector<String> vecTotal = new Vector<String>();\n"); 1098 out.write(" try {\n"); 1099 if (outParams>0) out.write(" vecTotal = "); 1100 out.write(" RDBMSIndependent.getCallableResult(" + (sql.sqlConnection.equals("true")?"conn":"null") + ", connectionProvider, strSql, parametersData, parametersTypes, " + outParams + ");\n"); 1101 if (outParams>0) out.write(paramsReceipt.toString()); 1102 out.write(" } catch(SQLException e){\n"); 1103 out.write(" log4j.error(\"SQL error in query: \" + strSql + \"Exception:\"+ e);\n"); 1104 out.write(" throw new ServletException(\"@CODE=\" + Integer.toString(e.getErrorCode()) + \"@\" + e.getMessage());\n"); 1105 out.write(" } catch(NoConnectionAvailableException ec){\n"); 1106 out.write(" log4j.error(\"Connection error in query: \" + strSql + \"Exception:\"+ ec);\n"); 1107 out.write(" throw new ServletException(\"@CODE=NoConnectionAvailable\");\n"); 1108 out.write(" } catch(PoolNotFoundException ep){\n"); 1109 out.write(" log4j.error(\"Pool error in query: \" + strSql + \"Exception:\"+ ep);\n"); 1110 out.write(" throw new ServletException(\"@CODE=NoConnectionAvailable\");\n"); 1111 out.write(" } catch(Exception ex){\n"); 1112 out.write(" log4j.error(\"Exception in query: \" + strSql + \"Exception:\"+ ex);\n"); 1113 out.write(" throw new ServletException(\"@CODE=@\" + ex.getMessage());\n"); 1114 out.write(" }\n"); 1115 out.write(" }\n"); 1116 } 1117 1118 if (sql.sqlReturn.equalsIgnoreCase("MULTIPLE")) { 1119 out.write(" " + sqlcName + " object" + sqlcName + "[] = new " + sqlcName + "[vector.size()];\n"); 1120 out.write(" vector.copyInto(object" + sqlcName + ");\n"); 1121 } 1122 1123 if (sql.sqlReturn.equalsIgnoreCase("MULTIPLE")) { 1124 out.write(" return(object" + sqlcName + ");\n"); 1125 } else if (sql.sqlReturn.equalsIgnoreCase("SINGLE")) { 1126 out.write(" return(object" + sqlcName + ");\n"); 1127 } else if (sql.sqlReturn.equalsIgnoreCase("STRING")) { 1128 out.write(" return(strReturn);\n"); 1129 } else if (sql.sqlReturn.equalsIgnoreCase("BOOLEAN")) { 1130 out.write(" return(boolReturn);\n"); 1131 } else if (sql.sqlReturn.equalsIgnoreCase("DATE")) { 1132 out.write(" return(dateReturn);\n"); 1133 } else if (sql.sqlReturn.equalsIgnoreCase("ROWCOUNT")) { 1134 out.write(" return(updateCount);\n"); 1135 } else if (sql.sqlReturn.equalsIgnoreCase("SEQUENCE")) { 1136 out.write(" return(Long.toString(keySequence));\n"); 1137 } else if (sql.sqlReturn.equalsIgnoreCase("OBJECT")) { 1138 out.write(" return(object" + sql.sqlObject + ");\n"); 1139 } 1140 out.write(" }\n"); 1141 } 1142 1143 public void printHeadFunctionSql(boolean printProviderConnection, boolean boolPagin, boolean boolSequence) throws IOException { 1144 out.write("\n"); 1145 String [] strSqlCommentsVector = stringToVector(sql.strSqlComments, false); 1146 for (int i=0; i<strSqlCommentsVector.length; i++) { 1147 if (i == 0) { 1148 out.write("/**\n" + strSqlCommentsVector[i] + "\n"); 1149 } else { 1150 out.write(" *" + strSqlCommentsVector[i] + "\n"); 1151 } 1152 if (i == strSqlCommentsVector.length - 1) { 1153 out.write(" */\n"); 1154 } 1155 } 1156 out.write(" public "); 1157 if (sql.sqlStatic.equals("true")) { 1158 out.write("static "); 1159 } 1160 if (sql.sqlReturn.equalsIgnoreCase("MULTIPLE")) { 1161 out.write(sqlcName + "[] "); 1162 } else if (sql.sqlReturn.equalsIgnoreCase("SINGLE")) { 1163 out.write(sqlcName + " "); 1164 } else if (sql.sqlReturn.equalsIgnoreCase("STRING")) { 1165 out.write("String "); 1166 } else if (sql.sqlReturn.equalsIgnoreCase("BOOLEAN")) { 1167 out.write("boolean "); 1168 } else if (sql.sqlReturn.equalsIgnoreCase("DATE")) { 1169 out.write("String "); 1170 } else if (sql.sqlReturn.equalsIgnoreCase("SEQUENCE")) { 1171 out.write("String "); 1172 } else if (sql.sqlReturn.equalsIgnoreCase("ROWCOUNT")) { 1173 out.write("int "); 1174 } else if (sql.sqlReturn.equalsIgnoreCase("OBJECT")) { 1175 out.write(sql.sqlClass + " "); 1176 } 1177 out.write(sql.sqlName + "("); 1178 boolean firstParameter = true; 1179 if (sql.sqlConnection.equals("true")) { 1180 firstParameter = false; 1181 out.write("Connection conn"); 1182 } 1183 if (printProviderConnection) { 1184 if (firstParameter) { 1185 firstParameter = false; 1186 } else { 1187 out.write(", "); 1188 } 1189 out.write("ConnectionProvider connectionProvider"); 1190 } 1191 if (log4j.isDebugEnabled()) log4j.debug("Parameters numbering"); 1192 for (Enumeration e = sql.vecParameter.elements() ; e.hasMoreElements() ;) { 1193 Parameter parameter = (Parameter)e.nextElement(); 1194 if (sql.sqlStatic.equals("true")) { 1195 if (parameter.strName != null && !parameter.boolRepeated && !parameter.boolSequence && !parameter.strInOut.equals("out")) { 1196 if (firstParameter) { 1197 firstParameter = false; 1198 } else { 1199 out.write(", "); 1200 } 1201 out.write("String " + parameter.strName); 1202 } 1203 } 1204 } 1205 if (boolPagin) { if (firstParameter) { 1207 firstParameter = false; 1208 } else { 1209 out.write(", "); 1210 } 1211 out.write("int firstRegister, int numberRegisters"); 1212 } 1214 if (boolSequence) { 1215 if (firstParameter) { 1216 firstParameter = false; 1217 } else { 1218 out.write(", "); 1219 } 1220 out.write("String keyValue, String keyName, int numberRegisters"); 1221 } 1222 out.write(")"); 1223 out.write(" throws ServletException {\n"); 1224 } 1225 1226 public void printCallFunctionSql(boolean printProviderConnection) throws IOException { 1227 out.write(sql.sqlName + "("); 1228 boolean firstParameter = true; 1229 if (sql.sqlConnection.equals("true")) { 1230 firstParameter = false; 1231 out.write("conn"); 1232 } 1233 if (printProviderConnection) { 1234 if (firstParameter) { 1235 firstParameter = false; 1236 } else { 1237 out.write(", "); 1238 } 1239 out.write("connectionProvider"); 1240 } 1241 if (log4j.isDebugEnabled()) log4j.debug("Parameters numbering"); 1242 for (Enumeration e = sql.vecParameter.elements() ; e.hasMoreElements() ;) { 1243 Parameter parameter = (Parameter)e.nextElement(); 1244 if (sql.sqlStatic.equals("true")) { 1245 if (parameter.strName != null && !parameter.boolRepeated && !parameter.boolSequence && !parameter.strInOut.equals("out")) { 1246 if (firstParameter) { 1247 firstParameter = false; 1248 } else { 1249 out.write(", "); 1250 } 1251 out.write(parameter.strName); 1252 } 1253 } 1254 } 1255 if (firstParameter) { 1256 firstParameter = false; 1257 } else { 1258 out.write(", "); 1259 } 1260 out.write("0, 0"); 1261 out.write(")"); 1262 } 1263 1264 public void printEndClass() throws IOException { 1265 out.write("}\n"); 1266 } 1267 1268 public int posFinal(String strSQL, String strPattern) { 1269 int index = sql.strSQL.indexOf(strPattern); 1270 if (index != -1) index = index + strPattern.length(); 1271 return index; 1272 } 1273 1274 public void imprimirSubstring(String strSQL, int posIni, int posFin, OutputStreamWriter out) throws IOException { 1275 String [] strSqlVector = stringToVector(strSQL.substring(posIni, posFin), true); 1276 for (int i=0; i<strSqlVector.length; i++) { 1277 if (i == 0) { 1278 out.write(" strSql = strSql + \n"); 1279 } 1280 if (i<strSqlVector.length-1) { 1281 out.write(" \"" + strSqlVector[i] + "\" +\n"); 1282 } else { 1283 out.write(" \"" + strSqlVector[i] + "\";\n"); 1284 } 1285 } 1286 } 1287 1288 1292 public String [] stringToVector(String strSQL, boolean suppressBlankLines) { 1293 byte tab[] = {10}; 1294 String strTab = new String (tab); 1295 Vector <String > vector = new Vector <String >(); 1296 if (strSQL == null) { 1297 return new String [0]; 1298 } 1299 StringTokenizer tok = new StringTokenizer (strSQL, strTab); 1300 while (tok.hasMoreTokens()) { 1301 String sql = tok.nextToken(); 1302 if (suppressBlankLines && sql.trim().equals("")) { 1303 continue; 1304 } 1305 vector.addElement(sql); 1306 } 1307 String [] strSqlVector = new String [vector.size()]; 1308 vector.copyInto(strSqlVector); 1309 return strSqlVector; 1310 } 1311 1312 static public String TransformaNombreColumna(String strColumn){ 1313 return TransformaNombreColumna(strColumn, false); 1314 } 1315 1316 static public String TransformaNombreFichero(String strFile){ 1317 return TransformaNombreColumna(strFile, true); 1318 } 1319 1320 static public String TransformaNombreColumna(String strName, boolean isFile){ 1321 String strNombreTransformado = ""; 1322 String BarraBaja = "_"; 1324 int intNumCaracteres = strName.length(); 1325 boolean blnFueBarraBaja = false; 1326 for (int i=0; i<intNumCaracteres; i++){ 1327 if (i==0) { 1328 if (isFile) { 1329 strNombreTransformado = new Character (Character.toUpperCase(strName.charAt(i))).toString(); 1330 } else { 1331 strNombreTransformado = new Character (Character.toLowerCase(strName.charAt(i))).toString(); 1332 } 1333 } else { 1334 if (strName.substring(i,i+1).compareTo(BarraBaja) == 0) blnFueBarraBaja = true; 1336 else{ 1337 if (blnFueBarraBaja){ 1338 strNombreTransformado = strNombreTransformado + new Character (Character.toUpperCase(strName.charAt(i))).toString(); 1339 blnFueBarraBaja = false; 1340 } else { 1341 if (isFile) { 1342 strNombreTransformado = strNombreTransformado + new Character (strName.charAt(i)).toString(); 1343 } else { 1344 strNombreTransformado = strNombreTransformado + new Character (Character.toLowerCase(strName.charAt(i))).toString(); 1345 } 1346 } 1347 } 1348 } 1349 } 1350 return(strNombreTransformado); 1351 } 1352 1353 public void readProperties(String strFileProperties) { 1354 Properties properties = new Properties (); 1356 try { 1357 log4j.info("strFileProperties: " + strFileProperties); 1358 properties.load(new FileInputStream (strFileProperties)); 1359 javaDateFormat = properties.getProperty("dateFormat.java"); 1360 log4j.info("javaDateFormat: " + javaDateFormat); 1361 } catch (IOException e) { 1362 System.out.println("Uh oh, got an IOException error!"); 1364 e.printStackTrace(); 1365 } 1366 } 1367 } 1368 1369 1370 1390 | Popular Tags |