1 32 33 package com.knowgate.dataobjs; 34 35 import java.io.IOException ; 36 import java.io.File ; 37 import java.io.InputStream ; 38 import java.io.ByteArrayInputStream ; 39 import java.io.StringBufferInputStream ; 40 import java.io.FileInputStream ; 41 42 import java.math.BigDecimal ; 43 44 import java.sql.SQLException ; 45 import java.sql.DatabaseMetaData ; 46 import java.sql.Connection ; 47 import java.sql.PreparedStatement ; 48 import java.sql.ResultSet ; 49 import java.sql.ResultSetMetaData ; 50 import java.sql.Statement ; 51 import java.sql.Timestamp ; 52 import java.sql.Types ; 53 import java.sql.ParameterMetaData ; 54 55 import java.util.HashMap ; 56 import java.util.LinkedList ; 57 import java.util.ListIterator ; 58 59 import com.knowgate.debug.*; 60 import com.knowgate.jdc.*; 61 62 67 68 public class DBTable { 69 70 76 77 public DBTable(String sTableName) { 78 sCatalog = null; 79 sSchema = null; 80 sName = sTableName; 81 iHashCode = 1; 82 } 83 84 91 92 public DBTable(String sCatalogName, String sSchemaName, String sTableName, int iIndex) { 93 sName = sTableName; 94 sCatalog = sCatalogName; 95 sSchema = sSchemaName; 96 iHashCode = iIndex; 97 } 98 99 101 105 106 public int columnCount() throws IllegalStateException { 107 if (null==oColumns) 108 throw new IllegalStateException ("Table columns list not initialized"); 109 return oColumns.size(); 110 } 111 112 114 124 public boolean loadRegister(JDCConnection oConn, Object [] PKValues, HashMap AllValues) 125 throws SQLException , NullPointerException , IllegalStateException { 126 int c; 127 boolean bFound; 128 Object oVal; 129 DBColumn oDBCol; 130 ListIterator oColIterator; 131 PreparedStatement oStmt = null; 132 ResultSet oRSet = null; 133 134 if (null==oColumns) 135 throw new IllegalStateException ("Table columns list not initialized"); 136 137 if (DebugFile.trace) { 138 DebugFile.writeln("Begin DBTable.loadRegister([Connection], Object[], [HashMap])" ); 139 DebugFile.incIdent(); 140 141 boolean bAllNull = true; 142 for (int n=0; n<PKValues.length; n++) 143 bAllNull &= (PKValues[n]==null); 144 145 if (bAllNull) 146 throw new NullPointerException (sName + " cannot retrieve register, value supplied for primary key is NULL."); 147 } 148 149 if (sSelect==null) { 150 throw new SQLException ("Primary key not found", "42S12"); 151 } 152 153 AllValues.clear(); 154 155 bFound = false; 156 157 try { 158 159 if (DebugFile.trace) DebugFile.writeln(" Connection.prepareStatement(" + sSelect + ")"); 160 161 oStmt = oConn.prepareStatement(sSelect); 163 164 try { 165 oStmt.setQueryTimeout(10); 166 } 167 catch (SQLException sqle) { 168 if (DebugFile.trace) DebugFile.writeln("Error at PreparedStatement.setQueryTimeout(10)" + sqle.getMessage()); 169 } 170 171 172 for (int p=0; p<oPrimaryKeys.size(); p++) { 174 if (DebugFile.trace) DebugFile.writeln(" binding primary key " + PKValues[p] + "."); 175 176 bindParameter(oConn, oStmt, p+1, PKValues[p]); 177 } 180 if (DebugFile.trace) DebugFile.writeln(" Connection.executeQuery()"); 181 182 oRSet = oStmt.executeQuery(); 183 184 if (oRSet.next()) { 185 if (DebugFile.trace) DebugFile.writeln(" ResultSet.next()"); 186 187 bFound = true; 188 189 oColIterator = oColumns.listIterator(); 192 193 c = 1; 194 while (oColIterator.hasNext()) { 195 oVal = oRSet.getObject(c++); 196 oDBCol = (DBColumn) oColIterator.next(); 197 if (null!=oVal) 198 AllValues.put(oDBCol.getName(), oVal); 199 } 200 } 201 202 if (DebugFile.trace) DebugFile.writeln(" ResultSet.close()"); 203 204 oRSet.close(); 205 oRSet = null; 206 207 oStmt.close(); 208 oStmt = null; 209 } 210 catch (SQLException sqle) { 211 try { 212 if (null!=oRSet) oRSet.close(); 213 if (null!=oStmt) oStmt.close(); 214 } 215 catch (Exception ignore) { } 216 217 throw new SQLException (sqle.getMessage(), sqle.getSQLState(), sqle.getErrorCode()); 218 } 219 220 if (DebugFile.trace) { 221 DebugFile.decIdent(); 222 DebugFile.writeln("End DBTable.loadRegister() : " + (bFound ? "true" : "false")); 223 } 224 225 return bFound; 226 227 } 229 231 private static void bindParameter (JDCConnection oConn, PreparedStatement oStmt, 232 int iParamIndex, Object oParamValue, int iSQLType) 233 throws SQLException { 234 235 Class oParamClass; 236 237 switch (oConn.getDataBaseProduct()) { 238 239 case JDCConnection.DBMS_ORACLE: 240 if (oParamValue!=null) { 241 242 oParamClass = oParamValue.getClass(); 243 244 if (DebugFile.trace) DebugFile.writeln("binding " + oParamClass.getName() + " as SQL " + DBColumn.typeName(iSQLType)); 245 246 if (oParamClass.equals(Short .class) || oParamClass.equals(Integer .class) || oParamClass.equals(Float .class) || oParamClass.equals(Double .class)) 247 oStmt.setBigDecimal (iParamIndex, new java.math.BigDecimal (oParamValue.toString())); 248 249 else if ((oParamClass.getName().equals("java.sql.Timestamp") || 255 oParamClass.getName().equals("java.util.Date")) && 256 iSQLType==Types.DATE) { 257 try { 258 Class [] aTimestamp = new Class [1]; 259 aTimestamp[0] = Class.forName("java.sql.Timestamp"); 260 Class cDATE = Class.forName("oracle.sql.DATE"); 261 java.lang.reflect.Constructor cNewDATE = cDATE.getConstructor(aTimestamp); 262 Object oDATE; 263 if (oParamClass.getName().equals("java.sql.Timestamp")) { 264 oDATE = cNewDATE.newInstance(new Object []{oParamValue}); 265 } else { 266 oDATE = cNewDATE.newInstance(new Object []{new Timestamp (((java.util.Date )oParamValue).getTime())}); 267 } 268 oStmt.setObject (iParamIndex, oDATE, iSQLType); 269 } catch (ClassNotFoundException cnf) { 270 throw new SQLException ("ClassNotFoundException oracle.sql.DATE " + cnf.getMessage()); 271 } catch (NoSuchMethodException nsm) { 272 throw new SQLException ("NoSuchMethodException " + nsm.getMessage()); 273 } catch (IllegalAccessException iae) { 274 throw new SQLException ("IllegalAccessException " + iae.getMessage()); 275 } catch (InstantiationException ine) { 276 throw new SQLException ("InstantiationException " + ine.getMessage()); 277 } catch (java.lang.reflect.InvocationTargetException ite) { 278 throw new SQLException ("InvocationTargetException " + ite.getMessage()); 279 } 280 } 281 else if (oParamClass.getName().equals("java.util.Date") && iSQLType==Types.TIMESTAMP) { 282 oStmt.setTimestamp(iParamIndex, new Timestamp (((java.util.Date )oParamValue).getTime())); 283 } 284 else { 285 oStmt.setObject (iParamIndex, oParamValue, iSQLType); 286 } 287 } 288 else 289 oStmt.setObject (iParamIndex, null, iSQLType); 290 break; 291 292 default: 293 String sParamClassName; 294 if (null!=oParamValue) 295 sParamClassName = oParamValue.getClass().getName(); 296 else 297 sParamClassName = "null"; 298 299 if ((Types.TIMESTAMP==iSQLType) && (oParamValue!=null)) { 300 if (sParamClassName.equals("java.util.Date")) { 301 if (DebugFile.trace) DebugFile.writeln("binding java.sql.Timestamp as SQL " + DBColumn.typeName(Types.TIMESTAMP)); 302 oStmt.setTimestamp(iParamIndex, new Timestamp (((java.util.Date )oParamValue).getTime())); 303 } 304 else { 305 if (DebugFile.trace) DebugFile.writeln("binding " + sParamClassName + " as SQL " + DBColumn.typeName(iSQLType)); 306 oStmt.setObject(iParamIndex, oParamValue, iSQLType); 307 } 308 } 309 else if ((Types.DATE==iSQLType) && (oParamValue!=null)) { 310 if (sParamClassName.equals("java.util.Date")) { 311 if (DebugFile.trace) DebugFile.writeln("binding java.sql.Date as SQL " + DBColumn.typeName(Types.DATE)); 312 oStmt.setDate(iParamIndex, new java.sql.Date (((java.util.Date )oParamValue).getTime())); 313 } 314 else { 315 if (DebugFile.trace) DebugFile.writeln("binding " + sParamClassName + " as SQL " + DBColumn.typeName(iSQLType)); 316 oStmt.setObject(iParamIndex, oParamValue, iSQLType); 317 } 318 } 319 else { 320 if ((oParamValue!=null) && DebugFile.trace) { 321 if (DebugFile.trace) DebugFile.writeln("binding " + sParamClassName + " as SQL " + DBColumn.typeName(iSQLType)); 322 } 323 oStmt.setObject(iParamIndex, oParamValue, iSQLType); 324 } 325 } 326 } 328 330 private static void bindParameter (JDCConnection oConn, PreparedStatement oStmt, 331 int iParamIndex, Object oParamValue) 332 throws SQLException { 333 334 if (oConn.getDataBaseProduct()==JDCConnection.DBMS_ORACLE) { 335 if (oParamValue.getClass().equals(Integer .class) || 336 oParamValue.getClass().equals(Short .class) || 337 oParamValue.getClass().equals(Float .class) || 338 oParamValue.getClass().equals(Double .class)) { 339 bindParameter(oConn, oStmt, iParamIndex, oParamValue, Types.NUMERIC); 340 } 341 else if (oParamValue.getClass().getName().equals("java.util.Date") || 342 oParamValue.getClass().getName().equals("java.sql.Timestamp") ) { 343 bindParameter(oConn, oStmt, iParamIndex, oParamValue, Types.DATE); 344 } 345 else { 346 oStmt.setObject(iParamIndex, oParamValue); 347 } 348 } else { 349 oStmt.setObject(iParamIndex, oParamValue); 350 } 351 } 353 355 366 367 public boolean storeRegister(JDCConnection oConn, HashMap AllValues) throws SQLException { 368 int c; 369 boolean bNewRow = false; 370 DBColumn oCol; 371 String sCol; 372 String sSQL = ""; 373 ListIterator oColIterator; 374 int iAffected = 0; 375 PreparedStatement oStmt = null; 376 377 if (DebugFile.trace) 378 { 379 DebugFile.writeln("Begin DBTable.storeRegister([Connection], {" + AllValues.toString() + "})" ); 380 DebugFile.incIdent(); 381 } 382 383 try { 384 if (null!=sUpdate) { 385 if (DebugFile.trace) DebugFile.writeln("Connection.prepareStatement(" + sUpdate + ")"); 386 387 sSQL = sUpdate; 388 389 oStmt = oConn.prepareStatement(sSQL); 390 391 try { oStmt.setQueryTimeout(10); } catch (SQLException sqle) { if (DebugFile.trace) DebugFile.writeln("Error at PreparedStatement.setQueryTimeout(10)" + sqle.getMessage()); } 392 393 c = 1; 394 oColIterator = oColumns.listIterator(); 395 396 while (oColIterator.hasNext()) { 397 oCol = (DBColumn) oColIterator.next(); 398 sCol = oCol.getName().toLowerCase(); 399 400 if (!oPrimaryKeys.contains(sCol) && 401 (sCol.compareTo(DB.dt_created)!=0)) { 402 403 if (DebugFile.trace) { 404 if (oCol.getSqlType()==java.sql.Types.CHAR || oCol.getSqlType()==java.sql.Types.VARCHAR) { 405 406 if (AllValues.get(sCol)!=null) { 407 DebugFile.writeln("Binding " + sCol + "=" + AllValues.get(sCol).toString()); 408 409 if (AllValues.get(sCol).toString().length() > oCol.getPrecision()) 410 DebugFile.writeln("ERROR: value for " + oCol.getName() + " exceeds columns precision of " + String.valueOf(oCol.getPrecision())); 411 } else 413 DebugFile.writeln("Binding " + sCol + "=NULL"); 414 } 415 } 417 try { 418 bindParameter (oConn, oStmt, c, AllValues.get(sCol), oCol.getSqlType()); 419 c++; 420 } catch (ClassCastException e) { 421 if (AllValues.get(sCol)!=null) 422 throw new SQLException ("ClassCastException at column " + sCol + " Cannot cast Java " + AllValues.get(sCol).getClass().getName() + " to SQL type " + oCol.getSqlTypeName(), "07006"); 423 else 424 throw new SQLException ("ClassCastException at column " + sCol + " Cannot cast NULL to SQL type " + oCol.getSqlTypeName(), "07006"); 425 } 426 427 } } 430 oColIterator = oPrimaryKeys.listIterator(); 431 while (oColIterator.hasNext()) { 432 sCol = (String ) oColIterator.next(); 433 oCol = getColumnByName(sCol); 434 435 if (DebugFile.trace) DebugFile.writeln("PreparedStatement.setObject (" + String.valueOf(c) + "," + AllValues.get(sCol) + "," + oCol.getSqlTypeName() + ")"); 436 437 bindParameter (oConn, oStmt, c, AllValues.get(sCol), oCol.getSqlType()); 438 c++; 439 } 441 if (DebugFile.trace) DebugFile.writeln("PreparedStatement.executeUpdate()"); 442 443 iAffected = oStmt.executeUpdate(); 444 445 if (DebugFile.trace) DebugFile.writeln(String.valueOf(iAffected) + " affected rows"); 446 447 oStmt.close(); 448 oStmt = null; 449 } else 451 iAffected = 0; 452 453 if (0==iAffected) 454 { 455 bNewRow = true; 456 457 if (DebugFile.trace) DebugFile.writeln("Connection.prepareStatement(" + sInsert + ")"); 458 459 sSQL = sInsert; 460 461 oStmt = oConn.prepareStatement(sInsert); 462 463 try { oStmt.setQueryTimeout(10); } catch (SQLException sqle) { if (DebugFile.trace) DebugFile.writeln("Error at PreparedStatement.setQueryTimeout(10)" + sqle.getMessage()); } 464 465 c = 1; 466 oColIterator = oColumns.listIterator(); 467 468 while (oColIterator.hasNext()) { 469 470 oCol = (DBColumn)oColIterator.next(); 471 sCol = oCol.getName(); 472 473 if (DebugFile.trace) { 474 if (null!=AllValues.get(sCol)) 475 DebugFile.writeln("Binding " + sCol + "=" + AllValues.get(sCol).toString()); 476 else 477 DebugFile.writeln("Binding " + sCol + "=NULL"); 478 } 480 bindParameter (oConn, oStmt, c, AllValues.get(sCol), oCol.getSqlType()); 481 c++; 482 } 484 if (DebugFile.trace) DebugFile.writeln("PreparedStatement.executeUpdate()"); 485 486 iAffected = oStmt.executeUpdate(); 487 488 if (DebugFile.trace) DebugFile.writeln(String.valueOf(iAffected) + " affected rows"); 489 490 oStmt.close(); 491 oStmt =null; 492 } 493 else 494 bNewRow = false; 495 } 496 catch (SQLException sqle) { 497 try { if (null!=oStmt) oStmt.close(); } catch (Exception ignore) { } 498 499 throw new SQLException (sqle.getMessage() + " " + sSQL, sqle.getSQLState(), sqle.getErrorCode()); 500 } 501 502 if (DebugFile.trace) { 503 DebugFile.decIdent(); 504 DebugFile.writeln("End DBTable.storeRegister() : " + String.valueOf(bNewRow && (iAffected>0))); 505 } 506 507 return bNewRow && (iAffected>0); 508 } 510 512 524 525 public boolean storeRegisterLong(JDCConnection oConn, HashMap AllValues, HashMap BinaryLengths) throws IOException , SQLException { 526 int c; 527 boolean bNewRow = false; 528 DBColumn oCol; 529 String sCol; 530 ListIterator oColIterator; 531 PreparedStatement oStmt; 532 int iAffected; 533 534 LinkedList oStreams; 535 InputStream oStream; 536 String sClassName; 537 538 if (DebugFile.trace) 539 { 540 DebugFile.writeln("Begin DBTable.storeRegisterLong([Connection], {" + AllValues.toString() + "})" ); 541 DebugFile.incIdent(); 542 } 543 544 oStreams = new LinkedList (); 545 546 if (null!=sUpdate) { 547 548 if (DebugFile.trace) DebugFile.writeln("Connection.prepareStatement(" + sUpdate + ")"); 549 550 oStmt = oConn.prepareStatement(sUpdate); 551 552 try { oStmt.setQueryTimeout(10); } catch (SQLException sqle) { if (DebugFile.trace) DebugFile.writeln("Error at PreparedStatement.setQueryTimeout(10)" + sqle.getMessage()); } 553 554 c = 1; 555 oColIterator = oColumns.listIterator(); 556 while (oColIterator.hasNext()) { 557 oCol = (DBColumn) oColIterator.next(); 558 sCol = oCol.getName().toLowerCase(); 559 560 if (!oPrimaryKeys.contains(sCol) && 561 (!sCol.equalsIgnoreCase(DB.dt_created))) { 562 563 if (DebugFile.trace) { 564 if (oCol.getSqlType()==java.sql.Types.CHAR || oCol.getSqlType()==java.sql.Types.VARCHAR) { 565 if (AllValues.get(sCol) != null) { 566 DebugFile.writeln("Binding " + sCol + "=" + 567 AllValues.get(sCol).toString()); 568 if (AllValues.get(sCol).toString().length() > oCol.getPrecision()) 569 DebugFile.writeln("ERROR: value for " + oCol.getName() + 570 " exceeds columns precision of " + 571 String.valueOf(oCol.getPrecision())); 572 } else 574 DebugFile.writeln("Binding " + sCol + "=NULL"); 575 } 576 } 578 if (oCol.getSqlType()==java.sql.Types.LONGVARCHAR || oCol.getSqlType()==java.sql.Types.CLOB || oCol.getSqlType()==java.sql.Types.LONGVARBINARY || oCol.getSqlType()==java.sql.Types.BLOB) { 579 if (BinaryLengths.containsKey(sCol)) { 580 if (((Long )BinaryLengths.get(sCol)).intValue()>0) { 581 sClassName = AllValues.get(sCol).getClass().getName(); 582 if (sClassName.equals("java.io.File")) 583 oStream = new FileInputStream ((File ) AllValues.get(sCol)); 584 else if (sClassName.equals("[B")) 585 oStream = new ByteArrayInputStream ((byte[]) AllValues.get(sCol)); 586 else if (sClassName.equals("[C")) 587 oStream = new StringBufferInputStream (new String ((char[]) AllValues.get(sCol))); 588 else 589 throw new SQLException ("Invalid object binding for column " + sCol); 590 oStreams.addLast(oStream); 591 oStmt.setBinaryStream(c++, oStream, ((Long )BinaryLengths.get(sCol)).intValue()); 592 } 593 else 594 oStmt.setObject (c++, null, oCol.getSqlType()); 595 } 596 else 597 oStmt.setObject (c++, null, oCol.getSqlType()); 598 } 599 else 600 bindParameter (oConn, oStmt, c++, AllValues.get(sCol), oCol.getSqlType()); 601 } } 604 oColIterator = oPrimaryKeys.listIterator(); 605 while (oColIterator.hasNext()) { 606 sCol = (String ) oColIterator.next(); 607 oCol = getColumnByName(sCol); 608 609 if (DebugFile.trace) DebugFile.writeln("PreparedStatement.setObject (" + String.valueOf(c) + "," + AllValues.get(sCol) + "," + oCol.getSqlTypeName() + ")"); 610 611 bindParameter (oConn, oStmt, c, AllValues.get(sCol), oCol.getSqlType()); 612 c++; 613 } 615 if (DebugFile.trace) DebugFile.writeln("PreparedStatement.executeUpdate()"); 616 617 iAffected = oStmt.executeUpdate(); 618 619 if (DebugFile.trace) DebugFile.writeln(String.valueOf(iAffected) + " affected rows"); 620 621 oStmt.close(); 622 623 oColIterator = oStreams.listIterator(); 624 625 while (oColIterator.hasNext()) 626 ((InputStream ) oColIterator.next()).close(); 627 628 oStreams.clear(); 629 630 } 631 else 632 iAffected = 0; 633 634 if (0==iAffected) 635 { 636 bNewRow = true; 637 638 if (DebugFile.trace) DebugFile.writeln("Connection.prepareStatement(" + sInsert + ")"); 639 640 oStmt = oConn.prepareStatement(sInsert); 641 642 c = 1; 643 oColIterator = oColumns.listIterator(); 644 645 while (oColIterator.hasNext()) { 646 647 oCol = (DBColumn)oColIterator.next(); 648 sCol = oCol.getName(); 649 650 if (DebugFile.trace) { 651 if (null!=AllValues.get(sCol)) 652 DebugFile.writeln("Binding " + sCol + "=" + AllValues.get(sCol).toString()); 653 else 654 DebugFile.writeln("Binding " + sCol + "=NULL"); 655 } 656 657 if (oCol.getSqlType()==java.sql.Types.LONGVARCHAR || oCol.getSqlType()==java.sql.Types.CLOB || oCol.getSqlType()==java.sql.Types.LONGVARBINARY || oCol.getSqlType()==java.sql.Types.BLOB) { 658 if (BinaryLengths.containsKey(sCol)) { 659 if ( ( (Long ) BinaryLengths.get(sCol)).intValue() > 0) { 660 sClassName = AllValues.get(sCol).getClass().getName(); 661 if (sClassName.equals("java.io.File")) 662 oStream = new FileInputStream ((File ) AllValues.get(sCol)); 663 else if (sClassName.equals("[B")) 664 oStream = new ByteArrayInputStream ((byte[]) AllValues.get(sCol)); 665 else if (sClassName.equals("[C")) 666 oStream = new StringBufferInputStream (new String ((char[]) AllValues.get(sCol))); 667 else 668 throw new SQLException ("Invalid object binding for column " + sCol); 669 oStreams.addLast(oStream); 670 oStmt.setBinaryStream(c++, oStream, ((Long ) BinaryLengths.get(sCol)).intValue()); 671 } 672 else 673 oStmt.setObject(c++, null, oCol.getSqlType()); 674 } 675 else 676 oStmt.setObject(c++, null, oCol.getSqlType()); 677 } 678 else 679 bindParameter (oConn, oStmt, c++, AllValues.get(sCol), oCol.getSqlType()); 680 } 682 if (DebugFile.trace) DebugFile.writeln("PreparedStatement.executeUpdate()"); 683 684 iAffected = oStmt.executeUpdate(); 685 686 if (DebugFile.trace) DebugFile.writeln(String.valueOf(iAffected) + " affected rows"); 687 688 oStmt.close(); 689 690 oColIterator = oStreams.listIterator(); 691 692 while (oColIterator.hasNext()) 693 ((InputStream ) oColIterator.next()).close(); 694 695 oStreams.clear(); 696 } 697 698 else 699 bNewRow = false; 700 701 703 if (DebugFile.trace) 704 { 705 DebugFile.decIdent(); 706 DebugFile.writeln("End DBTable.storeRegisterLong() : " + String.valueOf(bNewRow)); 707 } 708 709 return bNewRow; 710 } 712 714 721 722 public boolean deleteRegister(JDCConnection oConn, HashMap AllValues) throws SQLException { 723 int c; 724 boolean bDeleted; 725 ListIterator oColIterator; 726 PreparedStatement oStmt; 727 Object oPK; 728 DBColumn oCol; 729 730 if (DebugFile.trace) 731 { 732 DebugFile.writeln("Begin DBTable.deleteRegister([Connection], {" + AllValues.toString() + "})" ); 733 DebugFile.incIdent(); 734 } 735 736 if (sDelete==null) { 737 throw new SQLException ("Primary key not found", "42S12"); 738 } 739 740 742 if (DebugFile.trace) DebugFile.writeln("Connection.prepareStatement(" + sDelete + ")"); 743 744 oStmt = oConn.prepareStatement(sDelete); 745 746 c = 1; 747 oColIterator = oPrimaryKeys.listIterator(); 748 749 while (oColIterator.hasNext()) { 750 oPK = oColIterator.next(); 751 oCol = getColumnByName((String ) oPK); 752 753 if (DebugFile.trace) DebugFile.writeln("PreparedStatement.setObject(" + String.valueOf(c) + "," + AllValues.get(oPK) + "," + oCol.getSqlTypeName() + ")"); 754 755 oStmt.setObject (c++, AllValues.get(oPK), oCol.getSqlType()); 756 } 758 if (DebugFile.trace) DebugFile.writeln("PreparedStatement.executeUpdate()"); 759 760 bDeleted = (oStmt.executeUpdate()>0); 761 762 764 if (DebugFile.trace) 765 { 766 DebugFile.decIdent(); 767 DebugFile.writeln("End DBTable.deleteRegister() : " + (bDeleted ? "true" : "false")); 768 } 769 770 return bDeleted; 771 } 773 775 782 783 public boolean existsRegister(JDCConnection oConn, String sQueryString) throws SQLException { 784 Statement oStmt = oConn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 785 786 ResultSet oRSet = oStmt.executeQuery("SELECT NULL FROM " + getName() + " WHERE " + sQueryString); 787 boolean bExists = oRSet.next(); 788 oRSet.close(); 789 oStmt.close(); 790 791 return bExists; 792 } 793 794 796 803 804 public boolean existsRegister(JDCConnection oConn, String sQueryString, Object [] oQueryParams) throws SQLException { 805 PreparedStatement oStmt = oConn.prepareStatement("SELECT NULL FROM " + getName() + " WHERE " + sQueryString, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 806 807 if (oQueryParams!=null) { 808 for (int p=0; p<oQueryParams.length; p++) 809 oStmt.setObject(p+1, oQueryParams[p]); 810 } 811 812 ResultSet oRSet = oStmt.executeQuery(); 813 boolean bExists = oRSet.next(); 814 oRSet.close(); 815 oStmt.close(); 816 817 return bExists; 818 } 819 820 822 828 829 public boolean existsRegister(JDCConnection oConn, HashMap AllValues) throws SQLException { 830 int c; 831 boolean bExists; 832 PreparedStatement oStmt; 833 ResultSet oRSet; 834 ListIterator oColIterator; 835 Object oPK; 836 DBColumn oCol; 837 838 if (DebugFile.trace) 839 { 840 DebugFile.writeln("Begin DBTable.existsRegister([Connection], {" + AllValues.toString() + "})" ); 841 DebugFile.incIdent(); 842 } 843 844 oStmt = oConn.prepareStatement(sExists, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 845 846 c = 1; 847 oColIterator = oPrimaryKeys.listIterator(); 848 849 while (oColIterator.hasNext()) { 850 oPK = oColIterator.next(); 851 oCol = getColumnByName((String ) oPK); 852 853 oStmt.setObject (c++, AllValues.get(oPK), oCol.getSqlType()); 854 } 856 oRSet = oStmt.executeQuery(); 857 bExists = oRSet.next(); 858 859 oRSet.close(); 860 oStmt.close(); 861 862 if (DebugFile.trace) 863 { 864 DebugFile.decIdent(); 865 DebugFile.writeln("End DBTable.existsRegister() : " + String.valueOf(bExists)); 866 } 867 868 return bExists; 869 } 871 873 876 877 public LinkedList getColumns() { 878 return oColumns; 879 } 880 881 883 887 888 public String getColumnsStr() throws IllegalStateException { 889 890 if (null==oColumns) 891 throw new IllegalStateException ("Table columns list has not been initialized"); 892 893 ListIterator oColIterator = oColumns.listIterator(); 894 String sGetAllCols = new String (""); 895 896 while (oColIterator.hasNext()) 897 sGetAllCols += ((DBColumn) oColIterator.next()).getName() + ","; 898 899 return sGetAllCols.substring(0, sGetAllCols.length()-1); 900 901 } 903 905 911 912 public DBColumn getColumnByName (String sColumnName) throws IllegalStateException { 913 914 if (null==oColumns) 915 throw new IllegalStateException ("Table columns list not initialized"); 916 917 ListIterator oColIterator = oColumns.listIterator(); 918 DBColumn oCol = null; 919 920 while (oColIterator.hasNext()) { 921 oCol = (DBColumn) oColIterator.next(); 922 if (oCol.getName().equalsIgnoreCase(sColumnName)) { 923 break; 924 } 925 oCol = null; 926 } 928 return oCol; 929 930 } 932 934 939 940 public int getColumnIndex (String sColumnName) { 941 ListIterator oColIterator = oColumns.listIterator(); 942 DBColumn oCol = null; 943 944 while (oColIterator.hasNext()) { 945 oCol = (DBColumn) oColIterator.next(); 946 if (oCol.getName().equalsIgnoreCase(sColumnName)) { 947 return oCol.getPosition(); 948 } 949 oCol = null; 950 } 952 return -1; 953 954 } 956 958 961 962 public LinkedList getPrimaryKey() { 963 return oPrimaryKeys; 964 965 } 966 967 969 972 973 public String getName() { return sName; } 974 975 977 980 981 public String getCatalog() { return sCatalog; } 982 983 985 public void setCatalog(String sCatalogName) { sCatalog=sCatalogName; } 986 987 989 992 993 public String getSchema() { return sSchema; } 994 995 997 1001 public void setSchema(String sSchemaName) { sSchema=sSchemaName; } 1002 1003 1005 public int hashCode() { return iHashCode; } 1006 1007 1009 1017 public void readColumns(Connection oConn, DatabaseMetaData oMData) throws SQLException { 1018 int iErrCode; 1019 Statement oStmt; 1020 ResultSet oRSet; 1021 ResultSetMetaData oRData; 1022 DBColumn oCol; 1023 String sCol; 1024 int iCols; 1025 ListIterator oColIterator; 1026 1027 String sColName; 1028 short iSQLType; 1029 String sTypeName; 1030 int iPrecision; 1031 int iDigits; 1032 int iNullable; 1033 int iColPos; 1034 1035 int iDBMS; 1036 1037 String sGetAllCols = ""; 1038 String sSetPKCols = ""; 1039 String sSetAllCols = ""; 1040 String sSetNoPKCols = ""; 1041 1042 oColumns = new LinkedList (); 1043 oPrimaryKeys = new LinkedList (); 1044 1045 if (DebugFile.trace) 1046 { 1047 DebugFile.writeln("Begin DBTable.readColumns([DatabaseMetaData])" ); 1048 DebugFile.incIdent(); 1049 1050 DebugFile.writeln("DatabaseMetaData.getColumns(" + sCatalog + "," + sSchema + "," + sName + ",%)"); 1051 } 1052 1053 if (oConn.getMetaData().getDatabaseProductName().equals("PostgreSQL")) 1054 iDBMS = JDCConnection.DBMS_POSTGRESQL; 1055 else if (oConn.getMetaData().getDatabaseProductName().equals("Oracle")) 1056 iDBMS = JDCConnection.DBMS_ORACLE; 1057 else 1058 iDBMS = 0; 1059 1060 oStmt = oConn.createStatement(); 1061 1062 try { 1063 if (DebugFile.trace) DebugFile.writeln("Statement.executeQuery(SELECT * FROM " + sName + " WHERE 1=0)"); 1064 1065 oRSet = oStmt.executeQuery("SELECT * FROM " + sName + " WHERE 1=0"); 1066 iErrCode = 0; 1067 } 1068 catch (SQLException sqle) { 1069 1072 oStmt.close(); 1073 oRSet = null; 1074 1075 if (DebugFile.trace) DebugFile.writeln("SQLException " + sName + " " + sqle.getMessage()); 1076 1077 iErrCode = sqle.getErrorCode(); 1078 if (iErrCode==0) iErrCode=-1; 1079 if (!sqle.getSQLState().equals("42000")) 1080 throw new SQLException (sqle.getMessage(), sqle.getSQLState(), sqle.getErrorCode()); 1081 } 1082 1083 if (0==iErrCode) { 1084 if (DebugFile.trace) DebugFile.writeln("ResultSet.getMetaData()"); 1085 1086 oRData= oRSet.getMetaData(); 1087 1088 iCols = oRData.getColumnCount(); 1089 1090 if (DebugFile.trace) DebugFile.writeln("table has " + String.valueOf(iCols) + " columns"); 1091 1092 for (int c=1; c<=iCols; c++) { 1093 sColName = oRData.getColumnName(c).toLowerCase(); 1094 sTypeName = oRData.getColumnTypeName(c); 1095 iSQLType = (short) oRData.getColumnType(c); 1096 1097 if (iDBMS==JDCConnection.DBMS_POSTGRESQL) 1098 switch (iSQLType) { 1099 case Types.CHAR: 1100 case Types.VARCHAR: 1101 iPrecision = oRData.getColumnDisplaySize(c); 1102 break; 1103 default: 1104 iPrecision = oRData.getPrecision(c); 1105 } 1106 else { 1107 if (iSQLType==Types.BLOB || iSQLType==Types.CLOB) 1109 iPrecision = 2147483647; 1110 else 1112 iPrecision = oRData.getPrecision(c); 1113 } 1114 1115 iDigits = oRData.getScale(c); 1116 iNullable = oRData.isNullable(c); 1117 iColPos = c; 1118 1119 if (5==iDBMS && iSQLType==Types.NUMERIC && iPrecision<=6 && iDigits==0) { 1120 oCol = new DBColumn (sName,sColName,(short) Types.SMALLINT, sTypeName, iPrecision, iDigits, iNullable,iColPos); 1122 } 1123 else { 1124 oCol = new DBColumn (sName,sColName,iSQLType,sTypeName,iPrecision,iDigits,iNullable,iColPos); 1125 } 1126 1127 if (!sColName.equals(DB.dt_created)) 1130 oColumns.add(oCol); 1131 } 1133 if (DebugFile.trace) DebugFile.writeln("ResultSet.close()"); 1134 1135 oRSet.close(); 1136 oRSet = null; 1137 oStmt.close(); 1138 oStmt = null; 1139 1140 if (5==iDBMS) { 1141 1142 1152 1153 oStmt = oConn.createStatement(); 1154 1155 if (DebugFile.trace) { 1156 if (null==sSchema) 1157 DebugFile.writeln("Statement.executeQuery(SELECT NULL AS TABLE_CAT, COLS.OWNER AS TABLE_SCHEM, COLS.TABLE_NAME, COLS.COLUMN_NAME, COLS.POSITION AS KEY_SEQ, COLS.CONSTRAINT_NAME AS PK_NAME FROM USER_CONS_COLUMNS COLS, USER_CONSTRAINTS CONS WHERE CONS.OWNER=COLS.OWNER AND CONS.CONSTRAINT_NAME=COLS.CONSTRAINT_NAME AND CONS.CONSTRAINT_TYPE='P' AND CONS.TABLE_NAME='" + sName.toUpperCase()+ "')"); 1158 else 1159 DebugFile.writeln("Statement.executeQuery(SELECT NULL AS TABLE_CAT, COLS.OWNER AS TABLE_SCHEM, COLS.TABLE_NAME, COLS.COLUMN_NAME, COLS.POSITION AS KEY_SEQ, COLS.CONSTRAINT_NAME AS PK_NAME FROM USER_CONS_COLUMNS COLS, USER_CONSTRAINTS CONS WHERE CONS.OWNER=COLS.OWNER AND CONS.CONSTRAINT_NAME=COLS.CONSTRAINT_NAME AND CONS.CONSTRAINT_TYPE='P' AND CONS.OWNER='" + sSchema.toUpperCase() + "' AND CONS.TABLE_NAME='" + sName.toUpperCase()+ "')"); 1160 } 1161 1162 if (null==sSchema) 1163 oRSet = oStmt.executeQuery("SELECT NULL AS TABLE_CAT, COLS.OWNER AS TABLE_SCHEM, COLS.TABLE_NAME, COLS.COLUMN_NAME, COLS.POSITION AS KEY_SEQ, COLS.CONSTRAINT_NAME AS PK_NAME FROM USER_CONS_COLUMNS COLS, USER_CONSTRAINTS CONS WHERE CONS.OWNER=COLS.OWNER AND CONS.CONSTRAINT_NAME=COLS.CONSTRAINT_NAME AND CONS.CONSTRAINT_TYPE='P' AND CONS.TABLE_NAME='" + sName.toUpperCase()+ "'"); 1164 else 1165 oRSet = oStmt.executeQuery("SELECT NULL AS TABLE_CAT, COLS.OWNER AS TABLE_SCHEM, COLS.TABLE_NAME, COLS.COLUMN_NAME, COLS.POSITION AS KEY_SEQ, COLS.CONSTRAINT_NAME AS PK_NAME FROM USER_CONS_COLUMNS COLS, USER_CONSTRAINTS CONS WHERE CONS.OWNER=COLS.OWNER AND CONS.CONSTRAINT_NAME=COLS.CONSTRAINT_NAME AND CONS.CONSTRAINT_TYPE='P' AND CONS.OWNER='" + sSchema.toUpperCase() + "' AND CONS.TABLE_NAME='" + sName.toUpperCase()+ "'"); 1166 1167 } 1169 else { 1170 if (DebugFile.trace) 1171 DebugFile.writeln("DatabaseMetaData.getPrimaryKeys(" + sCatalog + "," + sSchema + "," + sName + ")"); 1172 1173 oRSet = oMData.getPrimaryKeys(sCatalog, sSchema, sName); 1174 } 1175 1176 if (oRSet!=null) { 1177 while (oRSet.next()) { 1178 oPrimaryKeys.add(oRSet.getString(4).toLowerCase()); 1179 sSetPKCols += oRSet.getString(4) + "=? AND "; 1180 } 1182 if (DebugFile.trace) DebugFile.writeln("pk cols " + sSetPKCols); 1183 1184 if (sSetPKCols.length()>7) 1185 sSetPKCols = sSetPKCols.substring(0, sSetPKCols.length()-5); 1186 1187 if (DebugFile.trace) DebugFile.writeln("ResultSet.close()"); 1188 1189 oRSet.close(); 1190 oRSet = null; 1191 } 1193 if (null!=oStmt) { oStmt.close(); oStmt = null; } 1194 1195 oColIterator = oColumns.listIterator(); 1196 1197 while (oColIterator.hasNext()) { 1198 sCol = ((DBColumn) oColIterator.next()).getName(); 1199 1200 sGetAllCols += sCol + ","; 1201 sSetAllCols += "?,"; 1202 1203 if (!oPrimaryKeys.contains(sCol) && !sCol.equalsIgnoreCase(DB.dt_created)) 1204 sSetNoPKCols += sCol + "=?,"; 1205 } 1207 if (DebugFile.trace) DebugFile.writeln("get all cols " + sGetAllCols ); 1208 1209 if (sGetAllCols.length()>0) 1210 sGetAllCols = sGetAllCols.substring(0, sGetAllCols.length()-1); 1211 else 1212 sGetAllCols = "*"; 1213 1214 if (DebugFile.trace) DebugFile.writeln("set all cols " + sSetAllCols ); 1215 1216 if (sSetAllCols.length()>0) 1217 sSetAllCols = sSetAllCols.substring(0, sSetAllCols.length()-1); 1218 1219 if (DebugFile.trace) DebugFile.writeln("set no pk cols " + sSetNoPKCols ); 1220 1221 if (sSetNoPKCols.length()>0) 1222 sSetNoPKCols = sSetNoPKCols.substring(0, sSetNoPKCols.length()-1); 1223 1224 if (DebugFile.trace) DebugFile.writeln("set pk cols " + sSetPKCols ); 1225 1226 if (sSetPKCols.length()>0) { 1227 sSelect = "SELECT " + sGetAllCols + " FROM " + sName + " WHERE " + sSetPKCols; 1228 sInsert = "INSERT INTO " + sName + "(" + sGetAllCols + ") VALUES (" + sSetAllCols + ")"; 1229 if (sSetNoPKCols.length()>0) 1230 sUpdate = "UPDATE " + sName + " SET " + sSetNoPKCols + " WHERE " + sSetPKCols; 1231 else 1232 sUpdate = null; 1233 sDelete = "DELETE FROM " + sName + " WHERE " + sSetPKCols; 1234 sExists = "SELECT NULL FROM " + sName + " WHERE " + sSetPKCols; 1235 } 1236 else { 1237 sSelect = null; 1238 sInsert = "INSERT INTO " + sName + "(" + sGetAllCols + ") VALUES (" + sSetAllCols + ")"; 1239 sUpdate = null; 1240 sDelete = null; 1241 sExists = null; 1242 } 1243 } 1245 if (DebugFile.trace) 1246 { 1247 DebugFile.writeln(sSelect!=null ? sSelect : "NO SELECT STATEMENT"); 1248 DebugFile.writeln(sInsert!=null ? sInsert : "NO INSERT STATEMENT"); 1249 DebugFile.writeln(sUpdate!=null ? sUpdate : "NO UPDATE STATEMENT"); 1250 DebugFile.writeln(sDelete!=null ? sDelete : "NO DELETE STATEMENT"); 1251 DebugFile.writeln(sExists!=null ? sExists : "NO EXISTS STATEMENT"); 1252 1253 DebugFile.decIdent(); 1254 DebugFile.writeln("End DBTable.readColumns()"); 1255 } 1256 1257 } 1260 private String sCatalog; 1261 private String sSchema; 1262 private String sName; 1263 private int iHashCode; 1264 private LinkedList oColumns; 1265 private LinkedList oPrimaryKeys; 1266 1267 private String sSelect; 1268 private String sInsert; 1269 private String sUpdate; 1270 private String sDelete; 1271 private String sExists; 1272 1273}
| Popular Tags
|