Author: tomdz
Date: Wed Aug 24 12:10:56 2005
New Revision: 239734

URL: http://svn.apache.org/viewcvs?rev=239734&view=rev
Log:
Unified ddlToDatabase & databaseToDdl tasks so that specifying the database 
works the same for both

Added:
    db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DatabaseTaskBase.java
Removed:
    db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WantsDatabaseInfo.java
Modified:
    db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DatabaseCommand.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DatabaseToDdlTask.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DdlToDatabaseTask.java
    
db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteDataToDatabaseCommand.java
    
db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteDataToFileCommand.java

Modified: 
db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DatabaseCommand.java
URL: 
http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DatabaseCommand.java?rev=239734&r1=239733&r2=239734&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DatabaseCommand.java 
(original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DatabaseCommand.java 
Wed Aug 24 12:10:56 2005
@@ -29,16 +29,6 @@
     }

 

     /**

-     * Sets the database type.

-     * 

-     * @param type The database type

-     */

-    public void setDatabaseType(String type)

-    {

-        _databaseType = type;

-    }

-

-    /**

      * Returns the data source to use for accessing the database.

      * 

      * @return The data source

@@ -49,13 +39,15 @@
     }

 

     /**

-     * Adds the data source to use for accessing the database.

+     * Sets the database info.

      * 

-     * @param dataSource The data source

+     * @param dataSource The data source pointing to the database

+     * @param type       The database type

      */

-    public void addConfiguredDatabase(BasicDataSource dataSource)

