db-torque-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tfisc...@apache.org
Subject svn commit: r1375888 - in /db/torque/torque4/trunk: torque-runtime/src/main/java/org/apache/torque/ torque-runtime/src/main/java/org/apache/torque/avalon/ torque-runtime/src/main/java/org/apache/torque/map/ torque-runtime/src/main/java/org/apache/torqu...
Date Wed, 22 Aug 2012 03:51:01 GMT
Author: tfischer
Date: Wed Aug 22 03:51:00 2012
New Revision: 1375888

URL: http://svn.apache.org/viewvc?rev=1375888&view=rev
Log:
TORQUE-220 fix serialisation issue in DatabaseMap
Fixed some corner cases in Torque initialisation

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/Torque.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/avalon/TorqueComponent.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/MapHelper.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/util/BasePeerImpl.java
    db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/BaseTestCase.java
    db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/sql/SqlBuilderOldCriteriaTest.java
    db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/sql/SqlBuilderTest.java
    db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/base/initDatabaseMap.vm
    db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/base/peerImplCreator.vm
    db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/base/staticInit.vm

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=1375888&r1=1375887&r2=1375888&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 Wed Aug 22 03:51:00 2012
@@ -75,17 +75,23 @@ public class Database
     /**
      * Creates a new Database with the given name.
      *
-     * @param aName the name of the database, not null.
+     * @param name the name of the database, not null.
+     *
+     * @throws NullPointerException if name is null.
      */
-    Database(String aName)
+    Database(String name)
     {
-        this.name = aName;
+        if (name == null)
+        {
+            throw new NullPointerException("name is null");
+        }
+        this.name = name;
     }
 
     /**
-     * returns the name of the database.
+     * Rturns the name of the database.
      *
-     * @return the name of the database. May be null.
+     * @return the name of the database, not null.
      */
     public String getName()
     {
@@ -197,15 +203,36 @@ public class Database
     }
 
     /**
-     * Creates the IDBroker for this Database and starts it.
+     * Creates the IDBroker for this Database and registers it with Torque.
+     * so it is either started instantly if Torque is already initialized
+     * or it is started when Torque is initialized.
      * The information about the IdTable is stored in the databaseMap.
-     * If an IDBroker already exists for the DatabaseMap, the method
+     * If an IDBroker already exists for this Database, the method
      * does nothing.
      *
      * @return true if a new IDBroker was created, false otherwise.
+     *
+     * @deprecated This method will be removed in a future version of Torque.
+     *             Please use createAndRegisterIdBroker() instead.
      */
