Author: tfischer
Date: Sun May 15 18:41:15 2011
New Revision: 1103503
URL: http://svn.apache.org/viewvc?rev=1103503&view=rev
Log:
TORQUE-157: move fields from DatabaseMap to Database
Modified:
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/Database.java
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/TorqueInstance.java
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/map/DatabaseMap.java
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/map/TableMap.java
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/oid/IDBroker.java
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/SqlBuilder.java
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/BasePeerImpl.java
Modified:
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/Database.java
URL:
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/Database.java?rev=1103503&r1=1103502&r2=1103503&view=diff
==============================================================================
---
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/Database.java
(original)
+++
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/Database.java
Sun May 15 18:41:15 2011
@@ -19,9 +19,14 @@ package org.apache.torque;
* under the License.
*/
+import java.util.HashMap;
+import java.util.Map;
+
import org.apache.torque.adapter.DB;
+import org.apache.torque.adapter.IDMethod;
import org.apache.torque.dsfactory.DataSourceFactory;
import org.apache.torque.map.DatabaseMap;
+import org.apache.torque.map.TableMap;
import org.apache.torque.oid.IDBroker;
import org.apache.torque.oid.IdGenerator;
@@ -31,6 +36,9 @@ import org.apache.torque.oid.IdGenerator
*/
public class Database
{
+ /** The initial size of the Id-Generators map. */
+ private static final int ID_GENERATORS_INITIAL_SIZE = 6;
+
/**
* The name of the database. Must be the same as the key in Torque's
* databaseMap.
@@ -48,11 +56,23 @@ public class Database
private DatabaseMap databaseMap;
/**
- * The DataSourceFactory to optain connections to this database.
+ * The DataSourceFactory to obtain connections to this database.
*/
private DataSourceFactory dataSourceFactory;
/**
+ * A special table used to generate primary keys for the other tables.
+ */
+ private TableMap idTable = null;
+
+ /** The IDBroker that goes with the idTable. */
+ private IDBroker idBroker = null;
+
+ /** The IdGenerators, keyed by type of idMethod. */
+ private Map<String, IdGenerator> idGenerators
+ = new HashMap<String, IdGenerator>(ID_GENERATORS_INITIAL_SIZE);
+
+ /**
* Creates a new Database with the given name.
*
* @param aName the name of the database, not null.
@@ -101,7 +121,7 @@ public class Database
{
if (databaseMap == null)
{
- databaseMap = new DatabaseMap(name);
+ databaseMap = new DatabaseMap(this);
}
return databaseMap;
}
@@ -133,63 +153,98 @@ public class Database
}
/**
+ * Get the ID table for this database.
+ *
+ * @return A TableMap, or null if not yet initialized or no id table exists
+ * for this database.
+ */
+ public TableMap getIdTable()
+ {
+ return idTable;
+ }
+
+ /**
+ * Set the ID table for this database.
+ *
+ * @param idTable The TableMap representation for the ID table.
+ */
+ public void setIdTable(TableMap idTable)
+ {
+ this.idTable = idTable;
+ getDatabaseMap().addTable(idTable);
+ }
+
+ /**
+ * Set the ID table for this database.
+ *
+ * @param tableName The name for the ID table.
+ */
+ public void setIdTable(String tableName)
+ {
+ TableMap tmap = new TableMap(tableName, getDatabaseMap());
+ setIdTable(tmap);
+ }
+
+ /**
* Get the IDBroker for this database.
*
* @return The IDBroker for this database, or null if no IdBroker has
* been started for this database.
*/
- public IDBroker getIDBroker()
+ public IDBroker getIdBroker()
{
- if (databaseMap == null)
- {
- return null;
- }
- return databaseMap.getIDBroker();
+ return idBroker;
}
/**
- * Creates the IDBroker for this DatabaseMap and starts it for the
- * given database.
+ * Creates the IDBroker for this Database and starts it.
* The information about the IdTable is stored in the databaseMap.
* If an IDBroker already exists for the DatabaseMap, the method
* does nothing.
*
* @return true if a new IDBroker was created, false otherwise.
*/
- public synchronized boolean startIDBroker()
+ public synchronized boolean startIdBroker()
{
- DatabaseMap dbMap = getDatabaseMap();
- if (dbMap.getIDBroker() != null)
+ if (idBroker != null)
{
return false;
}
- return dbMap.startIdBroker();
+ setIdTable("ID_TABLE");
+ TableMap tMap = getIdTable();
+ tMap.addPrimaryKey("ID_TABLE_ID", new Integer(0));
+ tMap.addColumn("TABLE_NAME", "");
+ tMap.addColumn("NEXT_ID", new Integer(0));
+ tMap.addColumn("QUANTITY", new Integer(0));
+ idBroker = new IDBroker(idTable);
+ addIdGenerator(IDMethod.ID_BROKER, idBroker);
+ return true;
}
/**
* Returns the IdGenerator of the given type for this Database.
- * @param type The type (i.e.name) of the IdGenerator
+ *
+ * @param type The type (i.e.name) of the IdGenerator.
+ *
* @return The IdGenerator of the requested type, or null if no IdGenerator
* exists for the requested type.
*/
public IdGenerator getIdGenerator(String type)
{
- if (databaseMap == null)
- {
- return null;
- }
- return databaseMap.getIdGenerator(type);
+ return (IdGenerator) idGenerators.get(type);
}
/**
* Adds an IdGenerator to the database.
- * @param type The type of the IdGenerator
+ *
+ * @param type The type of the IdGenerator.
+ *
* @param idGen The new IdGenerator for the type, or null
* to remove the IdGenerator of the given type.
*/
public void addIdGenerator(String type, IdGenerator idGen)
{
- getDatabaseMap().addIdGenerator(type, idGen);
+ idGenerators.put(type, idGen);
}
/**
Modified:
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/TorqueInstance.java
URL:
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/TorqueInstance.java?rev=1103503&r1=1103502&r2=1103503&view=diff
==============================================================================
---
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/TorqueInstance.java
(original)
+++
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/TorqueInstance.java
Sun May 15 18:41:15 2011
@@ -281,12 +281,6 @@ public class TorqueInstance
+ handle + " as Adapter");
// add Id generators
-
- // first make sure that the dtabaseMap exists for the name
- // as the idGenerators are still stored in the database map
- // TODO: change when the idGenerators are stored in the
- // database
- getDatabaseMap(handle);
for (int i = 0;
i < IDGeneratorFactory.ID_GENERATOR_METHODS.length;
i++)
@@ -303,12 +297,6 @@ public class TorqueInstance
log.error("Error creating a database adapter instance", e);
throw new TorqueException(e);
}
- catch (TorqueException e)
- {
- log.error("Error reading configuration seeking database "
- + "adapters", e);
- throw new TorqueException(e);
- }
// check that at least the default database has got an adapter.
Database defaultDatabase
@@ -681,7 +669,7 @@ public class TorqueInstance
{
for (Database database : databases.values())
{
- IDBroker idBroker = database.getIDBroker();
+ IDBroker idBroker = database.getIdBroker();
if (idBroker != null)
{
idBroker.stop();
Modified:
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/map/DatabaseMap.java
URL:
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/map/DatabaseMap.java?rev=1103503&r1=1103502&r2=1103503&view=diff
==============================================================================
---
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/map/DatabaseMap.java
(original)
+++
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/map/DatabaseMap.java
Sun May 15 18:41:15 2011
@@ -22,17 +22,14 @@ package org.apache.torque.map;
import java.lang.reflect.Method;
import java.text.MessageFormat;
import java.util.Collections;
-import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.StringTokenizer;
import org.apache.commons.lang.StringUtils;
+import org.apache.torque.Database;
import org.apache.torque.TorqueException;
-import org.apache.torque.adapter.IDMethod;
-import org.apache.torque.oid.IDBroker;
-import org.apache.torque.oid.IdGenerator;
/**
* DatabaseMap is used to model a database.
@@ -82,29 +79,13 @@ public class DatabaseMap implements java
/** The serialVersionUID for this class. */
private static final long serialVersionUID = 955251837095032274L;
- /** The initial size of the Id-Generators map. */
- private static final int ID_GENERATORS_INITIAL_SIZE = 6;
-
- /** Name of the database. */
- private String name;
+ /** The database where this databaseMap belongs to. */
+ private Database database;
/** Name of the tables in the database. */
private Map<String, TableMap> tables
= Collections.synchronizedMap(new LinkedHashMap<String, TableMap>());
- /**
- * A special table used to generate primary keys for the other
- * tables.
- */
- private TableMap idTable = null;
-
- /** The IDBroker that goes with the idTable. */
- private IDBroker idBroker = null;
-
- /** The IdGenerators, keyed by type of idMethod. */
- private Map<String, IdGenerator> idGenerators
- = new HashMap<String, IdGenerator>(ID_GENERATORS_INITIAL_SIZE);
-
/** Flag indicating that all tables have been loaded via initialize() */
private boolean isInitialized = false;
@@ -115,33 +96,13 @@ public class DatabaseMap implements java
/**
* Constructs a new DatabaseMap.
*/
- public DatabaseMap()
+ public DatabaseMap(Database database)
{
- }
-
- /**
- * Constructor.
- *
- * @param name Name of the database.
- * @param numberOfTables Number of tables in the database.
- * @deprecated use DatabaseMap() instead. Will be removed
- * in a future version of Torque.
- */
- public DatabaseMap(String name, int numberOfTables)
- {
- this.name = name;
- }
-
- /**
- * Constructor.
- *
- * @param name Name of the database.
- * @deprecated use DatabaseMap() instead. Will be removed
- * in a future version of Torque.
- */
- public DatabaseMap(String name)
- {
- this.name = name;
+ if (database == null)
+ {
+ throw new NullPointerException("database must not be null");
+ }
+ this.database = database;
}
/**
@@ -171,38 +132,13 @@ public class DatabaseMap implements java
}
/**
- * Get the ID table for this database.
- *
- * @return A TableMap.
- */
- public TableMap getIdTable()
- {
- return idTable;
- }
-
- /**
- * Get the IDBroker for this database.
- *
- * @return An IDBroker.
- * @deprecated Will be removed in a future version of Torque.
- * Use DatabaseInfo#getIdBroker() instead
- * to access the IDBroker.
- */
- public IDBroker getIDBroker()
- {
- return idBroker;
- }
-
- /**
- * Get the name of this database.
+ * Get the associated database object.
*
- * @return A String.
- * @deprecated Will be removed in a future version of Torque.
- * Use the name of the corresponding database instead.
+ * @return the associated database, not null.
*/
- public String getName()
+ public Database getDatabase()
{
- return name;
+ return database;
}
/**
@@ -282,29 +218,6 @@ public class DatabaseMap implements java
}
/**
- * Set the ID table for this database.
- *
- * @param idTable The TableMap representation for the ID table.
- */
- public void setIdTable(TableMap idTable)
- {
- this.idTable = idTable;
- addTable(idTable);
- }
-
- /**
- * Set the ID table for this database.
- *
- * @param tableName The name for the ID table.
- */
- public void setIdTable(String tableName)
- {
- TableMap tmap = new TableMap(tableName, this);
- setIdTable(tmap);
- }
-
-
- /**
* Returns an unmodifiable map of all options.
*
* @return A map containing all options, not null.
@@ -338,58 +251,6 @@ public class DatabaseMap implements java
}
/**
- * Add a type of id generator for access by a TableMap.
- *
- * @param type a <code>String</code> value
- * @param idGen an <code>IdGenerator</code> value
- * @deprecated use DatabaseInfo.addGenerator() instead.
- * Will be removed in a future version of Torque.
- */
- public void addIdGenerator(String type, IdGenerator idGen)
- {
- idGenerators.put(type, idGen);
- }
-
- /**
- * Get a type of id generator. Valid values are listed in the
- * {@link org.apache.torque.adapter.IDMethod} interface.
- *
- * @param type a <code>String</code> value
- * @return an <code>IdGenerator</code> value
- * @deprecated use DatabaseInfo.getIdGenerator() instead.
- * Will be removed in a future version of Torque.
- */
- public IdGenerator getIdGenerator(String type)
- {
- return (IdGenerator) idGenerators.get(type);
- }
-
- /**
- * Creates the Idbroker for this DatabaseMap.
- * If an IDBroker already exists for the DatabaseMap, the method
- * does nothing.
- * @return true if a new IdBroker was created, false otherwise.
- * @deprecated Will be removed in a future version of Torque.
- * Use DatabaseInfo.startIdBroker() instead.
- */
- public synchronized boolean startIdBroker()
- {
- if (idBroker == null)
- {
- setIdTable("ID_TABLE");
- TableMap tMap = getIdTable();
- tMap.addPrimaryKey("ID_TABLE_ID", new Integer(0));
- tMap.addColumn("TABLE_NAME", "");
- tMap.addColumn("NEXT_ID", new Integer(0));
- tMap.addColumn("QUANTITY", new Integer(0));
- idBroker = new IDBroker(idTable);
- addIdGenerator(IDMethod.ID_BROKER, idBroker);
- return true;
- }
- return false;
- }
-
- /**
* Fully populate this DatabaseMap with all the TablesMaps. This
* is only needed if the application needs to use the complete OM
* mapping information. Otherwise, the OM Mapping information
@@ -433,7 +294,7 @@ public class DatabaseMap implements java
}
String initClassName = MessageFormat.format(INIT_CLASS_NAME_FORMAT,
new Object[] {
- javanameMethod(getName())
+ javanameMethod(getDatabase().getName())
});
Class<?> initClass = null;
@@ -446,7 +307,7 @@ public class DatabaseMap implements java
throw new TorqueException(MessageFormat.format(
ERROR_MESSAGES_INIT[0],
new Object[] {
- getName(),
+ getDatabase().getName(),
initClassName
}),
e);
@@ -456,7 +317,7 @@ public class DatabaseMap implements java
throw new TorqueException(MessageFormat.format(
ERROR_MESSAGES_INIT[1],
new Object[] {
- getName(), initClassName
+ getDatabase().getName(), initClassName
}),
e);
}
@@ -465,7 +326,7 @@ public class DatabaseMap implements java
throw new TorqueException(MessageFormat.format(
ERROR_MESSAGES_INIT[2],
new Object[] {
- getName(), initClassName
+ getDatabase().getName(), initClassName
}),
e);
}
@@ -479,7 +340,7 @@ public class DatabaseMap implements java
throw new TorqueException(MessageFormat.format(
ERROR_MESSAGES_INIT[3],
new Object[] {
- getName(), initClassName
+ getDatabase().getName(), initClassName
}),
e);
}
Modified:
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/map/TableMap.java
URL:
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/map/TableMap.java?rev=1103503&r1=1103502&r2=1103503&view=diff
==============================================================================
---
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/map/TableMap.java
(original)
+++
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/map/TableMap.java
Sun May 15 18:41:15 2011
@@ -30,7 +30,6 @@ import java.util.StringTokenizer;
import org.apache.commons.lang.StringUtils;
import org.apache.torque.TorqueException;
import org.apache.torque.adapter.IDMethod;
-import org.apache.torque.oid.IdGenerator;
/**
* TableMap is used to model a table in a database.
@@ -273,17 +272,6 @@ public class TableMap implements IDMetho
}
/**
- * Get the value of idGenerator.
- * @return value of idGenerator.
- * @deprecated use DatabaseInfo.getIdGenerator(getPrimaryKeyMethod())
- * instead. Will be removed in a future version of Torque.
- */
- public IdGenerator getIdGenerator()
- {
- return getDatabaseMap().getIdGenerator(primaryKeyMethod);
- }
-
- /**
* Get the information used to generate a primary key
*
* @return An Object.
@@ -587,7 +575,7 @@ public class TableMap implements IDMetho
}
if (ID_BROKER.equalsIgnoreCase(method))
{
- getDatabaseMap().startIdBroker();
+ getDatabaseMap().getDatabase().startIdBroker();
}
}
Modified:
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/oid/IDBroker.java
URL:
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/oid/IDBroker.java?rev=1103503&r1=1103502&r2=1103503&view=diff
==============================================================================
---
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/oid/IDBroker.java
(original)
+++
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/oid/IDBroker.java
Sun May 15 18:41:15 2011
@@ -203,7 +203,7 @@ public class IDBroker implements Runnabl
*/
public IDBroker(TableMap tMap)
{
- this(tMap.getDatabaseMap().getName());
+ this(tMap.getDatabaseMap().getDatabase().getName());
}
/**
Modified:
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/SqlBuilder.java
URL:
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/SqlBuilder.java?rev=1103503&r1=1103502&r2=1103503&view=diff
==============================================================================
---
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/SqlBuilder.java
(original)
+++
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/sql/SqlBuilder.java
Sun May 15 18:41:15 2011
@@ -398,7 +398,7 @@ public final class SqlBuilder
// the joins are processed
addTableToFromClause(getFullTableName(
tableName,
- databaseMap.getName()),
+ databaseMap.getDatabase().getName()),
criteria,
query);
}
Modified:
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/BasePeerImpl.java
URL:
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/BasePeerImpl.java?rev=1103503&r1=1103502&r2=1103503&view=diff
==============================================================================
---
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/BasePeerImpl.java
(original)
+++
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/BasePeerImpl.java
Sun May 15 18:41:15 2011
@@ -402,7 +402,7 @@ public class BasePeerImpl implements Ser
}
else
{
- databaseName = table.getDatabaseMap().getName();
+ databaseName = table.getDatabaseMap().getDatabase().getName();
}
}
Connection connection = null;
@@ -452,7 +452,7 @@ public class BasePeerImpl implements Ser
{
TableMap tableMap = insertValues.getTable();
DatabaseMap dbMap = tableMap.getDatabaseMap();
- Database database = Torque.getDatabase(dbMap.getName());
+ Database database = Torque.getDatabase(dbMap.getDatabase().getName());
Object keyInfo = tableMap.getPrimaryKeyMethodInfo();
IdGenerator keyGen
= database.getIdGenerator(tableMap.getPrimaryKeyMethod());
@@ -498,7 +498,7 @@ public class BasePeerImpl implements Ser
String fullTableName = SqlBuilder.getFullTableName(
tableMap.getName(),
- dbMap.getName());
+ dbMap.getDatabase().getName());
StringBuilder query = new StringBuilder("INSERT INTO ")
.append(fullTableName)
.append("(")
@@ -1109,7 +1109,7 @@ public class BasePeerImpl implements Ser
}
else
{
- databaseName = table.getDatabaseMap().getName();
+ databaseName = table.getDatabaseMap().getDatabase().getName();
}
}
Connection connection = null;
@@ -1209,7 +1209,7 @@ public class BasePeerImpl implements Ser
}
else
{
- databaseName = table.getDatabaseMap().getName();
+ databaseName = table.getDatabaseMap().getDatabase().getName();
}
}
Connection connection = null;
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]