db-ddlutils-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From to...@apache.org
Subject svn commit: r289319 - in /db/ddlutils/trunk/src/java/org/apache/ddlutils: Platform.java platform/DerbyPlatform.java platform/PlatformImplBase.java platform/PostgreSqlPlatform.java task/CreateDatabaseCommand.java task/NamedValue.java
Date Thu, 15 Sep 2005 21:11:53 GMT
Author: tomdz
Date: Thu Sep 15 14:11:43 2005
New Revision: 289319

URL: http://svn.apache.org/viewcvs?rev=289319&view=rev
Log:
Added support for defining additional parameters when creating databases; currently this is
implemented for PostgreSQL and Derby

Added:
    db/ddlutils/trunk/src/java/org/apache/ddlutils/task/NamedValue.java
Modified:
    db/ddlutils/trunk/src/java/org/apache/ddlutils/Platform.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/CreateDatabaseCommand.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=289319&r1=289318&r2=289319&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/Platform.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/Platform.java Thu Sep 15 14:11:43 2005
@@ -20,6 +20,7 @@
 import java.util.Collection;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
 
 import javax.sql.DataSource;
 
@@ -117,16 +118,17 @@
      * use a data source set via {@link #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. 
+     * database.<br/>
+     * On some platforms, this method suppurts additional parameters. These are documented
in the
+     * manual section for the individual platforms. 
      * 
      * @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.)
+     * @param parameters          Additional parameters relevant to database creation (which
are platform specific)
      */
-    public void createDatabase(String jdbcDriverClassName, String connectionUrl, String username,
String password) throws DynaSqlException, UnsupportedOperationException;
+    public void createDatabase(String jdbcDriverClassName, String connectionUrl, String username,
String password, Map parameters) throws DynaSqlException, UnsupportedOperationException;
 
     /**
      * Drops the database specified by the given parameters. Please note that this method
does not

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=289319&r1=289318&r2=289319&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 Thu Sep 15
14:11:43 2005
@@ -3,6 +3,8 @@
 import java.sql.Connection;
 import java.sql.DriverManager;
 import java.sql.SQLException;
+import java.util.Iterator;
+import java.util.Map;
 
 import org.apache.ddlutils.DynaSqlException;
 
@@ -48,21 +50,47 @@
     }
 
     /* (non-Javadoc)
-     * @see org.apache.ddlutils.platform.PlatformImplBase#createDatabase(java.lang.String,
java.lang.String, java.lang.String, java.lang.String)
+     * @see org.apache.ddlutils.Platform#createDatabase(java.lang.String, java.lang.String,
java.lang.String, java.lang.String, java.util.Map)
      */
-    public void createDatabase(String jdbcDriverClassName, String connectionUrl, String username,
String password) throws DynaSqlException, UnsupportedOperationException
+    public void createDatabase(String jdbcDriverClassName, String connectionUrl, String username,
String password, Map parameters) 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;
+            StringBuffer creationUrl = new StringBuffer();
+            Connection   connection  = null;
 
+            creationUrl.append(connectionUrl);
+            creationUrl.append(";create=true");
+            if ((parameters != null) && !parameters.isEmpty())
+            {
+                for (Iterator it = parameters.entrySet().iterator(); it.hasNext();)
+                {
+                    Map.Entry entry = (Map.Entry)it.next();
+
+                    // no need to specify create twice (and create=false wouldn't help anyway)
+                    if ("create".equalsIgnoreCase(entry.getKey().toString()))
+                    {
+                        creationUrl.append(";");
+                        creationUrl.append(entry.getKey().toString());
+                        creationUrl.append("=");
+                        if (entry.getValue() != null)
+                        {
+                            creationUrl.append(entry.getValue().toString());
+                        }
+                    }
+                }
+            }
+            if (getLog().isDebugEnabled())
+            {
+                getLog().debug("About to create database using this URL: "+creationUrl.toString());
+            }
             try
             {
                 Class.forName(jdbcDriverClassName);
 
-                connection = DriverManager.getConnection(connectionUrl + ";create=true",
username, password);
+                connection = DriverManager.getConnection(creationUrl.toString(), username,
password);
                 logWarnings(connection);
             }
             catch (Exception ex)

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=289319&r1=289318&r2=289319&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 Thu Sep
15 14:11:43 2005
@@ -30,6 +30,7 @@
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
 import java.util.StringTokenizer;
 
 import org.apache.commons.beanutils.DynaBean;
@@ -230,9 +231,9 @@
     }
 
     /* (non-Javadoc)
-     * @see org.apache.ddlutils.Platform#createDatabase(java.lang.String, java.lang.String,
java.lang.String, java.lang.String)
+     * @see org.apache.ddlutils.Platform#createDatabase(java.lang.String, java.lang.String,
java.lang.String, java.lang.String, java.util.Map)
      */
-    public void createDatabase(String jdbcDriverClassName, String connectionUrl, String username,
String password) throws DynaSqlException, UnsupportedOperationException
+    public void createDatabase(String jdbcDriverClassName, String connectionUrl, String username,
String password, Map parameters) throws DynaSqlException, UnsupportedOperationException
     {
         throw new UnsupportedOperationException("Database creation is not supported for the
database platform "+getDatabaseName());
     }

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=289319&r1=289318&r2=289319&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 Thu Sep
15 14:11:43 2005
@@ -21,6 +21,8 @@
 import java.sql.SQLException;
 import java.sql.Statement;
 import java.sql.Types;
+import java.util.Iterator;
+import java.util.Map;
 
 import org.apache.ddlutils.DynaSqlException;
 import org.apache.ddlutils.PlatformInfo;
@@ -97,9 +99,10 @@
      * @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 parameters          Additional parameters for the operation
      * @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
+    private void createOrDropDatabase(String jdbcDriverClassName, String connectionUrl, String
username, String password, Map parameters, boolean createDb) throws DynaSqlException, UnsupportedOperationException
     {
         if (JDBC_DRIVER.equals(jdbcDriverClassName))
         {
@@ -110,25 +113,42 @@
                 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;
+            int          paramPos   = connectionUrl.lastIndexOf('?');
+            String       baseDb     = connectionUrl.substring(0, slashPos + 1) + "template1";
+            String       dbName     = (paramPos > slashPos ? connectionUrl.substring(slashPos
+ 1, paramPos) : connectionUrl.substring(slashPos + 1));
+            Connection   connection = null;
+            Statement    stmt       = null;
+            StringBuffer sql        = new StringBuffer();
+
+            sql.append(createDb ? "CREATE" : "DROP");
+            sql.append(" DATABASE ");
+            sql.append(dbName);
+            if ((parameters != null) && !parameters.isEmpty())
+            {
+                for (Iterator it = parameters.entrySet().iterator(); it.hasNext();)
+                {
+                    Map.Entry entry = (Map.Entry)it.next();
 
+                    sql.append(" ");
+                    sql.append(entry.getKey().toString());
+                    if (entry.getValue() != null)
+                    {
+                        sql.append(" ");
+                        sql.append(entry.getValue().toString());
+                    }
+                }
+            }
+            if (getLog().isDebugEnabled())
+            {
+                getLog().debug("About to create database via "+baseDb+" using this SQL: "+sql.toString());
+            }
             try
             {
                 Class.forName(jdbcDriverClassName);
 
-                connection = DriverManager.getConnection(connectionUrl.substring(0, slashPos
+ 1) + "template1", username, password);
+                connection = DriverManager.getConnection(baseDb, username, password);
                 stmt       = connection.createStatement();
-                if (createDb)
-                {
-                    stmt.execute("CREATE DATABASE "+dbName);
-                }
-                else
-                {
-                    stmt.execute("DROP DATABASE "+dbName);
-                }
+                stmt.execute(sql.toString());
                 logWarnings(connection);
             }
             catch (Exception ex)
@@ -164,13 +184,13 @@
     }
 
     /* (non-Javadoc)
-     * @see org.apache.ddlutils.platform.PlatformImplBase#createDatabase(java.lang.String,
java.lang.String, java.lang.String, java.lang.String)
+     * @see org.apache.ddlutils.Platform#createDatabase(java.lang.String, java.lang.String,
java.lang.String, java.lang.String, java.util.Map)
      */
-    public void createDatabase(String jdbcDriverClassName, String connectionUrl, String username,
String password) throws DynaSqlException, UnsupportedOperationException
+    public void createDatabase(String jdbcDriverClassName, String connectionUrl, String username,
String password, Map parameters) 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);
+        createOrDropDatabase(jdbcDriverClassName, connectionUrl, username, password, parameters,
true);
     }
 
     /* (non-Javadoc)
@@ -180,7 +200,7 @@
     {
         // 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);
+        createOrDropDatabase(jdbcDriverClassName, connectionUrl, username, password, null,
false);
     }
 
     

Modified: 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=289319&r1=289318&r2=289319&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/task/CreateDatabaseCommand.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/task/CreateDatabaseCommand.java Thu Sep
15 14:11:43 2005
@@ -16,6 +16,7 @@
  * limitations under the License.
  */
 