+    @Deprecated
     public synchronized boolean startIdBroker()
     {
+        return createAndRegisterIdBroker();
+    }
+
+    /**
+     * Creates the IDBroker for this Database and registers it with Torque.
+     * so it is either started instantly if Torque is already initialized
+     * or it is started when Torque is initialized.
+     * The information about the IdTable is stored in the databaseMap.
+     * If an IDBroker already exists for this Database, the method
+     * does nothing.
+     *
+     * @return true if a new IDBroker was created, false otherwise.
+     */
+    public synchronized boolean createAndRegisterIdBroker()
+    {
         if (idBroker != null)
         {
             return false;

Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/Torque.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/Torque.java?rev=1375888&r1=1375887&r2=1375888&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/Torque.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/Torque.java Wed Aug 22 03:51:00 2012
@@ -206,7 +206,8 @@ public final class Torque
      *
      * @return a Manager
      */
-    public static <T extends AbstractBaseManager<? extends Persistent>> T getManager(String name,
+    public static <T extends AbstractBaseManager<? extends Persistent>> T getManager(
+            String name,
             String defaultClassName)
     {
         return getInstance().getManager(name, defaultClassName);
@@ -224,7 +225,7 @@ public final class Torque
      *            exceptions are logged but ignored.
      */
     public static void shutdown()
-        throws TorqueException
+            throws TorqueException
     {
         getInstance().shutdown();
     }
@@ -233,18 +234,17 @@ public final class Torque
      * Returns the default database map information.
      *
      * @return A DatabaseMap.
-     * @throws TorqueException Any exceptions caught during processing will be
-     *         rethrown wrapped into a TorqueException.
+     *
+     * @throws TorqueException if Torque is not initialized.
      */
     public static DatabaseMap getDatabaseMap()
-        throws TorqueException
+            throws TorqueException
     {
         return getInstance().getDatabaseMap();
     }
 
     /**
-     * Returns the database map information. Name relates to the name
-     * of the connection pool to associate with the map.
+     * Returns the database map information for a given database.
      *
      * @param name The name of the database corresponding to the
      *        <code>DatabaseMap</code> to retrieve, or null
@@ -252,9 +252,10 @@ public final class Torque
      *
      * @return The named <code>DatabaseMap</code>, not null.
      *
-     * @throws NullPointerException if name is null.
+     * @throws TorqueException if Torque is not initialized and name is null.
      */
     public static DatabaseMap getDatabaseMap(String name)
+            throws TorqueException
     {
         return getInstance().getDatabaseMap(name);
     }
@@ -384,11 +385,9 @@ public final class Torque
      * @param name The database name.
      * @param schema The current schema name.
      *
-     * @throws TorqueException Any exceptions caught during processing will be
-     *         rethrown wrapped into a TorqueException.
+     * @throws NullPointerException if databaseName is null.
      */
     public static void setSchema(String name, String schema)
-            throws TorqueException
     {
         getInstance().setSchema(name, schema);
     }
@@ -426,6 +425,23 @@ public final class Torque
     }
 
     /**
+     * Returns the database for the key <code>databaseName</code>.
+     * If no database is associated to the specified key,
+     * a new database is created, mapped to the specified key, and returned.
+     *
+     * @param name the key to get the database for, not null.
+     *
+     * @return the database associated with specified key, or the newly created
+     *         database, never null.
+     *
+     * @throws IllegalArgumentException if databaseName is null.
+     */
+    public static Database getOrCreateDatabase(String name)
+    {
+        return getInstance().getOrCreateDatabase(name);
+    }
+
+    /**
      * Returns a Map containing all Databases registered to Torque.
      * The key of the Map is the name of the database, and the value is the
      * database instance. <br/>

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=1375888&r1=1375887&r2=1375888&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 Wed Aug 22 03:51:00 2012
@@ -908,18 +908,21 @@ public class TorqueInstance
      *
      * @return the requested DatabaseMap, not null.
      *
-     * @throws TorqueException Any exceptions caught during processing will be
-     *         rethrown wrapped into a TorqueException.
+     * @throws TorqueException if Torque is not initialized.
      */
     public DatabaseMap getDatabaseMap()
             throws TorqueException
     {
+        String name = getDefaultDB();
+        if (name == null)
+        {
+            throw new TorqueException("Torque is not initialized");
+        }
         return getDatabaseMap(getDefaultDB());
     }
 
     /**
-     * Returns the database map information. Name relates to the name
-     * of the connection pool to associate with the map.
+     * Returns the database map information for the given database name.
      *
      * @param name The name of the database corresponding to the
      *        <code>DatabaseMap</code> to retrieve, or null
@@ -927,15 +930,19 @@ public class TorqueInstance
      *
      * @return The named <code>DatabaseMap</code>, not null.
      *
-     * @throws NullPointerException if name is null.
+     * @throws TorqueException if Torque is not initialized and name is null.
      */
     public DatabaseMap getDatabaseMap(String name)
+            throws TorqueException
     {
         if (name == null)
         {
+            if (!Torque.isInit())
+            {
+                throw new TorqueException("Torque is not initialized");
+            }
             name = getDefaultDB();
         }
-
         Database database = getOrCreateDatabase(name);
         return database.getDatabaseMap();
     }
@@ -980,7 +987,8 @@ public class TorqueInstance
      *
      * @return a database connection to the named database, never null.
      *
-     * @throws TorqueException If no DataSourceFactory is configured for the
+     * @throws TorqueException if Torque is not initialized,
+     *         if no DataSourceFactory is configured for the
      *         named database, the connection information is wrong, or the
      *         connection cannot be returned for any other reason.
      */
@@ -1018,6 +1026,11 @@ public class TorqueInstance
     public DataSourceFactory getDataSourceFactory(String name)
             throws TorqueException
     {
+        if (!isInit())
+        {
+            throw new TorqueException("Torque is not initialized");
+        }
+
         Database database = getDatabase(name);
 
         DataSourceFactory dsf = null;
@@ -1077,8 +1090,7 @@ public class TorqueInstance
      * @return The corresponding database adapter, or null if no database
      *         adapter is defined for the given database.
      *
-     * @throws TorqueException Any exceptions caught during processing will be
-     *         rethrown wrapped into a TorqueException.
+     * @throws TorqueException if Torque is not initialized.
      *
      * @deprecated use getAdapter(String) instead.
      *             This method will be removed in a future version of Torque.
@@ -1086,6 +1098,10 @@ public class TorqueInstance
     @Deprecated
     public Adapter getDB(String name) throws TorqueException
     {
+        if (!isInit())
+        {
+            throw new TorqueException("Torque is not initialized");
+        }
         Database database = getDatabase(name);
         if (database == null)
         {
@@ -1102,10 +1118,14 @@ public class TorqueInstance
      * @return The corresponding database adapter, or null if no database
      *         adapter is defined for the given database.
      *
-     * @throws TorqueException If Torque is not initialized.
+     * @throws TorqueException if Torque is not initialized.
      */
     public Adapter getAdapter(String name) throws TorqueException
     {
+        if (!isInit())
+        {
+            throw new TorqueException("Torque is not initialized");
+        }
         Database database = getDatabase(name);
         if (database == null)
         {
@@ -1149,14 +1169,12 @@ public class TorqueInstance
     /**
      * Sets the current schema for a database connection
      *
-     * @param name The database name.
+     * @param name The database name, not null.
      * @param schema The current schema name.
      *
-     * @throws TorqueException Any exceptions caught during processing will be
-     *         rethrown wrapped into a TorqueException.
+     * @throws NullPointerException if databaseName is null.
      */
     public void setSchema(String name, String schema)
-            throws TorqueException
     {
         getOrCreateDatabase(name).setSchema(schema);
     }
@@ -1166,14 +1184,18 @@ public class TorqueInstance
      *
      * @param name The database name.
      *
-     * @return The current schema name. Null means, no schema has been set.
+     * @return The current schema name. Null means, no schema has been set
+     *         or no database with the given name exists.
      *
-     * @throws TorqueException Any exceptions caught during processing will be
-     *         rethrown wrapped into a TorqueException.
+     * @throws TorqueException if Torque is not yet initialized.
      */
     public String getSchema(String name)
-        throws TorqueException
+            throws TorqueException
     {
+        if (!isInit())
+        {
+            throw new TorqueException("Torque is not initialized");
+        }
         Database database = getDatabase(name);
         if (database == null)
         {
@@ -1233,12 +1255,19 @@ public class TorqueInstance
      * If no database is associated to the specified key,
      * a new database is created, mapped to the specified key, and returned.
      *
-     * @param databaseName the key to get the database for.
+     * @param databaseName the key to get the database for, not null.
+     *
      * @return the database associated with specified key, or the newly created
      *         database, never null.
+     *
+     * @throws NullPointerException if databaseName is null.
      */
     public Database getOrCreateDatabase(String databaseName)
     {
+        if (databaseName == null)
+        {
+            throw new NullPointerException("databaseName is null");
+        }
         synchronized (databases)
         {
             Database result = databases.get(databaseName);

Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/avalon/TorqueComponent.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/avalon/TorqueComponent.java?rev=1375888&r1=1375887&r2=1375888&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/avalon/TorqueComponent.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/avalon/TorqueComponent.java Wed Aug 22 03:51:00 2012
@@ -189,7 +189,7 @@ public class TorqueComponent
                 Database database = getDatabase(otherDatabase.getName());
                 if (otherDatabase.getIdBroker() != null)
                 {
-                    database.startIdBroker();
+                    database.createAndRegisterIdBroker();
                 }
             }
         }

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=1375888&r1=1375887&r2=1375888&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 Wed Aug 22 03:51:00 2012
@@ -88,8 +88,11 @@ public class DatabaseMap implements Seri
         "Invalid Torque OM setup for Database \"{0}\".\n"
         + "An error occured invoking the init() method in class, \"{1}\"";
 
-    /** The database where this databaseMap belongs to. */
-    private final Database database;
+    /**
+     * The name of the database where this databaseMap belongs to,
+     * for internal purposes only.
+     */
+    private final String name;
 
     /** Name of the tables in the database. */
     private final Map<String, TableMap> tables
@@ -114,7 +117,17 @@ public class DatabaseMap implements Seri
         {
             throw new NullPointerException("database must not be null");
         }
-        this.database = database;
+        this.name = database.getName();
+    }
+
+    /**
+     * The name of the database to which this database map belongs.
+     *
+     * @return the database name, not null.
+     */
+    String getName()
+    {
+        return name;
     }
 
     /**
@@ -149,16 +162,6 @@ public class DatabaseMap implements Seri
     }
 
     /**
-     * Get the associated database object.
-     *
-     * @return the associated database, not null.
-     */
-    public Database getDatabase()
-    {
-        return database;
-    }
-
-    /**
      * Get a TableMap for the table by name. <p>
      *
      * Note that by default Torque uses lazy initialization to minimize
@@ -317,7 +320,7 @@ public class DatabaseMap implements Seri
         }
         String initClassName = MessageFormat.format(INIT_CLASS_NAME_FORMAT,
                 new Object[] {
-                    javanameMethod(getDatabase().getName())
+                    javanameMethod(name)
                 });
 
         Class<?> initClass = null;
@@ -330,8 +333,8 @@ public class DatabaseMap implements Seri
             throw new TorqueException(MessageFormat.format(
                     ERROR_MESSAGES_CLASS_NOT_FOUND,
                     new Object[] {
-                        getDatabase().getName(),
-                        initClassName
+                            name,
+                            initClassName
                     }),
                     e);
         }
@@ -340,7 +343,8 @@ public class DatabaseMap implements Seri
             throw new TorqueException(MessageFormat.format(
                     ERROR_MESSAGES_DEPENDENT_CLASS_NOT_FOUND,
                     new Object[] {
-                        getDatabase().getName(), initClassName
+                            name,
+                            initClassName
                     }),
                     e);
         }
@@ -349,7 +353,8 @@ public class DatabaseMap implements Seri
             throw new TorqueException(MessageFormat.format(
                     ERROR_MESSAGES_CLASS_FOR_NAME,
                     new Object[] {
-                        getDatabase().getName(), initClassName
+                            name,
+                            initClassName
                     }),
                     e);
         }
@@ -363,7 +368,8 @@ public class DatabaseMap implements Seri
             throw new TorqueException(MessageFormat.format(
                     ERROR_MESSAGES_INIT,
                     new Object[] {
-                        getDatabase().getName(), initClassName
+                            name,
+                            initClassName
                     }),
                     e);
         }
@@ -424,8 +430,8 @@ public class DatabaseMap implements Seri
     public String toString()
     {
         StringBuilder result = new StringBuilder();
-        result.append("DatabaseMap[database.name=")
-             .append(database.getName())
+        result.append("DatabaseMap[name=")
+             .append(name)
              .append(", tables=(");
         boolean first = true;
         for (TableMap table : tables.values())

Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/map/MapHelper.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/map/MapHelper.java?rev=1375888&r1=1375887&r2=1375888&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/map/MapHelper.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/map/MapHelper.java Wed Aug 22 03:51:00 2012
@@ -23,6 +23,7 @@ import java.util.Map;
 
 import org.apache.torque.Column;
 import org.apache.torque.Torque;
+import org.apache.torque.TorqueException;
 import org.apache.torque.criteria.CriteriaInterface;
 
 /**
@@ -52,11 +53,14 @@ public final class MapHelper
      *
      * @return the table map, or null if possibleColumn does not implement
      *         the column interface or if the table name cannot be resolved.
+     *
+     * @throws TorqueException possibly if Torque is not initialized.
      */
     public static TableMap getTableMap(
-            Object possibleColumn,
-            CriteriaInterface<?> criteria,
-            TableMap defaultTableMap)
+                Object possibleColumn,
+                CriteriaInterface<?> criteria,
+                TableMap defaultTableMap)
+            throws TorqueException
     {
         if (!(possibleColumn instanceof Column))
         {
@@ -111,11 +115,16 @@ public final class MapHelper
      * @param column the column to get the column map for.
      * @param criteria A criteria containing the database name and perhaps
      *        aliases for the column and table name, not null.
+     *
      * @return the column map, or null if the column name cannot be resolved.
+     *
+     * @throws TorqueException if Torque is not initialized and criteria's
+     *         databaseName is null.
      */
     public static ColumnMap getColumnMap(
-            Column column,
-            CriteriaInterface<?> criteria)
+                Column column,
+                CriteriaInterface<?> criteria)
+            throws TorqueException
     {
         String tableName = column.getTableName();
         {

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=1375888&r1=1375887&r2=1375888&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 Wed Aug 22 03:51:00 2012
@@ -32,6 +32,8 @@ import java.util.Set;
 import java.util.StringTokenizer;
 
 import org.apache.commons.lang.StringUtils;
+import org.apache.torque.Database;
+import org.apache.torque.Torque;
 import org.apache.torque.TorqueException;
 import org.apache.torque.adapter.IDMethod;
 
@@ -401,7 +403,9 @@ public class TableMap implements Seriali
         primaryKeyMethod = method;
         if (IDMethod.ID_BROKER == method)
         {
-            getDatabaseMap().getDatabase().startIdBroker();
+            Database database = Torque.getOrCreateDatabase(
+                    getDatabaseMap().getName());
+            database.createAndRegisterIdBroker();
         }
     }
 

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=1375888&r1=1375887&r2=1375888&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 Wed Aug 22 03:51:00 2012
@@ -745,7 +745,7 @@ public class BasePeerImpl<T> implements 
 
         String fullTableName = SqlBuilder.getFullTableName(
                 getTableMap().getName(),
-                getTableMap().getDatabaseMap().getDatabase().getName());
+                databaseNameFromInsertValues);
         StringBuilder query = new StringBuilder("INSERT INTO ")
             .append(fullTableName)
             .append("(")

Modified: db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/BaseTestCase.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/BaseTestCase.java?rev=1375888&r1=1375887&r2=1375888&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/BaseTestCase.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/BaseTestCase.java Wed Aug 22 03:51:00 2012
@@ -46,6 +46,9 @@ public abstract class BaseTestCase exten
     protected DatabaseMap databaseMap;
 
     /** A Reference to the postgresql (default) database. */
+    protected Database database;
+
+    /** A Reference to the postgresql (default) database. */
     protected Database databasePostgresql;
 
     /** A Reference to the mysql database. */
@@ -66,15 +69,17 @@ public abstract class BaseTestCase exten
      *
      * @throws Exception if initialisation fails.
      */
+    @Override
     public void setUp() throws Exception
     {
         Torque.setInstance(null);
         Torque.init(CONFIG_FILE);
 
-        databaseMap = Torque.getDatabaseMap("postgresql");
-        databasePostgresql = databaseMap.getDatabase();
-        databaseMysql = Torque.getDatabaseMap("mysql").getDatabase();
-        databaseOracle = Torque.getDatabaseMap("oracle").getDatabase();
+        databaseMap = Torque.getDatabaseMap(Torque.getDefaultDB());
+        database = Torque.getDatabase(Torque.getDefaultDB());
+        databasePostgresql = Torque.getDatabase("postgresql");
+        databaseMysql = Torque.getDatabase("mysql");
+        databaseOracle = Torque.getDatabase("oracle");
         tableMap = databaseMap.addTable("TABLE");
         {
             stringColumnMap = new ColumnMap("COLUMN1", tableMap);

Modified: db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/sql/SqlBuilderOldCriteriaTest.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/sql/SqlBuilderOldCriteriaTest.java?rev=1375888&r1=1375887&r2=1375888&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/sql/SqlBuilderOldCriteriaTest.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/sql/SqlBuilderOldCriteriaTest.java Wed Aug 22 03:51:00 2012
@@ -137,14 +137,21 @@ public class SqlBuilderOldCriteriaTest e
     {
         Criteria criteria = new Criteria();
         criteria.addAscendingOrderByColumn("table.column1");
-        Database database = databaseMap.getDatabase();
-        database.setSchema("schema1");
-        criteria.setDbName(database.getName());
-        Query query = SqlBuilder.buildQuery(criteria);
-        assertEquals(
-                "SELECT table.column1 FROM schema1.table "
-                    + "ORDER BY table.column1 ASC",
-                query.toString());
+        String oldSchema = database.getSchema();
+        try
+        {
+            database.setSchema("schema1");
+            criteria.setDbName(database.getName());
+            Query query = SqlBuilder.buildQuery(criteria);
+            assertEquals(
+                    "SELECT table.column1 FROM schema1.table "
+                        + "ORDER BY table.column1 ASC",
+                    query.toString());
+        }
+        finally
+        {
+            database.setSchema(oldSchema);
+        }
     }
 
     public void testOrderByAscendingIgnoreCaseString() throws Exception
@@ -267,13 +274,20 @@ public class SqlBuilderOldCriteriaTest e
         Criteria criteria = new Criteria();
         criteria.addAlias("alias", "table");
         criteria.addSelectColumn("alias.column1");
-        Database database = databaseMap.getDatabase();
-        database.setSchema("schema1");
-        criteria.setDbName(database.getName());
-        Query query = SqlBuilder.buildQuery(criteria);
-        assertEquals(
-                "SELECT alias.column1 FROM schema1.table alias",
-                query.toString());
+        String oldSchema = database.getSchema();
+        try
+        {
+            database.setSchema("schema1");
+            criteria.setDbName(database.getName());
+            Query query = SqlBuilder.buildQuery(criteria);
+            assertEquals(
+                    "SELECT alias.column1 FROM schema1.table alias",
+                    query.toString());
+        }
+        finally
+        {
+            database.setSchema(oldSchema);
+        }
     }
 
     public void testAliasWithIgnoreCaseUnknownColumnType() throws Exception
@@ -326,14 +340,21 @@ public class SqlBuilderOldCriteriaTest e
         criteria.addSelectColumn("alias.COLUMN1");
         criteria.add("alias.COLUMN1", "1");
         criteria.setIgnoreCase(true);
-        Database database = databaseMap.getDatabase();
-        database.setSchema("schema1");
-        criteria.setDbName(database.getName());
-        Query query = SqlBuilder.buildQuery(criteria);
-        assertEquals(
-                "SELECT alias.COLUMN1 FROM schema1.TABLE alias "
-                   + "WHERE UPPER(alias.COLUMN1)=UPPER(?)",
-                query.toString());
+        String oldSchema = database.getSchema();
+        try
+        {
+            database.setSchema("schema1");
+            criteria.setDbName(database.getName());
+            Query query = SqlBuilder.buildQuery(criteria);
+            assertEquals(
+                    "SELECT alias.COLUMN1 FROM schema1.TABLE alias "
+                       + "WHERE UPPER(alias.COLUMN1)=UPPER(?)",
+                    query.toString());
+        }
+        finally
+        {
+            database.setSchema(oldSchema);
+        }
     }
 
     public void testAliasWithIgnoreCaseIntegerColumnTypeAndDefaultSchema()
@@ -344,14 +365,21 @@ public class SqlBuilderOldCriteriaTest e
         criteria.addSelectColumn("alias.COLUMN4");
         criteria.add("alias.COLUMN4", "1");
         criteria.setIgnoreCase(true);
-        Database database = databaseMap.getDatabase();
-        database.setSchema("schema1");
-        criteria.setDbName(database.getName());
-        Query query = SqlBuilder.buildQuery(criteria);
-        assertEquals(
-                "SELECT alias.COLUMN4 FROM schema1.TABLE alias "
-                   + "WHERE alias.COLUMN4=?",
-                query.toString());
+        String oldSchema = database.getSchema();
+        try
+        {
+            database.setSchema("schema1");
+            criteria.setDbName(database.getName());
+            Query query = SqlBuilder.buildQuery(criteria);
+            assertEquals(
+                    "SELECT alias.COLUMN4 FROM schema1.TABLE alias "
+                       + "WHERE alias.COLUMN4=?",
+                    query.toString());
+        }
+        finally
+        {
+            database.setSchema(oldSchema);
+        }
     }
 
     public void testAsColumn() throws Exception
@@ -411,14 +439,21 @@ public class SqlBuilderOldCriteriaTest e
         criteria.addSelectColumn("columnAlias");
         criteria.add("columnAlias", "1");
         criteria.setIgnoreCase(true);
-        Database database = databaseMap.getDatabase();
-        database.setSchema("schema1");
-        criteria.setDbName(database.getName());
-        Query query = SqlBuilder.buildQuery(criteria);
-        assertEquals(
-                "SELECT TABLE.COLUMN1 AS columnAlias FROM schema1.TABLE"
-                    + " WHERE UPPER(columnAlias)=UPPER(?)",
-                query.toString());
+        String oldSchema = database.getSchema();
+        try
+        {
+            database.setSchema("schema1");
+            criteria.setDbName(database.getName());
+            Query query = SqlBuilder.buildQuery(criteria);
+            assertEquals(
+                    "SELECT TABLE.COLUMN1 AS columnAlias FROM schema1.TABLE"
+                        + " WHERE UPPER(columnAlias)=UPPER(?)",
+                    query.toString());
+        }
+        finally
+        {
+            database.setSchema(oldSchema);
+        }
     }
 
     public void testAsColumnWithIgnoreCaseIntegerColumnAndDefaultSchema()
@@ -429,14 +464,21 @@ public class SqlBuilderOldCriteriaTest e
         criteria.addSelectColumn("columnAlias");
         criteria.add("columnAlias", "1");
         criteria.setIgnoreCase(true);
-        Database database = databaseMap.getDatabase();
-        database.setSchema("schema1");
-        criteria.setDbName(database.getName());
-        Query query = SqlBuilder.buildQuery(criteria);
-        assertEquals(
-                "SELECT TABLE.COLUMN4 AS columnAlias FROM schema1.TABLE"
-                    + " WHERE columnAlias=?",
-                query.toString());
+        String oldSchema = database.getSchema();
+        try
+        {
+            database.setSchema("schema1");
+            criteria.setDbName(database.getName());
+            Query query = SqlBuilder.buildQuery(criteria);
+            assertEquals(
+                    "SELECT TABLE.COLUMN4 AS columnAlias FROM schema1.TABLE"
+                        + " WHERE columnAlias=?",
+                    query.toString());
+        }
+        finally
+        {
+            database.setSchema(oldSchema);
+        }
     }
 
     public void testInnerJoinImplicit()
@@ -611,15 +653,22 @@ public class SqlBuilderOldCriteriaTest e
         Criteria criteria = new Criteria();
         criteria.addSelectColumn("*");
         criteria.addJoin("table1.column", "table2.column");
-        Database database = databaseMap.getDatabase();
-        database.setSchema("schema1");
-        criteria.setDbName(database.getName());
-        Query query = SqlBuilder.buildQuery(criteria);
-        assertEquals(
-                "SELECT *"
-                    + " FROM schema1.table1, schema1.table2"
-                    + " WHERE table1.column=table2.column",
-                query.toString());
+        String oldSchema = database.getSchema();
+        try
+        {
+            database.setSchema("schema1");
+            criteria.setDbName(database.getName());
+            Query query = SqlBuilder.buildQuery(criteria);
+            assertEquals(
+                    "SELECT *"
+                        + " FROM schema1.table1, schema1.table2"
+                        + " WHERE table1.column=table2.column",
+                    query.toString());
+        }
+        finally
+        {
+            database.setSchema(oldSchema);
+        }
     }
 
     public void testInnerJoinImplicitWithAliasAndDefaultSchema()
@@ -629,14 +678,22 @@ public class SqlBuilderOldCriteriaTest e
         criteria.addSelectColumn("alias.column1");
         criteria.addAlias("alias", "table1");
         criteria.addJoin("alias.column", "table2.column");
-        Database database = databaseMap.getDatabase();
-        database.setSchema("schema1");
-        criteria.setDbName(database.getName());
-        Query query = SqlBuilder.buildQuery(criteria);
-        assertEquals(
-                "SELECT alias.column1 FROM schema1.table1 alias, schema1.table2"
-                    + " WHERE alias.column=table2.column",
-                query.toString());
+        String oldSchema = database.getSchema();
+        try
+        {
+            database.setSchema("schema1");
+            criteria.setDbName(database.getName());
+            Query query = SqlBuilder.buildQuery(criteria);
+            assertEquals(
+                    "SELECT alias.column1 "
+                        + "FROM schema1.table1 alias, schema1.table2"
+                        + " WHERE alias.column=table2.column",
+                    query.toString());
+        }
+        finally
+        {
+            database.setSchema(oldSchema);
+        }
     }
 
     public void testInnerJoinImplicitWithAliasAndSchema()

Modified: db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/sql/SqlBuilderTest.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/sql/SqlBuilderTest.java?rev=1375888&r1=1375887&r2=1375888&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/sql/SqlBuilderTest.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/test/java/org/apache/torque/sql/SqlBuilderTest.java Wed Aug 22 03:51:00 2012
@@ -506,14 +506,21 @@ public class SqlBuilderTest extends Base
     {
         Criteria criteria = new Criteria();
         criteria.addAscendingOrderByColumn(new ColumnImpl("table.column1"));
-        Database database = databaseMap.getDatabase();
-        database.setSchema("schema1");
-        criteria.setDbName(database.getName());
-        Query query = SqlBuilder.buildQuery(criteria);
-        assertEquals(
-                "SELECT table.column1 FROM schema1.table "
-                    + "ORDER BY table.column1 ASC",
-                query.toString());
+        String oldSchema = database.getSchema();
+        try
+        {
+            database.setSchema("schema1");
+            criteria.setDbName(database.getName());
+            Query query = SqlBuilder.buildQuery(criteria);
+            assertEquals(
+                    "SELECT table.column1 FROM schema1.table "
+                        + "ORDER BY table.column1 ASC",
+                    query.toString());
+        }
+        finally
+        {
+            database.setSchema(oldSchema);
+        }
     }
 
     public void testOrderByWithFunction() throws Exception
@@ -690,13 +697,20 @@ public class SqlBuilderTest extends Base
         Criteria criteria = new Criteria();
         criteria.addAlias("alias", "table");
         criteria.addSelectColumn(new ColumnImpl("alias.column1"));
-        Database database = databaseMap.getDatabase();
-        database.setSchema("schema1");
-        criteria.setDbName(database.getName());
-        Query query = SqlBuilder.buildQuery(criteria);
-        assertEquals(
-                "SELECT alias.column1 FROM schema1.table alias",
-                query.toString());
+        String oldSchema = database.getSchema();
+        try
+        {
+            database.setSchema("schema1");
+            criteria.setDbName(database.getName());
+            Query query = SqlBuilder.buildQuery(criteria);
+            assertEquals(
+                    "SELECT alias.column1 FROM schema1.table alias",
+                    query.toString());
+            }
+        finally
+        {
+            database.setSchema(oldSchema);
+        }
     }
 
     public void testAliasWithIgnoreCaseUnknownColumnType() throws Exception
@@ -793,14 +807,21 @@ public class SqlBuilderTest extends Base
         criteria.addSelectColumn(new ColumnImpl("alias.COLUMN1"));
         criteria.where(new ColumnImpl("alias.COLUMN1"), "1");
         criteria.setIgnoreCase(true);
-        Database database = databaseMap.getDatabase();
-        database.setSchema("schema1");
-        criteria.setDbName(database.getName());
-        Query query = SqlBuilder.buildQuery(criteria);
-        assertEquals(
-                "SELECT alias.COLUMN1 FROM schema1.TABLE alias "
-                   + "WHERE UPPER(alias.COLUMN1)=UPPER(?)",
-                query.toString());
+        String oldSchema = database.getSchema();
+        try
+        {
+            database.setSchema("schema1");
+            criteria.setDbName(database.getName());
+            Query query = SqlBuilder.buildQuery(criteria);
+            assertEquals(
+                    "SELECT alias.COLUMN1 FROM schema1.TABLE alias "
+                       + "WHERE UPPER(alias.COLUMN1)=UPPER(?)",
+                    query.toString());
+        }
+        finally
+        {
+            database.setSchema(oldSchema);
+        }
     }
 
     public void testAliasWithIgnoreCaseIntegerColumnTypeAndDefaultSchema()
@@ -811,14 +832,21 @@ public class SqlBuilderTest extends Base
         criteria.addSelectColumn(new ColumnImpl("alias.COLUMN4"));
         criteria.where(new ColumnImpl("alias.COLUMN4"), "1");
         criteria.setIgnoreCase(true);
-        Database database = databaseMap.getDatabase();
-        database.setSchema("schema1");
-        criteria.setDbName(database.getName());
-        Query query = SqlBuilder.buildQuery(criteria);
-        assertEquals(
-                "SELECT alias.COLUMN4 FROM schema1.TABLE alias "
-                   + "WHERE alias.COLUMN4=?",
-                query.toString());
+        String oldSchema = database.getSchema();
+        try
+        {
+            database.setSchema("schema1");
+            criteria.setDbName(database.getName());
+            Query query = SqlBuilder.buildQuery(criteria);
+            assertEquals(
+                    "SELECT alias.COLUMN4 FROM schema1.TABLE alias "
+                       + "WHERE alias.COLUMN4=?",
+                    query.toString());
+        }
+        finally
+        {
+            database.setSchema(oldSchema);
+        }
     }
 
     public void testAsColumn() throws Exception
@@ -878,14 +906,21 @@ public class SqlBuilderTest extends Base
         criteria.addSelectColumn(new ColumnImpl("columnAlias"));
         criteria.where(new ColumnImpl("columnAlias"), "1");
         criteria.setIgnoreCase(true);
-        Database database = databaseMap.getDatabase();
-        database.setSchema("schema1");
-        criteria.setDbName(database.getName());
-        Query query = SqlBuilder.buildQuery(criteria);
-        assertEquals(
-                "SELECT TABLE.COLUMN1 AS columnAlias FROM schema1.TABLE"
-                    + " WHERE UPPER(columnAlias)=UPPER(?)",
-                query.toString());
+        String oldSchema = database.getSchema();
+        try
+        {
+            database.setSchema("schema1");
+            criteria.setDbName(database.getName());
+            Query query = SqlBuilder.buildQuery(criteria);
+            assertEquals(
+                    "SELECT TABLE.COLUMN1 AS columnAlias FROM schema1.TABLE"
+                        + " WHERE UPPER(columnAlias)=UPPER(?)",
+                    query.toString());
+        }
+        finally
+        {
+            database.setSchema(oldSchema);
+        }
     }
 
     public void testAsColumnWithIgnoreCaseIntegerColumnAndDefaultSchema()
