1 package org.apache.torque.map;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.util.Iterator;
20 import java.util.HashMap;
21 import java.util.Hashtable;
22 import org.apache.torque.adapter.IDMethod;
23 import org.apache.torque.oid.IDBroker;
24 import org.apache.torque.oid.IdGenerator;
25
26 /***
27 * DatabaseMap is used to model a database.
28 *
29 * @author <a href="mailto:jmcnally@collab.net">John D. McNally</a>
30 * @author <a href="mailto:dlr@collab.net">Daniel Rall</a>
31 * @version $Id: DatabaseMap.java 239630 2005-08-24 12:25:32Z henning $
32 */
33 public class DatabaseMap implements java.io.Serializable
34 {
35 /*** Name of the database. */
36 private String name;
37
38 /*** Name of the tables in the database. */
39 private Hashtable tables;
40
41 /***
42 * A special table used to generate primary keys for the other
43 * tables.
44 */
45 private TableMap idTable = null;
46
47 /*** The IDBroker that goes with the idTable. */
48 private IDBroker idBroker = null;
49
50 /*** The IdGenerators, keyed by type of idMethod. */
51 private HashMap idGenerators;
52
53 /***
54 * Required by proxy. Not used.
55 */
56 public DatabaseMap()
57 {
58 }
59
60 /***
61 * Constructor.
62 *
63 * @param name Name of the database.
64 * @param numberOfTables Number of tables in the database.
65 */
66 public DatabaseMap(String name, int numberOfTables)
67 {
68 this.name = name;
69 tables = new Hashtable((int) (1.25 * numberOfTables) + 1);
70 idGenerators = new HashMap(6);
71 }
72
73 /***
74 * Constructor.
75 *
76 * @param name Name of the database.
77 */
78 public DatabaseMap(String name)
79 {
80 this.name = name;
81 tables = new Hashtable();
82 idGenerators = new HashMap(6);
83 }
84
85 /***
86 * Does this database contain this specific table?
87 *
88 * @param table The TableMap representation of the table.
89 * @return True if the database contains the table.
90 */
91 public boolean containsTable(TableMap table)
92 {
93 return containsTable(table.getName());
94 }
95
96 /***
97 * Does this database contain this specific table?
98 *
99 * @param name The String representation of the table.
100 * @return True if the database contains the table.
101 */
102 public boolean containsTable(String name)
103 {
104 if (name.indexOf('.') > 0)
105 {
106 name = name.substring(0, name.indexOf('.'));
107 }
108 return tables.containsKey(name);
109 }
110
111 /***
112 * Get the ID table for this database.
113 *
114 * @return A TableMap.
115 */
116 public TableMap getIdTable()
117 {
118 return idTable;
119 }
120
121 /***
122 * Get the IDBroker for this database.
123 *
124 * @return An IDBroker.
125 */
126 public IDBroker getIDBroker()
127 {
128 return idBroker;
129 }
130
131 /***
132 * Get the name of this database.
133 *
134 * @return A String.
135 */
136 public String getName()
137 {
138 return name;
139 }
140
141 /***
142 * Get a TableMap for the table by name.
143 *
144 * @param name Name of the table.
145 * @return A TableMap, null if the table was not found.
146 */
147 public TableMap getTable(String name)
148 {
149 return (TableMap) tables.get(name);
150 }
151
152 /***
153 * Get a TableMap[] of all of the tables in the database.
154 *
155 * @return A TableMap[].
156 */
157 public TableMap[] getTables()
158 {
159 TableMap[] dbTables = new TableMap[tables.size()];
160 Iterator it = tables.values().iterator();
161 int i = 0;
162 while (it.hasNext())
163 {
164 dbTables[i++] = (TableMap) it.next() ;
165 }
166 return dbTables;
167 }
168
169 /***
170 * Add a new table to the database by name. It creates an empty
171 * TableMap that you need to populate.
172 *
173 * @param tableName The name of the table.
174 */
175 public void addTable(String tableName)
176 {
177 TableMap tmap = new TableMap(tableName, this);
178 tables.put(tableName, tmap);
179 }
180
181 /***
182 * Add a new table to the database by name. It creates an empty
183 * TableMap that you need to populate.
184 *
185 * @param tableName The name of the table.
186 * @param numberOfColumns The number of columns in the table.
187 */
188 public void addTable(String tableName, int numberOfColumns)
189 {
190 TableMap tmap = new TableMap(tableName, numberOfColumns, this);
191 tables.put(tableName, tmap);
192 }
193
194 /***
195 * Add a new TableMap to the database.
196 *
197 * @param map The TableMap representation.
198 */
199 public void addTable(TableMap map)
200 {
201 tables.put(map.getName(), map);
202 }
203
204 /***
205 * Set the ID table for this database.
206 *
207 * @param idTable The TableMap representation for the ID table.
208 */
209 public void setIdTable(TableMap idTable)
210 {
211 this.idTable = idTable;
212 addTable(idTable);
213 idBroker = new IDBroker(idTable);
214 addIdGenerator(IDMethod.ID_BROKER, idBroker);
215 }
216
217 /***
218 * Set the ID table for this database.
219 *
220 * @param tableName The name for the ID table.
221 */
222 public void setIdTable(String tableName)
223 {
224 TableMap tmap = new TableMap(tableName, this);
225 setIdTable(tmap);
226 }
227
228 /***
229 * Add a type of id generator for access by a TableMap.
230 *
231 * @param type a <code>String</code> value
232 * @param idGen an <code>IdGenerator</code> value
233 */
234 public void addIdGenerator(String type, IdGenerator idGen)
235 {
236 idGenerators.put(type, idGen);
237 }
238
239 /***
240 * Get a type of id generator. Valid values are listed in the
241 * {@link org.apache.torque.adapter.IDMethod} interface.
242 *
243 * @param type a <code>String</code> value
244 * @return an <code>IdGenerator</code> value
245 */
246 IdGenerator getIdGenerator(String type)
247 {
248 return (IdGenerator) idGenerators.get(type);
249 }
250 }