+import org.apache.commons.collections.map.ListOrderedMap;
 import org.apache.commons.dbcp.BasicDataSource;
 import org.apache.ddlutils.Platform;
 import org.apache.ddlutils.model.Database;
@@ -25,9 +26,25 @@
 
 /**
  * Command for creating a database.
+ * 
+ * @author <a href="mailto:tomdz@apache.org">Thomas Dudziak</a>
+ * @version $Revision: 231306 $
  */
 public class CreateDatabaseCommand extends DatabaseCommand
 {
+    /** The additional creation parameters */
+    private ListOrderedMap _parameters = new ListOrderedMap();
+
+    /**
+     * Adds a parameter which is a name-value pair.
+     * 
+     * @param param The parameter
+     */
+    public void addConfiguredParameter(NamedValue param)
+    {
+        _parameters.put(param.getName(), param.getValue());
+    }
+
     /* (non-Javadoc)
      * @see org.apache.ddlutils.task.Command#execute(org.apache.tools.ant.Task, org.apache.ddlutils.model.Database)
      */
@@ -47,7 +64,8 @@
             platform.createDatabase(dataSource.getDriverClassName(),
                                     dataSource.getUrl(),
                                     dataSource.getUsername(),
-                                    dataSource.getPassword());
+                                    dataSource.getPassword(),
+                                    _parameters);
 
             task.log("Created database", Project.MSG_INFO);
         }

Added: db/ddlutils/trunk/src/java/org/apache/ddlutils/task/NamedValue.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/task/NamedValue.java?rev=289319&view=auto
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/task/NamedValue.java (added)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/task/NamedValue.java Thu Sep 15 14:11:43
2005
@@ -0,0 +1,55 @@
+package org.apache.ddlutils.task;
+
+/**
+ * A name-value pair.
+ * 
+ * @author <a href="mailto:tomdz@apache.org">Thomas Dudziak</a>
+ * @version $Revision: 231306 $
+ */
+public class NamedValue
+{
+    /** The name */
+    private String _name;
+    /** The value */
+    private String _value;
+
+    /**
+     * Returns the name.
+     *
+     * @return The name
+     */
+    public String getName()
+    {
+        return _name;
+    }
+
+    /**
+     * Sets the name.
+     *
+     * @param name The name
+     */
+    public void setName(String name)
+    {
+        _name = name;
+    }
+
+    /**
+     * Returns the value.
+     *
+     * @return The value
+     */
+    public String getValue()
+    {
+        return _value;
+    }
+
+    /**
+     * Sets the value.
+     *
+     * @param value The value
+     */
+    public void setValue(String value)
+    {
+        _value = value;
+    }
+}



Mime
View raw message