@@ -896,14 +931,21 @@ public class SqlBuilderTest extends Base
         criteria.addSelectColumn(new ColumnImpl("columnAlias"));
         criteria.where(new ColumnImpl("columnAlias"), "1");
         criteria.setIgnoreCase(true);
-        Database database = databaseMap.getDatabase();
-        database.setSchema("schema1");
-        criteria.setDbName(database.getName());
-        Query query = SqlBuilder.buildQuery(criteria);
-        assertEquals(
-                "SELECT TABLE.COLUMN4 AS columnAlias FROM schema1.TABLE"
-                    + " WHERE columnAlias=?",
-                query.toString());
+        String oldSchema = database.getSchema();
+        try
+        {
+            database.setSchema("schema1");
+            criteria.setDbName(database.getName());
+            Query query = SqlBuilder.buildQuery(criteria);
+            assertEquals(
+                    "SELECT TABLE.COLUMN4 AS columnAlias FROM schema1.TABLE"
+                        + " WHERE columnAlias=?",
+                    query.toString());
+        }
+        finally
+        {
+            database.setSchema(oldSchema);
+        }
     }
 
     public void testInnerJoinImplicit()
@@ -1203,15 +1245,22 @@ public class SqlBuilderTest extends Base
         criteria.addJoin(
                 new ColumnImpl("table1.column"),
                 new ColumnImpl("table2.column"));
