%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.torque.map.DatabaseMap |
|
|
1 | package org.apache.torque.map; |
|
2 | ||
3 | /* |
|
4 | * Copyright 2001-2005 The Apache Software Foundation. |
|
5 | * |
|
6 | * Licensed under the Apache License, Version 2.0 (the "License") |
|
7 | * you may not use this file except in compliance with the License. |
|
8 | * You may obtain a copy of the License at |
|
9 | * |
|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
11 | * |
|
12 | * Unless required by applicable law or agreed to in writing, software |
|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15 | * See the License for the specific language governing permissions and |
|
16 | * limitations under the License. |
|
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 | 1 | private TableMap idTable = null; |
46 | ||
47 | /** The IDBroker that goes with the idTable. */ |
|
48 | 1 | 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 | 0 | { |
58 | 0 | } |
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 | 0 | { |
68 | 0 | this.name = name; |
69 | 0 | tables = new Hashtable((int) (1.25 * numberOfTables) + 1); |
70 | 0 | idGenerators = new HashMap(6); |
71 | 0 | } |
72 | ||
73 | /** |
|
74 | * Constructor. |
|
75 | * |
|
76 | * @param name Name of the database. |
|
77 | */ |
|
78 | public DatabaseMap(String name) |
|
79 | 1 | { |
80 | 1 | this.name = name; |
81 | 1 | tables = new Hashtable(); |
82 | 1 | idGenerators = new HashMap(6); |
83 | 1 | } |
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 | 0 | 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 | 0 | if (name.indexOf('.') > 0) |
105 | { |
|
106 | 0 | name = name.substring(0, name.indexOf('.')); |
107 | } |
|
108 | 0 | 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 | 1 | return idTable; |
119 | } |
|
120 | ||
121 | /** |
|
122 | * Get the IDBroker for this database. |
|
123 | * |
|
124 | * @return An IDBroker. |
|
125 | */ |
|
126 | public IDBroker getIDBroker() |
|
127 | { |
|
128 | 0 | return idBroker; |
129 | } |
|
130 | ||
131 | /** |
|
132 | * Get the name of this database. |
|
133 | * |
|
134 | * @return A String. |
|
135 | */ |
|
136 | public String getName() |
|
137 | { |
|
138 | 1 | 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 | 0 | 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 | 0 | TableMap[] dbTables = new TableMap[tables.size()]; |
160 | 0 | Iterator it = tables.values().iterator(); |
161 | 0 | int i = 0; |
162 | 0 | while (it.hasNext()) |
163 | { |
|
164 | 0 | dbTables[i++] = (TableMap) it.next() ; |
165 | } |
|
166 | 0 | 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 | 0 | TableMap tmap = new TableMap(tableName, this); |
178 | 0 | tables.put(tableName, tmap); |
179 | 0 | } |
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 | 0 | TableMap tmap = new TableMap(tableName, numberOfColumns, this); |
191 | 0 | tables.put(tableName, tmap); |
192 | 0 | } |
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 | 1 | tables.put(map.getName(), map); |
202 | 1 | } |
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 | 1 | this.idTable = idTable; |
212 | 1 | addTable(idTable); |
213 | 1 | idBroker = new IDBroker(idTable); |
214 | 1 | addIdGenerator(IDMethod.ID_BROKER, idBroker); |
215 | 1 | } |
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 | 1 | TableMap tmap = new TableMap(tableName, this); |
225 | 1 | setIdTable(tmap); |
226 | 1 | } |
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 | 4 | idGenerators.put(type, idGen); |
237 | 4 | } |
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 | 0 | return (IdGenerator) idGenerators.get(type); |
249 | } |
|
250 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |