db-torque-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From henn...@apache.org
Subject cvs commit: db-torque/xdocs schema-howto.xml changes.xml navigation.xml
Date Mon, 13 Dec 2004 17:08:28 GMT
henning     2004/12/13 09:08:28

  Modified:    src/java/org/apache/torque Tag: TORQUE_3_1_BRANCH
                        Torque.java TorqueInstance.java
               src/java/org/apache/torque/dsfactory Tag: TORQUE_3_1_BRANCH
                        AbstractDataSourceFactory.java
                        DataSourceFactory.java JndiDataSourceFactory.java
                        PerUserPoolDataSourceFactory.java
                        SharedPoolDataSourceFactory.java
                        TorqueDataSourceFactory.java
               src/java/org/apache/torque/oid Tag: TORQUE_3_1_BRANCH
                        AutoIncrementIdGenerator.java
                        IDGeneratorFactory.java SequenceIdGenerator.java
               src/java/org/apache/torque/util Tag: TORQUE_3_1_BRANCH
                        BasePeer.java SQLBuilder.java
               src/test Tag: TORQUE_3_1_BRANCH TurbineResources.properties
               xdocs    Tag: TORQUE_3_1_BRANCH changes.xml navigation.xml
  Added:       xdocs    Tag: TORQUE_3_1_BRANCH schema-howto.xml
  Log:
  Add database schema support for Peers. No support yet for SQL
  generation.
  
  See schema-howto.xml for a short explanation. Passes all Unit tests
  and the runtime tests with PostgreSQL.
  
  Revision  Changes    Path
  No                   revision
  No                   revision
  1.91.2.4  +29 -1     db-torque/src/java/org/apache/torque/Torque.java
  
  Index: Torque.java
  ===================================================================
  RCS file: /home/cvs/db-torque/src/java/org/apache/torque/Torque.java,v
  retrieving revision 1.91.2.3
  retrieving revision 1.91.2.4
  diff -u -r1.91.2.3 -r1.91.2.4
  --- Torque.java	1 Nov 2004 15:12:39 -0000	1.91.2.3
  +++ Torque.java	13 Dec 2004 17:08:27 -0000	1.91.2.4
  @@ -299,4 +299,32 @@
       {
           getInstance().closeConnection(con);
       }
  +
  +    /**
  +     * Sets the current schema for a database connection
  +     *
  +     * @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.
  +     */
  +    public static void setSchema(String name, String schema)
  +            throws TorqueException
  +    {
  +        getInstance().setSchema(name, schema);
  +    }
  +
  +    /**
  +     * This method returns the current schema for a database connection
  +     *
  +     * @param name The database name.
  +     * @return The current schema name. Null means, no schema has been set.
  +     * @throws TorqueException Any exceptions caught during processing will be
  +     *         rethrown wrapped into a TorqueException.
  +     */
  +    public static String getSchema(String name)
  +        throws TorqueException
  +    {
  +        return getInstance().getSchema(name);
  +    }
   }
  
  
  
  1.5.2.6   +66 -30    db-torque/src/java/org/apache/torque/TorqueInstance.java
  
  Index: TorqueInstance.java
  ===================================================================
  RCS file: /home/cvs/db-torque/src/java/org/apache/torque/TorqueInstance.java,v
  retrieving revision 1.5.2.5
  retrieving revision 1.5.2.6
  diff -u -r1.5.2.5 -r1.5.2.6
  --- TorqueInstance.java	23 Aug 2004 02:54:16 -0000	1.5.2.5
  +++ TorqueInstance.java	13 Dec 2004 17:08:27 -0000	1.5.2.6
  @@ -611,7 +611,7 @@
                    i++)
               {
                   map.addIdGenerator(IDGeneratorFactory.ID_GENERATOR_METHODS[i],
  -                        IDGeneratorFactory.create(db));
  +                        IDGeneratorFactory.create(db, name));
               }
           }
           catch (java.lang.InstantiationException e)
  @@ -699,25 +699,45 @@
       {
           Connection con = null;
           DataSourceFactory dsf = null;
  +
  +        try
  +        {
  +            return getDataSourceFactory(name).getDataSource().getConnection();
  +        }
  +        catch(SQLException se)
  +        {
  +            throw new TorqueException(se);
  +        }
  +    }
  +
  +    /**
  +     * Returns a DataSourceFactory 
  +     *
  +     * @param name Name of the DSF to get
  +     * @return A DataSourceFactory object
  +     */
  +    protected DataSourceFactory getDataSourceFactory(String name)
  +            throws TorqueException
  +    {
  +        DataSourceFactory dsf = null;
  +
           try
           {
               dsf = (DataSourceFactory) dsFactoryMap.get(name);
  -            con = dsf.getDataSource().getConnection();
           }
           catch (Exception e)
           {
  -            if (dsf == null && e instanceof NullPointerException)
  -            {
  -                throw new NullPointerException(
  -                        "There was no DataSourceFactory "
  -                        + "configured for the connection " + name);
  -            }
  -            else
  -            {
  -                throw new TorqueException(e);
  -            }
  +            throw new TorqueException(e);
           }
  -        return con;
  +            
  +        if (dsf == null)
  +        {
  +            throw new NullPointerException(
  +                    "There was no DataSourceFactory "
  +                    + "configured for the connection " + name);
  +        }
  +
  +        return dsf;
       }
   
       /**
  @@ -736,27 +756,14 @@
               String password)
               throws TorqueException
       {
  -        Connection con = null;
  -        DataSourceFactory dsf = null;
           try
           {
  -            dsf = (DataSourceFactory) dsFactoryMap.get(name);
  -            con = dsf.getDataSource().getConnection(username, password);
  +            return getDataSourceFactory(name).getDataSource().getConnection(username, password);
           }
  -        catch (Exception e)
  +        catch(SQLException se)
           {
  -            if (dsf == null && e instanceof NullPointerException)
  -            {
  -                throw new NullPointerException(
  -                        "There was no DataSourceFactory "
  -                        + "configured for the connection " + name);
  -            }
  -            else
  -            {
  -                throw new TorqueException(e);
  -            }
  +            throw new TorqueException(se);
           }
  -        return con;
       }
   
       /**
  @@ -815,4 +822,33 @@
               }
           }
       }
  +
  +    /**
  +     * Sets the current schema for a database connection
  +     *
  +     * @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.
  +     */
  +    public void setSchema(String name, String schema)
  +            throws TorqueException
  +    {
  +        getDataSourceFactory(name).setSchema(schema);
  +    }
  +
  +    /**
  +     * This method returns the current schema for a database connection
  +     *
  +     * @param name The database name.
  +     * @return The current schema name. Null means, no schema has been set.
  +     * @throws TorqueException Any exceptions caught during processing will be
  +     *         rethrown wrapped into a TorqueException.
  +     */
  +    public String getSchema(String name)
  +        throws TorqueException
  +    {
  +        return getDataSourceFactory(name).getSchema();
  +    }
  +
   }
  
  
  
  No                   revision
  No                   revision
  1.12.2.3  +71 -1     db-torque/src/java/org/apache/torque/dsfactory/AbstractDataSourceFactory.java
  
  Index: AbstractDataSourceFactory.java
  ===================================================================
  RCS file: /home/cvs/db-torque/src/java/org/apache/torque/dsfactory/AbstractDataSourceFactory.java,v
  retrieving revision 1.12.2.2
  retrieving revision 1.12.2.3
  diff -u -r1.12.2.2 -r1.12.2.3
  --- AbstractDataSourceFactory.java	20 May 2004 04:35:14 -0000	1.12.2.2
  +++ AbstractDataSourceFactory.java	13 Dec 2004 17:08:27 -0000	1.12.2.3
  @@ -19,6 +19,7 @@
   import java.util.Iterator;
   
   import javax.sql.ConnectionPoolDataSource;
  +import javax.sql.DataSource;
   
   import org.apache.commons.beanutils.ConvertUtils;
   import org.apache.commons.beanutils.MappedPropertyDescriptor;
  @@ -27,6 +28,8 @@
   import org.apache.commons.configuration.Configuration;
   import org.apache.commons.dbcp.cpdsadapter.DriverAdapterCPDS;
   
  +import org.apache.commons.lang.StringUtils;
  +
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
   
  @@ -42,6 +45,7 @@
    * @version $Id$
    */
   public abstract class AbstractDataSourceFactory
  +        implements DataSourceFactory
   {
       /** "pool" Key for the configuration */
       public static final String POOL_KEY = "pool";
  @@ -49,15 +53,25 @@
       /** "connection" Key for the configuration */
       public static final String CONNECTION_KEY = "connection";
   
  +    /** "schema" Key for the configuration */
  +    public static final String SCHEMA_KEY = "schema";
  +
       /** "default.pool" Key for the configuration */
       public static final String DEFAULT_POOL_KEY = "defaults.pool";
   
       /** "default.connection" Key for the configuration */
       public static final String DEFAULT_CONNECTION_KEY = "defaults.connection";
   
  +    /** "default schema name" for the configuration */
  +    public static final String DEFAULT_SCHEMA_KEY = "defaults.schema";
  +
  +
       /** The log */
       private static Log log = LogFactory.getLog(AbstractDataSourceFactory.class);
   
  +    /** Internal Marker for the Schema name of this database connection */
  +    private String schema = null;
  +
       /**
        * Encapsulates setting configuration properties on
        * <code>DataSource</code> objects.
  @@ -195,5 +209,61 @@
           applyConfiguration(conf, cpds);
           
           return cpds;
  +    }
  +
  +    /**
  +     * Sets the current schema for the database connection
  +     *
  +     * @param schema The current schema name
  +     */
  +    public void setSchema(String schema)
  +    {
  +        this.schema = schema;
  +    }
  +
  +    /**
  +     * This method returns the current schema for the database connection
  +     *
  +     * @return The current schema name. Null means, no schema has been set.
  +     * @throws TorqueException Any exceptions caught during processing will be
  +     *         rethrown wrapped into a TorqueException.
  +     */
  +    public String getSchema()
  +    {
  +        return schema;
  +    }
  +
  +    /**
  +     * @return the <code>DataSource</code> configured by the factory.
  +     * @throws TorqueException if the source can't be returned
  +     */
  +    public abstract DataSource getDataSource()
  +            throws TorqueException;
  +
  +    /**
  +     * Initialize the factory.
  +     *
  +     * @param configuration where to load the factory settings from
  +     * @throws TorqueException Any exceptions caught during processing will be
  +     *         rethrown wrapped into a TorqueException.
  +     */
  +    public void initialize(Configuration configuration) 
  +        throws TorqueException
  +    {
  +        if (configuration == null)
  +        {
  +            throw new TorqueException(
  +                "Torque cannot be initialized without "
  +                    + "a valid configuration. Please check the log files "
  +                    + "for further details.");
  +        }
  +
  +        schema = configuration.getString(SCHEMA_KEY, null);
  +
  +        if (StringUtils.isEmpty(schema))
  +        {
  +            Configuration conf = Torque.getConfiguration();
  +            schema = conf.getString(DEFAULT_SCHEMA_KEY, null);
  +        }
       }
   }
  
  
  
  1.2.4.3   +18 -1     db-torque/src/java/org/apache/torque/dsfactory/DataSourceFactory.java
  
  Index: DataSourceFactory.java
  ===================================================================
  RCS file: /home/cvs/db-torque/src/java/org/apache/torque/dsfactory/DataSourceFactory.java,v
  retrieving revision 1.2.4.2
  retrieving revision 1.2.4.3
  diff -u -r1.2.4.2 -r1.2.4.3
  --- DataSourceFactory.java	20 May 2004 04:35:14 -0000	1.2.4.2
  +++ DataSourceFactory.java	13 Dec 2004 17:08:27 -0000	1.2.4.3
  @@ -44,4 +44,21 @@
        */
       void initialize(Configuration configuration) 
           throws TorqueException;
  +
  +    /**
  +     * Sets the current schema for the database connection
  +     *
  +     * @param schema The current schema name
  +     */
  +    void setSchema(String schema);
  +
  +    /**
  +     * This method returns the current schema for the database connection
  +     *
  +     * @return The current schema name. Null means, no schema has been set.
  +     * @throws TorqueException Any exceptions caught during processing will be
  +     *         rethrown wrapped into a TorqueException.
  +     */
  +    String getSchema();
  +
   }
  
  
  
  1.6.2.5   +3 -8      db-torque/src/java/org/apache/torque/dsfactory/JndiDataSourceFactory.java
  
  Index: JndiDataSourceFactory.java
  ===================================================================
  RCS file: /home/cvs/db-torque/src/java/org/apache/torque/dsfactory/JndiDataSourceFactory.java,v
  retrieving revision 1.6.2.4
  retrieving revision 1.6.2.5
  diff -u -r1.6.2.4 -r1.6.2.5
  --- JndiDataSourceFactory.java	1 Dec 2004 15:29:10 -0000	1.6.2.4
  +++ JndiDataSourceFactory.java	13 Dec 2004 17:08:27 -0000	1.6.2.5
  @@ -96,13 +96,8 @@
        */
       public void initialize(Configuration configuration) throws TorqueException
       {
  -        if (configuration == null)
  -        {
  -            throw new TorqueException(
  -                "Torque cannot be initialized without "
  -                    + "a valid configuration. Please check the log files "
  -                    + "for further details.");
  -        }
  +        super.initialize(configuration);
  +
           initJNDI(configuration);
           initDataSource(configuration);
       }
  
  
  
  1.1.2.3   +2 -7      db-torque/src/java/org/apache/torque/dsfactory/PerUserPoolDataSourceFactory.java
  
  Index: PerUserPoolDataSourceFactory.java
  ===================================================================
  RCS file: /home/cvs/db-torque/src/java/org/apache/torque/dsfactory/PerUserPoolDataSourceFactory.java,v
  retrieving revision 1.1.2.2
  retrieving revision 1.1.2.3
  diff -u -r1.1.2.2 -r1.1.2.3
  --- PerUserPoolDataSourceFactory.java	20 May 2004 04:35:14 -0000	1.1.2.2
  +++ PerUserPoolDataSourceFactory.java	13 Dec 2004 17:08:27 -0000	1.1.2.3
  @@ -61,12 +61,7 @@
        */
       public void initialize(Configuration configuration) throws TorqueException
       {
  -        if (configuration == null)
  -        {
  -            throw new TorqueException(
  -                "Torque cannot be initialized without a valid configuration. "
  -                + "Please check the log files for further details.");
  -        }
  +        super.initialize(configuration);
   
           ConnectionPoolDataSource cpds = initCPDS(configuration);
           PerUserPoolDataSource ds = initJdbc2Pool(configuration);
  
  
  
  1.1.2.3   +2 -7      db-torque/src/java/org/apache/torque/dsfactory/SharedPoolDataSourceFactory.java
  
  Index: SharedPoolDataSourceFactory.java
  ===================================================================
  RCS file: /home/cvs/db-torque/src/java/org/apache/torque/dsfactory/SharedPoolDataSourceFactory.java,v
  retrieving revision 1.1.2.2
  retrieving revision 1.1.2.3
  diff -u -r1.1.2.2 -r1.1.2.3
  --- SharedPoolDataSourceFactory.java	20 May 2004 04:35:14 -0000	1.1.2.2
  +++ SharedPoolDataSourceFactory.java	13 Dec 2004 17:08:27 -0000	1.1.2.3
  @@ -61,12 +61,7 @@
        */
       public void initialize(Configuration configuration) throws TorqueException
       {
  -        if (configuration == null)
  -        {
  -            throw new TorqueException(
  -                "Torque cannot be initialized without a valid configuration. "
  -                + "Please check the log files for further details.");
  -        }
  +        super.initialize(configuration);
   
           ConnectionPoolDataSource cpds = initCPDS(configuration);
           SharedPoolDataSource ds = initJdbc2Pool(configuration);
  
  
  
  1.13.2.4  +3 -8      db-torque/src/java/org/apache/torque/dsfactory/Attic/TorqueDataSourceFactory.java
  
  Index: TorqueDataSourceFactory.java
  ===================================================================
  RCS file: /home/cvs/db-torque/src/java/org/apache/torque/dsfactory/Attic/TorqueDataSourceFactory.java,v
  retrieving revision 1.13.2.3
  retrieving revision 1.13.2.4
  diff -u -r1.13.2.3 -r1.13.2.4
  --- TorqueDataSourceFactory.java	20 May 2004 04:35:14 -0000	1.13.2.3
  +++ TorqueDataSourceFactory.java	13 Dec 2004 17:08:27 -0000	1.13.2.4
  @@ -60,13 +60,8 @@
        */
       public void initialize(Configuration configuration) throws TorqueException
       {
  -        if (configuration == null)
  -        {
  -            throw new TorqueException(
  -                "Torque cannot be initialized without "
  -                    + "a valid configuration. Please check the log files "
  -                    + "for further details.");
  -        }
  +        super.initialize(configuration);
  +
           ConnectionPoolDataSource cpds = initCPDS(configuration);
           TorqueClassicDataSource tcds = initTorqueClassic(configuration);
           tcds.setConnectionPoolDataSource(cpds);
  
  
  
  No                   revision
  No                   revision
  1.5.4.3   +12 -5     db-torque/src/java/org/apache/torque/oid/AutoIncrementIdGenerator.java
  
  Index: AutoIncrementIdGenerator.java
  ===================================================================
  RCS file: /home/cvs/db-torque/src/java/org/apache/torque/oid/AutoIncrementIdGenerator.java,v
  retrieving revision 1.5.4.2
  retrieving revision 1.5.4.3
  diff -u -r1.5.4.2 -r1.5.4.3
  --- AutoIncrementIdGenerator.java	20 May 2004 04:36:07 -0000	1.5.4.2
  +++ AutoIncrementIdGenerator.java	13 Dec 2004 17:08:27 -0000	1.5.4.3
  @@ -19,6 +19,7 @@
   import java.math.BigDecimal;
   import java.sql.Connection;
   import org.apache.torque.adapter.DB;
  +import org.apache.torque.util.SQLBuilder;
   import com.workingdogs.village.QueryDataSet;
   import com.workingdogs.village.Record;
   import com.workingdogs.village.Value;
  @@ -36,14 +37,19 @@
       /** the adapter that knows the correct sql syntax */
       private DB dbAdapter;
   
  +    /** The internal name of the Database that this Generator is connected to */
  +    private String name = null;
  +
       /**
        * Creates an IdGenerator which will work with the specified database.
        *
  -     * @param adapter the adapter that knows the correct sql syntax.
  +     * @param dbAdapter the adapter that knows the correct sql syntax.
  +     * @param name The name of the datasource to find the correct schema
        */
  -    public AutoIncrementIdGenerator(DB adapter)
  +    public AutoIncrementIdGenerator(final DB dbAdapter, final String name)
       {
  -        dbAdapter = adapter;
  +        this.dbAdapter = dbAdapter;
  +        this.name = name;
       }
   
       /**
  @@ -148,7 +154,8 @@
       private Value getIdAsVillageValue(Connection connection, Object keyInfo)
           throws Exception
       {
  -        String idSQL = dbAdapter.getIDMethodSQL(keyInfo);
  +        String tableName = SQLBuilder.getFullTableName(String.valueOf(keyInfo), name);
  +        String idSQL = dbAdapter.getIDMethodSQL(tableName);
           Value id = null;
           QueryDataSet qds = null;
           try
  
  
  
  1.2.4.3   +4 -4      db-torque/src/java/org/apache/torque/oid/IDGeneratorFactory.java
  
  Index: IDGeneratorFactory.java
  ===================================================================
  RCS file: /home/cvs/db-torque/src/java/org/apache/torque/oid/IDGeneratorFactory.java,v
  retrieving revision 1.2.4.2
  retrieving revision 1.2.4.3
  diff -u -r1.2.4.2 -r1.2.4.3
  --- IDGeneratorFactory.java	20 May 2004 04:36:07 -0000	1.2.4.2
  +++ IDGeneratorFactory.java	13 Dec 2004 17:08:27 -0000	1.2.4.3
  @@ -47,16 +47,16 @@
        * @param dbAdapter The type of adapter to create an ID generator for.
        * @return The appropriate ID generator (possibly <code>null</code>).
        */
  -    public static IdGenerator create(DB dbAdapter)
  +    public static IdGenerator create(DB dbAdapter, String name)
       {
           String idMethod = dbAdapter.getIDMethodType();
           if (IDMethod.AUTO_INCREMENT.equals(idMethod))
           {
  -            return new AutoIncrementIdGenerator(dbAdapter);
  +            return new AutoIncrementIdGenerator(dbAdapter, name);
           }
           else if (IDMethod.SEQUENCE.equals(idMethod))
           {
  -            return new SequenceIdGenerator(dbAdapter);
  +            return new SequenceIdGenerator(dbAdapter, name);
           }
           else
           {
  
  
  
  1.10.2.3  +11 -4     db-torque/src/java/org/apache/torque/oid/SequenceIdGenerator.java
  
  Index: SequenceIdGenerator.java
  ===================================================================
  RCS file: /home/cvs/db-torque/src/java/org/apache/torque/oid/SequenceIdGenerator.java,v
  retrieving revision 1.10.2.2
  retrieving revision 1.10.2.3
  diff -u -r1.10.2.2 -r1.10.2.3
  --- SequenceIdGenerator.java	20 May 2004 04:36:07 -0000	1.10.2.2
  +++ SequenceIdGenerator.java	13 Dec 2004 17:08:27 -0000	1.10.2.3
  @@ -23,6 +23,7 @@
   import org.apache.commons.logging.LogFactory;
   
   import org.apache.torque.adapter.DB;
  +import org.apache.torque.util.SQLBuilder;
   
   import com.workingdogs.village.QueryDataSet;
   import com.workingdogs.village.Record;
  @@ -43,14 +44,19 @@
       /** the adapter that knows the correct sql syntax */
       private DB dbAdapter;
   
  +    /** The internal name of the Database that this Generator is connected to */
  +    private String name = null;
  +
       /**
        * Creates an IdGenerator which will work with the specified database.
        *
        * @param adapter the adapter that knows the correct sql syntax.
  +     * @param name The name of the datasource to find the correct schema
        */
  -    public SequenceIdGenerator(DB adapter)
  +    public SequenceIdGenerator(final DB dbAdapter, final String name)
       {
  -        dbAdapter = adapter;
  +        this.dbAdapter = dbAdapter;
  +        this.name = name;
       }
   
       /**
  @@ -151,7 +157,8 @@
       private Value getIdAsVillageValue(Connection connection, Object keyInfo)
           throws Exception
       {
  -        String idSql = dbAdapter.getIDMethodSQL(keyInfo);
  +        String sequenceName = SQLBuilder.getFullTableName(String.valueOf(keyInfo), name);
  +        String idSql = dbAdapter.getIDMethodSQL(sequenceName);
           if (log.isDebugEnabled())
           {
               log.debug(idSql);
  
  
  
  No                   revision
  No                   revision
  1.76.2.10 +34 -23    db-torque/src/java/org/apache/torque/util/BasePeer.java
  
  Index: BasePeer.java
  ===================================================================
  RCS file: /home/cvs/db-torque/src/java/org/apache/torque/util/BasePeer.java,v
  retrieving revision 1.76.2.9
  retrieving revision 1.76.2.10
  diff -u -r1.76.2.9 -r1.76.2.10
  --- BasePeer.java	3 Dec 2004 18:38:40 -0000	1.76.2.9
  +++ BasePeer.java	13 Dec 2004 17:08:28 -0000	1.76.2.10
  @@ -331,8 +331,9 @@
       public static void doDelete(Criteria criteria, Connection con)
           throws TorqueException
       {
  -        final DB db = Torque.getDB(criteria.getDbName());
  -        final DatabaseMap dbMap = Torque.getDatabaseMap(criteria.getDbName());
  +        String dbName = criteria.getDbName();
  +        final DB db = Torque.getDB(dbName);
  +        final DatabaseMap dbMap = Torque.getDatabaseMap(dbName);
   
           // This Callback adds all tables to the Table set which 
           // are referenced from a cascading criteria. As a result, all
  @@ -373,7 +374,7 @@
           try
           {
               processTables(criteria, tables, con, new ProcessCallback() {
  -                    public void process (String table, Record rec)
  +                    public void process(String table, String dbName, Record rec)
                           throws Exception
                       {
                           rec.markToBeDeleted();
  @@ -465,11 +466,11 @@
   
           // Get the table name and method for determining the primary
           // key value.
  -        String tableName = null;
  +        String table = null;
           Iterator keys = criteria.keySet().iterator();
           if (keys.hasNext())
           {
  -            tableName = criteria.getTableName((String) keys.next());
  +            table = criteria.getTableName((String) keys.next());
           }
           else
           {
  @@ -477,8 +478,9 @@
                       + "anything specified to insert");
           }
   
  -        DatabaseMap dbMap = Torque.getDatabaseMap(criteria.getDbName());
  -        TableMap tableMap = dbMap.getTable(tableName);
  +        String dbName = criteria.getDbName();
  +        DatabaseMap dbMap = Torque.getDatabaseMap(dbName);
  +        TableMap tableMap = dbMap.getTable(table);
           Object keyInfo = tableMap.getPrimaryKeyMethodInfo();
           IdGenerator keyGen = tableMap.getIdGenerator();
   
  @@ -495,7 +497,7 @@
                   if (keyGen == null)
                   {
                       throw new TorqueException(
  -                            "IdGenerator for table '" + tableName + "' is null");
  +                            "IdGenerator for table '" + table + "' is null");
                   }
                   
                   id = getId(pk, keyGen, con, keyInfo);
  @@ -507,9 +509,11 @@
           TableDataSet tds = null;
           try
           {
  +            String tableName = SQLBuilder.getFullTableName(table, dbName);
               tds = new TableDataSet(con, tableName);
               Record rec = tds.addRecord();
  -            BasePeer.insertOrUpdateRecord(rec, tableName, criteria);
  +            // not the fully qualified name, insertOrUpdateRecord wants to use table as
an index...
  +            BasePeer.insertOrUpdateRecord(rec, table, dbName, criteria);
           }
           catch (Exception e)
           {
  @@ -573,20 +577,21 @@
        * methods.  Sets up a Record for saving.
        *
        * @param rec A Record.
  -     * @param tableName Name of table.
  +     * @param table Name of table.
        * @param criteria A Criteria.
        * @throws TorqueException Any exceptions caught during processing will be
        *         rethrown wrapped into a TorqueException.
        */
       private static void insertOrUpdateRecord(
           Record rec,
  -        String tableName,
  +        String table,
  +        String dbName,
           Criteria criteria)
           throws TorqueException
       {
  -        DatabaseMap dbMap = Torque.getDatabaseMap(criteria.getDbName());
  +        DatabaseMap dbMap = Torque.getDatabaseMap(dbName);
   
  -        ColumnMap[] columnMaps = dbMap.getTable(tableName).getColumns();
  +        ColumnMap[] columnMaps = dbMap.getTable(table).getColumns();
           boolean shouldSave = false;
           for (int j = 0; j < columnMaps.length; j++)
           {
  @@ -1201,18 +1206,20 @@
           Connection con)
           throws TorqueException
       {
  -        DB db = Torque.getDB(criteria.getDbName());
  -        DatabaseMap dbMap = Torque.getDatabaseMap(criteria.getDbName());
  +        String dbName = criteria.getDbName();
  +        DB db = Torque.getDB(dbName);
  +        DatabaseMap dbMap = Torque.getDatabaseMap(dbName);
   
           Set tables = SQLBuilder.getTableSet(criteria, null);
   
           try
           {
               processTables(criteria, tables, con, new ProcessCallback() {
  -                    public void process (String table, Record rec)
  +                    public void process (String table, String dbName, Record rec)
                           throws Exception
                       {
  -                        BasePeer.insertOrUpdateRecord(rec, table, updateValues);
  +                        // Callback must be called with table name without Schema!
  +                        BasePeer.insertOrUpdateRecord(rec, table, dbName, updateValues);
                       }
                   });
           }
  @@ -1532,8 +1539,9 @@
       private static void processTables(Criteria crit, Set tables, Connection con, ProcessCallback
pc)
               throws Exception
       {
  -        DB db = Torque.getDB(crit.getDbName());
  -        DatabaseMap dbMap = Torque.getDatabaseMap(crit.getDbName());
  +        String dbName = crit.getDbName();
  +        DB db = Torque.getDB(dbName);
  +        DatabaseMap dbMap = Torque.getDatabaseMap(dbName);
   
           // create the statements for the tables
           for (Iterator it = tables.iterator(); it.hasNext(); )
  @@ -1582,8 +1590,10 @@
               TableDataSet tds = null;
               try
               {
  +                String tableName = SQLBuilder.getFullTableName(table, dbName);
  +
                   // Get affected records.
  -                tds = new TableDataSet(con, table, kd);
  +                tds = new TableDataSet(con, tableName, kd);
                   String sqlSnippet = StringUtils.join(whereClause.iterator(), " AND ");
   
                   if (log.isDebugEnabled())
  @@ -1605,7 +1615,8 @@
   
                       if (pc != null)
                       {
  -                        pc.process(table, rec);
  +                        // Table name _without_ schema!
  +                        pc.process(table, dbName, rec);
                       }
                   }
               }
  @@ -1622,7 +1633,7 @@
        */
       protected interface ProcessCallback
       {
  -        void process (String table, Record rec)
  +        void process (String table, String dbName, Record rec)
                   throws Exception;
       }
   }
  
  
  
  1.1.2.6   +184 -53   db-torque/src/java/org/apache/torque/util/Attic/SQLBuilder.java
  
  Index: SQLBuilder.java
  ===================================================================
  RCS file: /home/cvs/db-torque/src/java/org/apache/torque/util/Attic/SQLBuilder.java,v
  retrieving revision 1.1.2.5
  retrieving revision 1.1.2.6
  diff -u -r1.1.2.5 -r1.1.2.6
  --- SQLBuilder.java	6 Dec 2004 19:50:26 -0000	1.1.2.5
  +++ SQLBuilder.java	13 Dec 2004 17:08:28 -0000	1.1.2.6
  @@ -49,6 +49,159 @@
       protected static Log log = LogFactory.getLog(SQLBuilder.class);
   
       /**
  +     * Fully qualify a table name with an optional schema reference
  +     *
  +     * @param table The table name to use. If null is passed in, null is returned.
  +     * @param dbName The name of the database to which this tables belongs.
  +     *               If null is passed, the default database is used.
  +     *
  +     * @return The table name to use inside the SQL statement. If null is passed
  +     *         into this method, null is returned.
  +     * @exception TorqueException if an error occurs
  +     */
  +    public static final String getFullTableName(final String table, final String dbName)
  +            throws TorqueException
  +    {
  +        if (table != null)
  +        {
  +            int dotIndex = table.indexOf(".");
  +
  +            if (dotIndex == -1) // No schema given
  +            {
  +                String targetDBName = (dbName == null) 
  +                        ? Torque.getDefaultDB()
  +                        : dbName;
  +                
  +                String targetSchema = Torque.getSchema(targetDBName);
  +                
  +                // If we have a default schema, fully qualify the
  +                // table and return.
  +                if (StringUtils.isNotEmpty(targetSchema))
  +                {
  +                    return new StringBuffer()
  +                            .append(targetSchema)
  +                            .append(".")
  +                            .append(table)
  +                            .toString();
  +                }
  +            }
  +        }
  +
  +        return table;
  +    }
  +
  +    /**
  +     * Remove a possible schema name from the table name.
  +     *
  +     * @param table The table name to use
  +     *
  +     * @return The table name with a possible schema name
  +     *         stripped off
  +     */
  +    public static final String getUnqualifiedTableName(final String table)
  +    {
  +        if (table != null)
  +        {
  +            int dotIndex = table.lastIndexOf("."); // Do we have a dot?
  +
  +            if (++dotIndex > 0) // Incrementation allows for better test _and_ substring...
  +            {
  +                return table.substring(dotIndex);
  +            }
  +        }
  +
  +        return table;
  +    }
  +
  +    /**
  +     * Removes a possible function name or clause from a column name
  +     *
  +     * @param name The column name, possibly containing a clause
  +     *
  +     * @return The column name
  +     *
  +     * @throws TorqueException If the column name was malformed
  +     */
  +    private static String removeSQLFunction(final String name)
  +            throws TorqueException
  +    {
  +        // Empty name => return it
  +        if (StringUtils.isEmpty(name))
  +        {
  +            return name;
  +        }
  +
  +        final int leftParent = name.lastIndexOf('(');
  +        final int rightParent = name.indexOf(')');
  +
  +        // Do we have Parentheses?
  +        if (leftParent < 0)
  +        {
  +            if (rightParent < 0)
  +            {
  +                // No left, no right => No function ==> return it
  +                return name;
  +            }
  +        }
  +
  +        // We have a left parenthesis. Is the right one behind it?
  +        if (rightParent > leftParent)
  +        {
  +            // Yes. Strip off the function, return the column name
  +            return name.substring(leftParent + 1, rightParent);
  +        }
  +
  +        // Bracket mismatch or wrong order ==> Exception
  +        throwMalformedColumnNameException(
  +                "removeSQLFunction",
  +                name);
  +
  +        return null; // Ugh
  +    }
  +
  +    /**
  +     * Returns a table name from an identifier. Each identifier is to be qualified 
  +     * as [schema.]table.column. This could also contain FUNCTION([schema.]table.column).
  +     *
  +     * @param name The (possible fully qualified) identifier name
  +     *
  +     * @return the fully qualified table name
  +     *
  +     * @throws TorqueException If the identifier name was malformed
  +     */
  +    public static String getTableName(final String name, final String dbName)
  +            throws TorqueException
  +    {
  +        final String testName = removeSQLFunction(name);
  +
  +        if (StringUtils.isEmpty(testName))
  +        {
  +            throwMalformedColumnNameException(
  +                    "getTableName",
  +                    name);
  +        }
  +
  +        // Everything before the last dot is the table name
  +        int rightDotIndex = testName.lastIndexOf('.');
  +
  +        if (rightDotIndex < 0)
  +        {
  +            if ("*".equals(testName))
  +            {
  +                return null;
  +            }
  +
  +            throwMalformedColumnNameException(
  +                    "getTableName",
  +                    name);
  +        }
  +
  +        return getFullTableName(testName.substring(0, rightDotIndex), dbName);
  +    }
  +            
  +            
  +
  +    /**
        * Returns a set of all tables and possible aliases referenced
        * from a criterion. The resulting Set can be directly used to
        * build a WHERE clause
  @@ -115,14 +268,15 @@
       {
           Query query = new Query();
   
  -        DB db = Torque.getDB(crit.getDbName());
  -        DatabaseMap dbMap = Torque.getDatabaseMap(crit.getDbName());
  +        final String dbName = crit.getDbName();
  +        final DB db = Torque.getDB(dbName);
  +        final DatabaseMap dbMap = Torque.getDatabaseMap(dbName);
   
           JoinBuilder.processJoins(db, dbMap, crit, query);
           processModifiers(crit, query);
  -        processSelectColumns(crit, query);
  +        processSelectColumns(crit, query, dbName);
           processAsColumns(crit, query);
  -        processCriterions(db, dbMap, crit, query,  params, qc);
  +        processCriterions(db, dbMap, dbName, crit, query,  params, qc);
           processGroupBy(crit, query);
           processHaving(crit, query);
           processOrderBy(db, dbMap, crit, query);
  @@ -144,7 +298,8 @@
        */
       private static final void processSelectColumns(
               final Criteria criteria,
  -            final Query query)
  +            final Query query,
  +            final String dbName)
           throws TorqueException
       {
           UniqueList selectClause = query.getSelectClause();
  @@ -152,46 +307,9 @@
           
           for (int i = 0; i < select.size(); i++)
           {
  -            String columnName = (String) select.get(i);
  -            if (columnName.indexOf('.') == -1 
  -                    && columnName.indexOf('*') == -1)
  -            {
  -                throwMalformedColumnNameException("select", columnName);
  -            }
  -            // the following breaks a test case, find out why
  -            // if a dot is mandatory, change malformed column if statement
  -            /*if (columnName.indexOf('.') == -1) 
  -            {
  -                // no table name can be extracted from statement,
  -                // as it contains only a * as table name
  -                break;
  -            }*/
  -            String tableName = null;
  -            selectClause.add(columnName);
  -            int parenPos = columnName.lastIndexOf('(');
  -            if (parenPos == -1)
  -            {
  -                tableName = columnName.substring(0, 
  -                        columnName.lastIndexOf('.'));
  -            }
  -            else if (columnName.indexOf('.') > -1)
  -            {
  -                tableName =
  -                        columnName.substring(parenPos + 1,
  -                                columnName.lastIndexOf('.'));
  -                // functions may contain qualifiers so only take the last
  -                // word as the table name.
  -                int lastSpace = tableName.lastIndexOf(' ');
  -                if (lastSpace != -1)
  -                {
  -                    tableName = tableName.substring(lastSpace + 1);
  -                }
  -            }
  -            // If the table was not already added to the from clause,
  -            // add it.
  -            // it is important that this piece of code is executed AFTER
  -            // the joins are processed
  -            addTableToFromClause(tableName, criteria, query); 
  +            String identifier = (String) select.get(i);
  +            selectClause.add(identifier);
  +            addTableToFromClause(getTableName(identifier, dbName), criteria, query); 
           }
       }
       
  @@ -247,6 +365,7 @@
       private static final void processCriterions(
               final DB db,
               final DatabaseMap dbMap,
  +            final String dbName,
               final Criteria crit, 
               final Query query,
               final List params,
  @@ -272,7 +391,7 @@
                   // contained there
                   // it is important that this piece of code is executed AFTER
                   // the joins are processed
  -                addTableToFromClause(tableName, crit, query);
  +                addTableToFromClause(getFullTableName(tableName, dbName), crit, query);
   
                   table = crit.getTableForAlias(tableName);
                   if (table == null)
  @@ -455,11 +574,14 @@
           final String columnName)
           throws TorqueException
       {
  -        throw new TorqueException("Malformed column name in Criteria "
  -                + criteriaPhrase
  -                + ": '"
  -                + columnName
  -                + "' is not of the form 'table.column'");
  +        StringBuffer sb = new StringBuffer()
  +                .append("Malformed column name in Criteria ")
  +                .append(criteriaPhrase)
  +                .append(": '")
  +                .append(StringUtils.isEmpty(columnName) ? "<empty>" : columnName)
  +                .append("' is not of the form 'table.column'");
  +
  +        throw new TorqueException(sb.toString());
       }
   
       /**
  @@ -479,7 +601,16 @@
               final String tableName, 
               final Criteria criteria) 
       {
  -        String aliasName = criteria.getTableForAlias(tableName);
  +        String shortTableName = getUnqualifiedTableName(tableName);
  +
  +        // Most of the time, the alias would be for the short name...
  +        String aliasName = criteria.getTableForAlias(shortTableName);
  +        if (StringUtils.isEmpty(aliasName))
  +        {
  +            // But we should also check the FQN...
  +            aliasName = criteria.getTableForAlias(tableName);
  +        }
  +
           if (StringUtils.isNotEmpty(aliasName))
           {
               // If the tables have an alias, add an "<xxx> <yyy> statement"
  
  
  
  No                   revision
  No                   revision
  1.5.4.1   +2 -1      db-torque/src/test/TurbineResources.properties
  
  Index: TurbineResources.properties
  ===================================================================
  RCS file: /home/cvs/db-torque/src/test/TurbineResources.properties,v
  retrieving revision 1.5
  retrieving revision 1.5.4.1
  diff -u -r1.5 -r1.5.4.1
  --- TurbineResources.properties	23 May 2002 18:27:15 -0000	1.5
  +++ TurbineResources.properties	13 Dec 2004 17:08:28 -0000	1.5.4.1
  @@ -57,6 +57,7 @@
   # -------------------------------------------------------------------
   
   torque.database.default.adapter=mysql
  +torque.dsfactory.default.factory= org.apache.torque.dsfactory.SharedPoolDataSourceFactory
   torque.idbroker.prefetch=false
   
   
  
  
  
  No                   revision
  No                   revision
  1.140.2.26 +3 -0      db-torque/xdocs/changes.xml
  
  Index: changes.xml
  ===================================================================
  RCS file: /home/cvs/db-torque/xdocs/changes.xml,v
  retrieving revision 1.140.2.25
  retrieving revision 1.140.2.26
  diff -u -r1.140.2.25 -r1.140.2.26
  --- changes.xml	10 Dec 2004 11:42:14 -0000	1.140.2.25
  +++ changes.xml	13 Dec 2004 17:08:28 -0000	1.140.2.26
  @@ -27,6 +27,9 @@
     <body>
     <release version="3.1.2-dev" date="in CVS">
       <action type="add" dev="henning">
  +      Add rudimentary schema support (see <a href="schema-howto.html">Schema Support
Howto</a>).
  +    </action>
  +    <action type="add" dev="henning">
         Add Support for Apache Derby
       </action>
       <action type="add" dev="henning" issue="TRQS255">
  
  
  
  1.11.2.13 +1 -0      db-torque/xdocs/navigation.xml
  
  Index: navigation.xml
  ===================================================================
  RCS file: /home/cvs/db-torque/xdocs/navigation.xml,v
  retrieving revision 1.11.2.12
  retrieving revision 1.11.2.13
  diff -u -r1.11.2.12 -r1.11.2.13
  --- navigation.xml	29 Oct 2004 11:52:51 -0000	1.11.2.12
  +++ navigation.xml	13 Dec 2004 17:08:28 -0000	1.11.2.13
  @@ -34,6 +34,7 @@
         <item name="Pool-config Howto"     href="/configuration-howto.html"/>
         <item name="Maven Howto"           href="/maven-howto.html"/>
         <item name="Peers Howto"           href="/peers-howto.html"/>
  +      <item name="DB Schema Support"     href="/schema-howto.html"/>
       </menu>
       <menu name="Database Howto Guides">
         <item name="MS SQL Server Howto"   href="/mssql-howto.html"/>
  
  
  
  No                   revision
  
  Index: navigation.xml
  ===================================================================
  RCS file: /home/cvs/db-torque/xdocs/navigation.xml,v
  retrieving revision 1.11.2.12
  retrieving revision 1.11.2.13
  diff -u -r1.11.2.12 -r1.11.2.13
  --- navigation.xml	29 Oct 2004 11:52:51 -0000	1.11.2.12
  +++ navigation.xml	13 Dec 2004 17:08:28 -0000	1.11.2.13
  @@ -34,6 +34,7 @@
         <item name="Pool-config Howto"     href="/configuration-howto.html"/>
         <item name="Maven Howto"           href="/maven-howto.html"/>
         <item name="Peers Howto"           href="/peers-howto.html"/>
  +      <item name="DB Schema Support"     href="/schema-howto.html"/>
       </menu>
       <menu name="Database Howto Guides">
         <item name="MS SQL Server Howto"   href="/mssql-howto.html"/>
  
  
  
  No                   revision
  
  Index: navigation.xml
  ===================================================================
  RCS file: /home/cvs/db-torque/xdocs/navigation.xml,v
  retrieving revision 1.11.2.12
  retrieving revision 1.11.2.13
  diff -u -r1.11.2.12 -r1.11.2.13
  --- navigation.xml	29 Oct 2004 11:52:51 -0000	1.11.2.12
  +++ navigation.xml	13 Dec 2004 17:08:28 -0000	1.11.2.13
  @@ -34,6 +34,7 @@
         <item name="Pool-config Howto"     href="/configuration-howto.html"/>
         <item name="Maven Howto"           href="/maven-howto.html"/>
         <item name="Peers Howto"           href="/peers-howto.html"/>
  +      <item name="DB Schema Support"     href="/schema-howto.html"/>
       </menu>
       <menu name="Database Howto Guides">
         <item name="MS SQL Server Howto"   href="/mssql-howto.html"/>
  
  
  
  1.1.2.1   +91 -0     db-torque/xdocs/Attic/schema-howto.xml
  
  
  
  

---------------------------------------------------------------------
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