-        Database database = databaseMap.getDatabase();
-        database.setSchema("schema1");
-        criteria.setDbName(database.getName());
-        Query query = SqlBuilder.buildQuery(criteria);
-        assertEquals(
-                "SELECT *"
-                    + " FROM schema1.table1, schema1.table2"
-                    + " WHERE table1.column=table2.column",
-                query.toString());
+        String oldSchema = database.getSchema();
+        try
+        {
+            database.setSchema("schema1");
+            criteria.setDbName(database.getName());
+            Query query = SqlBuilder.buildQuery(criteria);
+            assertEquals(
+                    "SELECT *"
+                        + " FROM schema1.table1, schema1.table2"
+                        + " WHERE table1.column=table2.column",
+                    query.toString());
+        }
+        finally
+        {
+            database.setSchema(oldSchema);
+        }
     }
 
     public void testInnerJoinImplicitWithAliasAndDefaultSchema()
@@ -1223,14 +1272,21 @@ public class SqlBuilderTest extends Base
         criteria.addJoin(
                 new ColumnImpl("alias.column"),
                 new ColumnImpl("table2.column"));
-        Database database = databaseMap.getDatabase();
-        database.setSchema("schema1");
-        criteria.setDbName(database.getName());
-        Query query = SqlBuilder.buildQuery(criteria);
-        assertEquals(
-                "SELECT alias.column1 FROM schema1.table1 alias, schema1.table2"
-                    + " WHERE alias.column=table2.column",
-                query.toString());
+        String oldSchema = database.getSchema();
+        try
+        {
+            database.setSchema("schema1");
+            criteria.setDbName(database.getName());
+            Query query = SqlBuilder.buildQuery(criteria);
+            assertEquals(
+                    "SELECT alias.column1 FROM schema1.table1 alias, schema1.table2"
+                        + " WHERE alias.column=table2.column",
+                    query.toString());
+        }
+        finally
+        {
+            database.setSchema(oldSchema);
+        }
     }
 
     public void testInnerJoinImplicitWithAliasAndSchema()
