001: /**
002: * com.mckoi.database.GTTableInfoDataSource 27 Apr 2001
003: *
004: * Mckoi SQL Database ( http://www.mckoi.com/database )
005: * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * Version 2 as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License Version 2 for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * Version 2 along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: * Change Log:
021: *
022: *
023: */package com.mckoi.database;
024:
025: import java.util.Arrays;
026:
027: /**
028: * An implementation of MutableTableDataSource that presents information
029: * about the tables in all schema.
030: * <p>
031: * NOTE: This is not designed to be a long kept object. It must not last
032: * beyond the lifetime of a transaction.
033: *
034: * @author Tobias Downer
035: */
036:
037: final class GTTableInfoDataSource extends GTDataSource {
038:
039: /**
040: * The transaction that is the view of this information.
041: */
042: private Transaction transaction;
043:
044: /**
045: * The list of all TableName visible to the transaction.
046: */
047: private TableName[] table_list;
048:
049: /**
050: * The list of all table types that are visible.
051: */
052: private String[] table_types;
053:
054: /**
055: * The number of rows in this table.
056: */
057: private int row_count;
058:
059: /**
060: * Constructor.
061: */
062: public GTTableInfoDataSource(Transaction transaction) {
063: super (transaction.getSystem());
064: this .transaction = transaction;
065: }
066:
067: /**
068: * Initialize the data source.
069: */
070: public GTTableInfoDataSource init() {
071: // All the tables
072: table_list = transaction.getTableList();
073: Arrays.sort(table_list);
074: table_types = new String[table_list.length];
075: row_count = table_list.length;
076:
077: for (int i = 0; i < table_list.length; ++i) {
078: String cur_type = transaction.getTableType(table_list[i]);
079: // If the table is in the SYS_INFO schema, the type is defined as a
080: // SYSTEM TABLE.
081: if (cur_type.equals("TABLE")
082: && table_list[i].getSchema().equals("SYS_INFO")) {
083: cur_type = "SYSTEM TABLE";
084: }
085: table_types[i] = cur_type;
086: }
087:
088: return this ;
089: }
090:
091: // ---------- Implemented from GTDataSource ----------
092:
093: public DataTableDef getDataTableDef() {
094: return DEF_DATA_TABLE_DEF;
095: }
096:
097: public int getRowCount() {
098: return row_count;
099: }
100:
101: public TObject getCellContents(final int column, final int row) {
102: final TableName tname = table_list[row];
103: switch (column) {
104: case 0: // schema
105: return columnValue(column, tname.getSchema());
106: case 1: // name
107: return columnValue(column, tname.getName());
108: case 2: // type
109: return columnValue(column, table_types[row]);
110: case 3: // other
111: // Table notes, etc. (future enhancement)
112: return columnValue(column, "");
113: default:
114: throw new Error("Column out of bounds.");
115: }
116: }
117:
118: // ---------- Overwritten from GTDataSource ----------
119:
120: public void dispose() {
121: super .dispose();
122: table_list = null;
123: transaction = null;
124: }
125:
126: // ---------- Static ----------
127:
128: /**
129: * The data table def that describes this table of data source.
130: */
131: static final DataTableDef DEF_DATA_TABLE_DEF;
132:
133: static {
134:
135: DataTableDef def = new DataTableDef();
136: def.setTableName(new TableName(Database.SYSTEM_SCHEMA,
137: "sUSRTableInfo"));
138:
139: // Add column definitions
140: def.addColumn(stringColumn("schema"));
141: def.addColumn(stringColumn("name"));
142: def.addColumn(stringColumn("type"));
143: def.addColumn(stringColumn("other"));
144:
145: // Set to immutable
146: def.setImmutable();
147:
148: DEF_DATA_TABLE_DEF = def;
149:
150: }
151:
152: }
|