KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * com.mckoi.database.AbstractInternalTableInfo2 14 Mar 2003
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 /**
28  * An implementation of InternalTableInfo that provides a number of methods to
29  * aid in the productions of the InternalTableInfo interface for a transaction
30  * specific model of a set of tables that is based on a single system table.
31  * This would be used to model table views for triggers, views, procedures and
32  * sequences all of which are table sets tied to a single table respectively,
33  * and the number of items in the table represent the number of tables to model.
34  * <p>
35  * This abstraction assumes that the name of the schema/table are in columns 0
36  * and 1 of the backed system table.
37  *
38  * @author Tobias Downer
39  */

40
41 abstract class AbstractInternalTableInfo2 implements InternalTableInfo {
42
43   /**
44    * The transaction we are connected to.
45    */

46   protected final Transaction transaction;
47
48   /**
49    * The table in the transaction that contains the list of items we are
50    * modelling.
51    */

52   protected final TableName table_name;
53   
54   /**
55    * Constructor.
56    */

57   public AbstractInternalTableInfo2(Transaction transaction,
58                                     TableName table_name) {
59     this.transaction = transaction;
60     this.table_name = table_name;
61   }
62   
63   public int getTableCount() {
64     if (transaction.tableExists(table_name)) {
65       return transaction.getTable(table_name).getRowCount();
66     }
67     else {
68       return 0;
69     }
70   }
71
72   public int findTableName(TableName name) {
73     if (transaction.realTableExists(table_name)) {
74       // Search the table. We assume that the schema and name of the object
75
// are in columns 0 and 1 respectively.
76
MutableTableDataSource table = transaction.getTable(table_name);
77       RowEnumeration row_e = table.rowEnumeration();
78       int p = 0;
79       while (row_e.hasMoreRows()) {
80         int row_index = row_e.nextRowIndex();
81         TObject ob_name = table.getCellContents(1, row_index);
82         if (ob_name.getObject().toString().equals(name.getName())) {
83           TObject ob_schema = table.getCellContents(0, row_index);
84           if (ob_schema.getObject().toString().equals(name.getSchema())) {
85             // Match so return this
86
return p;
87           }
88         }
89         ++p;
90       }
91     }
92     return -1;
93   }
94
95   public TableName getTableName(int i) {
96     if (transaction.realTableExists(table_name)) {
97       // Search the table. We assume that the schema and name of the object
98
// are in columns 0 and 1 respectively.
99
MutableTableDataSource table = transaction.getTable(table_name);
100       RowEnumeration row_e = table.rowEnumeration();
101       int p = 0;
102       while (row_e.hasMoreRows()) {
103         int row_index = row_e.nextRowIndex();
104         if (i == p) {
105           TObject ob_schema = table.getCellContents(0, row_index);
106           TObject ob_name = table.getCellContents(1, row_index);
107           return new TableName(ob_schema.getObject().toString(),
108                                ob_name.getObject().toString());
109         }
110         ++p;
111       }
112     }
113     throw new RuntimeException JavaDoc("Out of bounds.");
114   }
115
116   public boolean containsTableName(TableName name) {
117     // This set can not contain the table that is backing it, so we always
118
// return false for that. This check stops an annoying recursive
119
// situation for table name resolution.
120
if (name.equals(table_name)) {
121       return false;
122     }
123     else {
124       return findTableName(name) != -1;
125     }
126   }
127
128   public abstract DataTableDef getDataTableDef(int i);
129
130   public abstract String JavaDoc getTableType(int i);
131   
132   public abstract MutableTableDataSource createInternalTable(int index);
133
134 }
135
136
Popular Tags