1 23 24 package org.dbforms.event.datalist.dao; 25 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 29 import org.dbforms.config.Constants; 30 import org.dbforms.config.DbEventInterceptorData; 31 import org.dbforms.config.DbFormsConfigRegistry; 32 import org.dbforms.config.Field; 33 import org.dbforms.config.FieldTypes; 34 import org.dbforms.config.FieldValue; 35 import org.dbforms.config.FieldValues; 36 import org.dbforms.config.JDBCDataHelper; 37 import org.dbforms.config.ResultSetVector; 38 39 import org.dbforms.util.FileHolder; 40 import org.dbforms.util.UniqueIDGenerator; 41 import org.dbforms.util.Util; 42 43 import java.sql.Connection ; 44 import java.sql.PreparedStatement ; 45 import java.sql.ResultSet ; 46 import java.sql.ResultSetMetaData ; 47 import java.sql.SQLException ; 48 import java.sql.Statement ; 49 50 import java.util.ArrayList ; 51 import java.util.HashMap ; 52 import java.util.Iterator ; 53 import java.util.List ; 54 import java.util.Map ; 55 import java.util.Vector ; 56 57 58 59 65 public class DataSourceJDBC extends DataSource { 66 private static Log logCat = LogFactory.getLog(DataSourceJDBC.class); 67 private Connection connection; 68 private List data; 69 private Map keys; 70 private ResultSet rs; 71 private Statement stmt; 72 private String connectionName; 73 private String query; 74 private String sqlFilter; 75 private String tableList; 76 private String whereClause; 77 private FieldValue[] filterConstraint; 78 private FieldValue[] orderConstraint; 79 private FieldValue[] sqlFilterParams; 80 private boolean calcRowCount = false; 81 private boolean fetchedAll = false; 82 private int colCount; 83 private int rowCount = 0; 84 85 88 public DataSourceJDBC() { 89 data = new ArrayList (); 90 keys = new HashMap (); 91 } 92 93 98 public void setCalcRowCount(boolean calcRowCount) { 99 this.calcRowCount = calcRowCount; 100 } 101 102 103 108 public boolean isCalcRowCount() { 109 return calcRowCount; 110 } 111 112 113 120 public void setSelect(String tableList, 121 String whereClause) { 122 this.tableList = tableList; 123 this.whereClause = whereClause; 124 } 125 126 127 138 public void setSelect(FieldValue[] filterConstraint, 139 FieldValue[] orderConstraint, 140 String sqlFilter, 141 FieldValue[] sqlFilterParams) { 142 this.filterConstraint = filterConstraint; 143 this.orderConstraint = orderConstraint; 144 this.sqlFilter = sqlFilter; 145 this.sqlFilterParams = sqlFilterParams; 146 } 147 148 149 164 public void doDelete(DbEventInterceptorData interceptorData, 165 String keyValuesStr) 166 throws SQLException { 167 FieldValues fieldValues = null; 168 169 if (getTable() 171 .containsDiskblob()) { 172 ResultSet diskblobs = null; 173 StringBuffer queryBuf = new StringBuffer (); 174 queryBuf.append(getTable().getDisblobSelectStatement()); 175 queryBuf.append(" WHERE "); 176 queryBuf.append(getTable().getWhereClauseForKeyFields()); 177 178 PreparedStatement diskblobsPs = interceptorData.getConnection() 179 .prepareStatement(queryBuf 180 .toString()); 181 182 try { 183 getTable() 184 .populateWhereClauseWithKeyFields(keyValuesStr, diskblobsPs, 1); 185 186 diskblobs = diskblobsPs.executeQuery(); 187 188 try { 189 ResultSetVector rsv = new ResultSetVector(getTable(), 190 getTable().getDiskblobs()); 191 rsv.addResultSet(interceptorData, diskblobs); 192 193 if (!ResultSetVector.isNull(rsv)) { 194 rsv.moveFirst(); 195 fieldValues = rsv.getCurrentRowAsFieldValues(); 196 } 197 } finally { 198 diskblobs.close(); 199 } 200 } finally { 201 diskblobsPs.close(); 202 } 203 } 204 205 PreparedStatement ps = interceptorData.getConnection() 207 .prepareStatement(getTable().getDeleteStatement()); 208 209 try { 210 getTable() 214 .populateWhereClauseWithKeyFields(keyValuesStr, ps, 1); 215 216 ps.executeUpdate(); 218 219 if (fieldValues != null) { 220 deleteBlobFilesFromDisk(fieldValues); 221 } 222 } finally { 223 ps.close(); 224 } 225 } 226 227 228 236 public void doInsert(DbEventInterceptorData interceptorData, 237 FieldValues fieldValues) 238 throws SQLException { 239 PreparedStatement ps = interceptorData.getConnection() 240 .prepareStatement(getTable().getInsertStatement(fieldValues)); 241 242 try { 243 fillWithData(ps, fieldValues); 245 ps.executeUpdate(); 246 } finally { 247 ps.close(); 248 } 249 250 saveBlobFilesToDisk(fieldValues); 252 } 253 254 255 271 public void doUpdate(DbEventInterceptorData interceptorData, 272 FieldValues fieldValues, 273 String keyValuesStr) 274 throws SQLException { 275 PreparedStatement ps = interceptorData.getConnection() 276 .prepareStatement(getTable().getUpdateStatement(fieldValues)); 277 278 try { 279 int col = fillWithData(ps, fieldValues); 280 getTable() 281 .populateWhereClauseWithKeyFields(keyValuesStr, ps, col); 282 283 ps.executeUpdate(); 285 } finally { 286 ps.close(); 287 } 288 289 saveBlobFilesToDisk(fieldValues); 291 } 292 293 294 304 protected void setConnection(Connection con, 305 String dbConnectionName) { 306 close(); 307 308 connectionName = Util.isNull(dbConnectionName) ? "default" 310 : dbConnectionName; 311 } 312 313 314 323 protected final Object [] getRow(int i) throws SQLException { 324 Object [] result = null; 325 326 if (i >= 0) { 327 if (i < data.size()) { 328 result = (Object []) data.get(i); 329 } else { 330 if (!fetchedAll) { 331 while (rs.next()) { 332 addRow(); 333 334 if (i < data.size()) { 335 result = (Object []) data.get(i); 336 337 break; 338 } 339 } 340 341 checkResultSetEnd(); 342 } 343 } 344 } 345 346 return result; 347 } 348 349 350 355 protected final void close() { 356 if (data != null) { 357 data.clear(); 358 } 359 360 if (keys != null) { 361 keys.clear(); 362 } 363 364 closeConnection(); 365 366 fetchedAll = false; 368 } 369 370 371 380 protected final int findStartRow(String startRow) throws SQLException { 381 int result = 0; 382 boolean found = false; 383 384 if (startRow != null) { 385 Integer i = (Integer ) keys.get(startRow); 386 387 if (i != null) { 388 result = i.intValue(); 389 found = true; 390 } 391 392 if (!found && !fetchedAll) { 393 while (rs.next()) { 394 String key = addRow(); 395 396 if (startRow.equals(key)) { 397 result = data.size() - 1; 398 399 break; 400 } 401 } 402 403 checkResultSetEnd(); 404 } 405 } 406 407 return result; 408 } 409 410 411 422 protected final boolean hasMore(int i) throws SQLException { 423 return !fetchedAll || (i < size()); 424 } 425 426 427 432 protected void open() throws SQLException { 433 if (!fetchedAll && (rs == null)) { 434 if ((connection == null) || connection.isClosed()) { 435 try { 436 this.connection = DbFormsConfigRegistry.instance() 437 .lookup() 438 .getConnection(connectionName); 439 } catch (Exception e) { 440 logCat.error("open", e); 441 } 442 } 443 444 if (connection == null) { 445 throw new SQLException ("no connection found!"); 446 } 447 448 if (Util.isNull(whereClause)) { 449 query = getTable() 450 .getSelectQuery(getTable().getFields(), 451 filterConstraint, orderConstraint, 452 sqlFilter, Constants.COMPARE_NONE); 453 stmt = connection.prepareStatement(query); 454 455 if (stmt == null) { 456 throw new SQLException ("no statement: " + query); 457 } 458 459 rs = getTable() 462 .getDoSelectResultSet(filterConstraint, orderConstraint, 463 sqlFilterParams, 464 Constants.COMPARE_NONE, 465 (PreparedStatement ) stmt); 466 } else { 467 query = getTable() 468 .getFreeFormSelectQuery(getTable().getFields(), 469 whereClause, tableList); 470 stmt = connection.createStatement(); 471 472 if (stmt == null) { 473 throw new SQLException ("no statement"); 474 } 475 476 rs = stmt.executeQuery(query); 477 } 478 479 ResultSetMetaData rsmd = rs.getMetaData(); 480 colCount = rsmd.getColumnCount(); 481 482 if (isCalcRowCount()) { 483 Field f = new Field(); 484 f.setName("count(*) cnt"); 485 Vector v = new Vector(); 486 v.add(f); 487 ResultSet prs = null; 489 if (Util.isNull(whereClause)) { 490 String pquery = getTable() 491 .getSelectQuery(v, 492 filterConstraint, orderConstraint, 493 sqlFilter, Constants.COMPARE_NONE); 494 PreparedStatement pstmt = connection.prepareStatement(pquery); 495 496 if (pstmt == null) { 497 throw new SQLException ("no statement: " + pquery); 498 } 499 500 prs = getTable() 501 .getDoSelectResultSet(filterConstraint, orderConstraint, 502 sqlFilterParams, 503 Constants.COMPARE_NONE, 504 pstmt); 505 } else { 506 String pquery = getTable() 507 .getFreeFormSelectQuery(v, 508 whereClause, tableList); 509 Statement pstmt = connection.createStatement(); 510 511 if (pstmt == null) { 512 throw new SQLException ("no statement"); 513 } 514 515 prs = pstmt.executeQuery(pquery); 516 } 517 prs.next(); 518 rowCount = prs.getInt(prs.findColumn("cnt")); 519 } 520 } 521 } 522 523 524 531 protected final int size() throws SQLException { 532 if (!fetchedAll) { 539 while (rs.next()) { 540 try { 541 addRow(); 542 } catch (Exception e) { 543 logCat.error("size", e); 544 545 break; 546 } 547 } 548 549 closeConnection(); 550 } 551 552 return data.size(); 553 } 554 555 556 561 protected int getRowCount() { 562 return rowCount; 563 } 564 565 566 private String addRow() throws SQLException { 567 Integer j = new Integer (data.size()); 568 Object [] objectRow = new Object [colCount]; 569 String [] stringRow = new String [colCount]; 570 571 for (int i = 0; i < colCount; i++) { 572 objectRow[i] = JDBCDataHelper.getData(rs, 573 getTable().getField(i).getEscaper(), 574 i + 1); 575 stringRow[i] = (objectRow[i] != null) ? objectRow[i].toString() 576 : null; 577 } 578 579 data.add(objectRow); 580 581 String key = getTable() 582 .getKeyPositionString(stringRow); 583 keys.put(key, j); 584 585 return key; 586 } 587 588 589 private void checkResultSetEnd() throws SQLException { 590 if ((rs.getRow() != 0)) { 591 if (rs.next()) { 594 addRow(); 595 } 596 } 597 598 if ((rs.getRow() == 0) ) { 599 closeConnection(); 600 } 601 } 602 603 604 private void closeConnection() { 605 fetchedAll = true; 606 607 if (rs != null) { 608 try { 609 rs.close(); 610 } catch (SQLException e) { 611 logCat.info("closeConnection", e); 612 } 613 614 rs = null; 615 } 616 617 if (stmt != null) { 618 try { 619 stmt.close(); 620 } catch (SQLException e) { 621 logCat.info("closeConnection", e); 622 } 623 624 stmt = null; 625 } 626 627 if (connection != null) { 628 try { 629 if (!connection.isClosed()) { 630 connection.close(); 631 } 632 } catch (SQLException e) { 633 logCat.info("closeConnection", e); 634 } 635 636 connection = null; 637 } 638 } 639 640 641 private int fillWithData(PreparedStatement ps, 644 FieldValues fieldValues) 645 throws SQLException { 646 Iterator e = fieldValues.keys(); 649 int col = 1; 650 651 while (e.hasNext()) { 652 String fieldName = (String ) e.next(); 653 Field curField = getTable() 654 .getFieldByName(fieldName); 655 656 if (curField != null) { 657 FieldValue fv = fieldValues.get(fieldName); 658 659 logCat.debug("Retrieved curField:" + curField.getName() + " type:" 660 + curField.getType()); 661 662 int fieldType = curField.getType(); 663 Object value = null; 664 665 if (fieldType == FieldTypes.BLOB) { 666 if (fv.getFileHolder() == null) { value = fv.getFieldValue(); 669 } else { value = fv.getFileHolder(); 671 } 672 } else if (fieldType == FieldTypes.DISKBLOB) { 673 FileHolder fileHolder = fv.getFileHolder(); 674 675 String fileName = fileHolder.getFileName(); 677 678 if (curField.hasEncodedSet()) { 680 int dotIndex = fileName.lastIndexOf('.'); 681 String suffix = (dotIndex != -1) 682 ? fileName.substring(dotIndex) 683 : ""; 684 fileHolder.setFileName(UniqueIDGenerator.getUniqueID() 685 + suffix); 686 687 value = fileHolder.getFileName(); 690 } else { 691 value = fileName; 694 } 695 } else { 696 value = fv.getFieldValueAsObject(); 699 } 700 701 logCat.info("field=" + curField.getName() + " col=" + col 702 + " value=" + value + " type=" + fieldType); 703 JDBCDataHelper.fillWithData(ps, curField.getEscaper(), col, value, 704 fieldType, 705 getTable().getBlobHandlingStrategy()); 706 col++; 707 } 708 } 709 710 return col; 711 } 712 } 713
| Popular Tags
|