@@ -1454,12 +1510,12 @@ public class SqlBuilderTest extends Base
         assertEquals(0, query.getPreparedStatementReplacements().size());
     }
 
-    public void testCriterionCustom() throws Exception
+    public void testCriterionCustomSql() throws Exception
     {
         Criteria criteria = new Criteria();
         criteria.addSelectColumn(new ColumnImpl("table.column"));
         Criterion criterion
-                = new Criterion("A", "A = functionOf(B)", SqlEnum.CUSTOM);
+                = new Criterion("A", null, null, "A = functionOf(B)", null);
         criteria.where(criterion);
         Query query = SqlBuilder.buildQuery(criteria);
         assertEquals("SELECT table.column FROM table WHERE A = functionOf(B)",

Modified: db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/base/initDatabaseMap.vm
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/base/initDatabaseMap.vm?rev=1375888&r1=1375887&r2=1375888&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/base/initDatabaseMap.vm (original)
+++ db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/base/initDatabaseMap.vm Wed Aug 22 03:51:00 2012
@@ -39,7 +39,8 @@
 ## TODO the following needs not be executed for every table
 ## but it causes no harm
 #if (${databaseOptionElements.size()} > 0)
-        DatabaseMap databaseMap = Torque.getDatabaseMap("$databaseName");
+        DatabaseMap databaseMap = Torque.getOrCreateDatabase("$databaseName")
+                .getDatabaseMap();
 #end
 #foreach($databaseOptionElement in $databaseOptionElements)
   #set ( $key = $databaseOptionElement.getAttribute("key") )

Modified: db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/base/peerImplCreator.vm
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/base/peerImplCreator.vm?rev=1375888&r1=1375887&r2=1375888&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/base/peerImplCreator.vm (original)
+++ db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/base/peerImplCreator.vm Wed Aug 22 03:51:00 2012
@@ -34,9 +34,7 @@
     {
         ${peerImplClassName} instance = new ${peerImplClassName}();
         instance.setRecordMapper(new $recordMapperClassName());
-        instance.setTableMap(
-            Torque.getDatabaseMap(${peerClassName}.DATABASE_NAME)
-                 .getTable(${peerClassName}.TABLE_NAME));
+        instance.setTableMap(${peerClassName}.TABLE);
         instance.setDatabaseName(${peerClassName}.DATABASE_NAME);
         
         return instance;

Modified: db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/base/staticInit.vm
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/base/staticInit.vm?rev=1375888&r1=1375887&r2=1375888&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/base/staticInit.vm (original)
+++ db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/base/staticInit.vm Wed Aug 22 03:51:00 2012
@@ -31,7 +31,8 @@
 #set ( $databaseName = $databaseElement.getAttribute("name") )
     static
     {
-        DatabaseMap dbMap = Torque.getDatabaseMap("$databaseName");
+        DatabaseMap dbMap = Torque.getOrCreateDatabase("$databaseName")
+                .getDatabaseMap();
         if (dbMap.getTable("$name") == null)
         {
             dbMap.addTable("$name");



---------------------------------------------------------------------
To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org
For additional commands, e-mail: torque-dev-help@db.apache.org


Mime
View raw message