1 package jodd.db; 2 3 import java.sql.Array; 4 import java.sql.Connection; 5 import java.sql.PreparedStatement; 6 import java.sql.ResultSet; 7 import java.sql.SQLException; 8 import java.sql.Statement; 9 10 import jodd.db.pool.ConnectionPool; 11 12 18 public class SqlUtil { 19 20 22 private String[] debugSql; 23 private String[] debugSqlParams; 24 private Connection conn = null; 25 private Statement st = null; private PreparedStatement pst = null; private ResultSet resultSet = null; 28 29 31 private ConnectionPool pool = null; 32 33 private Connection providedConnection = null; 34 35 40 public SqlUtil(ConnectionPool cp) { 41 pool = cp; 42 } 43 48 public SqlUtil(Connection conenction) { 49 providedConnection = conenction; 50 } 51 52 54 63 public void setSql(String sql) throws SQLException { 64 setSql(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 65 } 66 67 82 public void setSql(String sql, int resultType, int resultSetConcurrency) throws SQLException { 83 if (resultSet != null) { 84 resultSet.close(); 85 resultSet = null; 86 } 87 if (pst != null) { 88 pst.close(); 89 pst = null; 90 } 91 if (conn == null) { 92 if (pool != null) { 93 conn = pool.getConnection(); 94 } else { 95 conn = providedConnection; 96 } 97 } 98 setCommitMode(); 99 pst = conn.prepareStatement(sql, resultType, resultSetConcurrency); 100 createDebugSql(sql); 101 } 102 103 112 public void setStaticSql(String sql) throws SQLException { 113 setSql(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 114 } 115 116 117 132 public void setStaticSql(String sql, int resultType, int resultSetConcurrency) throws SQLException { 133 if (resultSet != null) { 134 resultSet.close(); 135 resultSet = null; 136 } 137 if (st != null) { 138 st.close(); 139 st = null; 140 } 141 if (conn == null) { 142 if (pool != null) { 143 conn = pool.getConnection(); 144 } else { 145 conn = providedConnection; 146 } 147 } 148 setCommitMode(); 149 st = conn.createStatement(resultType, resultSetConcurrency); 150 debugSql = new String[] { sql }; 151 debugSqlParams = new String[] {""}; 152 } 153 154 156 163 public ResultSet executeQuery() throws SQLException { 164 if (resultSet != null) { 165 resultSet.close(); 166 resultSet = null; 167 } 168 if (st != null) { 169 resultSet = st.executeQuery(debugSql[0]); 170 return resultSet; 171 } 172 if (pst != null) { 173 resultSet = pst.executeQuery(); 174 return resultSet; 175 } 176 return null; 177 } 178 179 185 public int executeUpdate() throws SQLException { 186 if (st != null) { 187 return st.executeUpdate(debugSql[0]); 188 } 189 if (pst != null) { 190 return pst.executeUpdate(); 191 } 192 return -1; 193 } 194 195 196 198 206 public void close(ResultSet rs) { 207 try { 208 if (rs != null) { 209 rs.close(); 210 } 211 } catch (SQLException sex) { 212 } 213 214 try { 215 if (pst != null) { 216 pst.close(); 217 } 218 } catch (SQLException sex) { 219 } finally { 220 pst = null; 221 } 222 223 try { 224 if (st != null) { 225 st.close(); 226 } 227 } catch (SQLException sex) { 228 } finally { 229 st = null; 230 } 231 } 232 233 236 public void close() { 237 try { 239 if (resultSet != null) { 240 resultSet.close(); 241 } 242 } catch (SQLException sex) { 243 } finally { 244 resultSet = null; 245 } 246 247 try { 249 if (pst != null) { 250 pst.close(); 251 } 252 } catch (SQLException sex) { 253 } finally { 254 pst = null; 255 } 256 257 try { 259 if (st != null) { 260 st.close(); 261 } 262 } catch (SQLException sex) { 263 } finally { 264 st = null; 265 } 266 } 267 268 276 public void closeAll(ResultSet rs) { 277 close(rs); 278 resetCommitMode(); 279 if (conn != null) { 280 if (pool != null) { 281 pool.freeConnection(conn); 282 } else { 283 try { 284 conn.close(); 285 } catch (SQLException e) { 286 } 287 } 288 conn = null; 289 } 290 } 291 292 296 public void closeAll() { 297 close(); 298 resetCommitMode(); 299 if (conn != null) { 300 if (pool != null) { 301 pool.freeConnection(conn); 302 } else { 303 try { 304 conn.close(); 305 } catch (SQLException e) { 306 } 307 } 308 conn = null; 309 } 310 } 311 312 314 private static Boolean defaultAutoCommit = null; 315 328 public static void setDefaultAutoCommit(boolean autoCommit) { 329 defaultAutoCommit = Boolean.valueOf(autoCommit); 330 } 331 336 public static Boolean getDefaultAutoCommit() { 337 return defaultAutoCommit; 338 } 339 340 341 private Boolean storedAutoCommit = null; 343 private Boolean currentAutoCommit = null; 345 370 public void setAutoCommit(boolean autoCommit) throws SQLException { 371 currentAutoCommit = Boolean.valueOf(autoCommit); 372 setCommitMode(); 373 } 374 375 381 public Boolean getAutoCommit() { 382 return currentAutoCommit; 383 } 384 385 393 public Boolean getStoredAutoCommit() { 394 return storedAutoCommit; 395 } 396 397 403 private void setCommitMode() throws SQLException { 404 if (conn == null) { 405 return; 406 } 407 if (storedAutoCommit == null) { if (defaultAutoCommit == null) { 410 storedAutoCommit = Boolean.valueOf(conn.getAutoCommit()); } else { 412 storedAutoCommit = defaultAutoCommit; } 414 } 415 boolean newMode; 417 if (currentAutoCommit != null) { newMode = currentAutoCommit.booleanValue(); 419 } else { 420 newMode = storedAutoCommit.booleanValue(); 421 } 422 if (newMode != conn.getAutoCommit()) { conn.setAutoCommit(newMode); } 425 } 426 427 431 private void resetCommitMode() { 432 if (conn == null) { 433 return; 434 } 435 try { 436 if (currentAutoCommit != null) { 437 boolean currentMode = conn.getAutoCommit(); boolean storedMode = storedAutoCommit.booleanValue(); if (currentMode != storedMode) { conn.setAutoCommit(storedMode); } 442 } 443 } catch (SQLException sex) { 444 } finally { 445 currentAutoCommit = null; 446 storedAutoCommit = null; 447 } 448 } 449 450 451 458 public void commit() throws SQLException { 459 conn.commit(); 460 } 461 462 467 public void rollback() { 468 try { 469 conn.rollback(); 470 } catch (SQLException sex) { 471 } 472 } 473 474 476 private static boolean debugMode = false; 477 482 public static void setDebugMode(boolean b) { 483 debugMode = b; 484 } 485 490 public static boolean getDebugMode() { 491 return debugMode; 492 } 493 494 private void createDebugSql(String sql) { 495 if (getDebugMode() == false) { 496 debugSql = new String[] {sql}; 497 return; 498 } 499 debugSql = sql.split("\\?"); 500 debugSqlParams = new String[debugSql.length]; 501 int i = 0; 502 int limit = debugSql.length - 1; 503 while (i < limit) { 504 debugSqlParams[i] = "?"; 505 i++; 506 } 507 if (sql.endsWith("?")) { 508 debugSqlParams[i] = "?"; 509 } else { 510 debugSqlParams[i] = ""; 511 } 512 } 513 514 private void setDebugSql(int ndx, String s) { 515 if (getDebugMode() == false) { 516 return; 517 } 518 debugSqlParams[ndx - 1] = s; 519 } 520 521 private void setDebugSql(int ndx, int i) { 522 if (getDebugMode() == false) { 523 return; 524 } 525 debugSqlParams[ndx - 1] = Integer.toString(i); 526 } 527 528 private void setDebugSql(int ndx, boolean i) { 529 if (getDebugMode() == false) { 530 return; 531 } 532 debugSqlParams[ndx - 1] = Boolean.toString(i); 533 } 534 535 private void setDebugSql(int ndx, long i) { 536 if (getDebugMode() == false) { 537 return; 538 } 539 debugSqlParams[ndx - 1] = Long.toString(i); 540 } 541 542 private void setDebugSql(int ndx, byte i) { 543 if (getDebugMode() == false) { 544 return; 545 } 546 debugSqlParams[ndx - 1] = Byte.toString(i); 547 } 548 549 private void setDebugSql(int ndx, double i) { 550 if (getDebugMode() == false) { 551 return; 552 } 553 debugSqlParams[ndx - 1] = Double.toString(i); 554 } 555 556 private void setDebugSql(int ndx, float i) { 557 if (getDebugMode() == false) { 558 return; 559 } 560 debugSqlParams[ndx - 1] = Float.toString(i); 561 } 562 563 564 565 572 public String toString() { 573 if (getDebugMode() == false) { 574 return debugSql[0]; 575 } 576 StringBuffer result = new StringBuffer(); 577 int i = 0; 578 while (i < debugSql.length) { 579 result.append(debugSql[i]); 580 result.append(debugSqlParams[i]); 581 i++; 582 } 583 return result.toString(); 584 } 585 586 588 589 597 public void setInt(int ndx, int value) throws SQLException { 598 pst.setInt(ndx, value); 599 setDebugSql(ndx, value); 600 } 601 602 610 public void setBoolean(int ndx, boolean value) throws SQLException { 611 pst.setBoolean(ndx, value); 612 setDebugSql(ndx, value); 613 } 614 615 623 public void setLong(int ndx, long value) throws SQLException { 624 pst.setLong(ndx, value); 625 setDebugSql(ndx, value); 626 } 627 628 636 public void setByte(int ndx, byte value) throws SQLException { 637 pst.setByte(ndx, value); 638 setDebugSql(ndx, value); 639 } 640 641 649 public void setDouble(int ndx, double value) throws SQLException { 650 pst.setDouble(ndx, value); 651 setDebugSql(ndx, value); 652 } 653 654 655 663 public void setFloat(int ndx, float value) throws SQLException { 664 pst.setFloat(ndx, value); 665 setDebugSql(ndx, value); 666 } 667 668 669 671 679 public void setString(int ndx, String value) throws SQLException { 680 if (value != null) { 681 pst.setString(ndx, value); 682 } else { 683 pst.setNull(ndx, java.sql.Types.VARCHAR); 684 } 685 setDebugSql(ndx, value); 686 } 687 688 689 697 public void setDate(int ndx, java.sql.Date value) throws SQLException { 698 if (value != null) { 699 pst.setDate(ndx, value); 700 } else { 701 pst.setNull(ndx, java.sql.Types.DATE); 702 } 703 setDebugSql(ndx, value.toString()); 704 } 705 706 714 public void setTimestamp(int ndx, java.sql.Timestamp value) throws SQLException { 715 if (value != null) { 716 pst.setTimestamp(ndx, value); 717 } else { 718 pst.setNull(ndx, java.sql.Types.TIMESTAMP); 719 } 720 setDebugSql(ndx, value.toString()); 721 } 722 723 731 public void setArray(int ndx, Array value) throws SQLException { 732 if (value != null) { 733 pst.setArray(ndx, value); 734 } else { 735 pst.setNull(ndx, java.sql.Types.ARRAY); 736 } 737 setDebugSql(ndx, value.toString()); 738 } 739 740 741 749 public void setBigDecimal(int ndx, java.math.BigDecimal value) throws SQLException { 750 if (value != null) { 751 pst.setBigDecimal(ndx, value); 752 } else { 753 pst.setNull(ndx, java.sql.Types.NUMERIC); 754 } 755 setDebugSql(ndx, value.toString()); 756 } 757 758 766 public void setBlob(int ndx, java.sql.Blob value) throws SQLException { 767 if (value != null) { 768 pst.setBlob(ndx, value); 769 } else { 770 pst.setNull(ndx, java.sql.Types.BLOB); 771 } 772 setDebugSql(ndx, value.toString()); 773 } 774 775 783 public void setClob(int ndx, java.sql.Clob value) throws SQLException { 784 if (value != null) { 785 pst.setClob(ndx, value); 786 } else { 787 pst.setNull(ndx, java.sql.Types.CLOB); 788 } 789 setDebugSql(ndx, value.toString()); 790 } 791 792 793 } 794 | Popular Tags |