EMMA Coverage Report (generated Wed Jun 28 22:15:27 PDT 2006)
[all classes][org.apache.derby.tools]

COVERAGE SUMMARY FOR SOURCE FILE [JDBCDisplayUtil.java]

nameclass, %method, %block, %line, %
JDBCDisplayUtil.java100% (1/1)69%  (37/54)67%  (1156/1717)68%  (308/456)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class JDBCDisplayUtil100% (1/1)69%  (37/54)67%  (1156/1717)68%  (308/456)
<static initializer> 100% (1/1)100% (7/7)100% (4/4)
DisplayBanner (PrintStream, ResultSetMetaData): int 0%   (0/1)0%   (0/5)0%   (0/1)
DisplayBanner (PrintWriter, ResultSetMetaData): int 0%   (0/1)0%   (0/5)0%   (0/1)
DisplayCurrentRow (PrintStream, ResultSet, Connection): void 0%   (0/1)0%   (0/6)0%   (0/2)
DisplayCurrentRow (PrintWriter, ResultSet, Connection): void 100% (1/1)100% (6/6)100% (2/2)
DisplayNestedResults (PrintStream, Vector, Connection, int): void 100% (1/1)25%  (16/65)35%  (5.3/15)
DisplayNestedResults (PrintWriter, Vector, Connection, int): void 100% (1/1)25%  (15/59)31%  (4.3/14)
DisplayNextRow (PrintStream, ResultSet, Connection): void 0%   (0/1)0%   (0/6)0%   (0/2)
DisplayNextRow (PrintWriter, ResultSet, Connection): void 0%   (0/1)0%   (0/6)0%   (0/2)
DisplayResults (PrintStream, ResultSet, Connection): void 100% (1/1)100% (6/6)100% (2/2)
DisplayResults (PrintStream, Statement, Connection): void 0%   (0/1)0%   (0/6)0%   (0/2)
DisplayResults (PrintWriter, ResultSet, Connection): void 0%   (0/1)0%   (0/6)0%   (0/2)
DisplayResults (PrintWriter, Statement, Connection): void 100% (1/1)100% (6/6)100% (2/2)
DisplayRow (PrintStream, ResultSet, ResultSetMetaData, int, Vector, Connectio... 100% (1/1)64%  (92/143)68%  (21/31)
DisplayRow (PrintWriter, ResultSet, ResultSetMetaData, int, Vector, Connectio... 100% (1/1)89%  (126/142)86%  (26.6/31)
DisplayUpdateCount (PrintStream, int, int): void 0%   (0/1)0%   (0/27)0%   (0/6)
DisplayUpdateCount (PrintWriter, int, int): void 100% (1/1)100% (25/25)100% (6/6)
JDBCDisplayUtil (): void 0%   (0/1)0%   (0/3)0%   (0/1)
ShowException (PrintStream, Throwable): void 100% (1/1)73%  (11/15)73%  (3.7/5)
ShowException (PrintWriter, Throwable): void 100% (1/1)73%  (11/15)73%  (3.7/5)
ShowSQLException (PrintStream, SQLException): void 100% (1/1)74%  (37/50)88%  (7/8)
ShowSQLException (PrintWriter, SQLException): void 100% (1/1)83%  (34/41)90%  (9/10)
ShowWarnings (PrintStream, Connection): void 100% (1/1)78%  (14/18)78%  (7/9)
ShowWarnings (PrintStream, ResultSet): void 100% (1/1)78%  (14/18)78%  (7/9)
ShowWarnings (PrintStream, SQLWarning): void 100% (1/1)100% (27/27)100% (4/4)
ShowWarnings (PrintStream, Statement): void 100% (1/1)78%  (14/18)78%  (7/9)
ShowWarnings (PrintWriter, Connection): void 100% (1/1)78%  (14/18)78%  (7/9)
ShowWarnings (PrintWriter, ResultSet): void 100% (1/1)78%  (14/18)78%  (7/9)
ShowWarnings (PrintWriter, SQLWarning): void 100% (1/1)100% (25/25)100% (6/6)
ShowWarnings (PrintWriter, Statement): void 100% (1/1)78%  (14/18)78%  (7/9)
checkNotNull (Object, String): void 100% (1/1)100% (6/6)100% (3/3)
doTrace (PrintStream, Exception): void 100% (1/1)44%  (4/9)50%  (2/4)
doTrace (PrintWriter, Exception): void 100% (1/1)44%  (4/9)50%  (2/4)
indent (PrintStream, int): void 100% (1/1)55%  (6/11)75%  (1.5/2)
indent (PrintWriter, int): void 100% (1/1)55%  (6/11)75%  (1.5/2)
indent_DisplayBanner (PrintStream, ResultSetMetaData, int): int 100% (1/1)90%  (178/197)89%  (32/36)
indent_DisplayBanner (PrintWriter, ResultSetMetaData, int): int 100% (1/1)100% (177/177)100% (35/35)
indent_DisplayCurrentRow (PrintStream, ResultSet, Connection, int): void 0%   (0/1)0%   (0/48)0%   (0/14)
indent_DisplayCurrentRow (PrintWriter, ResultSet, Connection, int): void 100% (1/1)100% (48/48)100% (14/14)
indent_DisplayNextRow (PrintStream, ResultSet, Connection, int): void 0%   (0/1)0%   (0/52)0%   (0/14)
indent_DisplayNextRow (PrintWriter, ResultSet, Connection, int): void 0%   (0/1)0%   (0/52)0%   (0/14)
indent_DisplayResults (PrintStream, ResultSet, Connection, int): void 100% (1/1)62%  (58/93)64%  (19.2/30)
indent_DisplayResults (PrintStream, Statement, Connection, int): void 0%   (0/1)0%   (0/25)0%   (0/8)
indent_DisplayResults (PrintWriter, ResultSet, Connection, int): void 100% (1/1)96%  (86/90)94%  (28.2/30)
indent_DisplayResults (PrintWriter, Statement, Connection, int): void 100% (1/1)100% (25/25)100% (8/8)
indentedPrintLine (PrintStream, int, String): void 0%   (0/1)0%   (0/7)0%   (0/3)
indentedPrintLine (PrintStream, int, StringBuffer): void 100% (1/1)100% (7/7)100% (3/3)
indentedPrintLine (PrintWriter, int, String): void 100% (1/1)100% (7/7)100% (3/3)
indentedPrintLine (PrintWriter, int, StringBuffer): void 100% (1/1)100% (7/7)100% (3/3)
init (): boolean 0%   (0/1)0%   (0/6)0%   (0/1)
init (String): boolean 0%   (0/1)0%   (0/4)0%   (0/1)
init (String, String): boolean 0%   (0/1)0%   (0/7)0%   (0/2)
mapNull (String, String): String 100% (1/1)100% (6/6)100% (2/2)
setMaxDisplayWidth (int): void 100% (1/1)100% (3/3)100% (2/2)

1/*
2 
3   Derby - Class org.apache.derby.tools.JDBCDisplayUtil
4 
5   Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable.
6 
7   Licensed under the Apache License, Version 2.0 (the "License");
8   you may not use this file except in compliance with the License.
9   You may obtain a copy of the License at
10 
11      http://www.apache.org/licenses/LICENSE-2.0
12 
13   Unless required by applicable law or agreed to in writing, software
14   distributed under the License is distributed on an "AS IS" BASIS,
15   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   See the License for the specific language governing permissions and
17   limitations under the License.
18 
19 */
20 
21package org.apache.derby.tools;
22 
23import java.io.PrintStream;
24import java.io.PrintWriter;
25import java.io.File;
26import java.io.FileNotFoundException;
27import java.io.IOException;
28 
29import java.sql.Connection;
30import java.sql.SQLException;
31import java.sql.SQLWarning;
32import java.sql.Statement;
33import java.sql.PreparedStatement;
34import java.sql.ResultSet;
35import java.sql.ResultSetMetaData;
36import java.sql.Types;
37 
38import java.util.Properties;
39import java.util.Enumeration;
40import java.util.Vector;
41 
42import org.apache.derby.iapi.tools.i18n.LocalizedResource;
43 
44import org.apache.derby.impl.tools.ij.ijException;
45 
46/**
47        
48        This class contains utility methods for displaying JDBC objects and results.
49        
50        <p>
51        All of the methods are static. The output stream
52        to write to is always passed in, along with the
53        JDBC objects to display.
54 
55        @author ames
56 */
57public class JDBCDisplayUtil {
58 
59        // used to control display
60        static final private int MINWIDTH = 4;
61        static private int maxWidth = 128;
62    static public boolean showSelectCount = false;
63 
64    static {
65        // initialize the locale support functions to default value of JVM 
66        LocalizedResource.getInstance();
67    }
68 
69 
70        //-----------------------------------------------------------------
71        // Methods for initialization resource bundle and codeset's output
72        
73        /**
74         * init method - will init the class to support a locale and
75         * codeset based on the derby.ui.locale and derby.ui.codeset
76         * properties if exists or using the default values from the JVM.
77         */
78        static public boolean init() {
79                return (LocalizedResource.getInstance() != null);
80        }
81 
82        /**
83         * init method - will init the class to support a locale and
84         * codeset based on the derby.ui.locale properties and on the 
85     * given codeset if exists or using the default values from the JVM.
86         */
87        public static boolean init(String codeset) {
88                return init(codeset, null);
89        }
90 
91        /**
92         * init method - will init the class to support a locale and
93         * codeset based on the given codeset and locale.
94         * If the parameters are null it will try to init use derby.ui.locale
95         * and derby.ui.codeset properties if exists or using the default
96         * values from the JVM.
97         */
98        public static boolean init(String pCodeset, String pLocale) {
99                LocalizedResource.getInstance().init(pCodeset, pLocale,null);
100                return true;
101        }
102 
103        //-----------------------------------------------------------------
104        // Methods for displaying and checking errors
105 
106        /**
107                Print information about the exception to the given PrintWriter.
108                For non-SQLExceptions, does a stack trace. For SQLExceptions,
109                print a standard error message and walk the list, if any.
110 
111                @param out the place to write to
112                @param e the exception to display
113         */
114        static public void ShowException(PrintWriter out, Throwable e) {
115                if (e == null) return;
116 
117                if (e instanceof SQLException)
118                        ShowSQLException(out, (SQLException)e);
119                else
120                        e.printStackTrace(out);
121        }
122 
123        /**
124                Print information about the SQL exception to the given PrintWriter.
125                Walk the list of exceptions, if any.
126 
127                @param out the place to write to
128                @param e the exception to display
129         */
130        static public void ShowSQLException(PrintWriter out, SQLException e) {
131                String errorCode;
132 
133                if (Boolean.getBoolean("ij.showErrorCode")) {
134                        errorCode = LocalizedResource.getMessage("UT_Error0", LocalizedResource.getNumber(e.getErrorCode()));
135                }
136                else {
137                        errorCode = "";
138                }
139 
140                while (e!=null) {
141                        String p1 = mapNull(e.getSQLState(),LocalizedResource.getMessage("UT_NoSqlst"));
142                        String p2 = mapNull(e.getMessage(),LocalizedResource.getMessage("UT_NoMessa"));
143                        out.println(LocalizedResource.getMessage("UT_Error012", p1, p2,errorCode));
144                        doTrace(out, e);
145                        e=e.getNextException();
146                }
147        }
148 
149        /**
150                Print information about the SQL warnings for the connection
151                to the given PrintWriter.
152                Walks the list of exceptions, if any.
153 
154                @param out the place to write to
155                @param theConnection the connection that may have warnings.
156         */
157        static public void ShowWarnings(PrintWriter out, Connection theConnection) {
158            try {
159                // GET CONNECTION WARNINGS
160                SQLWarning warning = null;
161 
162                if (theConnection != null) {
163                        ShowWarnings(out, theConnection.getWarnings());
164                }
165 
166                if (theConnection != null) {
167                        theConnection.clearWarnings();
168                }
169            } catch (SQLException e) {
170                        ShowSQLException(out, e);
171            }
172        } // ShowWarnings
173 
174        /**
175                @param out the place to write to
176                @param warning the SQLWarning
177        */
178        static public void ShowWarnings(PrintWriter out, SQLWarning warning) {
179                while (warning != null) {
180                        String p1 = mapNull(warning.getSQLState(),LocalizedResource.getMessage("UT_NoSqlst_7"));
181                        String p2 = mapNull(warning.getMessage(),LocalizedResource.getMessage("UT_NoMessa_8"));
182                        out.println(LocalizedResource.getMessage("UT_Warni01", p1, p2));
183                        warning = warning.getNextWarning();
184                }
185        }
186 
187        /**
188                Print information about the SQL warnings for the ResultSet
189                to the given PrintWriter.
190                Walk the list of exceptions, if any.
191        
192                @param out the place to write to
193                @param rs the ResultSet that may have warnings on it
194         */
195        static public void ShowWarnings(PrintWriter out, ResultSet rs) {
196            try {
197                // GET RESULTSET WARNINGS
198                SQLWarning warning = null;
199 
200                if (rs != null) {
201                        ShowWarnings(out, rs.getWarnings());
202                }
203 
204                if (rs != null) {
205                        rs.clearWarnings();
206                }
207            } catch (SQLException e) {
208                        ShowSQLException(out, e);
209            }
210        } // ShowResultSetWarnings
211 
212        /**
213                Print information about the SQL warnings for the Statement
214                to the given PrintWriter.
215                Walks the list of exceptions, if any.
216 
217                @param out the place to write to
218                @param s the Statement that may have warnings on it
219         */
220        static public void ShowWarnings(PrintWriter out, Statement s)
221        {
222            try {
223                // GET STATEMENT WARNINGS
224                SQLWarning warning = null;
225 
226                if (s != null) {
227                        ShowWarnings(out, s.getWarnings());
228                }
229 
230                if (s != null) {
231                        s.clearWarnings();
232                }
233            } catch (SQLException e) {
234                        ShowSQLException(out, e);
235            }
236        } // ShowStatementWarnings
237 
238        //-----------------------------------------------------------------
239        // Methods for displaying and checking results
240 
241        // REMIND: make this configurable...
242        static final private int MAX_RETRIES = 0;
243 
244        /**
245                Pretty-print the results of a statement that has been executed.
246                If it is a select, gathers and prints the results.  Display
247                partial results up to the first error.
248                If it is not a SELECT, determine if rows were involved or not,
249                and print the appropriate message.
250 
251                @param out the place to write to
252                @param stmt the Statement to display
253                @param conn the Connection against which the statement was executed
254 
255                @exception SQLException on JDBC access failure
256         */
257        static public void DisplayResults(PrintWriter out, Statement stmt, Connection conn )
258                throws SQLException
259        {
260                indent_DisplayResults( out, stmt, conn, 0);                        
261        }
262 
263        static private void indent_DisplayResults
264        (PrintWriter out, Statement stmt, Connection conn, int indentLevel)
265                throws SQLException {
266 
267                checkNotNull(stmt, "Statement");
268 
269                ResultSet rs = stmt.getResultSet();
270                if (rs != null) {
271                        indent_DisplayResults(out, rs, conn, indentLevel);
272                        rs.close(); // let the result set go away
273                }
274                else {
275                        DisplayUpdateCount(out,stmt.getUpdateCount(), indentLevel);
276                }
277 
278                ShowWarnings(out,stmt);
279        } // DisplayResults
280 
281        /**
282                @param out the place to write to
283                @param count the update count to display
284                @param indentLevel number of tab stops to indent line
285         */
286        static void DisplayUpdateCount(PrintWriter out, int count, int indentLevel ) {
287                if (count == 1) {
288                        indentedPrintLine( out, indentLevel, LocalizedResource.getMessage("UT_1RowInserUpdatDelet"));
289                }
290                else if (count >= 0) {
291                        indentedPrintLine( out, indentLevel, LocalizedResource.getMessage("UT_0RowsInserUpdatDelet", LocalizedResource.getNumber(count)));
292                }
293                else {
294                        indentedPrintLine( out, indentLevel, LocalizedResource.getMessage("UT_StateExecu"));
295                }
296        }
297 
298        /**
299                @param out the place to write to
300                @param rs the ResultSet to display
301                @param conn the Connection against which the ResultSet was retrieved
302 
303                @exception SQLException on JDBC access failure
304         */
305        static public void DisplayResults(PrintWriter out, ResultSet rs, Connection conn)
306                throws SQLException
307        {
308                indent_DisplayResults( out, rs, conn, 0);
309        }
310 
311        static private void indent_DisplayResults
312        (PrintWriter out, ResultSet rs, Connection conn, int indentLevel)
313                throws SQLException {
314                ResultSetMetaData rsmd = rs.getMetaData();
315                checkNotNull(rsmd, "ResultSetMetaData");
316                Vector nestedResults;
317    int numberOfRowsSelected = 0;
318 
319                // autocommit must be off or the nested cursors
320                // are closed when the outer statement completes.
321                if (!conn.getAutoCommit())
322                        nestedResults = new Vector();
323                else
324                        nestedResults = null;
325 
326                int len = indent_DisplayBanner(out,rsmd, indentLevel);
327 
328                // When displaying rows, keep going past errors
329                // unless/until the maximum # of errors is reached.
330                boolean doNext = true;
331                int retry = 0;
332                while (doNext) {
333                        try {
334                                doNext = rs.next();
335                                if (doNext) {
336 
337                                    DisplayRow(out, rs, rsmd, len, nestedResults, conn, indentLevel);
338                                        ShowWarnings(out, rs);
339                                        numberOfRowsSelected++;
340                                }
341                        } catch (SQLException e) {
342                                // REVISIT: might want to check the exception
343                                // and for some, not bother with the retry.
344                                if (++retry > MAX_RETRIES)
345                                        throw e;
346                                else
347                                        ShowSQLException(out, e);
348                        }
349                }
350                if (showSelectCount == true) {
351                   if (numberOfRowsSelected == 1) {
352                           out.println();
353                           indentedPrintLine( out, indentLevel, LocalizedResource.getMessage("UT_1RowSelec"));
354                   } else if (numberOfRowsSelected >= 0) {
355                           out.println();
356                       indentedPrintLine( out, indentLevel, 
357                        LocalizedResource.getMessage("UT_0RowsSelec", LocalizedResource.getNumber(numberOfRowsSelected)));
358                   }
359                }
360 
361                DisplayNestedResults(out, nestedResults, conn, indentLevel );
362                nestedResults = null;
363        }
364 
365        /**
366                @param out the place to write to
367                @param nr the vector of results
368                @param conn the Connection against which the ResultSet was retrieved
369                @param indentLevel number of tab stops to indent line
370 
371                @exception SQLException thrown on access error
372         */
373        static private void DisplayNestedResults(PrintWriter out, Vector nr, Connection conn, int indentLevel )
374                throws SQLException {
375 
376                if (nr == null) return;
377 
378                String b=LocalizedResource.getMessage("UT_JDBCDisplayUtil_16");
379                String oldString="0";
380 
381                for (int i=0; i < nr.size(); i++) {
382                        LocalizedResource.OutputWriter().println();
383 
384                        //just too clever to get the extra +s
385                        String t = Integer.toString(i);
386                        if (t.length() > oldString.length()) {
387                                oldString = t;
388                                b=b+LocalizedResource.getMessage("UT_JDBCDisplayUtil_17");
389                        }
390 
391                        LocalizedResource.OutputWriter().println(b);
392                        LocalizedResource.OutputWriter().println(LocalizedResource.getMessage("UT_Resul0", LocalizedResource.getNumber(i)));
393                        LocalizedResource.OutputWriter().println(b);
394                        indent_DisplayResults(out, (ResultSet) nr.elementAt(i), conn, indentLevel);
395                }
396        }
397 
398        /**
399                Fetch the next row of the result set, and if it
400                exists format and display a banner and the row.
401 
402                @param out the place to write to
403                @param rs the ResultSet in use
404                @param conn the Connection against which the ResultSet was retrieved
405 
406                @exception SQLException on JDBC access failure
407         */
408        static public void DisplayNextRow(PrintWriter out, ResultSet rs, Connection conn )
409                throws SQLException
410        {
411                indent_DisplayNextRow( out, rs, conn, 0 );
412        }
413 
414        static private void indent_DisplayNextRow(PrintWriter out, ResultSet rs, Connection conn, int indentLevel )
415                throws SQLException {
416 
417                Vector nestedResults;
418 
419                // autocommit must be off or the nested cursors
420                // are closed when the outer statement completes.
421                if (!conn.getAutoCommit())
422                        nestedResults = new Vector();
423                else
424                        nestedResults = null;
425 
426                checkNotNull(rs, "ResultSet");
427 
428                ResultSetMetaData rsmd = rs.getMetaData();
429                checkNotNull(rsmd, "ResultSetMetaData");
430 
431                // Only print stuff out if there is a row to be had.
432                if (rs.next()) {
433                        int rowLen = indent_DisplayBanner(out, rsmd, indentLevel);
434                    DisplayRow(out, rs, rsmd, rowLen, nestedResults, conn, indentLevel );
435                }
436                else {
437                        indentedPrintLine( out, indentLevel, LocalizedResource.getMessage("UT_NoCurreRow"));
438                }
439 
440                ShowWarnings(out, rs);
441 
442                DisplayNestedResults(out, nestedResults, conn, indentLevel );
443                nestedResults = null;
444 
445        } // DisplayNextRow
446 
447        /**
448                Display the current row of the result set along with
449                a banner. Assume the result set is on a row.
450 
451                @param out the place to write to
452                @param rs the ResultSet in use
453                @param conn the Connection against which the ResultSet was retrieved
454 
455                @exception SQLException on JDBC access failure
456         */
457        static public void DisplayCurrentRow(PrintWriter out, ResultSet rs, Connection conn )
458                throws SQLException
459        {
460                indent_DisplayCurrentRow( out, rs, conn, 0 );
461        }
462 
463        static private void indent_DisplayCurrentRow(PrintWriter out, ResultSet rs, Connection conn, int indentLevel )
464                throws SQLException {
465 
466                Vector nestedResults;
467 
468                if (rs == null) {
469                        indentedPrintLine( out, indentLevel, LocalizedResource.getMessage("UT_NoCurreRow_19"));
470                        return;
471                }
472 
473                // autocommit must be off or the nested cursors
474                // are closed when the outer statement completes.
475                if (!conn.getAutoCommit())
476                        nestedResults = new Vector();
477                else
478                        nestedResults = null;
479 
480                ResultSetMetaData rsmd = rs.getMetaData();
481                checkNotNull(rsmd, "ResultSetMetaData");
482 
483                int rowLen = indent_DisplayBanner(out, rsmd, indentLevel);
484                   DisplayRow(out, rs, rsmd, rowLen, nestedResults, conn, indentLevel );
485 
486                ShowWarnings(out, rs);
487 
488                DisplayNestedResults(out, nestedResults, conn, indentLevel );
489                nestedResults = null;
490 
491        } // DisplayNextRow
492 
493        /**
494                Print a banner containing the column labels separated with '|'s
495                and a line of '-'s.  Each field is as wide as the display
496                width reported by the metadata.
497 
498                @param out the place to write to
499                @param rsmd the ResultSetMetaData to use
500 
501                @exception SQLException on JDBC access failure
502         */
503        static public int DisplayBanner(PrintWriter out, ResultSetMetaData rsmd )
504                throws SQLException
505        {
506                return indent_DisplayBanner( out, rsmd, 0 );
507        }
508 
509        static private int indent_DisplayBanner(PrintWriter out, ResultSetMetaData rsmd, int indentLevel )
510                throws SQLException        {
511 
512                StringBuffer buf = new StringBuffer();
513 
514                int numCols = rsmd.getColumnCount();
515                int rowLen;
516 
517                // do some precalculation so the buffer is allocated only once
518                // buffer is twice as long as the display length plus one for a newline
519                rowLen = (numCols - 1); // for the column separators
520                for (int i=1; i <= numCols; i++) {
521                        rowLen += Math.min(maxWidth,
522                                Math.max((rsmd.isNullable(i) == 
523                                                        ResultSetMetaData.columnNoNulls)?
524                                                        0 : MINWIDTH, LocalizedResource.getInstance().getColumnDisplaySize(rsmd, i)));
525                }
526                buf.ensureCapacity(rowLen);
527 
528                // get column header info
529                // truncate it to the column display width
530                // add a bar between each item.
531                for (int i=1; i <= numCols; i++) {
532 
533                        if (i>1)
534                                buf.append('|');
535 
536                        String s = rsmd.getColumnLabel(i);
537 
538                        int w = Math.min(maxWidth,
539                                Math.max(((rsmd.isNullable(i) == 
540                                                        ResultSetMetaData.columnNoNulls)?
541                                                        0 : MINWIDTH), LocalizedResource.getInstance().getColumnDisplaySize(rsmd, i)));
542 
543                        if (s.length() < w) {
544                                
545                                buf.append(s);
546 
547                                // try to paste on big chunks of space at a time.
548                                int k = w - s.length();
549                                for (; k >= 64; k -= 64)
550                                        buf.append(
551          "                                                                ");
552                                for (; k >= 16; k -= 16)
553                                        buf.append("                ");
554                                for (; k >= 4; k -= 4)
555                                        buf.append("    ");
556                                for (; k > 0; k--)
557                                        buf.append(' ');
558                        }
559                        else if (s.length() > w)  {
560                                if (w > 1) 
561                                        buf.append(s.substring(0,w-1));
562                                if (w > 0) 
563                                        buf.append('&');
564                        }
565                        else {
566                                buf.append(s);
567                        }
568                }
569 
570                buf.setLength(Math.min(rowLen, 1024));
571                indentedPrintLine( out, indentLevel, buf);
572 
573                // now print a row of '-'s
574                for (int i=0; i<Math.min(rowLen, 1024); i++)
575                        buf.setCharAt(i, '-');
576                indentedPrintLine( out, indentLevel, buf);
577 
578                buf = null;
579 
580                return rowLen;
581        } // DisplayBanner
582 
583        /**
584                Print one row of a result set, padding each field to the
585                display width and separating them with '|'s
586 
587                @param out the place to write to
588                @param rs the ResultSet to use
589                @param rsmd the ResultSetMetaData to use
590                @param rowLen
591                @param nestedResults
592                @param conn
593                @param indentLevel number of tab stops to indent line
594 
595                @exception SQLException thrown on JDBC access failure
596         */
597        static private void DisplayRow(PrintWriter out, ResultSet rs, ResultSetMetaData rsmd, int rowLen, Vector nestedResults, Connection conn, int indentLevel )
598                throws SQLException
599        {
600                StringBuffer buf = new StringBuffer();
601                buf.ensureCapacity(rowLen);
602 
603                int numCols = rsmd.getColumnCount();
604                int i;
605 
606                // get column header info
607                // truncate it to the column display width
608                // add a bar between each item.
609                for (i=1; i <= numCols; i++){
610                        if (i>1)
611                                buf.append('|');
612 
613                        String s;
614                        switch (rsmd.getColumnType(i)) {
615                        default:
616                                s = LocalizedResource.getInstance().getLocalizedString(rs, rsmd, i );
617                                break;
618                        case org.apache.derby.iapi.reference.JDBC20Translation.SQL_TYPES_JAVA_OBJECT:
619                        case Types.OTHER:
620                        {
621                                Object o = rs.getObject(i);
622                                if (o == null) { s = "NULL"; }
623                                else if (o instanceof ResultSet && nestedResults != null)
624                                {
625                                        s = LocalizedResource.getMessage("UT_Resul0_20", LocalizedResource.getNumber(nestedResults.size()));
626                                        nestedResults.addElement(o);
627                                }
628                                else
629                                {
630                                        try {
631                                                s = rs.getString(i);
632                                        } catch (SQLException se) {
633                                                // oops, they don't support refetching the column
634                                                s = o.toString();
635                                        }
636                                }
637                        }
638                        break;
639                        }
640                        if (s==null) s = "NULL";
641 
642                        int w = Math.min(maxWidth,
643                                Math.max((rsmd.isNullable(i) == 
644                                                        ResultSetMetaData.columnNoNulls)?
645                                                        0 : MINWIDTH, LocalizedResource.getInstance().getColumnDisplaySize(rsmd, i)));
646                        if (s.length() < w) {
647                                StringBuffer fullS = new StringBuffer(s);
648                                fullS.ensureCapacity(w);
649                                for (int k=s.length(); k<w; k++)
650                                        fullS.append(' ');
651                                s = fullS.toString();
652                        }
653                        else if (s.length() > w)
654                                // add the & marker to know it got cut off
655                                s = s.substring(0,w-1)+"&";
656 
657                        buf.append(s);
658                }
659                indentedPrintLine( out, indentLevel, buf);
660 
661        } // DisplayRow
662 
663        /**
664                Check if an object is null, and if it is, throw an exception
665                with an informative parameter about what was null.
666                The exception is a run-time exception that is internal to ij.
667 
668                @param o the object to test
669                @param what the information to include in the error if it is null
670         */
671        public static void checkNotNull(Object o, String what) {
672                if (o == null) {
673                        throw ijException.objectWasNull(what);
674                }
675        } // checkNotNull
676 
677        /**
678                Map the string to the value if it is null.
679 
680                @param s the string to test for null
681                @param nullValue the value to use if s is null
682 
683                @return if s is non-null, s; else nullValue.
684         */
685        static public String mapNull(String s, String nullValue) {
686                if (s==null) return nullValue;
687                return s;
688        }
689 
690        /**
691                If the property ij.exceptionTrace is true, display the stack
692                trace to the print stream. Otherwise, do nothing.
693 
694                @param out the output stream to write to
695                @param e the exception to display
696         */
697        static public void doTrace(PrintWriter out, Exception e) {
698                if (Boolean.getBoolean("ij.exceptionTrace")) {
699                        e.printStackTrace(out);
700                    out.flush();
701                }
702        }
703 
704        static public void setMaxDisplayWidth(int maxDisplayWidth) {
705                maxWidth = maxDisplayWidth;
706        }
707 
708        static        private        void        indentedPrintLine( PrintWriter out, int indentLevel, String text )
709        {
710                indent( out, indentLevel );
711                out.println( text );
712        }
713 
714        static        private        void        indentedPrintLine( PrintWriter out, int indentLevel, StringBuffer text )
715        {
716                indent( out, indentLevel );
717                out.println( text );
718        }
719 
720        static        private        void        indent( PrintWriter out, int indentLevel )
721        {
722                for ( int ictr = 0; ictr < indentLevel; ictr++ ) { out.print( "  " ); }
723        }
724 
725        // ================
726 
727        static public void ShowException(PrintStream out, Throwable e) {
728                if (e == null) return;
729 
730                if (e instanceof SQLException)
731                        ShowSQLException(out, (SQLException)e);
732                else
733                        e.printStackTrace(out);
734        }
735 
736        static public void ShowSQLException(PrintStream out, SQLException e) {
737                String errorCode;
738 
739                if (Boolean.getBoolean("ij.showErrorCode")) {
740                        errorCode = " (errorCode = " + e.getErrorCode() + ")";
741                }
742                else {
743                        errorCode = "";
744                }
745 
746                while (e!=null) {
747                        out.println("ERROR "+mapNull(e.getSQLState(),"(no SQLState)")+": "+
748                                 mapNull(e.getMessage(),"(no message)")+errorCode);
749                        doTrace(out, e);
750                        e=e.getNextException();
751                }
752        }
753 
754        static public void ShowWarnings(PrintStream out, Connection theConnection) {
755            try {
756                // GET CONNECTION WARNINGS
757                SQLWarning warning = null;
758 
759                if (theConnection != null) {
760                        ShowWarnings(out, theConnection.getWarnings());
761                }
762 
763                if (theConnection != null) {
764                        theConnection.clearWarnings();
765                }
766            } catch (SQLException e) {
767                        ShowSQLException(out, e);
768            }
769        } // ShowWarnings
770 
771        static public void ShowWarnings(PrintStream out, SQLWarning warning) {
772                while (warning != null) {
773                        out.println("WARNING "+
774                                mapNull(warning.getSQLState(),"(no SQLState)")+": "+
775                                mapNull(warning.getMessage(),"(no message)"));
776                        warning = warning.getNextWarning();
777                }
778        }
779 
780        static public void ShowWarnings(PrintStream out, ResultSet rs) {
781            try {
782                // GET RESULTSET WARNINGS
783                SQLWarning warning = null;
784 
785                if (rs != null) {
786                        ShowWarnings(out, rs.getWarnings());
787                }
788 
789                if (rs != null) {
790                        rs.clearWarnings();
791                }
792            } catch (SQLException e) {
793                        ShowSQLException(out, e);
794            }
795        } // ShowResultSetWarnings
796 
797        static public void ShowWarnings(PrintStream out, Statement s)
798        {
799            try {
800                // GET STATEMENT WARNINGS
801                SQLWarning warning = null;
802 
803                if (s != null) {
804                        ShowWarnings(out, s.getWarnings());
805                }
806 
807                if (s != null) {
808                        s.clearWarnings();
809                }
810            } catch (SQLException e) {
811                        ShowSQLException(out, e);
812            }
813        } // ShowStatementWarnings
814 
815        static public void DisplayResults(PrintStream out, Statement stmt, Connection conn )
816                throws SQLException
817        {
818                indent_DisplayResults( out, stmt, conn, 0);                        
819        }
820 
821        static private void indent_DisplayResults
822        (PrintStream out, Statement stmt, Connection conn, int indentLevel)
823                throws SQLException {
824 
825                checkNotNull(stmt, "Statement");
826 
827                ResultSet rs = stmt.getResultSet();
828                if (rs != null) {
829                        indent_DisplayResults(out, rs, conn, indentLevel);
830                        rs.close(); // let the result set go away
831                }
832                else {
833                        DisplayUpdateCount(out,stmt.getUpdateCount(), indentLevel);
834                }
835 
836                ShowWarnings(out,stmt);
837        } // DisplayResults
838 
839        static void DisplayUpdateCount(PrintStream out, int count, int indentLevel ) {
840                if (count == 1) {
841                        indentedPrintLine( out, indentLevel, "1 row inserted/updated/deleted");
842                }
843                else if (count >= 0) {
844                        indentedPrintLine( out, indentLevel, count+" rows inserted/updated/deleted");
845                }
846                else {
847                        indentedPrintLine( out, indentLevel, "Statement executed.");
848                }
849        }
850 
851        static public void DisplayResults(PrintStream out, ResultSet rs, Connection conn)
852                throws SQLException
853        {
854                indent_DisplayResults( out, rs, conn, 0);
855        }
856 
857        static private void indent_DisplayResults
858        (PrintStream out, ResultSet rs, Connection conn, int indentLevel)
859                throws SQLException {
860                ResultSetMetaData rsmd = rs.getMetaData();
861                checkNotNull(rsmd, "ResultSetMetaData");
862                Vector nestedResults;
863    int numberOfRowsSelected = 0;
864 
865                // autocommit must be off or the nested cursors
866                // are closed when the outer statement completes.
867                if (!conn.getAutoCommit())
868                        nestedResults = new Vector();
869                else
870                        nestedResults = null;
871 
872                int len = indent_DisplayBanner(out,rsmd, indentLevel);
873 
874                // When displaying rows, keep going past errors
875                // unless/until the maximum # of errors is reached.
876                boolean doNext = true;
877                int retry = 0;
878                while (doNext) {
879                        try {
880                                doNext = rs.next();
881                                if (doNext) {
882 
883                                    DisplayRow(out, rs, rsmd, len, nestedResults, conn, indentLevel);
884                                        ShowWarnings(out, rs);
885                                        numberOfRowsSelected++;
886                                }
887                        } catch (SQLException e) {
888                                // REVISIT: might want to check the exception
889                                // and for some, not bother with the retry.
890                                if (++retry > MAX_RETRIES)
891                                        throw e;
892                                else
893                                        ShowSQLException(out, e);
894                        }
895                }
896                if (showSelectCount == true) {
897                   if (numberOfRowsSelected == 1) {
898                           out.println();
899                           indentedPrintLine( out, indentLevel, "1 row selected");
900                   } else if (numberOfRowsSelected >= 0) {
901                           out.println();
902                       indentedPrintLine( out, indentLevel, numberOfRowsSelected + " rows selected");
903                   }
904                }
905 
906                DisplayNestedResults(out, nestedResults, conn, indentLevel );
907                nestedResults = null;
908        }
909 
910        static private void DisplayNestedResults(PrintStream out, Vector nr, Connection conn, int indentLevel )
911                throws SQLException {
912 
913                if (nr == null) return;
914 
915                String s="+ ResultSet #";
916                String b="++++++++++++++++";
917                String oldString="0";
918 
919                for (int i=0; i < nr.size(); i++) {
920                        System.out.println();
921 
922                        //just too clever to get the extra +s
923                        String t = Integer.toString(i);
924                        if (t.length() > oldString.length()) {
925                                oldString = t;
926                                b=b+"+";
927                        }
928 
929                        System.out.println(b);
930                        System.out.println(s+i+" +");
931                        System.out.println(b);
932                        indent_DisplayResults(out, (ResultSet) nr.elementAt(i), conn, indentLevel);
933                }
934        }
935 
936        static public void DisplayNextRow(PrintStream out, ResultSet rs, Connection conn )
937                throws SQLException
938        {
939                indent_DisplayNextRow( out, rs, conn, 0 );
940        }
941 
942        static private void indent_DisplayNextRow(PrintStream out, ResultSet rs, Connection conn, int indentLevel )
943                throws SQLException {
944 
945                Vector nestedResults;
946 
947                // autocommit must be off or the nested cursors
948                // are closed when the outer statement completes.
949                if (!conn.getAutoCommit())
950                        nestedResults = new Vector();
951                else
952                        nestedResults = null;
953 
954                checkNotNull(rs, "ResultSet");
955 
956                ResultSetMetaData rsmd = rs.getMetaData();
957                checkNotNull(rsmd, "ResultSetMetaData");
958 
959                // Only print stuff out if there is a row to be had.
960                if (rs.next()) {
961                        int rowLen = indent_DisplayBanner(out, rsmd, indentLevel);
962                    DisplayRow(out, rs, rsmd, rowLen, nestedResults, conn, indentLevel );
963                }
964                else {
965                        indentedPrintLine( out, indentLevel, LocalizedResource.getMessage("UT_NoCurreRow"));
966                }
967 
968                ShowWarnings(out, rs);
969 
970                DisplayNestedResults(out, nestedResults, conn, indentLevel );
971                nestedResults = null;
972 
973        } // DisplayNextRow
974 
975        static public void DisplayCurrentRow(PrintStream out, ResultSet rs, Connection conn )
976                throws SQLException
977        {
978                indent_DisplayCurrentRow( out, rs, conn, 0 );
979        }
980 
981        static private void indent_DisplayCurrentRow(PrintStream out, ResultSet rs, Connection conn, int indentLevel )
982                throws SQLException {
983 
984                Vector nestedResults;
985 
986                if (rs == null) {
987                        indentedPrintLine( out, indentLevel, LocalizedResource.getMessage("UT_NoCurreRow_19"));
988                        return;
989                }
990 
991                // autocommit must be off or the nested cursors
992                // are closed when the outer statement completes.
993                if (!conn.getAutoCommit())
994                        nestedResults = new Vector();
995                else
996                        nestedResults = null;
997 
998                ResultSetMetaData rsmd = rs.getMetaData();
999                checkNotNull(rsmd, "ResultSetMetaData");
1000 
1001                int rowLen = indent_DisplayBanner(out, rsmd, indentLevel);
1002                   DisplayRow(out, rs, rsmd, rowLen, nestedResults, conn, indentLevel );
1003 
1004                ShowWarnings(out, rs);
1005 
1006                DisplayNestedResults(out, nestedResults, conn, indentLevel );
1007                nestedResults = null;
1008 
1009        } // DisplayNextRow
1010 
1011        static public int DisplayBanner(PrintStream out, ResultSetMetaData rsmd )
1012                throws SQLException
1013        {
1014                return indent_DisplayBanner( out, rsmd, 0 );
1015        }
1016 
1017        static private int indent_DisplayBanner(PrintStream out, ResultSetMetaData rsmd, int indentLevel )
1018                throws SQLException        {
1019 
1020                StringBuffer buf = new StringBuffer();
1021 
1022                int numCols = rsmd.getColumnCount();
1023                int rowLen;
1024 
1025                // do some precalculation so the buffer is allocated only once
1026                // buffer is twice as long as the display length plus one for a newline
1027                rowLen = (numCols - 1); // for the column separators
1028                for (int i=1; i <= numCols; i++) {
1029                        rowLen += Math.min(maxWidth,
1030                                Math.max((rsmd.isNullable(i) == 
1031                                                        ResultSetMetaData.columnNoNulls)?
1032                                                        0 : MINWIDTH,
1033                                                rsmd.getColumnDisplaySize(i)));
1034                }
1035                buf.ensureCapacity(rowLen);
1036 
1037                // get column header info
1038                // truncate it to the column display width
1039                // add a bar between each item.
1040                for (int i=1; i <= numCols; i++) {
1041 
1042                        if (i>1)
1043                                buf.append('|');
1044 
1045                        String s = rsmd.getColumnLabel(i);
1046 
1047                        int w = Math.min(maxWidth,
1048                                Math.max(((rsmd.isNullable(i) == 
1049                                                        ResultSetMetaData.columnNoNulls)?
1050                                                        0 : MINWIDTH),
1051                                                rsmd.getColumnDisplaySize(i)));
1052 
1053                        if (s.length() < w) {
1054                                // build a string buffer to hold the whitespace
1055                                StringBuffer blanks = new StringBuffer(s);
1056                                blanks.ensureCapacity(w);
1057 
1058                                // try to paste on big chunks of space at a time.
1059                                for (int k=blanks.length()+64; k<=w; k+=64)
1060                                        blanks.append(
1061          "                                                                ");
1062                                for (int k=blanks.length()+16; k<=w; k+=16)
1063                                        blanks.append("                ");
1064                                for (int k=blanks.length()+4; k<=w; k+=4)
1065                                        blanks.append("    ");
1066                                for (int k=blanks.length(); k<w; k++)
1067                                        blanks.append(' ');
1068 
1069                                buf.append(blanks);
1070                                // REMIND: could do more cleverness, like keep around
1071                                // past buffers to reuse...
1072                        }
1073                        else if (s.length() > w)  {
1074                                if (w > 1) 
1075                                        buf.append(s.substring(0,w-1));
1076                                if (w > 0) 
1077                                        buf.append('&');
1078                        }
1079                        else {
1080                                buf.append(s);
1081                        }
1082                }
1083 
1084                buf.setLength(Math.min(rowLen, 1024));
1085                indentedPrintLine( out, indentLevel, buf);
1086 
1087                // now print a row of '-'s
1088                for (int i=0; i<Math.min(rowLen, 1024); i++)
1089                        buf.setCharAt(i, '-');
1090                indentedPrintLine( out, indentLevel, buf);
1091 
1092                buf = null;
1093 
1094                return rowLen;
1095        } // DisplayBanner
1096 
1097        static private void DisplayRow(PrintStream out, ResultSet rs, ResultSetMetaData rsmd, int rowLen, Vector nestedResults, Connection conn, int indentLevel )
1098                throws SQLException
1099        {
1100                StringBuffer buf = new StringBuffer();
1101                buf.ensureCapacity(rowLen);
1102 
1103                int numCols = rsmd.getColumnCount();
1104                int i;
1105 
1106                // get column header info
1107                // truncate it to the column display width
1108                // add a bar between each item.
1109                for (i=1; i <= numCols; i++){
1110                        if (i>1)
1111                                buf.append('|');
1112 
1113                        String s;
1114                        switch (rsmd.getColumnType(i)) {
1115                        default:
1116                                s = rs.getString(i);
1117                                break;
1118                        case org.apache.derby.iapi.reference.JDBC20Translation.SQL_TYPES_JAVA_OBJECT:
1119                        case Types.OTHER:
1120                        {
1121                                Object o = rs.getObject(i);
1122                                if (o == null) { s = "NULL"; }
1123                                else if (o instanceof ResultSet && nestedResults != null)
1124                                {
1125                                        s = "ResultSet #"+nestedResults.size();
1126                                        nestedResults.addElement(o);
1127                                }
1128                                else
1129                                {
1130                                        try {
1131                                                s = rs.getString(i);
1132                                        } catch (SQLException se) {
1133                                                // oops, they don't support refetching the column
1134                                                s = o.toString();
1135                                        }
1136                                }
1137                        }
1138                        break;
1139                        }
1140 
1141                        if (s==null) s = "NULL";
1142 
1143                        int w = Math.min(maxWidth,
1144                                Math.max((rsmd.isNullable(i) == 
1145                                                        ResultSetMetaData.columnNoNulls)?
1146                                                        0 : MINWIDTH,
1147                                                rsmd.getColumnDisplaySize(i)));
1148                        if (s.length() < w) {
1149                                StringBuffer fullS = new StringBuffer(s);
1150                                fullS.ensureCapacity(w);
1151                                for (int k=s.length(); k<w; k++)
1152                                        fullS.append(' ');
1153                                s = fullS.toString();
1154                        }
1155                        else if (s.length() > w)
1156                                // add the & marker to know it got cut off
1157                                s = s.substring(0,w-1)+"&";
1158 
1159                        buf.append(s);
1160                }
1161                indentedPrintLine( out, indentLevel, buf);
1162 
1163        } // DisplayRow
1164 
1165        static public void doTrace(PrintStream out, Exception e) {
1166                if (Boolean.getBoolean("ij.exceptionTrace")) {
1167                        e.printStackTrace(out);
1168                    out.flush();
1169                }
1170        }
1171 
1172        static        private        void        indentedPrintLine( PrintStream out, int indentLevel, String text )
1173        {
1174                indent( out, indentLevel );
1175                out.println( text );
1176        }
1177 
1178        static        private        void        indentedPrintLine( PrintStream out, int indentLevel, StringBuffer text )
1179        {
1180                indent( out, indentLevel );
1181                out.println( text );
1182        }
1183 
1184        static        private        void        indent( PrintStream out, int indentLevel )
1185        {
1186                for ( int ictr = 0; ictr < indentLevel; ictr++ ) { out.print( "  " ); }
1187        }
1188        
1189        // ==========================
1190}
1191 
1192 
1193 

[all classes][org.apache.derby.tools]
EMMA 2.0.5312 (C) Vladimir Roubtsov