KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > database > DumpHelper


1 /**
2  * com.mckoi.database.DumpHelper 18 Aug 1999
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.database;
26
27 import java.io.IOException JavaDoc;
28 import java.io.PrintStream JavaDoc;
29
30 /**
31  * A helper class for the 'Table.dumpTo' method. This provides variables
32  * static methods for formating the contents of a table and outputting it to
33  * an output stream.
34  *
35  * @author Tobias Downer
36  */

37 class DumpHelper {
38
39   /**
40    * Dumps the contents of a table to the given output stream. It uses a
41    * very simple method to format the text.
42    */

43   static void dump(Table table, PrintStream JavaDoc out) {
44
45     int col_count = table.getColumnCount();
46
47 // if (table instanceof DataTable) {
48
// DataTable data_tab = (DataTable) table;
49
// out.println("Total Hits: " + data_tab.getTotalHits());
50
// out.println("File Hits: " + data_tab.getFileHits());
51
// out.println("Cache Hits: " + data_tab.getCacheHits());
52
// out.println();
53
// }
54

55     out.println("Table row count: " + table.getRowCount());
56     out.print(" "); // 6 spaces
57

58     // First output the column header.
59
for (int i = 0; i < col_count; ++i) {
60       out.print(table.getResolvedVariable(i).toString());
61       if (i < col_count - 1) {
62         out.print(", ");
63       }
64     }
65     out.println();
66
67     // Print out the contents of each row
68
int row_num = 0;
69     RowEnumeration r_enum = table.rowEnumeration();
70     while (r_enum.hasMoreRows() && row_num < 250) {
71       // Print the row number
72
String JavaDoc num = Integer.toString(row_num);
73       int space_gap = 4 - num.length();
74       for (int i = 0; i < space_gap; ++i) {
75         out.print(' ');
76       }
77       out.print(num);
78       out.print(": ");
79
80       // Print each cell in the row
81
int row_index = r_enum.nextRowIndex();
82       for (int col_index = 0; col_index < col_count; ++col_index) {
83         TObject cell = table.getCellContents(col_index, row_index);
84         out.print(cell.toString());
85         if (col_index < col_count - 1) {
86           out.print(", ");
87         }
88       }
89       out.println();
90
91       ++row_num;
92     }
93     out.println("Finished: " + row_num + "/" + table.getRowCount());
94
95   }
96
97 }
98
Popular Tags