KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * com.mckoi.database.GTTableInfoDataSource 27 Apr 2001
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.util.Arrays JavaDoc;
28
29 /**
30  * An implementation of MutableTableDataSource that presents information
31  * about the tables in all schema.
32  * <p>
33  * NOTE: This is not designed to be a long kept object. It must not last
34  * beyond the lifetime of a transaction.
35  *
36  * @author Tobias Downer
37  */

38
39 final class GTTableInfoDataSource extends GTDataSource {
40
41   /**
42    * The transaction that is the view of this information.
43    */

44   private Transaction transaction;
45
46   /**
47    * The list of all TableName visible to the transaction.
48    */

49   private TableName[] table_list;
50
51   /**
52    * The list of all table types that are visible.
53    */

54   private String JavaDoc[] table_types;
55   
56   /**
57    * The number of rows in this table.
58    */

59   private int row_count;
60
61   /**
62    * Constructor.
63    */

64   public GTTableInfoDataSource(Transaction transaction) {
65     super(transaction.getSystem());
66     this.transaction = transaction;
67   }
68
69   /**
70    * Initialize the data source.
71    */

72   public GTTableInfoDataSource init() {
73     // All the tables
74
table_list = transaction.getTableList();
75     Arrays.sort(table_list);
76     table_types = new String JavaDoc[table_list.length];
77     row_count = table_list.length;
78     
79     for (int i = 0; i < table_list.length; ++i) {
80       String JavaDoc cur_type = transaction.getTableType(table_list[i]);
81       // If the table is in the SYS_INFO schema, the type is defined as a
82
// SYSTEM TABLE.
83
if (cur_type.equals("TABLE") &&
84           table_list[i].getSchema().equals("SYS_INFO")) {
85         cur_type = "SYSTEM TABLE";
86       }
87       table_types[i] = cur_type;
88     }
89     
90     return this;
91   }
92
93   // ---------- Implemented from GTDataSource ----------
94

95   public DataTableDef getDataTableDef() {
96     return DEF_DATA_TABLE_DEF;
97   }
98
99   public int getRowCount() {
100     return row_count;
101   }
102
103   public TObject getCellContents(final int column, final int row) {
104     final TableName tname = table_list[row];
105     switch (column) {
106       case 0: // schema
107
return columnValue(column, tname.getSchema());
108       case 1: // name
109
return columnValue(column, tname.getName());
110       case 2: // type
111
return columnValue(column, table_types[row]);
112       case 3: // other
113
// Table notes, etc. (future enhancement)
114
return columnValue(column, "");
115       default:
116         throw new Error JavaDoc("Column out of bounds.");
117     }
118   }
119
120   // ---------- Overwritten from GTDataSource ----------
121

122   public void dispose() {
123     super.dispose();
124     table_list = null;
125     transaction = null;
126   }
127
128   // ---------- Static ----------
129

130   /**
131    * The data table def that describes this table of data source.
132    */

133   static final DataTableDef DEF_DATA_TABLE_DEF;
134
135   static {
136
137     DataTableDef def = new DataTableDef();
138     def.setTableName(new TableName(Database.SYSTEM_SCHEMA, "sUSRTableInfo"));
139
140     // Add column definitions
141
def.addColumn(stringColumn("schema"));
142     def.addColumn(stringColumn("name"));
143     def.addColumn(stringColumn("type"));
144     def.addColumn(stringColumn("other"));
145
146     // Set to immutable
147
def.setImmutable();
148
149     DEF_DATA_TABLE_DEF = def;
150
151   }
152
153 }
154
Popular Tags