Author: tomdz
Date: Wed Aug 17 14:36:34 2005
New Revision: 233264

URL: http://svn.apache.org/viewcvs?rev=233264&view=rev
Log:
Added facility to create/drop a database to the Platform interface

Implemented this facility for Derby and PostgreSQL

Added subtasks for this facility to the ddlToDatabase task

Added:
    
db/ddlutils/trunk/src/java/org/apache/ddlutils/task/CreateDatabaseCommand.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DropDatabaseCommand.java
Modified:
    db/ddlutils/trunk/src/java/org/apache/ddlutils/Platform.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/PlatformFactory.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/DerbyPlatform.java
    
db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/PlatformImplBase.java
    
db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/PostgreSqlPlatform.java
    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
    
db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteSchemaToDatabaseCommand.java

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/Platform.java
URL: 
http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/Platform.java?rev=233264&r1=233263&r2=233264&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/Platform.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/Platform.java Wed Aug 17 
14:36:34 2005
@@ -113,6 +113,34 @@
     public int evaluateBatch(Connection connection, String sql, boolean 
continueOnError) throws DynaSqlException;

 

     /**

+     * Creates the database specified by the given parameters. Please note 
that this method does not

+     * use a data source set via [EMAIL PROTECTED] #setDataSource(DataSource)} 
because it is not possible to

+     * retrieve the connection information from it without establishing a 
connection.<br/>

+     * The given connection url is the url that you'd use to connect to the 
already-created

+     * database. 

+     * 

+     * @param jdbcDriverClassName The jdbc driver class name

+     * @param connectionUrl       The url to connect to the database if it 
were already created

+     * @param username            The username for creating the database

+     * @param password            The password for creating the database

+     * 

+     * TODO: Support additional parameters which are platform specific (eg. 
encoding etc.)

+     */

+    public void createDatabase(String jdbcDriverClassName, String 
connectionUrl, String username, String password) throws DynaSqlException, 
UnsupportedOperationException;

+

+    /**

+     * Drops the database specified by the given parameters. Please note that 
this method does not

+     * use a data source set via [EMAIL PROTECTED] #setDataSource(DataSource)} 
because it is not possible to

+     * retrieve the connection information from it without establishing a 
connection.

+     * 

+     * @param jdbcDriverClassName The jdbc driver class name

+     * @param connectionUrl       The url to connect to the database

+     * @param username            The username for creating the database

+     * @param password            The password for creating the database

+     */

+    public void dropDatabase(String jdbcDriverClassName, String connectionUrl, 
String username, String password) throws DynaSqlException, 
UnsupportedOperationException;

+

