1 21 22 package org.apache.derby.tools; 23 24 import java.io.PrintStream ; 25 import java.io.PrintWriter ; 26 import java.io.File ; 27 import java.io.FileNotFoundException ; 28 import java.io.IOException ; 29 30 import java.sql.Connection ; 31 import java.sql.SQLException ; 32 import java.sql.SQLWarning ; 33 import java.sql.Statement ; 34 import java.sql.PreparedStatement ; 35 import java.sql.ResultSet ; 36 import java.sql.ResultSetMetaData ; 37 import java.sql.Types ; 38 39 import java.util.Properties ; 40 import java.util.Enumeration ; 41 import java.util.Vector ; 42 43 import org.apache.derby.iapi.tools.i18n.LocalizedResource; 44 45 import org.apache.derby.impl.tools.ij.ijException; 46 47 58 public class JDBCDisplayUtil { 59 60 static final private int MINWIDTH = 4; 62 static private int maxWidth = 128; 63 static public boolean showSelectCount = false; 64 65 static { 66 LocalizedResource.getInstance(); 68 } 69 70 73 81 static public void ShowException(PrintWriter out, Throwable e) { 82 if (e == null) return; 83 84 if (e instanceof SQLException ) 85 ShowSQLException(out, (SQLException )e); 86 else 87 e.printStackTrace(out); 88 } 89 90 97 static public void ShowSQLException(PrintWriter out, SQLException e) { 98 String errorCode; 99 100 if (Boolean.getBoolean("ij.showErrorCode")) { 101 errorCode = LocalizedResource.getMessage("UT_Error0", LocalizedResource.getNumber(e.getErrorCode())); 102 } 103 else { 104 errorCode = ""; 105 } 106 107 while (e!=null) { 108 String p1 = mapNull(e.getSQLState(),LocalizedResource.getMessage("UT_NoSqlst")); 109 String p2 = mapNull(e.getMessage(),LocalizedResource.getMessage("UT_NoMessa")); 110 out.println(LocalizedResource.getMessage("UT_Error012", p1, p2,errorCode)); 111 doTrace(out, e); 112 e=e.getNextException(); 113 } 114 } 115 116 124 static public void ShowWarnings(PrintWriter out, Connection theConnection) { 125 try { 126 SQLWarning warning = null; 128 129 if (theConnection != null) { 130 ShowWarnings(out, theConnection.getWarnings()); 131 } 132 133 if (theConnection != null) { 134 theConnection.clearWarnings(); 135 } 136 } catch (SQLException e) { 137 ShowSQLException(out, e); 138 } 139 } 141 145 static public void ShowWarnings(PrintWriter out, SQLWarning warning) { 146 while (warning != null) { 147 String p1 = mapNull(warning.getSQLState(),LocalizedResource.getMessage("UT_NoSqlst_7")); 148 String p2 = mapNull(warning.getMessage(),LocalizedResource.getMessage("UT_NoMessa_8")); 149 out.println(LocalizedResource.getMessage("UT_Warni01", p1, p2)); 150 warning = warning.getNextWarning(); 151 } 152 } 153 154 162 static public void ShowWarnings(PrintWriter out, ResultSet rs) { 163 try { 164 SQLWarning warning = null; 166 167 if (rs != null) { 168 ShowWarnings(out, rs.getWarnings()); 169 } 170 171 if (rs != null) { 172 rs.clearWarnings(); 173 } 174 } catch (SQLException e) { 175 ShowSQLException(out, e); 176 } 177 } 179 187 static public void ShowWarnings(PrintWriter out, Statement s) 188 { 189 try { 190 SQLWarning warning = null; 192 193 if (s != null) { 194 ShowWarnings(out, s.getWarnings()); 195 } 196 197 if (s != null) { 198 s.clearWarnings(); 199 } 200 } catch (SQLException e) { 201 ShowSQLException(out, e); 202 } 203 } 205 208 static final private int MAX_RETRIES = 0; 210 211 224 static public void DisplayResults(PrintWriter out, Statement stmt, Connection conn ) 225 throws SQLException 226 { 227 indent_DisplayResults( out, stmt, conn, 0, null, null); 228 } 229 230 static private void indent_DisplayResults 231 (PrintWriter out, Statement stmt, Connection conn, int indentLevel, 232 int[] displayColumns, int[] displayColumnWidths) 233 throws SQLException { 234 235 checkNotNull(stmt, "Statement"); 236 237 ResultSet rs = stmt.getResultSet(); 238 if (rs != null) { 239 indent_DisplayResults(out, rs, conn, indentLevel, 240 displayColumns, displayColumnWidths); 241 rs.close(); } 243 else { 244 DisplayUpdateCount(out,stmt.getUpdateCount(), indentLevel); 245 } 246 247 ShowWarnings(out,stmt); 248 } 250 255 static void DisplayUpdateCount(PrintWriter out, int count, int indentLevel ) { 256 if (count == 1) { 257 indentedPrintLine( out, indentLevel, LocalizedResource.getMessage("UT_1RowInserUpdatDelet")); 258 } 259 else if (count >= 0) { 260 indentedPrintLine( out, indentLevel, LocalizedResource.getMessage("UT_0RowsInserUpdatDelet", LocalizedResource.getNumber(count))); 261 } 262 else { 263 indentedPrintLine( out, indentLevel, LocalizedResource.getMessage("UT_StateExecu")); 264 } 265 } 266 267 271 static private int[] getColumnDisplayWidths(ResultSetMetaData rsmd, int[] dispColumns, 272 boolean localizedOutput) 273 throws SQLException { 274 int count = (dispColumns == null) ? rsmd.getColumnCount() : dispColumns.length; 275 int[] widths = new int[count]; 276 277 for(int i=0; i<count; i++) { 278 int colnum = (dispColumns == null) ? (i + 1) : dispColumns[i]; 279 int dispsize = localizedOutput 280 ? LocalizedResource.getInstance().getColumnDisplaySize(rsmd, colnum) 281 : rsmd.getColumnDisplaySize(colnum); 282 widths[i] = Math.min(maxWidth, 283 Math.max((rsmd.isNullable(colnum) == ResultSetMetaData.columnNoNulls)? 284 0 : MINWIDTH, dispsize)); 285 } 286 return widths; 287 } 288 289 290 299 static public void DisplayResults(PrintWriter out, ResultSet rs, Connection conn, 300 int[] displayColumns, int[] displayColumnWidths) 301 throws SQLException 302 { 303 indent_DisplayResults( out, rs, conn, 0, displayColumns, 304 displayColumnWidths); 305 } 306 307 static private void indent_DisplayResults 308 (PrintWriter out, ResultSet rs, Connection conn, int indentLevel, 309 int[] displayColumns, int[] displayColumnWidths) 310 throws SQLException { 311 ResultSetMetaData rsmd = rs.getMetaData(); 312 checkNotNull(rsmd, "ResultSetMetaData"); 313 Vector nestedResults; 314 int numberOfRowsSelected = 0; 315 316 if (!conn.getAutoCommit()) 319 nestedResults = new Vector (); 320 else 321 nestedResults = null; 322 323 if(displayColumnWidths == null) 324 displayColumnWidths = getColumnDisplayWidths(rsmd, displayColumns,true); 325 326 int len = indent_DisplayBanner(out,rsmd, indentLevel, displayColumns, 327 displayColumnWidths); 328 329 boolean doNext = true; 332 int retry = 0; 333 while (doNext) { 334 try { 335 doNext = rs.next(); 336 if (doNext) { 337 338 DisplayRow(out, rs, rsmd, len, nestedResults, conn, 339 indentLevel, displayColumns, 340 displayColumnWidths); 341 ShowWarnings(out, rs); 342 numberOfRowsSelected++; 343 } 344 } catch (SQLException e) { 345 if (++retry > MAX_RETRIES) 348 throw e; 349 else 350 ShowSQLException(out, e); 351 } 352 } 353 if (showSelectCount == true) { 354 if (numberOfRowsSelected == 1) { 355 out.println(); 356 indentedPrintLine( out, indentLevel, LocalizedResource.getMessage("UT_1RowSelec")); 357 } else if (numberOfRowsSelected >= 0) { 358 out.println(); 359 indentedPrintLine( out, indentLevel, 360 LocalizedResource.getMessage("UT_0RowsSelec", LocalizedResource.getNumber(numberOfRowsSelected))); 361 } 362 } 363 364 DisplayNestedResults(out, nestedResults, conn, indentLevel ); 365 nestedResults = null; 366 } 367 368 376 static private void DisplayNestedResults(PrintWriter out, Vector nr, Connection conn, int indentLevel ) 377 throws SQLException { 378 379 if (nr == null) return; 380 381 String b=LocalizedResource.getMessage("UT_JDBCDisplayUtil_16"); 382 String oldString="0"; 383 384 for (int i=0; i < nr.size(); i++) { 385 LocalizedResource.OutputWriter().println(); 386 387 String t = Integer.toString(i); 389 if (t.length() > oldString.length()) { 390 oldString = t; 391 b=b+LocalizedResource.getMessage("UT_JDBCDisplayUtil_17"); 392 } 393 394 LocalizedResource.OutputWriter().println(b); 395 LocalizedResource.OutputWriter().println(LocalizedResource.getMessage("UT_Resul0", LocalizedResource.getNumber(i))); 396 LocalizedResource.OutputWriter().println(b); 397 indent_DisplayResults(out, (ResultSet ) nr.elementAt(i), conn, 398 indentLevel, null, null); 399 } 400 } 401 402 412 static public void DisplayNextRow(PrintWriter out, ResultSet rs, Connection conn ) 413 throws SQLException 414 { 415 indent_DisplayNextRow( out, rs, conn, 0, null, (rs == null) ? null 416 : getColumnDisplayWidths(rs.getMetaData(), null, true)); 417 } 418 419 static private void indent_DisplayNextRow(PrintWriter out, ResultSet rs, Connection conn, int indentLevel, 420 int[] displayColumns, int[] displayColumnWidths ) 421 throws SQLException { 422 423 Vector nestedResults; 424 425 if (!conn.getAutoCommit()) 428 nestedResults = new Vector (); 429 else 430 nestedResults = null; 431 432 checkNotNull(rs, "ResultSet"); 433 434 ResultSetMetaData rsmd = rs.getMetaData(); 435 checkNotNull(rsmd, "ResultSetMetaData"); 436 437 if (rs.next()) { 439 int rowLen = indent_DisplayBanner(out, rsmd, indentLevel, displayColumns, displayColumnWidths); 440 DisplayRow(out, rs, rsmd, rowLen, nestedResults, conn, indentLevel, 441 null, null ); 442 } 443 else { 444 indentedPrintLine( out, indentLevel, LocalizedResource.getMessage("UT_NoCurreRow")); 445 } 446 447 ShowWarnings(out, rs); 448 449 DisplayNestedResults(out, nestedResults, conn, indentLevel ); 450 nestedResults = null; 451 452 } 454 464 static public void DisplayCurrentRow(PrintWriter out, ResultSet rs, Connection conn ) 465 throws SQLException 466 { 467 indent_DisplayCurrentRow( out, rs, conn, 0, null, (rs == null) ? null 468 : getColumnDisplayWidths(rs.getMetaData(), null, true) ); 469 } 470 471 static private void indent_DisplayCurrentRow(PrintWriter out, ResultSet rs, Connection conn, 472 int indentLevel, int[] displayColumns, int[] displayColumnWidths ) 473 throws SQLException { 474 475 Vector nestedResults; 476 477 if (rs == null) { 478 indentedPrintLine( out, indentLevel, LocalizedResource.getMessage("UT_NoCurreRow_19")); 479 return; 480 } 481 482 if (!conn.getAutoCommit()) 485 nestedResults = new Vector (); 486 else 487 nestedResults = null; 488 489 ResultSetMetaData rsmd = rs.getMetaData(); 490 checkNotNull(rsmd, "ResultSetMetaData"); 491 492 int rowLen = indent_DisplayBanner(out, rsmd, indentLevel, displayColumns, displayColumnWidths); 493 DisplayRow(out, rs, rsmd, rowLen, nestedResults, conn, indentLevel, 494 displayColumns, displayColumnWidths ); 495 496 ShowWarnings(out, rs); 497 498 DisplayNestedResults(out, nestedResults, conn, indentLevel ); 499 nestedResults = null; 500 501 } 503 513 static public int DisplayBanner(PrintWriter out, ResultSetMetaData rsmd ) 514 throws SQLException 515 { 516 return indent_DisplayBanner( out, rsmd, 0, null, 517 getColumnDisplayWidths(rsmd, null, true) ); 518 } 519 520 static private int indent_DisplayBanner(PrintWriter out, ResultSetMetaData rsmd, int indentLevel, 521 int[] displayColumns, int[] displayColumnWidths ) 522 throws SQLException { 523 524 StringBuffer buf = new StringBuffer (); 525 526 int numCols = displayColumnWidths.length; 527 int rowLen; 528 529 rowLen = (numCols - 1); for (int i=1; i <= numCols; i++) 533 rowLen += displayColumnWidths[i-1]; 534 buf.ensureCapacity(rowLen); 535 536 for (int i=1; i <= numCols; i++) { 540 int colnum = displayColumns==null ? i : displayColumns[i-1]; 541 542 if (i>1) 543 buf.append('|'); 544 545 String s = rsmd.getColumnLabel(colnum); 546 547 int w = displayColumnWidths[i-1]; 548 549 if (s.length() < w) { 550 551 buf.append(s); 552 553 int k = w - s.length(); 555 for (; k >= 64; k -= 64) 556 buf.append( 557 " "); 558 for (; k >= 16; k -= 16) 559 buf.append(" "); 560 for (; k >= 4; k -= 4) 561 buf.append(" "); 562 for (; k > 0; k--) 563 buf.append(' '); 564 } 565 else if (s.length() > w) { 566 if (w > 1) 567 buf.append(s.substring(0,w-1)); 568 if (w > 0) 569 buf.append('&'); 570 } 571 else { 572 buf.append(s); 573 } 574 } 575 576 buf.setLength(Math.min(rowLen, 1024)); 577 indentedPrintLine( out, indentLevel, buf); 578 579 for (int i=0; i<Math.min(rowLen, 1024); i++) 581 buf.setCharAt(i, '-'); 582 indentedPrintLine( out, indentLevel, buf); 583 584 buf = null; 585 586 return rowLen; 587 } 589 606 static private void DisplayRow(PrintWriter out, ResultSet rs, ResultSetMetaData rsmd, int rowLen, Vector nestedResults, Connection conn, int indentLevel, 607 int[] displayColumns, int[] displayColumnWidths ) 608 throws SQLException 609 { 610 StringBuffer buf = new StringBuffer (); 611 buf.ensureCapacity(rowLen); 612 613 int numCols = displayColumnWidths.length; 614 int i; 615 616 for (i=1; i <= numCols; i++){ 620 int colnum = displayColumns==null ? i : displayColumns[i-1]; 621 if (i>1) 622 buf.append('|'); 623 624 String s; 625 switch (rsmd.getColumnType(colnum)) { 626 default: 627 s = LocalizedResource.getInstance().getLocalizedString(rs, rsmd, colnum ); 628 break; 629 case org.apache.derby.iapi.reference.JDBC20Translation.SQL_TYPES_JAVA_OBJECT: 630 case Types.OTHER: 631 { 632 Object o = rs.getObject(colnum); 633 if (o == null) { s = "NULL"; } 634 else if (o instanceof ResultSet && nestedResults != null) 635 { 636 s = LocalizedResource.getMessage("UT_Resul0_20", LocalizedResource.getNumber(nestedResults.size())); 637 nestedResults.addElement(o); 638 } 639 else 640 { 641 try { 642 s = rs.getString(colnum); 643 } catch (SQLException se) { 644 s = o.toString(); 646 } 647 } 648 } 649 break; 650 } 651 if (s==null) s = "NULL"; 652 653 int w = displayColumnWidths[i-1]; 654 if (s.length() < w) { 655 StringBuffer fullS = new StringBuffer (s); 656 fullS.ensureCapacity(w); 657 for (int k=s.length(); k<w; k++) 658 fullS.append(' '); 659 s = fullS.toString(); 660 } 661 else if (s.length() > w) 662 s = s.substring(0,w-1)+"&"; 664 665 buf.append(s); 666 } 667 indentedPrintLine( out, indentLevel, buf); 668 669 } 671 679 public static void checkNotNull(Object o, String what) { 680 if (o == null) { 681 throw ijException.objectWasNull(what); 682 } 683 } 685 693 static public String mapNull(String s, String nullValue) { 694 if (s==null) return nullValue; 695 return s; 696 } 697 698 705 static public void doTrace(PrintWriter out, Exception e) { 706 if (Boolean.getBoolean("ij.exceptionTrace")) { 707 e.printStackTrace(out); 708 out.flush(); 709 } 710 } 711 712 static public void setMaxDisplayWidth(int maxDisplayWidth) { 713 maxWidth = maxDisplayWidth; 714 } 715 716 static private void indentedPrintLine( PrintWriter out, int indentLevel, String text ) 717 { 718 indent( out, indentLevel ); 719 out.println( text ); 720 } 721 722 static private void indentedPrintLine( PrintWriter out, int indentLevel, StringBuffer text ) 723 { 724 indent( out, indentLevel ); 725 out.println( text ); 726 } 727 728 static private void indent( PrintWriter out, int indentLevel ) 729 { 730 for ( int ictr = 0; ictr < indentLevel; ictr++ ) { out.print( " " ); } 731 } 732 733 735 static public void ShowException(PrintStream out, Throwable e) { 736 if (e == null) return; 737 738 if (e instanceof SQLException ) 739 ShowSQLException(out, (SQLException )e); 740 else 741 e.printStackTrace(out); 742 } 743 744 static public void ShowSQLException(PrintStream out, SQLException e) { 745 String errorCode; 746 747 if (Boolean.getBoolean("ij.showErrorCode")) { 748 errorCode = " (errorCode = " + e.getErrorCode() + ")"; 749 } 750 else { 751 errorCode = ""; 752 } 753 754 while (e!=null) { 755 out.println("ERROR "+mapNull(e.getSQLState(),"(no SQLState)")+": "+ 756 mapNull(e.getMessage(),"(no message)")+errorCode); 757 doTrace(out, e); 758 e=e.getNextException(); 759 } 760 } 761 762 static public void ShowWarnings(PrintStream out, Connection theConnection) { 763 try { 764 SQLWarning warning = null; 766 767 if (theConnection != null) { 768 ShowWarnings(out, theConnection.getWarnings()); 769 } 770 771 if (theConnection != null) { 772 theConnection.clearWarnings(); 773 } 774 } catch (SQLException e) { 775 ShowSQLException(out, e); 776 } 777 } 779 static public void ShowWarnings(PrintStream out, SQLWarning warning) { 780 while (warning != null) { 781 out.println("WARNING "+ 782 mapNull(warning.getSQLState(),"(no SQLState)")+": "+ 783 mapNull(warning.getMessage(),"(no message)")); 784 warning = warning.getNextWarning(); 785 } 786 } 787 788 static public void ShowWarnings(PrintStream out, ResultSet rs) { 789 try { 790 SQLWarning warning = null; 792 793 if (rs != null) { 794 ShowWarnings(out, rs.getWarnings()); 795 } 796 797 if (rs != null) { 798 rs.clearWarnings(); 799 } 800 } catch (SQLException e) { 801 ShowSQLException(out, e); 802 } 803 } 805 static public void ShowWarnings(PrintStream out, Statement s) 806 { 807 try { 808 SQLWarning warning = null; 810 811 if (s != null) { 812 ShowWarnings(out, s.getWarnings()); 813 } 814 815 if (s != null) { 816 s.clearWarnings(); 817 } 818 } catch (SQLException e) { 819 ShowSQLException(out, e); 820 } 821 } 823 static public void DisplayResults(PrintStream out, Statement stmt, Connection conn ) 824 throws SQLException 825 { 826 indent_DisplayResults( out, stmt, conn, 0, null, null); 827 } 828 829 static private void indent_DisplayResults 830 (PrintStream out, Statement stmt, Connection conn, int indentLevel, 831 int[] displayColumns, int[] displayColumnWidths) 832 throws SQLException { 833 834 checkNotNull(stmt, "Statement"); 835 836 ResultSet rs = stmt.getResultSet(); 837 if (rs != null) { 838 indent_DisplayResults(out, rs, conn, indentLevel, displayColumns, 839 displayColumnWidths); 840 rs.close(); } 842 else { 843 DisplayUpdateCount(out,stmt.getUpdateCount(), indentLevel); 844 } 845 846 ShowWarnings(out,stmt); 847 } 849 static void DisplayUpdateCount(PrintStream out, int count, int indentLevel ) { 850 if (count == 1) { 851 indentedPrintLine( out, indentLevel, "1 row inserted/updated/deleted"); 852 } 853 else if (count >= 0) { 854 indentedPrintLine( out, indentLevel, count+" rows inserted/updated/deleted"); 855 } 856 else { 857 indentedPrintLine( out, indentLevel, "Statement executed."); 858 } 859 } 860 861 static public void DisplayResults(PrintStream out, ResultSet rs, Connection conn) 862 throws SQLException 863 { 864 indent_DisplayResults( out, rs, conn, 0, null, null); 865 } 866 867 static private void indent_DisplayResults 868 (PrintStream out, ResultSet rs, Connection conn, int indentLevel, 869 int[] displayColumns, int[] displayColumnWidths) 870 throws SQLException { 871 ResultSetMetaData rsmd = rs.getMetaData(); 872 checkNotNull(rsmd, "ResultSetMetaData"); 873 Vector nestedResults; 874 int numberOfRowsSelected = 0; 875 876 if (!conn.getAutoCommit()) 879 nestedResults = new Vector (); 880 else 881 nestedResults = null; 882 883 if(displayColumnWidths == null) 884 displayColumnWidths = getColumnDisplayWidths(rsmd, displayColumns, false); 885 886 int len = indent_DisplayBanner(out,rsmd, indentLevel, displayColumns, 887 displayColumnWidths); 888 889 boolean doNext = true; 892 int retry = 0; 893 while (doNext) { 894 try { 895 doNext = rs.next(); 896 if (doNext) { 897 898 DisplayRow(out, rs, rsmd, len, nestedResults, conn, 899 indentLevel, displayColumns, 900 displayColumnWidths); 901 ShowWarnings(out, rs); 902 numberOfRowsSelected++; 903 } 904 } catch (SQLException e) { 905 if (++retry > MAX_RETRIES) 908 throw e; 909 else 910 ShowSQLException(out, e); 911 } 912 } 913 if (showSelectCount == true) { 914 if (numberOfRowsSelected == 1) { 915 out.println(); 916 indentedPrintLine( out, indentLevel, "1 row selected"); 917 } else if (numberOfRowsSelected >= 0) { 918 out.println(); 919 indentedPrintLine( out, indentLevel, numberOfRowsSelected + " rows selected"); 920 } 921 } 922 923 DisplayNestedResults(out, nestedResults, conn, indentLevel ); 924 nestedResults = null; 925 } 926 927 static private void DisplayNestedResults(PrintStream out, Vector nr, Connection conn, int indentLevel ) 928 throws SQLException { 929 930 if (nr == null) return; 931 932 String s="+ ResultSet #"; 933 String b="++++++++++++++++"; 934 String oldString="0"; 935 936 for (int i=0; i < nr.size(); i++) { 937 System.out.println(); 938 939 String t = Integer.toString(i); 941 if (t.length() > oldString.length()) { 942 oldString = t; 943 b=b+"+"; 944 } 945 946 System.out.println(b); 947 System.out.println(s+i+" +"); 948 System.out.println(b); 949 indent_DisplayResults(out, (ResultSet ) nr.elementAt(i), conn, 950 indentLevel, null, null); 951 } 952 } 953 954 static public void DisplayNextRow(PrintStream out, ResultSet rs, Connection conn ) 955 throws SQLException 956 { 957 indent_DisplayNextRow( out, rs, conn, 0, null, (rs == null) ? null 958 : getColumnDisplayWidths(rs.getMetaData(),null,false) ); 959 } 960 961 static private void indent_DisplayNextRow(PrintStream out, ResultSet rs, Connection conn, int indentLevel, 962 int[] displayColumns, int[] displayColumnWidths ) 963 throws SQLException { 964 965 Vector nestedResults; 966 967 if (!conn.getAutoCommit()) 970 nestedResults = new Vector (); 971 else 972 nestedResults = null; 973 974 checkNotNull(rs, "ResultSet"); 975 976 ResultSetMetaData rsmd = rs.getMetaData(); 977 checkNotNull(rsmd, "ResultSetMetaData"); 978 979 if (rs.next()) { 981 int rowLen = indent_DisplayBanner(out, rsmd, indentLevel, null, null); 982 DisplayRow(out, rs, rsmd, rowLen, nestedResults, conn, indentLevel, 983 displayColumns, displayColumnWidths); 984 } 985 else { 986 indentedPrintLine( out, indentLevel, LocalizedResource.getMessage("UT_NoCurreRow")); 987 } 988 989 ShowWarnings(out, rs); 990 991 DisplayNestedResults(out, nestedResults, conn, indentLevel ); 992 nestedResults = null; 993 994 } 996 static public void DisplayCurrentRow(PrintStream out, ResultSet rs, Connection conn ) 997 throws SQLException 998 { 999 indent_DisplayCurrentRow( out, rs, conn, 0, null, (rs == null) ? null 1000 : getColumnDisplayWidths(rs.getMetaData(),null,false) ); 1001 } 1002 1003 static private void indent_DisplayCurrentRow(PrintStream out, ResultSet rs, Connection conn, 1004 int indentLevel, int[] displayColumns, int[] displayColumnWidths ) 1005 throws SQLException { 1006 1007 Vector nestedResults; 1008 1009 if (rs == null) { 1010 indentedPrintLine( out, indentLevel, LocalizedResource.getMessage("UT_NoCurreRow_19")); 1011 return; 1012 } 1013 1014 if (!conn.getAutoCommit()) 1017 nestedResults = new Vector (); 1018 else 1019 nestedResults = null; 1020 1021 ResultSetMetaData rsmd = rs.getMetaData(); 1022 checkNotNull(rsmd, "ResultSetMetaData"); 1023 1024 int rowLen = indent_DisplayBanner(out, rsmd, indentLevel, displayColumns, displayColumnWidths); 1025 DisplayRow(out, rs, rsmd, rowLen, nestedResults, conn, indentLevel, 1026 displayColumns, displayColumnWidths); 1027 1028 ShowWarnings(out, rs); 1029 1030 DisplayNestedResults(out, nestedResults, conn, indentLevel ); 1031 nestedResults = null; 1032 1033 } 1035 static public int DisplayBanner(PrintStream out, ResultSetMetaData rsmd ) 1036 throws SQLException 1037 { 1038 return indent_DisplayBanner( out, rsmd, 0, null, 1039 getColumnDisplayWidths(rsmd,null,false) ); 1040 } 1041 1042 static private int indent_DisplayBanner(PrintStream out, ResultSetMetaData rsmd, int indentLevel, 1043 int[] displayColumns, int[] displayColumnWidths ) 1044 throws SQLException { 1045 1046 StringBuffer buf = new StringBuffer (); 1047 1048 int numCols = displayColumnWidths.length; 1049 int rowLen; 1050 1051 rowLen = (numCols - 1); for (int i=1; i <= numCols; i++) { 1055 rowLen += displayColumnWidths[i-1]; 1056 } 1057 buf.ensureCapacity(rowLen); 1058 1059 for (int i=1; i <= numCols; i++) { 1063 int colnum = displayColumns==null ? i : displayColumns[i-1]; 1064 1065 if (i>1) 1066 buf.append('|'); 1067 1068 String s = rsmd.getColumnLabel(colnum); 1069 1070 int w = displayColumnWidths[i-1]; 1071 1072 if (s.length() < w) { 1073 StringBuffer blanks = new StringBuffer (s); 1075 blanks.ensureCapacity(w); 1076 1077 for (int k=blanks.length()+64; k<=w; k+=64) 1079 blanks.append( 1080 " "); 1081 for (int k=blanks.length()+16; k<=w; k+=16) 1082 blanks.append(" "); 1083 for (int k=blanks.length()+4; k<=w; k+=4) 1084 blanks.append(" "); 1085 for (int k=blanks.length(); k<w; k++) 1086 blanks.append(' '); 1087 1088 buf.append(blanks); 1089 } 1092 else if (s.length() > w) { 1093 if (w > 1) 1094 buf.append(s.substring(0,w-1)); 1095 if (w > 0) 1096 buf.append('&'); 1097 } 1098 else { 1099 buf.append(s); 1100 } 1101 } 1102 1103 buf.setLength(Math.min(rowLen, 1024)); 1104 indentedPrintLine( out, indentLevel, buf); 1105 1106 for (int i=0; i<Math.min(rowLen, 1024); i++) 1108 buf.setCharAt(i, '-'); 1109 indentedPrintLine( out, indentLevel, buf); 1110 1111 buf = null; 1112 1113 return rowLen; 1114 } 1116 static private void DisplayRow(PrintStream out, ResultSet rs, ResultSetMetaData rsmd, int rowLen, Vector nestedResults, Connection conn, int indentLevel, 1117 int[] displayColumns, int[] displayColumnWidths) 1118 throws SQLException 1119 { 1120 StringBuffer buf = new StringBuffer (); 1121 buf.ensureCapacity(rowLen); 1122 1123 int numCols = displayColumnWidths.length; 1124 int i; 1125 1126 for (i=1; i <= numCols; i++){ 1130 int colnum = displayColumns==null ? i : displayColumns[i-1]; 1131 if (i>1) 1132 buf.append('|'); 1133 1134 String s; 1135 switch (rsmd.getColumnType(colnum)) { 1136 default: 1137 s = rs.getString(colnum); 1138 break; 1139 case org.apache.derby.iapi.reference.JDBC20Translation.SQL_TYPES_JAVA_OBJECT: 1140 case Types.OTHER: 1141 { 1142 Object o = rs.getObject(colnum); 1143 if (o == null) { s = "NULL"; } 1144 else if (o instanceof ResultSet && nestedResults != null) 1145 { 1146 s = "ResultSet #"+nestedResults.size(); 1147 nestedResults.addElement(o); 1148 } 1149 else 1150 { 1151 try { 1152 s = rs.getString(colnum); 1153 } catch (SQLException se) { 1154 s = o.toString(); 1156 } 1157 } 1158 } 1159 break; 1160 } 1161 1162 if (s==null) s = "NULL"; 1163 1164 int w = displayColumnWidths[i-1]; 1165 if (s.length() < w) { 1166 StringBuffer fullS = new StringBuffer (s); 1167 fullS.ensureCapacity(w); 1168 for (int k=s.length(); k<w; k++) 1169 fullS.append(' '); 1170 s = fullS.toString(); 1171 } 1172 else if (s.length() > w) 1173 s = s.substring(0,w-1)+"&"; 1175 1176 buf.append(s); 1177 } 1178 indentedPrintLine( out, indentLevel, buf); 1179 1180 } 1182 static public void doTrace(PrintStream out, Exception e) { 1183 if (Boolean.getBoolean("ij.exceptionTrace")) { 1184 e.printStackTrace(out); 1185 out.flush(); 1186 } 1187 } 1188 1189 static private void indentedPrintLine( PrintStream out, int indentLevel, String text ) 1190 { 1191 indent( out, indentLevel ); 1192 out.println( text ); 1193 } 1194 1195 static private void indentedPrintLine( PrintStream out, int indentLevel, StringBuffer text ) 1196 { 1197 indent( out, indentLevel ); 1198 out.println( text ); 1199 } 1200 1201 static private void indent( PrintStream out, int indentLevel ) 1202 { 1203 for ( int ictr = 0; ictr < indentLevel; ictr++ ) { out.print( " " ); } 1204 } 1205 1206 } 1208 1209 1210 1211 | Popular Tags |