View Javadoc

1   package org.apache.torque;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import org.apache.torque.adapter.Adapter;
26  import org.apache.torque.adapter.IDMethod;
27  import org.apache.torque.dsfactory.DataSourceFactory;
28  import org.apache.torque.map.ColumnMap;
29  import org.apache.torque.map.DatabaseMap;
30  import org.apache.torque.map.TableMap;
31  import org.apache.torque.oid.IDBroker;
32  import org.apache.torque.oid.IdGenerator;
33  
34  /**
35   * Bundles all information about a database. This includes the database adapter,
36   * the database Map and the Data Source Factory.
37   */
38  public class Database
39  {
40      /**
41       * The name of the database.
42       */
43      private final String name;
44  
45      /**
46       * The Database adapter which encapsulates database-specific peculiarities.
47       */
48      private Adapter adapter;
49  
50      /**
51       * The Map of this database.
52       */
53      private DatabaseMap databaseMap;
54  
55      /**
56       * The DataSourceFactory to obtain connections to this database.
57       */
58      private DataSourceFactory dataSourceFactory;
59  
60      /** The Schema name of this database, may be null if not set. */
61      private String schema = null;
62  
63      /**
64       * A special table used to generate primary keys for the other tables.
65       */
66      private TableMap idTable = null;
67  
68      /** The IDBroker that goes with the idTable. */
69      private IDBroker idBroker = null;
70  
71      /** The IdGenerators, keyed by type of idMethod. */
72      private final Map<IDMethod, IdGenerator> idGenerators
73          = new HashMap<IDMethod, IdGenerator>();
74  
75      /**
76       * Creates a new Database with the given name.
77       *
78       * @param name the name of the database, not null.
79       *
80       * @throws NullPointerException if name is null.
81       */
82      Database(String name)
83      {
84          if (name == null)
85          {
86              throw new NullPointerException("name is null");
87          }
88          this.name = name;
89      }
90  
91      /**
92       * Rturns the name of the database.
93       *
94       * @return the name of the database, not null.
95       */
96      public String getName()
97      {
98          return name;
99      }
100 
101     /**
102      * Returns the adapter to this database.
103      *
104      * @return the adapter to this database, or null if no adapter is set.
105      */
106     public Adapter getAdapter()
107     {
108         return adapter;
109     }
110 
111     /**
112      * Sets the adapter for this database.
113      *
114      * @param anAdapter The adapter for this database, or null to remove the
115      *        current adapter from this database.
116      */
117     public void setAdapter(Adapter anAdapter)
118     {
119         this.adapter = anAdapter;
120     }
121 
122     /**
123      * Returns the database map for this database.
124      * If the database map does not exist yet, it is created by this method.
125      */
126     public synchronized DatabaseMap getDatabaseMap()
127     {
128         if (databaseMap == null)
129         {
130             databaseMap = new DatabaseMap(this);
131         }
132         return databaseMap;
133     }
134 
135     /**
136      * Returns the DataSourceFactory for this database.
137      * The DataSourceFactory is responsible to create connections
138      * to this database.
139      *
140      * @return the DataSourceFactory for this database, or null if no
141      *         DataSourceFactory exists for this database.
142      */
143     public DataSourceFactory getDataSourceFactory()
144     {
145         return dataSourceFactory;
146     }
147 
148     /**
149      * Sets the DataSourceFactory for this database.
150      * The DataSourceFactory is responsible to create connections
151      * to this database.
152      *
153      * @param aDataSourceFactory The new DataSorceFactory for this database,
154      *        or null to remove the current DataSourceFactory.
155      */
156     public void setDataSourceFactory(DataSourceFactory aDataSourceFactory)
157     {
158         this.dataSourceFactory = aDataSourceFactory;
159     }
160 
161     /**
162      * Get the ID table for this database.
163      *
164      * @return A TableMap, or null if not yet initialized or no id table exists
165      *         for this database.
166      */
167     public TableMap getIdTable()
168     {
169         return idTable;
170     }
171 
172     /**
173      * Set the ID table for this database.
174      *
175      * @param idTable The TableMap representation for the ID table.
176      */
177     public void setIdTable(TableMap idTable)
178     {
179         this.idTable = idTable;
180         getDatabaseMap().setIdTable(idTable);
181     }
182 
183     /**
184      * Set the ID table for this database.
185      *
186      * @param tableName The name for the ID table.
187      */
188     public void setIdTable(String tableName)
189     {
190         TableMap tmap = new TableMap(tableName, getDatabaseMap());
191         setIdTable(tmap);
192     }
193 
194     /**
195      * Get the IDBroker for this database.
196      *
197      * @return The IDBroker for this database, or null if no IdBroker has
198      *         been started for this database.
199      */
200     public IDBroker getIdBroker()
201     {
202         return idBroker;
203     }
204 
205     /**
206      * Creates the IDBroker for this Database and registers it with Torque.
207      * so it is either started instantly if Torque is already initialized
208      * or it is started when Torque is initialized.
209      * The information about the IdTable is stored in the databaseMap.
210      * If an IDBroker already exists for this Database, the method
211      * does nothing.
212      *
213      * @return true if a new IDBroker was created, false otherwise.
214      *
215      * @deprecated This method will be removed in a future version of Torque.
216      *             Please use createAndRegisterIdBroker() instead.
217      */
218     @Deprecated
219     public synchronized boolean startIdBroker()
220     {
221         return createAndRegisterIdBroker();
222     }
223 
224     /**
225      * Creates the IDBroker for this Database and registers it with Torque.
226      * so it is either started instantly if Torque is already initialized
227      * or it is started when Torque is initialized.
228      * The information about the IdTable is stored in the databaseMap.
229      * If an IDBroker already exists for this Database, the method
230      * does nothing.
231      *
232      * @return true if a new IDBroker was created, false otherwise.
233      */
234     public synchronized boolean createAndRegisterIdBroker()
235     {
236         if (idBroker != null)
237         {
238             return false;
239         }
240         setIdTable("ID_TABLE");
241         TableMap tMap = getIdTable();
242         ColumnMap idTableId = new ColumnMap("QUANTITY", tMap);
243         idTableId.setType(Integer.valueOf(0));
244         idTableId.setPrimaryKey(true);
245         tMap.addColumn(idTableId);
246         ColumnMap tableName = new ColumnMap("TABLE_NAME", tMap);
247         tableName.setType("");
248         tMap.addColumn(tableName);
249         ColumnMap nextId = new ColumnMap("NEXT_ID", tMap);
250         nextId.setType(Integer.valueOf(0));
251         tMap.addColumn(nextId);
252         ColumnMap quantity = new ColumnMap("QUANTITY", tMap);
253         quantity.setType(Integer.valueOf(0));
254         tMap.addColumn(quantity);
255         idBroker = new IDBroker(this);
256         addIdGenerator(IDMethod.ID_BROKER, idBroker);
257         return true;
258     }
259 
260     /**
261      * Returns the IdGenerator of the given type for this Database.
262      *
263      * @param type The type (i.e.name) of the IdGenerator.
264      *
265      * @return The IdGenerator of the requested type, or null if no IdGenerator
266      *         exists for the requested type.
267      */
268     public IdGenerator getIdGenerator(IDMethod type)
269     {
270         return idGenerators.get(type);
271     }
272 
273     /**
274      * Adds an IdGenerator to the database.
275      *
276      * @param type The type of the IdGenerator.
277      * @param idGen The new IdGenerator for the type, or null
278      *        to remove the IdGenerator of the given type.
279      */
280     public void addIdGenerator(IDMethod type, IdGenerator idGen)
281     {
282         idGenerators.put(type, idGen);
283     }
284 
285     /**
286      * Returns the database schema for this Database.
287      *
288      * @return the database schema for this database, or null if no schema
289      *         has been set.
290      */
291     public String getSchema()
292     {
293         return schema;
294     }
295 
296     /**
297      * Sets the schema for this database.
298      *
299      * @param schema the name of the database schema to set, or null to remove
300      *        the current schema.
301      */
302     public void setSchema(String schema)
303     {
304         this.schema = schema;
305     }
306 }