001: /**
002: * com.mckoi.database.GTTableColumnsDataSource 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 com.mckoi.util.BigNumber;
026: import com.mckoi.database.global.SQLTypes;
027:
028: /**
029: * An implementation of MutableTableDataSource that presents information
030: * about the columns of all tables in all schema.
031: * <p>
032: * NOTE: This is not designed to be a long kept object. It must not last
033: * beyond the lifetime of a transaction.
034: *
035: * @author Tobias Downer
036: */
037:
038: final class GTTableColumnsDataSource extends GTDataSource {
039:
040: /**
041: * The transaction that is the view of this information.
042: */
043: private Transaction transaction;
044:
045: /**
046: * The list of all DataTableDef visible to the transaction.
047: */
048: private DataTableDef[] visible_tables;
049:
050: /**
051: * The number of rows in this table.
052: */
053: private int row_count;
054:
055: /**
056: * Constructor.
057: */
058: public GTTableColumnsDataSource(Transaction transaction) {
059: super (transaction.getSystem());
060: this .transaction = transaction;
061: }
062:
063: /**
064: * Initialize the data source.
065: */
066: public GTTableColumnsDataSource init() {
067: // All the tables
068: TableName[] list = transaction.getTableList();
069: visible_tables = new DataTableDef[list.length];
070: row_count = 0;
071: for (int i = 0; i < list.length; ++i) {
072: DataTableDef def = transaction.getDataTableDef(list[i]);
073: row_count += def.columnCount();
074: visible_tables[i] = def;
075: }
076: return this ;
077: }
078:
079: // ---------- Implemented from GTDataSource ----------
080:
081: public DataTableDef getDataTableDef() {
082: return DEF_DATA_TABLE_DEF;
083: }
084:
085: public int getRowCount() {
086: return row_count;
087: }
088:
089: public TObject getCellContents(final int column, final int row) {
090:
091: final int sz = visible_tables.length;
092: int rs = 0;
093: for (int n = 0; n < sz; ++n) {
094: final DataTableDef def = visible_tables[n];
095: final int b = rs;
096: rs += def.columnCount();
097: if (row >= b && row < rs) {
098: // This is the column that was requested,
099: int seq_no = row - b;
100: DataTableColumnDef col_def = def.columnAt(seq_no);
101: switch (column) {
102: case 0: // schema
103: return columnValue(column, def.getSchema());
104: case 1: // table
105: return columnValue(column, def.getName());
106: case 2: // column
107: return columnValue(column, col_def.getName());
108: case 3: // sql_type
109: return columnValue(column, BigNumber
110: .fromLong(col_def.getSQLType()));
111: case 4: // type_desc
112: return columnValue(column, col_def
113: .getSQLTypeString());
114: case 5: // size
115: return columnValue(column, BigNumber
116: .fromLong(col_def.getSize()));
117: case 6: // scale
118: return columnValue(column, BigNumber
119: .fromLong(col_def.getScale()));
120: case 7: // not_null
121: return columnValue(column, new Boolean(col_def
122: .isNotNull()));
123: case 8: // default
124: return columnValue(column, col_def
125: .getDefaultExpressionString());
126: case 9: // index_str
127: return columnValue(column, col_def.getIndexScheme());
128: case 10: // seq_no
129: return columnValue(column, BigNumber
130: .fromLong(seq_no));
131: default:
132: throw new Error("Column out of bounds.");
133: }
134: }
135:
136: } // for each visible table
137:
138: throw new Error("Row out of bounds.");
139: }
140:
141: // ---------- Overwritten ----------
142:
143: public void dispose() {
144: super .dispose();
145: visible_tables = null;
146: transaction = null;
147: }
148:
149: // ---------- Static ----------
150:
151: /**
152: * The data table def that describes this table of data source.
153: */
154: static final DataTableDef DEF_DATA_TABLE_DEF;
155:
156: static {
157:
158: DataTableDef def = new DataTableDef();
159: def.setTableName(new TableName(Database.SYSTEM_SCHEMA,
160: "sUSRTableColumns"));
161:
162: // Add column definitions
163: def.addColumn(stringColumn("schema"));
164: def.addColumn(stringColumn("table"));
165: def.addColumn(stringColumn("column"));
166: def.addColumn(numericColumn("sql_type"));
167: def.addColumn(stringColumn("type_desc"));
168: def.addColumn(numericColumn("size"));
169: def.addColumn(numericColumn("scale"));
170: def.addColumn(booleanColumn("not_null"));
171: def.addColumn(stringColumn("default"));
172: def.addColumn(stringColumn("index_str"));
173: def.addColumn(numericColumn("seq_no"));
174:
175: // Set to immutable
176: def.setImmutable();
177:
178: DEF_DATA_TABLE_DEF = def;
179:
180: }
181:
182: }
|