KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

50   private DataTableDef[] visible_tables;
51
52   /**
53    * The number of rows in this table.
54    */

55   private int row_count;
56
57   /**
58    * Constructor.
59    */

60   public GTTableColumnsDataSource(Transaction transaction) {
61     super(transaction.getSystem());
62     this.transaction = transaction;
63   }
64
65   /**
66    * Initialize the data source.
67    */

68   public GTTableColumnsDataSource init() {
69     // All the tables
70
TableName[] list = transaction.getTableList();
71     visible_tables = new DataTableDef[list.length];
72     row_count = 0;
73     for (int i = 0; i < list.length; ++i) {
74       DataTableDef def = transaction.getDataTableDef(list[i]);
75       row_count += def.columnCount();
76       visible_tables[i] = def;
77     }
78     return this;
79   }
80
81   // ---------- Implemented from GTDataSource ----------
82

83   public DataTableDef getDataTableDef() {
84     return DEF_DATA_TABLE_DEF;
85   }
86
87   public int getRowCount() {
88     return row_count;
89   }
90
91   public TObject getCellContents(final int column, final int row) {
92
93     final int sz = visible_tables.length;
94     int rs = 0;
95     for (int n = 0; n < sz; ++n) {
96       final DataTableDef def = visible_tables[n];
97       final int b = rs;
98       rs += def.columnCount();
99       if (row >= b && row < rs) {
100         // This is the column that was requested,
101
int seq_no = row - b;
102         DataTableColumnDef col_def = def.columnAt(seq_no);
103         switch (column) {
104           case 0: // schema
105
return columnValue(column, def.getSchema());
106           case 1: // table
107
return columnValue(column, def.getName());
108           case 2: // column
109
return columnValue(column, col_def.getName());
110           case 3: // sql_type
111
return columnValue(column,
112                                BigNumber.fromLong(col_def.getSQLType()));
113           case 4: // type_desc
114
return columnValue(column, col_def.getSQLTypeString());
115           case 5: // size
116
return columnValue(column, BigNumber.fromLong(col_def.getSize()));
117           case 6: // scale
118
return columnValue(column, BigNumber.fromLong(col_def.getScale()));
119           case 7: // not_null
120
return columnValue(column, new Boolean JavaDoc(col_def.isNotNull()));
121           case 8: // default
122
return columnValue(column,
123                                col_def.getDefaultExpressionString());
124           case 9: // index_str
125
return columnValue(column, col_def.getIndexScheme());
126           case 10: // seq_no
127
return columnValue(column, BigNumber.fromLong(seq_no));
128           default:
129             throw new Error JavaDoc("Column out of bounds.");
130         }
131       }
132
133     } // for each visible table
134

135     throw new Error JavaDoc("Row out of bounds.");
136   }
137
138   // ---------- Overwritten ----------
139

140   public void dispose() {
141     super.dispose();
142     visible_tables = null;
143     transaction = null;
144   }
145
146   // ---------- Static ----------
147

148   /**
149    * The data table def that describes this table of data source.
150    */

151   static final DataTableDef DEF_DATA_TABLE_DEF;
152
153   static {
154
155     DataTableDef def = new DataTableDef();
156     def.setTableName(
157              new TableName(Database.SYSTEM_SCHEMA, "sUSRTableColumns"));
158
159     // Add column definitions
160
def.addColumn(stringColumn("schema"));
161     def.addColumn(stringColumn("table"));
162     def.addColumn(stringColumn("column"));
163     def.addColumn(numericColumn("sql_type"));
164     def.addColumn(stringColumn("type_desc"));
165     def.addColumn(numericColumn("size"));
166     def.addColumn(numericColumn("scale"));
167     def.addColumn(booleanColumn("not_null"));
168     def.addColumn(stringColumn("default"));
169     def.addColumn(stringColumn("index_str"));
170     def.addColumn(numericColumn("seq_no"));
171
172     // Set to immutable
173
def.setImmutable();
174
175     DEF_DATA_TABLE_DEF = def;
176
177   }
178
179 }
180
Popular Tags