+    /**

      * Creates the tables defined in the database model.

      * 

      * @param model           The database model


Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/PlatformFactory.java
URL: 
http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/PlatformFactory.java?rev=233264&r1=233263&r2=233264&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/PlatformFactory.java 
(original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/PlatformFactory.java Wed Aug 
17 14:36:34 2005
@@ -58,11 +58,11 @@
     }

 

     /**

-     * Creates a new SqlBuilder for the given (case insensitive) database name

+     * Creates a new platform for the given (case insensitive) database name

      * or returns null if the database is not recognized.

      * 

      * @param databaseName The name of the database (case is not important)

-     * @return The builder or <code>null</code> if the database is not 
supported

+     * @return The platform or <code>null</code> if the database is not 
supported

      */

     public static synchronized Platform createNewPlatformInstance(String 
databaseName) throws IllegalAccessException, InstantiationException

     {


Modified: 
db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/DerbyPlatform.java
URL: 
http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/DerbyPlatform.java?rev=233264&r1=233263&r2=233264&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/DerbyPlatform.java 
(original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/DerbyPlatform.java 
Wed Aug 17 14:36:34 2005
@@ -1,5 +1,11 @@
 package org.apache.ddlutils.platform;

 

+import java.sql.Connection;

+import java.sql.DriverManager;

+import java.sql.SQLException;

+

+import org.apache.ddlutils.DynaSqlException;

+

 /*

  * Copyright 1999-2004 The Apache Software Foundation.

  * 

@@ -39,5 +45,46 @@
     public String getDatabaseName()

     {

         return DATABASENAME;

+    }

+

+    /* (non-Javadoc)

+     * @see 
org.apache.ddlutils.platform.PlatformImplBase#createDatabase(java.lang.String, 
java.lang.String, java.lang.String, java.lang.String)

+     */

+    public void createDatabase(String jdbcDriverClassName, String 
connectionUrl, String username, String password) throws DynaSqlException, 
UnsupportedOperationException

+    {

+        // For Derby, you create databases by simply appending ";create=true" 
to the connection url

+        if (JDBC_DRIVER.equals(jdbcDriverClassName) ||

+            JDBC_DRIVER_EMBEDDED.equals(jdbcDriverClassName))

+        {

+            Connection connection = null;

+

+            try

+            {

+                Class.forName(jdbcDriverClassName);

+

+                connection = DriverManager.getConnection(connectionUrl + 
";create=true", username, password);

+                logWarnings(connection);

+            }

+            catch (Exception ex)

+            {

+                throw new DynaSqlException("Error while trying to create a 
database", ex);

+            }

+            finally

+            {

+                if (connection != null)

+                {

+                    try

+                    {

+                        connection.close();

+                    }

+                    catch (SQLException ex)

+                    {}

+                }

+            }

+        }

+        else

+        {

+            throw new UnsupportedOperationException("Unable to create a Derby 
database via the driver "+jdbcDriverClassName);

+        }

     }

 }


Modified: 
db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/PlatformImplBase.java
URL: 
http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/PlatformImplBase.java?rev=233264&r1=233263&r2=233264&view=diff
==============================================================================
--- 
db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/PlatformImplBase.java 
(original)
+++ 
db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/PlatformImplBase.java 
Wed Aug 17 14:36:34 2005
@@ -59,7 +59,7 @@
 public abstract class PlatformImplBase extends JdbcSupport implements Platform

 {

     /** The log for this platform */

-    private final Log _log = LogFactory.getLog(Platform.class);

+    private final Log _log = LogFactory.getLog(getClass());

 

     /** The sql builder for this platform */

     private SqlBuilder _builder;

@@ -90,6 +90,33 @@
         return _builder.getPlatformInfo();

     }

 

+    /**

+     * Returns the log for this platform.

+     * 

+     * @return The log

+     */

+    protected Log getLog()

+    {

+        return _log;

+    }

+

+    /**

+     * Logs any warnings associated to the given connection. Note that the 
connection needs

+     * to be open for this.

+     * 

+     * @param connection The open connection

+     */

+    protected void logWarnings(Connection connection) throws SQLException

+    {

+        SQLWarning warning = connection.getWarnings();

+

+        while (warning != null)

+        {

+            getLog().warn(warning.getLocalizedMessage(), warning.getCause());

+            warning = warning.getNextWarning();

+        }

+    }

+

     /* (non-Javadoc)

      * @see org.apache.ddlutils.Platform#evaluateBatch(java.lang.String, 
boolean)

      */

@@ -200,6 +227,22 @@
         {

             returnConnection(connection);

         }

+    }

+

+    /* (non-Javadoc)

+     * @see org.apache.ddlutils.Platform#createDatabase(java.lang.String, 
java.lang.String, java.lang.String, java.lang.String)

+     */

+    public void createDatabase(String jdbcDriverClassName, String 
connectionUrl, String username, String password) throws DynaSqlException, 
UnsupportedOperationException

+    {

+        throw new UnsupportedOperationException("Database creation is not 
supported for the database platform "+getDatabaseName());

+    }

+

+    /* (non-Javadoc)

+     * @see org.apache.ddlutils.Platform#dropDatabase(java.lang.String, 
java.lang.String, java.lang.String, java.lang.String)

+     */

+    public void dropDatabase(String jdbcDriverClassName, String connectionUrl, 
String username, String password) throws DynaSqlException, 
UnsupportedOperationException

+    {

+        throw new UnsupportedOperationException("Database deletion is not 
supported for the database platform "+getDatabaseName());

     }

 

     /* (non-Javadoc)


Modified: 
db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/PostgreSqlPlatform.java
URL: 
http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/PostgreSqlPlatform.java?rev=233264&r1=233263&r2=233264&view=diff
==============================================================================
--- 
db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/PostgreSqlPlatform.java 
(original)
+++ 
db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/PostgreSqlPlatform.java 
Wed Aug 17 14:36:34 2005
@@ -16,8 +16,13 @@
  * limitations under the License.

  */

 

+import java.sql.Connection;

+import java.sql.DriverManager;

+import java.sql.SQLException;

+import java.sql.Statement;

 import java.sql.Types;

 

+import org.apache.ddlutils.DynaSqlException;

 import org.apache.ddlutils.PlatformInfo;

 import org.apache.ddlutils.builder.PostgreSqlBuilder;

 

@@ -80,4 +85,99 @@
     {

         return DATABASENAME;

     }

+

+    /**

+     * Creates or drops the database referenced by the given connection url.

+     * 

+     * @param jdbcDriverClassName The jdbc driver class name

+     * @param connectionUrl       The url to connect to the database if it 
were already created

+     * @param username            The username for creating the database

+     * @param password            The password for creating the database

+     * @param createDb            Whether to create or drop the database

+     */

+    private void createOrDropDatabase(String jdbcDriverClassName, String 
connectionUrl, String username, String password, boolean createDb) throws 
DynaSqlException, UnsupportedOperationException

+    {

+        if (JDBC_DRIVER.equals(jdbcDriverClassName))

+        {

+            int slashPos = connectionUrl.lastIndexOf('/');

+

+            if (slashPos < 0)

+            {

+                throw new DynaSqlException("Cannot parse the given connection 
url "+connectionUrl);

+            }

+

+            int        paramPos   = connectionUrl.lastIndexOf('?');

+            String     dbName     = (paramPos > slashPos ? 
connectionUrl.substring(slashPos + 1, paramPos) : 
connectionUrl.substring(slashPos + 1));

+            Connection connection = null;

+            Statement  stmt       = null;

+

+            try

+            {

+                Class.forName(jdbcDriverClassName);

+

+                connection = 
DriverManager.getConnection(connectionUrl.substring(0, slashPos + 1) + 
"template1", username, password);

+                stmt       = connection.createStatement();

+                if (createDb)

+                {

+                    stmt.execute("CREATE DATABASE "+dbName);

+                }

+                else

+                {

+                    stmt.execute("DROP DATABASE "+dbName);

+                }

+                logWarnings(connection);

+            }

+            catch (Exception ex)

+            {

+                throw new DynaSqlException("Error while trying to " + 
(createDb ? "create" : "drop") + " a database: "+ex.getLocalizedMessage(), ex);

+            }

+            finally

+            {

+                if (stmt != null)

+                {

+                    try

+                    {

+                        stmt.close();

+                    }

+                    catch (SQLException ex)

+                    {}

+                }

+                if (connection != null)

+                {

+                    try

+                    {

+                        connection.close();

+                    }

+                    catch (SQLException ex)

+                    {}

+                }

+            }

+        }

+        else

+        {

+            throw new UnsupportedOperationException("Unable to " + (createDb ? 
"create" : "drop") + " a PostgreSQL database via the driver 
"+jdbcDriverClassName);

+        }

+    }

+

+    /* (non-Javadoc)

+     * @see 
org.apache.ddlutils.platform.PlatformImplBase#createDatabase(java.lang.String, 
java.lang.String, java.lang.String, java.lang.String)

+     */

+    public void createDatabase(String jdbcDriverClassName, String 
connectionUrl, String username, String password) throws DynaSqlException, 
UnsupportedOperationException

+    {

+        // With PostgreSQL, you create a database by executing "CREATE 
DATABASE" in an existing database (usually 

+        // the template1 database because it usually exists)

+        createOrDropDatabase(jdbcDriverClassName, connectionUrl, username, 
password, true);

+    }

+

+    /* (non-Javadoc)

+     * @see 
org.apache.ddlutils.platform.PlatformImplBase#dropDatabase(java.lang.String, 
java.lang.String, java.lang.String, java.lang.String)

+     */

+    public void dropDatabase(String jdbcDriverClassName, String connectionUrl, 
String username, String password) throws DynaSqlException, 
UnsupportedOperationException

+    {

+        // With PostgreSQL, you create a database by executing "DROP DATABASE" 
in an existing database (usually 

+        // the template1 database because it usually exists)

+        createOrDropDatabase(jdbcDriverClassName, connectionUrl, username, 
password, false);

+    }

+

+    

 }


Added: 
db/ddlutils/trunk/src/java/org/apache/ddlutils/task/CreateDatabaseCommand.java
URL: 
http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/task/CreateDatabaseCommand.java?rev=233264&view=auto
==============================================================================
--- 
db/ddlutils/trunk/src/java/org/apache/ddlutils/task/CreateDatabaseCommand.java 
(added)
+++ 
db/ddlutils/trunk/src/java/org/apache/ddlutils/task/CreateDatabaseCommand.java 
Wed Aug 17 14:36:34 2005
@@ -0,0 +1,63 @@
+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 org.apache.commons.dbcp.BasicDataSource;

+import org.apache.ddlutils.Platform;

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

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

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

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

+

+/**

+ * Command for creating a database.

+ */

+public class CreateDatabaseCommand extends DatabaseCommand

+{

+    /* (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

+    {

+        BasicDataSource dataSource = getDataSource();

+

+        if (dataSource == null)

+        {

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

+        }

+

+        Platform platform = getPlatform();

+

+        try

+        {

+            platform.createDatabase(dataSource.getDriverClassName(),

+                                    dataSource.getUrl(),

+                                    dataSource.getUsername(),

+                                    dataSource.getPassword());

+

+            task.log("Created database", Project.MSG_INFO);

+        }

+        catch (UnsupportedOperationException ex)

+        {

+            task.log("Database platform "+getPlatform().getDatabaseName()+" 
does not support database creation via JDBC", Project.MSG_ERR);

+        }

+        catch (Exception ex)

+        {

+            throw new BuildException(ex);

+        }

+    }

+}


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=233264&r1=233263&r2=233264&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 17 14:36:34 2005
@@ -85,17 +85,18 @@
      */

     protected Platform getPlatform() throws BuildException

     {

-        Platform platform = null;

+        BasicDataSource dataSource = getDataSource();

+        Platform        platform   = null;

 

         try

         {

             if (getDatabaseType() == null)

             {

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

-                if (getDatabaseType() == null)

-                {

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

-                }

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

+            }

+            if (getDatabaseType() == null)

+            {

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

             }

             platform = 
PlatformFactory.createNewPlatformInstance(getDatabaseType());

         }


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=233264&r1=233263&r2=233264&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 17 14:36:34 2005
@@ -24,6 +24,7 @@
 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;

 

 /**

@@ -168,11 +169,6 @@
      */

     private Database readSchema()

     {

-        // TODO: This should largely be deducable from the jdbc connection url

-        if (_databaseType == null)

-        {

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

-        }

         if (_dataSource == null)

         {

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

@@ -201,7 +197,7 @@
         }

         catch (Exception ex)

         {

-            throw new BuildException("Could not read the schema from the 
specified database", ex);

+            throw new BuildException("Could not read the schema from the 
specified database: "+ex.getLocalizedMessage(), ex);

         }

     }

 

@@ -212,7 +208,7 @@
     {

         if (_commands.isEmpty())

         {

-            System.out.println("No sub tasks specified, so there is nothing to 
do.");

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

             return;

         }

 

@@ -220,7 +216,7 @@
 

         if (model == null)

         {

-            System.out.println("No schemas read, so there is nothing to do.");

+            log("No schemas read, so there is nothing to do.", 
Project.MSG_INFO);

             return;

         }

 


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=233264&r1=233263&r2=233264&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 17 14:36:34 2005
@@ -62,6 +62,26 @@
     }

 

     /**

+     * Adds the "create database"-command.

+     * 

+     * @param command The command

+     */

+    public void addCreateDatabase(CreateDatabaseCommand command)

+    {

+        _commands.add(command);

+    }

+

+    /**

+     * Adds the "drop database"-command.

+     * 

+     * @param command The command

+     */

+    public void addDropDatabase(DropDatabaseCommand command)

+    {

+        _commands.add(command);

+    }

+

+    /**

      * Adds the "write dtd to file"-command.

      * 

      * @param command The command

@@ -152,7 +172,7 @@
                         }

                         catch (IllegalArgumentException ex)

                         {

-                            throw new BuildException("Could not merge with 
schema from file "+files[idx], ex);

+                            throw new BuildException("Could not merge with 
schema from file "+files[idx]+": "+ex.getLocalizedMessage(), ex);

                         }

                     }

                 }

@@ -189,7 +209,7 @@
             }

             catch (Exception ex)

             {

-                throw new BuildException("Could not read schema file 
"+schemaFile.getAbsolutePath(), ex);

+                throw new BuildException("Could not read schema file 
"+schemaFile.getAbsolutePath()+": "+ex.getLocalizedMessage(), ex);

             }

         }

         return model;

@@ -202,17 +222,11 @@
     {

         if (_commands.isEmpty())

         {

-            System.out.println("No sub tasks specified, so there is nothing to 
do.");

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

             return;

         }

 

         Database model = readSchemaFiles();

-

-        if (model == null)

-        {

-            System.out.println("No schemas read, so there is nothing to do.");

-            return;

-        }

 

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

         {


Added: 
db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DropDatabaseCommand.java
URL: 
http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DropDatabaseCommand.java?rev=233264&view=auto
==============================================================================
--- 
db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DropDatabaseCommand.java 
(added)
+++ 
db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DropDatabaseCommand.java 
Wed Aug 17 14:36:34 2005
@@ -0,0 +1,63 @@
+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 org.apache.commons.dbcp.BasicDataSource;

+import org.apache.ddlutils.Platform;

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

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

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

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

+

+/**

+ * Command for dropping a database.

+ */

+public class DropDatabaseCommand extends DatabaseCommand

+{

+    /* (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

+    {

+        BasicDataSource dataSource = getDataSource();

+

+        if (dataSource == null)

+        {

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

+        }

+

+        Platform platform = getPlatform();

+

+        try

+        {

+            platform.dropDatabase(dataSource.getDriverClassName(),

+                                  dataSource.getUrl(),

+                                  dataSource.getUsername(),

+                                  dataSource.getPassword());

+

+            task.log("Dropped database", Project.MSG_INFO);

+        }

+        catch (UnsupportedOperationException ex)

+        {

+            task.log("Database platform "+getPlatform().getDatabaseName()+" 
does not support database dropping via JDBC", Project.MSG_ERR);

+        }

+        catch (Exception ex)

+        {

+            throw new BuildException(ex);

+        }

+    }

+}


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=233264&r1=233263&r2=233264&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 17 14:36:34 2005
@@ -24,6 +24,7 @@
 

 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;

@@ -96,6 +97,15 @@
     {

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

             DataToDatabaseSink sink     = new DataToDatabaseSink(platform, 
model);

             DataReader         reader   = new DataReader();


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=233264&r1=233263&r2=233264&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 17 14:36:34 2005
@@ -24,6 +24,7 @@
 

 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;

@@ -80,6 +81,15 @@
     {

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

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

             


Modified: 
db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteSchemaToDatabaseCommand.java
URL: 
http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteSchemaToDatabaseCommand.java?rev=233264&r1=233263&r2=233264&view=diff
==============================================================================
--- 
db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteSchemaToDatabaseCommand.java
 (original)
+++ 
db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteSchemaToDatabaseCommand.java
 Wed Aug 17 14:36:34 2005
@@ -16,8 +16,6 @@
  * limitations under the License.

  */

 

-import java.io.StringWriter;

-

 import org.apache.ddlutils.Platform;

 import org.apache.ddlutils.model.Database;

 import org.apache.tools.ant.BuildException;

@@ -39,10 +37,8 @@
             throw new BuildException("No database specified.");

         }

 

-        Platform     platform   = getPlatform();

-        StringWriter writer     = new StringWriter();

+        Platform platform = getPlatform();

 

-        platform.getSqlBuilder().setWriter(writer);

         try

         {

             if (isAlterDatabase())

@@ -54,7 +50,7 @@
                 platform.createTables(model, true, true);

             }

 

-            task.log("Created database", Project.MSG_INFO);

+            task.log("Written schema to database", Project.MSG_INFO);

         }

         catch (Exception ex)

         {



Reply via email to