Source code: javatools/db/AbstractTable.java
1 /*
2 Javatools (modified version) - Some useful general classes.
3 Copyright (C) 2002-2003 Chris Bitmead (original) Antonio Petrelli (modified)
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19 Contact me at: brenmcguire@users.sourceforge.net
20 */
21
22 package javatools.db;
23 import java.util.*;
24 import java.sql.*;
25 import javatools.util.Compare;
26 /**
27 * A class representing tabular data. Could be a real database table or the result
28 * of a SELECT.
29 * Based upon class DbTable written by Chris Bitmead, of which it is an abstraction
30 * to differentiate between real tables and results of a SELECT command.
31 * @author Antonio Petrelli
32 * @version 0.2.0
33 * @created 5 September 2001
34 */
35
36 public abstract class AbstractTable extends Object implements DbTableUser {
37 /** Maps the columns to their names.
38 */
39 protected HashMap colNameMap = new HashMap();
40 /** The name of used table
41 */
42 protected String tableName;
43 /** This array contains the names of the fields of the table.
44 */
45 protected String names[];
46 /** This array contains the display size for each field.
47 */
48 protected int[] displaySize;
49 /** This array contains the SQL types for each field.
50 */
51 protected int[] types;
52
53 /** Sets the name of the referenced table.
54 * @param v The table name.
55 */
56 public void setTableName(String v) {
57 tableName = v;
58 }
59
60 /** Returns the complete table name.
61 * @return The complete table name.
62 */
63 public String getFullTableName() {
64 return tableName;
65 }
66
67 public String getColumnName(int index) {
68 return names[index];
69 }
70
71 public int getColumnIndex(String value) {
72 Integer tempValue;
73
74 tempValue = (Integer) colNameMap.get(value);
75 if (tempValue != null)
76 return tempValue.intValue();
77 else
78 return -1;
79 }
80
81 public void setColumnName(int index, String value) {
82 names[index] = value;
83 colNameMap.put(value, new Integer(index));
84 }
85
86 /** Returns the number of columns
87 * @return The number of columns.
88 */
89 public int getColumnCount() {
90 return names.length;
91 }
92
93 public void setColumnCount(int size) {
94 names = new String[size];
95 }
96
97 /** Check if an object is equal to the current one.
98 * @param o The object to be compared.
99 * @return <CODE>true</CODE>: if these object are equal;
100 * <CODE>false</CODE>: otherwise.
101 */
102 public boolean equals(Object o) {
103 if (!(o instanceof AbstractTable)) {
104 return false;
105 }
106 AbstractTable t = (AbstractTable) o;
107 return Compare.eq(tableName, t.tableName);
108 }
109
110 /** Returns used tables (useful in joined tables).
111 * @param c The set to put used tables into.
112 */
113 public void usesTables(Set c) {
114 c.add(this);
115 }
116 }