+    protected void setDatabaseInfo(BasicDataSource dataSource, String type)

     {

-        _dataSource = dataSource;

+        _dataSource   = dataSource;

+        _databaseType = type;

     }

 

     /**

@@ -86,32 +78,39 @@
      */

     protected Platform getPlatform() throws BuildException

     {

-        BasicDataSource dataSource = getDataSource();

-        Platform        platform   = null;

+        Platform platform = null;

 

-        try

+        if (_databaseType == null)

         {

-            if (getDatabaseType() == null)

+            if (_dataSource == null)

+            {

+                throw new BuildException("No database specified.");

+            }

+            if (_databaseType == null)

             {

-                setDatabaseType(new 
PlatformUtils().determineDatabaseType(dataSource.getDriverClassName(), 
dataSource.getUrl()));

+                _databaseType = new 
PlatformUtils().determineDatabaseType(_dataSource.getDriverClassName(),

+                                                                          
_dataSource.getUrl());

             }

-            if (getDatabaseType() == null)

+            if (_databaseType == null)

             {

-                setDatabaseType(new 
PlatformUtils().determineDatabaseType(dataSource));

+                _databaseType = new 
PlatformUtils().determineDatabaseType(_dataSource);

             }

-            platform = 
PlatformFactory.createNewPlatformInstance(getDatabaseType());

+        }

+        try

+        {

+            platform = 
PlatformFactory.createNewPlatformInstance(_databaseType);

         }

         catch (Exception ex)

         {

-            throw new BuildException("Database type "+getDatabaseType()+" is 
not supported.", ex);

+            throw new BuildException("Database type "+_databaseType+" is not 
supported.", ex);

         }

         if (platform == null)

         {

-            throw new BuildException("Database type "+getDatabaseType()+" is 
not supported.");

+            throw new BuildException("Database type "+_databaseType+" is not 
supported.");

         }

         else

         {

-            platform.setDataSource(getDataSource());

+            platform.setDataSource(_dataSource);

             return platform;

         }

     }


Added: db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DatabaseTaskBase.java
URL: 
http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DatabaseTaskBase.java?rev=239734&view=auto
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DatabaseTaskBase.java 
(added)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DatabaseTaskBase.java 
Wed Aug 24 12:10:56 2005
@@ -0,0 +1,113 @@
+package org.apache.ddlutils.task;

+

+/*

+ * Copyright 1999-2004 The Apache Software Foundation.

+ * 

+ * Licensed under the Apache License, Version 2.0 (the "License");

+ * you may not use this file except in compliance with the License.

+ * You may obtain a copy of the License at

+ * 

+ *      http://www.apache.org/licenses/LICENSE-2.0

+ * 

+ * Unless required by applicable law or agreed to in writing, software

+ * distributed under the License is distributed on an "AS IS" BASIS,

+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

+ * See the License for the specific language governing permissions and

+ * limitations under the License.

+ */

+

+import java.util.ArrayList;

+import java.util.Iterator;

+

+import org.apache.commons.dbcp.BasicDataSource;

+import org.apache.ddlutils.model.Database;

+import org.apache.tools.ant.Task;

+

+public abstract class DatabaseTaskBase extends Task

+{

+    /** The type of the database */

+    private String _databaseType;

+    /** The data source to use for accessing the database */

+    private BasicDataSource _dataSource;

+    /** The sub tasks to execute */

+    private ArrayList _commands = new ArrayList();

+

+    /**

+     * Returns the database type.

+     *

+     * @return The database type

+     */

+    public String getDatabaseType()

+    {

+        return _databaseType;

+    }

+

+    /**

+     * Sets the database type.

+     * 

+     * @param type The database type

+     */

+    public void setDatabaseType(String type)

+    {

+        _databaseType = type;

+    }

+

+    /**

+     * Returns the data source.

+     *

+     * @return The data source

+     */

+    public BasicDataSource getDataSource()

+    {

+        return _dataSource;

+    }

+

+    /**

+     * Adds the data source to use for accessing the database.

+     * 

+     * @param dataSource The data source

+     */

+    public void addConfiguredDatabase(BasicDataSource dataSource)

+    {

+        _dataSource = dataSource;

+    }

+

+    /**

+     * Adds a command.

+     * 

+     * @param command The command

+     */

+    protected void addCommand(Command command)

+    {

+        _commands.add(command);

+    }

+

+    /**

+     * Determines whether there are commands to perform.

+     * 

+     * @return <code>true</code> if there are commands

+     */

+    protected boolean hasCommands()

+    {

+        return !_commands.isEmpty();

+    }

+

+    /**

+     * Executes the commands.

+     * 

+     * @param model The database model

+     */

+    protected void executeCommands(Database model)

+    {

+        for (Iterator it = _commands.iterator(); it.hasNext();)

+        {

+            Command cmd = (Command)it.next();

+

+            if (cmd instanceof DatabaseCommand)

+            {

+                ((DatabaseCommand)cmd).setDatabaseInfo(getDataSource(), 
getDatabaseType());

+            }

+            cmd.execute(this, model);

+        }

+    }

+}


Modified: 
db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DatabaseToDdlTask.java
URL: 
http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DatabaseToDdlTask.java?rev=239734&r1=239733&r2=239734&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DatabaseToDdlTask.java 
(original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DatabaseToDdlTask.java 
Wed Aug 24 12:10:56 2005
@@ -17,43 +17,24 @@
  */

 

 import java.util.ArrayList;

-import java.util.Iterator;

 import java.util.StringTokenizer;

 

-import org.apache.commons.dbcp.BasicDataSource;

 import org.apache.ddlutils.io.JdbcModelReader;

 import org.apache.ddlutils.model.Database;

 import org.apache.tools.ant.BuildException;

 import org.apache.tools.ant.Project;

-import org.apache.tools.ant.Task;

 

 /**

  * Ant task for working with a database, e.g. retrieving the schema from a 
database, dumping data,

  */

-public class DatabaseToDdlTask extends Task

+public class DatabaseToDdlTask extends DatabaseTaskBase

 {

-    /** The type of the database */

-    private String _databaseType;

-    /** The data source to use for accessing the database */

-    private BasicDataSource _dataSource;

     /** The specific schema to use */

     private String _schema;

     /** The specific catalog to use */

     private String _catalog;

     /** The table types to recognize when reading the model from the database 
*/

     private String _tableTypes;

-    /** The sub tasks to execute */

-    private ArrayList _commands = new ArrayList();

-

-    /**

-     * Sets the database type.

-     * 

-     * @param type The database type

-     */

-    public void setDatabaseType(String type)

-    {

-        _databaseType = type;

-    }

 

     /**

      * Sets the database schema to access.

@@ -86,23 +67,13 @@
     }

 

     /**

-     * Adds the data source to use for accessing the database.

-     * 

-     * @param dataSource The data source

-     */

-    public void addConfiguredDatabase(BasicDataSource dataSource)

-    {

-        _dataSource = dataSource;

-    }

-

-    /**

      * Adds the "create dtd"-command.

      * 

      * @param command The command

      */

     public void addWriteDtdToFile(WriteDtdToFileCommand command)

     {

-        _commands.add(command);

+        addCommand(command);

     }

 

     /**

@@ -112,7 +83,7 @@
      */

     public void addWriteSchemaToFile(WriteSchemaToFileCommand command)

     {

-        _commands.add(command);

+        addCommand(command);

     }

 

     /**

@@ -122,7 +93,7 @@
      */

     public void addWriteSchemaSqlToFile(WriteSchemaSqlToFileCommand command)

     {

-        _commands.add(command);

+        addCommand(command);

     }

 

     /**

@@ -132,7 +103,7 @@
      */

     public void addWriteDataToDatabase(WriteDataToDatabaseCommand command)

     {

-        _commands.add(command);

+        addCommand(command);

     }

 

     /**

@@ -169,14 +140,14 @@
      */

     private Database readSchema()

     {

-        if (_dataSource == null)

+        if (getDataSource() == null)

         {

             throw new BuildException("No database specified.");

         }

 

         try

         {

-            JdbcModelReader reader = new 
JdbcModelReader(_dataSource.getConnection());

+            JdbcModelReader reader = new 
JdbcModelReader(getDataSource().getConnection());

 

             if ((_catalog != null) && (_catalog.length() > 0))

             {

@@ -206,7 +177,7 @@
      */

     public void execute() throws BuildException

     {

-        if (_commands.isEmpty())

+        if (!hasCommands())

         {

             log("No sub tasks specified, so there is nothing to do.", 
Project.MSG_INFO);

             return;

@@ -219,17 +190,6 @@
             log("No schemas read, so there is nothing to do.", 
Project.MSG_INFO);

             return;

         }

-

-        for (Iterator it = _commands.iterator(); it.hasNext();)

-        {

-            Command cmd = (Command)it.next();

-

-            if (cmd instanceof WantsDatabaseInfo)

-            {

-                ((WantsDatabaseInfo)cmd).setDatabaseInfo(_dataSource, 
_databaseType);

-            }

-            cmd.execute(this, model);

-        }

+        executeCommands(model);

     }

-

 }


Modified: 
db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DdlToDatabaseTask.java
URL: 
http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DdlToDatabaseTask.java?rev=239734&r1=239733&r2=239734&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DdlToDatabaseTask.java 
(original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DdlToDatabaseTask.java 
Wed Aug 24 12:10:56 2005
@@ -26,20 +26,17 @@
 import org.apache.tools.ant.BuildException;

 import org.apache.tools.ant.DirectoryScanner;

 import org.apache.tools.ant.Project;

-import org.apache.tools.ant.Task;

 import org.apache.tools.ant.types.FileSet;

 

 /**

  * Ant task for working with DDL, e.g. generating the database from a schema, 
inserting data,

  */

-public class DdlToDatabaseTask extends Task

+public class DdlToDatabaseTask extends DatabaseTaskBase

 {

     /** A single schema file to read */

     private File _singleSchemaFile = null;

     /** The input files */

     private ArrayList _fileSets = new ArrayList();

-    /** The sub tasks to execute */

-    private ArrayList _commands = new ArrayList();

 

     /**

      * Adds a fileset.

@@ -68,7 +65,7 @@
      */

     public void addCreateDatabase(CreateDatabaseCommand command)

     {

-        _commands.add(command);

+        addCommand(command);

     }

 

     /**

@@ -78,7 +75,7 @@
      */

     public void addDropDatabase(DropDatabaseCommand command)

     {

-        _commands.add(command);

+        addCommand(command);

     }

 

     /**

@@ -88,7 +85,7 @@
      */

     public void addWriteDtdToFile(WriteDtdToFileCommand command)

     {

-        _commands.add(command);

+        addCommand(command);

     }

 

     /**

@@ -98,7 +95,7 @@
      */

     public void addWriteSchemaToDatabase(WriteSchemaToDatabaseCommand command)

     {

-        _commands.add(command);

+        addCommand(command);

     }

 

     /**

@@ -108,7 +105,7 @@
      */

     public void addWriteSchemaSqlToFile(WriteSchemaSqlToFileCommand command)

     {

-        _commands.add(command);

+        addCommand(command);

     }

 

     /**

@@ -118,7 +115,7 @@
      */

     public void addWriteDataToDatabase(WriteDataToSpecifiedDatabaseCommand 
command)

     {

-        _commands.add(command);

+        addCommand(command);

     }

 

     /**

@@ -220,7 +217,7 @@
      */

     public void execute() throws BuildException

     {

-        if (_commands.isEmpty())

+        if (!hasCommands())

         {

             log("No sub tasks specified, so there is nothing to do.", 
Project.MSG_INFO);

             return;

@@ -228,12 +225,6 @@
 

         Database model = readSchemaFiles();

 

-        for (Iterator it = _commands.iterator(); it.hasNext();)

-        {

-            Command command = (Command)it.next();

-            

-            command.execute(this, model);

-        }

+        executeCommands(model);

     }

-

 }


Modified: 
db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteDataToDatabaseCommand.java
URL: 
http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteDataToDatabaseCommand.java?rev=239734&r1=239733&r2=239734&view=diff
==============================================================================
--- 
db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteDataToDatabaseCommand.java
 (original)
+++ 
db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteDataToDatabaseCommand.java
 Wed Aug 24 12:10:56 2005
@@ -20,11 +20,7 @@
 import java.util.ArrayList;

 import java.util.Iterator;

 

-import javax.sql.DataSource;

-

 import org.apache.ddlutils.Platform;

-import org.apache.ddlutils.PlatformFactory;

-import org.apache.ddlutils.PlatformUtils;

 import org.apache.ddlutils.io.DataConverterRegistration;

 import org.apache.ddlutils.io.DataReader;

 import org.apache.ddlutils.io.DataToDatabaseSink;

@@ -38,18 +34,14 @@
 /**

  * Command for inserting data into a database.

  */

-public class WriteDataToDatabaseCommand implements Command, WantsDatabaseInfo

+public class WriteDataToDatabaseCommand extends DatabaseCommand

 {

-    /** The data source pointing to the database */

-    private DataSource _dataSource;

-    /** The database type */

-    private String     _databaseType;

     /** A single data file to insert */

-    private File       _singleDataFile = null;

+    private File      _singleDataFile = null;

     /** The input files */

-    private ArrayList  _fileSets = new ArrayList();

+    private ArrayList _fileSets = new ArrayList();

     /** The converterd */

-    private ArrayList  _converters = new ArrayList();

+    private ArrayList _converters = new ArrayList();

 

     /**

      * Adds a fileset.

@@ -82,35 +74,16 @@
     }

 

     /* (non-Javadoc)

-     * @see 
org.apache.ddlutils.task.WantsDatabaseInfo#setDatabaseInfo(javax.sql.DataSource,
 java.lang.String)

-     */

-    public void setDatabaseInfo(DataSource dataSource, String type) throws 
BuildException

-    {

-        _dataSource   = dataSource;

-        _databaseType = type;

-    }

-

-    /* (non-Javadoc)

      * @see 
org.apache.ddlutils.task.Command#execute(org.apache.tools.ant.Task, 
org.apache.ddlutils.model.Database)

      */

     public void execute(Task task, Database model) throws BuildException

     {

         try

         {

-            if (_databaseType == null)

-            {

-                _databaseType = new 
PlatformUtils().determineDatabaseType(_dataSource);

-                if (_databaseType == null)

-                {

-                    throw new BuildException("The database type needs to be 
defined.");

-                }

-            }

-

-            Platform           platform = 
PlatformFactory.createNewPlatformInstance(_databaseType);

+            Platform           platform = getPlatform();

             DataToDatabaseSink sink     = new DataToDatabaseSink(platform, 
model);

             DataReader         reader   = new DataReader();

 

-            platform.setDataSource(_dataSource);

             reader.setModel(model);

             reader.setSink(sink);

             for (Iterator it = _converters.iterator(); it.hasNext();)


Modified: 
db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteDataToFileCommand.java
URL: 
http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteDataToFileCommand.java?rev=239734&r1=239733&r2=239734&view=diff
==============================================================================
--- 
db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteDataToFileCommand.java 
(original)
+++ 
db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteDataToFileCommand.java 
Wed Aug 24 12:10:56 2005
@@ -20,11 +20,7 @@
 import java.io.FileOutputStream;

 import java.util.Iterator;

 

-import javax.sql.DataSource;

-

 import org.apache.ddlutils.Platform;

-import org.apache.ddlutils.PlatformFactory;

-import org.apache.ddlutils.PlatformUtils;

 import org.apache.ddlutils.io.DataWriter;

 import org.apache.ddlutils.model.Database;

 import org.apache.ddlutils.model.Table;

@@ -34,16 +30,12 @@
 /**

  * Command to dump data from the database into an XML file.

  */

-public class WriteDataToFileCommand implements Command, WantsDatabaseInfo

+public class WriteDataToFileCommand extends DatabaseCommand

 {

-    /** The data source pointing to the database */

-    private DataSource _dataSource;

-    /** The database type */

-    private String     _databaseType;

     /** The file to output the data to */

-    private File       _outputFile;

+    private File   _outputFile;

     /** The character encoding to use */

-    private String     _encoding;

+    private String _encoding;

 

     /**

      * Sets the file to output the data to.

@@ -66,36 +58,17 @@
     }

 

     /* (non-Javadoc)

-     * @see 
org.apache.ddlutils.task.WantsDatabaseInfo#setDatabaseInfo(javax.sql.DataSource,
 java.lang.String)

-     */

-    public void setDatabaseInfo(DataSource dataSource, String type) throws 
BuildException

-    {

-        _dataSource   = dataSource;

-        _databaseType = type;

-    }

-

-    /* (non-Javadoc)

      * @see 
org.apache.ddlutils.task.Command#execute(org.apache.tools.ant.Task, 
org.apache.ddlutils.model.Database)

      */

     public void execute(Task task, Database model) throws BuildException

     {

         try

         {

-            if (_databaseType == null)

-            {

-                _databaseType = new 
PlatformUtils().determineDatabaseType(_dataSource);

-                if (_databaseType == null)

-                {

-                    throw new BuildException("The database type needs to be 
defined.");

-                }

-            }

-

-            Platform   platform = 
PlatformFactory.createNewPlatformInstance(_databaseType);

+            Platform   platform = getPlatform();

             DataWriter writer   = new DataWriter(model, new 
FileOutputStream(_outputFile), _encoding);

             

             // TODO: An advanced algorithm could be employed here that writes 
objects

             //       related by foreign keys, in the correct order

-            platform.setDataSource(_dataSource);

             writer.writeDocumentStart();

             for (Iterator tableIt = model.getTables().iterator(); 
tableIt.hasNext();)

             {



Reply via email to