You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ddlutils-dev@db.apache.org by to...@apache.org on 2005/08/13 23:39:40 UTC
svn commit: r232530 [3/4] - in /db/ddlutils/trunk/src:
java/org/apache/ddlutils/ java/org/apache/ddlutils/builder/
java/org/apache/ddlutils/dynabean/ java/org/apache/ddlutils/io/
java/org/apache/ddlutils/model/ java/org/apache/ddlutils/platform/ java/o...
Added: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/MaxDbPlatform.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/MaxDbPlatform.java?rev=232530&view=auto
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/MaxDbPlatform.java (added)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/MaxDbPlatform.java Sat Aug 13 14:36:44 2005
@@ -0,0 +1,41 @@
+package org.apache.ddlutils.platform;
+
+
+/*
+ * Copyright 1999-2002,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.
+ */
+
+/**
+ * The platform implementation for MaxDB. It is currently identical to the SapDB
+ * implementation as there is no difference in the functionality we're using.
+ * Note that DdlUtils is currently not able to distinguish them based on the
+ * jdbc driver or subprotocol as they are identical.
+ *
+ * @author <a href="mailto:tomdz@apache.org">Thomas Dudziak</a>
+ * @version $Revision: 231306 $
+ */
+public class MaxDbPlatform extends SapDbPlatform
+{
+ /** Database name of this platform */
+ public static final String DATABASENAME = "MaxDB";
+
+ /* (non-Javadoc)
+ * @see org.apache.ddlutils.builder.Platform#getDatabaseName()
+ */
+ public String getDatabaseName()
+ {
+ return DATABASENAME;
+ }
+}
Added: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/MckoiPlatform.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/MckoiPlatform.java?rev=232530&view=auto
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/MckoiPlatform.java (added)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/MckoiPlatform.java Sat Aug 13 14:36:44 2005
@@ -0,0 +1,71 @@
+package org.apache.ddlutils.platform;
+
+/*
+ * 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.sql.Types;
+
+import org.apache.ddlutils.PlatformInfo;
+import org.apache.ddlutils.builder.MckoiBuilder;
+
+/**
+ * The Mckoi database platform implementation.
+ *
+ * @author <a href="mailto:tomdz@apache.org">Thomas Dudziak</a>
+ * @version $Revision: 231306 $
+ */
+public class MckoiPlatform extends PlatformImplBase
+{
+ /** Database name of this platform */
+ public static final String DATABASENAME = "McKoi";
+ /** The standard McKoi jdbc driver */
+ public static final String JDBC_DRIVER = "com.mckoi.JDBCDriver";
+ /** The subprotocol used by the standard McKoi driver */
+ public static final String JDBC_SUBPROTOCOL = "mckoi";
+
+ /**
+ * Creates a new platform instance.
+ */
+ public MckoiPlatform()
+ {
+ PlatformInfo info = new PlatformInfo();
+
+ info.setRequiringNullAsDefaultValue(false);
+ info.setPrimaryKeyEmbedded(true);
+ info.setForeignKeysEmbedded(false);
+ info.setIndicesEmbedded(false);
+
+ info.addNativeTypeMapping(Types.ARRAY, "BLOB");
+ info.addNativeTypeMapping(Types.BIT, "BOOLEAN");
+ info.addNativeTypeMapping(Types.DISTINCT, "BLOB");
+ info.addNativeTypeMapping(Types.FLOAT, "DOUBLE");
+ info.addNativeTypeMapping(Types.NULL, "BLOB");
+ info.addNativeTypeMapping(Types.OTHER, "BLOB");
+ info.addNativeTypeMapping(Types.REF, "BLOB");
+ info.addNativeTypeMapping(Types.STRUCT, "BLOB");
+ info.addNativeTypeMapping("DATALINK", "BLOB");
+
+ setSqlBuilder(new MckoiBuilder(info));
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.ddlutils.builder.Platform#getDatabaseName()
+ */
+ public String getDatabaseName()
+ {
+ return DATABASENAME;
+ }
+}
Added: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/MySqlPlatform.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/MySqlPlatform.java?rev=232530&view=auto
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/MySqlPlatform.java (added)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/MySqlPlatform.java Sat Aug 13 14:36:44 2005
@@ -0,0 +1,85 @@
+package org.apache.ddlutils.platform;
+
+/*
+ * 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.sql.Types;
+
+import org.apache.ddlutils.PlatformInfo;
+import org.apache.ddlutils.builder.MySqlBuilder;
+
+/**
+ * The platform implementation for MySQL
+ *
+ * @author <a href="mailto:tomdz@apache.org">Thomas Dudziak</a>
+ * @version $Revision: 231306 $
+ */
+public class MySqlPlatform extends PlatformImplBase
+{
+ /** Database name of this platform */
+ public static final String DATABASENAME = "MySQL";
+ /** The standard MySQL jdbc driver */
+ public static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
+ /** The old MySQL jdbc driver */
+ public static final String JDBC_DRIVER_OLD = "org.gjt.mm.mysql.Driver";
+ /** The subprotocol used by the standard MySQL driver */
+ public static final String JDBC_SUBPROTOCOL = "mysql";
+
+ /**
+ * Creates a new platform instance.
+ */
+ public MySqlPlatform()
+ {
+ PlatformInfo info = new PlatformInfo();
+
+ info.setMaxIdentifierLength(64);
+ info.setRequiringNullAsDefaultValue(false);
+ info.setPrimaryKeyEmbedded(true);
+ info.setForeignKeysEmbedded(false);
+ info.setIndicesEmbedded(false);
+ info.setCommentPrefix("#");
+ // the BINARY types are also handled by MySqlBuilder.getSqlType(Column)
+ info.addNativeTypeMapping(Types.ARRAY, "LONGBLOB");
+ info.addNativeTypeMapping(Types.BINARY, "CHAR");
+ info.addNativeTypeMapping(Types.BIT, "TINYINT(1)");
+ info.addNativeTypeMapping(Types.BLOB, "LONGBLOB");
+ info.addNativeTypeMapping(Types.CLOB, "LONGTEXT");
+ info.addNativeTypeMapping(Types.DISTINCT, "LONGBLOB");
+ info.addNativeTypeMapping(Types.FLOAT, "DOUBLE");
+ info.addNativeTypeMapping(Types.JAVA_OBJECT, "LONGBLOB");
+ info.addNativeTypeMapping(Types.LONGVARBINARY, "MEDIUMBLOB");
+ info.addNativeTypeMapping(Types.LONGVARCHAR, "MEDIUMTEXT");
+ info.addNativeTypeMapping(Types.NULL, "MEDIUMBLOB");
+ info.addNativeTypeMapping(Types.NUMERIC, "DECIMAL");
+ info.addNativeTypeMapping(Types.OTHER, "LONGBLOB");
+ info.addNativeTypeMapping(Types.REAL, "FLOAT");
+ info.addNativeTypeMapping(Types.REF, "MEDIUMBLOB");
+ info.addNativeTypeMapping(Types.STRUCT, "LONGBLOB");
+ info.addNativeTypeMapping(Types.VARBINARY, "VARCHAR");
+ info.addNativeTypeMapping("BOOLEAN", "TINYINT(1)");
+ info.addNativeTypeMapping("DATALINK", "MEDIUMBLOB");
+
+ setSqlBuilder(new MySqlBuilder(info));
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.ddlutils.builder.Platform#getDatabaseName()
+ */
+ public String getDatabaseName()
+ {
+ return DATABASENAME;
+ }
+}
Added: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/Oracle8Platform.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/Oracle8Platform.java?rev=232530&view=auto
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/Oracle8Platform.java (added)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/Oracle8Platform.java Sat Aug 13 14:36:44 2005
@@ -0,0 +1,90 @@
+package org.apache.ddlutils.platform;
+
+/*
+ * 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.sql.Types;
+
+import org.apache.ddlutils.PlatformInfo;
+import org.apache.ddlutils.builder.OracleBuilder;
+
+/**
+ * The platform for Oracle 8.
+ *
+ * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
+ * @author <a href="mailto:tomdz@apache.org">Thomas Dudziak</a>
+ * @version $Revision: 231306 $
+ */
+public class Oracle8Platform extends PlatformImplBase
+{
+ /** Database name of this platform */
+ public static final String DATABASENAME = "Oracle";
+ /** The standard Oracle jdbc driver */
+ public static final String JDBC_DRIVER = "oracle.jdbc.driver.OracleDriver";
+ /** The subprotocol used by the standard Oracle driver */
+ public static final String JDBC_SUBPROTOCOL = "oracle";
+
+ /**
+ * Creates a new platform instance.
+ */
+ public Oracle8Platform()
+ {
+ PlatformInfo info = new PlatformInfo();
+
+ info.setMaxIdentifierLength(30);
+ info.setRequiringNullAsDefaultValue(false);
+ info.setPrimaryKeyEmbedded(true);
+ info.setForeignKeysEmbedded(false);
+ info.setIndicesEmbedded(false);
+
+ info.addNativeTypeMapping(Types.ARRAY, "BLOB");
+ info.addNativeTypeMapping(Types.BIGINT, "NUMBER(38,0)");
+ info.addNativeTypeMapping(Types.BINARY, "RAW");
+ info.addNativeTypeMapping(Types.BIT, "NUMBER(1,0)");
+ info.addNativeTypeMapping(Types.DECIMAL, "NUMBER");
+ info.addNativeTypeMapping(Types.DISTINCT, "BLOB");
+ info.addNativeTypeMapping(Types.DOUBLE, "NUMBER(38)");
+ info.addNativeTypeMapping(Types.FLOAT, "NUMBER(38)");
+ info.addNativeTypeMapping(Types.INTEGER, "NUMBER(20,0)");
+ info.addNativeTypeMapping(Types.JAVA_OBJECT, "BLOB");
+ info.addNativeTypeMapping(Types.LONGVARBINARY, "BLOB");
+ info.addNativeTypeMapping(Types.LONGVARCHAR, "CLOB");
+ info.addNativeTypeMapping(Types.NULL, "BLOB");
+ info.addNativeTypeMapping(Types.NUMERIC, "NUMBER");
+ info.addNativeTypeMapping(Types.OTHER, "BLOB");
+ info.addNativeTypeMapping(Types.REAL, "NUMBER(18)");
+ info.addNativeTypeMapping(Types.REF, "BLOB");
+ info.addNativeTypeMapping(Types.SMALLINT, "NUMBER(5,0)");
+ info.addNativeTypeMapping(Types.STRUCT, "BLOB");
+ info.addNativeTypeMapping(Types.TIME, "DATE");
+ info.addNativeTypeMapping(Types.TIMESTAMP, "DATE");
+ info.addNativeTypeMapping(Types.TINYINT, "NUMBER(3,0)");
+ info.addNativeTypeMapping(Types.VARBINARY, "RAW");
+ info.addNativeTypeMapping(Types.VARCHAR, "VARCHAR2");
+ info.addNativeTypeMapping("BOOLEAN", "NUMBER(1,0)");
+ info.addNativeTypeMapping("DATALINK", "BLOB");
+
+ setSqlBuilder(new OracleBuilder(info));
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.ddlutils.builder.Platform#getDatabaseName()
+ */
+ public String getDatabaseName()
+ {
+ return DATABASENAME;
+ }
+}
Added: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/Oracle9Platform.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/Oracle9Platform.java?rev=232530&view=auto
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/Oracle9Platform.java (added)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/Oracle9Platform.java Sat Aug 13 14:36:44 2005
@@ -0,0 +1,40 @@
+package org.apache.ddlutils.platform;
+
+/*
+ * 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.sql.Types;
+
+/**
+ * The platform for Oracle 9.
+ *
+ * @author <a href="mailto:tomdz@apache.org">Thomas Dudziak</a>
+ * @version $Revision: 231306 $
+ */
+public class Oracle9Platform extends Oracle8Platform
+{
+ /** Database name of this platform */
+ public static final String DATABASENAME = "Oracle9";
+
+ /**
+ * Creates a new platform instance.
+ */
+ public Oracle9Platform()
+ {
+ super();
+ getPlatformInfo().addNativeTypeMapping(Types.TIMESTAMP, "TIMESTAMP");
+ }
+}
Added: 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=232530&view=auto
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/PlatformImplBase.java (added)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/PlatformImplBase.java Sat Aug 13 14:36:44 2005
@@ -0,0 +1,1001 @@
+package org.apache.ddlutils.platform;
+
+/*
+ * 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.io.IOException;
+import java.io.StringWriter;
+import java.lang.reflect.InvocationTargetException;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.SQLWarning;
+import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.StringTokenizer;
+
+import org.apache.commons.beanutils.DynaBean;
+import org.apache.commons.beanutils.PropertyUtils;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.ddlutils.DynaSqlException;
+import org.apache.ddlutils.Platform;
+import org.apache.ddlutils.PlatformInfo;
+import org.apache.ddlutils.builder.SqlBuilder;
+import org.apache.ddlutils.dynabean.DynaSqlIterator;
+import org.apache.ddlutils.dynabean.SqlDynaClass;
+import org.apache.ddlutils.dynabean.SqlDynaProperty;
+import org.apache.ddlutils.io.JdbcModelReader;
+import org.apache.ddlutils.model.Column;
+import org.apache.ddlutils.model.Database;
+import org.apache.ddlutils.model.Table;
+import org.apache.ddlutils.util.JdbcSupport;
+
+/**
+ * Base class for platform implementations.
+ *
+ * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
+ * @author <a href="mailto:tomdz@apache.org">Thomas Dudziak</a>
+ * @version $Revision: 231110 $
+ */
+public abstract class PlatformImplBase extends JdbcSupport implements Platform
+{
+ /** The log for this platform */
+ private final Log _log = LogFactory.getLog(Platform.class);
+
+ /** The sql builder for this platform */
+ private SqlBuilder _builder;
+
+ /* (non-Javadoc)
+ * @see org.apache.ddlutils.Platform#getSqlBuilder()
+ */
+ public SqlBuilder getSqlBuilder()
+ {
+ return _builder;
+ }
+
+ /**
+ * Sets the sql builder for this platform.
+ *
+ * @param builder The sql builder
+ */
+ protected void setSqlBuilder(SqlBuilder builder)
+ {
+ _builder = builder;
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.ddlutils.Platform#getPlatformInfo()
+ */
+ public PlatformInfo getPlatformInfo()
+ {
+ return _builder.getPlatformInfo();
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.ddlutils.Platform#evaluateBatch(java.lang.String, boolean)
+ */
+ public int evaluateBatch(String sql, boolean continueOnError) throws DynaSqlException
+ {
+ Connection connection = borrowConnection();
+
+ try
+ {
+ return evaluateBatch(connection, sql, continueOnError);
+ }
+ finally
+ {
+ returnConnection(connection);
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.ddlutils.Platform#evaluateBatch(java.sql.Connection, java.lang.String, boolean)
+ */
+ public int evaluateBatch(Connection connection, String sql, boolean continueOnError) throws DynaSqlException
+ {
+ Statement statement = null;
+ int errors = 0;
+ int commandCount = 0;
+
+ try
+ {
+ statement = connection.createStatement();
+
+ StringTokenizer tokenizer = new StringTokenizer(sql, ";");
+
+ while (tokenizer.hasMoreTokens())
+ {
+ String command = tokenizer.nextToken();
+
+ // ignore whitespace
+ command = command.trim();
+ if (command.length() == 0)
+ {
+ continue;
+ }
+
+ commandCount++;
+
+ if (_log.isDebugEnabled())
+ {
+ _log.debug("About to execute SQL " + command);
+ }
+ try
+ {
+ int results = statement.executeUpdate(command);
+
+ if (_log.isDebugEnabled())
+ {
+ _log.debug("After execution, " + results + " row(s) have been changed");
+ }
+ }
+ catch (SQLException ex)
+ {
+ if (continueOnError)
+ {
+ System.err.println("SQL Command " + command + " failed with " + ex.getMessage());
+ errors++;
+ }
+ else
+ {
+ throw new DynaSqlException("Error while executing SQL "+command, ex);
+ }
+ }
+
+ // lets display any warnings
+ SQLWarning warning = connection.getWarnings();
+
+ while (warning != null)
+ {
+ _log.warn(warning.toString());
+ warning = warning.getNextWarning();
+ }
+ connection.clearWarnings();
+ }
+ _log.info("Executed "+ commandCount + " SQL command(s) with " + errors + " error(s)");
+ }
+ catch (SQLException ex)
+ {
+ throw new DynaSqlException("Error while executing SQL", ex);
+ }
+ finally
+ {
+ closeStatement(statement);
+ }
+
+ return errors;
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.ddlutils.Platform#createTables(org.apache.ddlutils.model.Database, boolean, boolean)
+ */
+ public void createTables(Database model, boolean dropTablesFirst, boolean continueOnError) throws DynaSqlException
+ {
+ Connection connection = borrowConnection();
+
+ try
+ {
+ createTables(connection, model, dropTablesFirst, continueOnError);
+ }
+ finally
+ {
+ returnConnection(connection);
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.ddlutils.Platform#createTables(java.sql.Connection, org.apache.ddlutils.model.Database, boolean, boolean)
+ */
+ public void createTables(Connection connection, Database model, boolean dropTablesFirst, boolean continueOnError) throws DynaSqlException
+ {
+ String sql = null;
+
+ try
+ {
+ StringWriter buffer = new StringWriter();
+
+ getSqlBuilder().setWriter(buffer);
+ getSqlBuilder().createTables(model, dropTablesFirst);
+ sql = buffer.toString();
+ }
+ catch (IOException e)
+ {
+ // won't happen because we're using a string writer
+ }
+ evaluateBatch(connection, sql, continueOnError);
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.ddlutils.Platform#alterTables(org.apache.ddlutils.model.Database, boolean)
+ */
+ public void alterTables(Database desiredDb, boolean continueOnError) throws DynaSqlException
+ {
+ alterTables(desiredDb, false, false, continueOnError);
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.ddlutils.Platform#alterTables(org.apache.ddlutils.model.Database, boolean, boolean, boolean)
+ */
+ public void alterTables(Database desiredDb, boolean doDrops, boolean modifyColumns, boolean continueOnError) throws DynaSqlException
+ {
+ Connection connection = borrowConnection();
+
+ try
+ {
+ alterTables(connection, desiredDb, doDrops, modifyColumns, continueOnError);
+ }
+ finally
+ {
+ returnConnection(connection);
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.ddlutils.Platform#alterTables(java.sql.Connection, org.apache.ddlutils.model.Database, boolean, boolean, boolean)
+ */
+ public void alterTables(Connection connection, Database desiredModel, boolean doDrops, boolean modifyColumns, boolean continueOnError) throws DynaSqlException
+ {
+ String sql = null;
+ Database currentModel = null;
+
+ try
+ {
+ currentModel = new JdbcModelReader(connection).getDatabase();
+ }
+ catch (SQLException ex)
+ {
+ throw new DynaSqlException("Error while reading the model from the database", ex);
+ }
+
+ try
+ {
+ StringWriter buffer = new StringWriter();
+
+ getSqlBuilder().setWriter(buffer);
+ getSqlBuilder().alterDatabase(currentModel, desiredModel, doDrops, modifyColumns);
+ sql = buffer.toString();
+ }
+ catch (IOException ex)
+ {
+ // won't happen because we're using a string writer
+ }
+ evaluateBatch(connection, sql, continueOnError);
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.ddlutils.Platform#alterTables(java.sql.Connection, org.apache.ddlutils.model.Database, boolean)
+ */
+ public void alterTables(Connection connection, Database desiredDb, boolean continueOnError) throws DynaSqlException
+ {
+ alterTables(connection, desiredDb, false, false, continueOnError);
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.ddlutils.Platform#dropTables(org.apache.ddlutils.model.Database, boolean)
+ */
+ public void dropTables(Database model, boolean continueOnError) throws DynaSqlException
+ {
+ Connection connection = borrowConnection();
+
+ try
+ {
+ dropTables(connection, model, continueOnError);
+ }
+ finally
+ {
+ returnConnection(connection);
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.ddlutils.Platform#dropTables(java.sql.Connection, org.apache.ddlutils.model.Database, boolean)
+ */
+ public void dropTables(Connection connection, Database model, boolean continueOnError) throws DynaSqlException
+ {
+ String sql = null;
+
+ try
+ {
+ StringWriter buffer = new StringWriter();
+
+ getSqlBuilder().setWriter(buffer);
+ getSqlBuilder().dropTables(model);
+ sql = buffer.toString();
+ }
+ catch (IOException e)
+ {
+ // won't happen because we're using a string writer
+ }
+ evaluateBatch(connection, sql, continueOnError);
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.ddlutils.Platform#query(org.apache.ddlutils.model.Database, java.lang.String)
+ */
+ public Iterator query(Database model, String sql) throws DynaSqlException
+ {
+ Connection connection = borrowConnection();
+ Statement statement = null;
+ ResultSet resultSet = null;
+ Iterator answer = null;
+
+ try
+ {
+ statement = connection.createStatement();
+ resultSet = statement.executeQuery(sql);
+ answer = createResultSetIterator(model, resultSet);
+ return answer;
+ }
+ catch (SQLException ex)
+ {
+ throw new DynaSqlException("Error while performing a query", ex);
+ }
+ finally
+ {
+ // if any exceptions are thrown, close things down
+ // otherwise we're leaving it open for the iterator
+ if (answer == null)
+ {
+ closeStatement(statement);
+ returnConnection(connection);
+ }
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.ddlutils.Platform#query(org.apache.ddlutils.model.Database, java.lang.String, java.util.Collection)
+ */
+ public Iterator query(Database model, String sql, Collection parameters) throws DynaSqlException
+ {
+ Connection connection = borrowConnection();
+ PreparedStatement statement = null;
+ ResultSet resultSet = null;
+ Iterator answer = null;
+
+ try
+ {
+ statement = connection.prepareStatement(sql);
+
+ int paramIdx = 1;
+
+ for (Iterator iter = parameters.iterator(); iter.hasNext(); paramIdx++)
+ {
+ statement.setObject(paramIdx, iter.next());
+ }
+ resultSet = statement.executeQuery();
+ answer = createResultSetIterator(model, resultSet);
+ return answer;
+ }
+ catch (SQLException ex)
+ {
+ throw new DynaSqlException("Error while performing a query", ex);
+ }
+ finally
+ {
+ // if any exceptions are thrown, close things down
+ // otherwise we're leaving it open for the iterator
+ if (answer == null)
+ {
+ closeStatement(statement);
+ returnConnection(connection);
+ }
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.ddlutils.Platform#fetch(org.apache.ddlutils.model.Database, java.lang.String)
+ */
+ public List fetch(Database model, String sql) throws DynaSqlException
+ {
+ return fetch(model, sql, 0, -1);
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.ddlutils.Platform#fetch(org.apache.ddlutils.model.Database, java.lang.String, int, int)
+ */
+ public List fetch(Database model, String sql, int start, int end) throws DynaSqlException
+ {
+ Connection connection = borrowConnection();
+ Statement statement = null;
+ ResultSet resultSet = null;
+ List result = new ArrayList();
+
+ try
+ {
+ statement = connection.createStatement();
+ resultSet = statement.executeQuery(sql);
+
+ int rowIdx = 0;
+
+ for (Iterator it = createResultSetIterator(model, resultSet); ((end < 0) || (rowIdx <= end)) && it.hasNext(); rowIdx++)
+ {
+ if (rowIdx >= start)
+ {
+ result.add(it.next());
+ }
+ }
+ }
+ catch (SQLException ex)
+ {
+ // any other exception comes from the iterator which closes the resources automatically
+ closeStatement(statement);
+ returnConnection(connection);
+ throw new DynaSqlException("Error while fetching data from the database", ex);
+ }
+ return result;
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.ddlutils.Platform#fetch(org.apache.ddlutils.model.Database, java.lang.String, java.util.Collection)
+ */
+ public List fetch(Database model, String sql, Collection parameters) throws DynaSqlException
+ {
+ return fetch(model, sql, parameters, 0, -1);
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.ddlutils.Platform#fetch(org.apache.ddlutils.model.Database, java.lang.String, java.util.Collection, int, int)
+ */
+ public List fetch(Database model, String sql, Collection parameters, int start, int end) throws DynaSqlException
+ {
+ Connection connection = borrowConnection();
+ PreparedStatement statement = null;
+ ResultSet resultSet = null;
+ List result = new ArrayList();
+
+ try
+ {
+ statement = connection.prepareStatement(sql);
+
+ int paramIdx = 1;
+
+ for (Iterator iter = parameters.iterator(); iter.hasNext(); paramIdx++)
+ {
+ statement.setObject(paramIdx, iter.next());
+ }
+ resultSet = statement.executeQuery();
+
+ int rowIdx = 0;
+
+ for (Iterator it = createResultSetIterator(model, resultSet); ((end < 0) || (rowIdx <= end)) && it.hasNext(); rowIdx++)
+ {
+ if (rowIdx >= start)
+ {
+ result.add(it.next());
+ }
+ }
+ }
+ catch (SQLException ex)
+ {
+ // any other exception comes from the iterator which closes the resources automatically
+ closeStatement(statement);
+ returnConnection(connection);
+ throw new DynaSqlException("Error while fetching data from the database", ex);
+ }
+ return result;
+ }
+
+ /**
+ * Creates the SQL for inserting an object of the given type. If a concrete bean is given,
+ * then a concrete insert statement is created, otherwise an insert statement usable in a
+ * prepared statement is build.
+ *
+ * @param model The database model
+ * @param dynaClass The type
+ * @param properties The properties to write
+ * @param bean Optionally the concrete bean to insert
+ * @return The SQL required to insert an instance of the class
+ */
+ protected String createInsertSql(Database model, SqlDynaClass dynaClass, SqlDynaProperty[] properties, DynaBean bean)
+ {
+ Table table = model.findTable(dynaClass.getTableName());
+ HashMap columnValues = toColumnValues(properties, bean);
+
+ return _builder.getInsertSql(table, columnValues, bean == null);
+ }
+
+ /**
+ * Creates the SQL for querying for the id generated by the last insert of an object of the given type.
+ *
+ * @param model The database model
+ * @param dynaClass The type
+ * @return The SQL required for querying for the id, or <code>null</code> if the database does not
+ * support this
+ */
+ protected String createSelectLastInsertIdSql(Database model, SqlDynaClass dynaClass)
+ {
+ Table table = model.findTable(dynaClass.getTableName());
+
+ return _builder.getSelectLastInsertId(table);
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.ddlutils.Platform#getInsertSql(org.apache.ddlutils.model.Database, org.apache.commons.beanutils.DynaBean)
+ */
+ public String getInsertSql(Database model, DynaBean dynaBean)
+ {
+ SqlDynaClass dynaClass = model.getDynaClassFor(dynaBean);
+ SqlDynaProperty[] properties = dynaClass.getSqlDynaProperties();
+
+ if (properties.length == 0)
+ {
+ _log.info("Cannot insert instances of type " + dynaClass + " because it has no properties");
+ return null;
+ }
+
+ return createInsertSql(model, dynaClass, properties, dynaBean);
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.ddlutils.Platform#insert(org.apache.ddlutils.model.Database, org.apache.commons.beanutils.DynaBean, java.sql.Connection)
+ */
+ public void insert(Database model, DynaBean dynaBean, Connection connection) throws DynaSqlException
+ {
+ SqlDynaClass dynaClass = model.getDynaClassFor(dynaBean);
+ SqlDynaProperty[] properties = dynaClass.getSqlDynaProperties();
+
+ if (properties.length == 0)
+ {
+ _log.warn("Cannot insert instances of type " + dynaClass + " because it has no properties");
+ return;
+ }
+
+ Column autoIdColumn = model.findTable(dynaClass.getTableName()).getAutoIncrementColumn();
+
+ if (autoIdColumn != null)
+ {
+ // We have to remove the auto-increment column as some databases won't like
+ // it being present in the insert command
+ SqlDynaProperty[] newProperties = new SqlDynaProperty[properties.length - 1];
+ int newIdx = 0;
+
+ for (int idx = 0; idx < properties.length; idx++)
+ {
+ if (properties[idx].getColumn() != autoIdColumn)
+ {
+ newProperties[newIdx++] = properties[idx];
+ }
+ }
+ properties = newProperties;
+ }
+
+ String insertSql = createInsertSql(model, dynaClass, properties, null);
+ String queryIdSql = autoIdColumn != null ? createSelectLastInsertIdSql(model, dynaClass) : null;
+ PreparedStatement statement = null;
+
+ if (_log.isDebugEnabled())
+ {
+ _log.debug("About to execute SQL: " + insertSql);
+ }
+ if ((autoIdColumn != null) && (queryIdSql == null))
+ {
+ _log.warn("The database does not support querying for auto-generated pk values");
+ }
+
+ try
+ {
+ statement = connection.prepareStatement(insertSql);
+
+ for (int idx = 0; idx < properties.length; idx++ )
+ {
+ setObject(statement, idx + 1, dynaBean, properties[idx]);
+ }
+
+ int count = statement.executeUpdate();
+
+ if (count != 1)
+ {
+ _log.warn("Attempted to insert a single row " + dynaBean +
+ " in table " + dynaClass.getTableName() +
+ " but changed " + count + " row(s)");
+ }
+ }
+ catch (SQLException ex)
+ {
+ throw new DynaSqlException("Error while inserting into the database", ex);
+ }
+ finally
+ {
+ closeStatement(statement);
+ }
+ if (queryIdSql != null)
+ {
+ Statement queryStmt = null;
+ ResultSet lastInsertedIds = null;
+
+ try
+ {
+ // we'll have to commit the statement(s) because otherwise most likely
+ // the auto increment hasn't happened yet (the db didn't actually
+ // perform the insert yet so no triggering of sequences did occur)
+ connection.commit();
+
+ queryStmt = connection.createStatement();
+ lastInsertedIds = queryStmt.executeQuery(queryIdSql);
+
+ lastInsertedIds.next();
+
+ Object value = lastInsertedIds.getObject(autoIdColumn.getName());
+
+ PropertyUtils.setProperty(dynaBean, autoIdColumn.getName(), value);
+ }
+ catch (NoSuchMethodException ex)
+ {
+ // Can't happen because we're using dyna beans
+ }
+ catch (IllegalAccessException ex)
+ {
+ // Can't happen because we're using dyna beans
+ }
+ catch (InvocationTargetException ex)
+ {
+ // Can't happen because we're using dyna beans
+ }
+ catch (SQLException ex)
+ {
+ throw new DynaSqlException("Error while retrieving the auto-generated primary key from the database", ex);
+ }
+ finally
+ {
+ if (lastInsertedIds != null)
+ {
+ try
+ {
+ lastInsertedIds.close();
+ }
+ catch (SQLException ex)
+ {
+ // we ignore this one
+ }
+ }
+ closeStatement(statement);
+ }
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.ddlutils.Platform#insert(org.apache.ddlutils.model.Database, org.apache.commons.beanutils.DynaBean)
+ */
+ public void insert(Database model, DynaBean dynaBean) throws DynaSqlException
+ {
+ Connection connection = borrowConnection();
+
+ try
+ {
+ insert(model, dynaBean, connection);
+ }
+ finally
+ {
+ returnConnection(connection);
+ }
+ }
+
+ /**
+ * Creates the SQL for updating an object of the given type. If a concrete bean is given,
+ * then a concrete update statement is created, otherwise an update statement usable in a
+ * prepared statement is build.
+ *
+ * @param model The database model
+ * @param dynaClass The type
+ * @param primaryKeys The primary keys
+ * @param properties The properties to write
+ * @param bean Optionally the concrete bean to update
+ * @return The SQL required to update the instance
+ */
+ protected String createUpdateSql(Database model, SqlDynaClass dynaClass, SqlDynaProperty[] primaryKeys, SqlDynaProperty[] properties, DynaBean bean)
+ {
+ Table table = model.findTable(dynaClass.getTableName());
+ HashMap columnValues = toColumnValues(properties, bean);
+
+ return _builder.getUpdateSql(table, columnValues, bean == null);
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.ddlutils.Platform#getUpdateSql(org.apache.ddlutils.model.Database, org.apache.commons.beanutils.DynaBean)
+ */
+ public String getUpdateSql(Database model, DynaBean dynaBean)
+ {
+ SqlDynaClass dynaClass = model.getDynaClassFor(dynaBean);
+ SqlDynaProperty[] primaryKeys = dynaClass.getPrimaryKeyProperties();
+
+ if (primaryKeys.length == 0)
+ {
+ _log.info("Cannot update instances of type " + dynaClass + " because it has no primary keys");
+ return null;
+ }
+
+ return createUpdateSql(model, dynaClass, primaryKeys, dynaClass.getNonPrimaryKeyProperties(), dynaBean);
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.ddlutils.Platform#update(org.apache.ddlutils.model.Database, org.apache.commons.beanutils.DynaBean, java.sql.Connection)
+ */
+ public void update(Database model, DynaBean dynaBean, Connection connection) throws DynaSqlException
+ {
+ SqlDynaClass dynaClass = model.getDynaClassFor(dynaBean);
+ SqlDynaProperty[] primaryKeys = dynaClass.getPrimaryKeyProperties();
+
+ if (primaryKeys.length == 0)
+ {
+ _log.info("Cannot update instances of type " + dynaClass + " because it has no primary keys");
+ return;
+ }
+
+ SqlDynaProperty[] properties = dynaClass.getNonPrimaryKeyProperties();
+ String sql = createUpdateSql(model, dynaClass, primaryKeys, properties, null);
+ PreparedStatement statement = null;
+
+ if (_log.isDebugEnabled())
+ {
+ _log.debug("About to execute SQL: " + sql);
+ }
+ try
+ {
+ statement = connection.prepareStatement(sql);
+
+ int sqlIndex = 1;
+
+ for (int idx = 0; idx < properties.length; idx++)
+ {
+ setObject(statement, sqlIndex++, dynaBean, properties[idx]);
+ }
+ for (int idx = 0; idx < properties.length; idx++)
+ {
+ setObject(statement, sqlIndex++, dynaBean, primaryKeys[idx]);
+ }
+
+ int count = statement.executeUpdate();
+
+ if (count != 1)
+ {
+ _log.warn("Attempted to insert a single row " + dynaBean +
+ " into table " + dynaClass.getTableName() +
+ " but changed " + count + " row(s)");
+ }
+ }
+ catch (SQLException ex)
+ {
+ throw new DynaSqlException("Error while updating in the database", ex);
+ }
+ finally
+ {
+ closeStatement(statement);
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.ddlutils.Platform#update(org.apache.ddlutils.model.Database, org.apache.commons.beanutils.DynaBean)
+ */
+ public void update(Database model, DynaBean dynaBean) throws DynaSqlException
+ {
+ Connection connection = borrowConnection();
+
+ try
+ {
+ update(model, dynaBean, connection);
+ }
+ finally
+ {
+ returnConnection(connection);
+ }
+ }
+
+ /**
+ * Determines whether the given dyna bean is stored in the database.
+ *
+ * @param dynaBean The bean
+ * @param connection The connection
+ * @return <code>true</code> if this dyna bean has a primary key
+ */
+ protected boolean exists(DynaBean dynaBean, Connection connection)
+ {
+ // TODO: check for the pk value, and if present, query against database
+ return false;
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.ddlutils.Platform#store(org.apache.ddlutils.model.Database, org.apache.commons.beanutils.DynaBean)
+ */
+ public void store(Database model, DynaBean dynaBean) throws DynaSqlException
+ {
+ Connection connection = borrowConnection();
+
+ try
+ {
+ if (exists(dynaBean, connection))
+ {
+ update(model, dynaBean, connection);
+ }
+ else
+ {
+ insert(model, dynaBean, connection);
+ }
+ }
+ finally
+ {
+ returnConnection(connection);
+ }
+ }
+
+ /**
+ * Creates the SQL for deleting an object of the given type. If a concrete bean is given,
+ * then a concrete delete statement is created, otherwise a delete statement usable in a
+ * prepared statement is build.
+ *
+ * @param model The database model
+ * @param dynaClass The type
+ * @param primaryKeys The primary keys
+ * @param bean Optionally the concrete bean to update
+ * @return The SQL required to delete the instance
+ */
+ protected String createDeleteSql(Database model, SqlDynaClass dynaClass, SqlDynaProperty[] primaryKeys, DynaBean bean)
+ {
+ Table table = model.findTable(dynaClass.getTableName());
+ HashMap pkValues = toColumnValues(primaryKeys, bean);
+
+ return _builder.getDeleteSql(table, pkValues, bean == null);
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.ddlutils.Platform#getDeleteSql(org.apache.ddlutils.model.Database, org.apache.commons.beanutils.DynaBean)
+ */
+ public String getDeleteSql(Database model, DynaBean dynaBean)
+ {
+ SqlDynaClass dynaClass = model.getDynaClassFor(dynaBean);
+ SqlDynaProperty[] primaryKeys = dynaClass.getPrimaryKeyProperties();
+
+ if (primaryKeys.length == 0)
+ {
+ _log.warn("Cannot delete instances of type " + dynaClass + " because it has no primary keys");
+ return null;
+ }
+ else
+ {
+ return createDeleteSql(model, dynaClass, primaryKeys, dynaBean);
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.ddlutils.Platform#delete(org.apache.ddlutils.model.Database, org.apache.commons.beanutils.DynaBean)
+ */
+ public void delete(Database model, DynaBean dynaBean) throws DynaSqlException
+ {
+ Connection connection = borrowConnection();
+
+ try
+ {
+ delete(model, dynaBean, connection);
+ }
+ finally
+ {
+ returnConnection(connection);
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.ddlutils.Platform#delete(org.apache.ddlutils.model.Database, org.apache.commons.beanutils.DynaBean, java.sql.Connection)
+ */
+ public void delete(Database model, DynaBean dynaBean, Connection connection) throws DynaSqlException
+ {
+ PreparedStatement statement = null;
+
+ try
+ {
+ SqlDynaClass dynaClass = model.getDynaClassFor(dynaBean);
+ SqlDynaProperty[] primaryKeys = dynaClass.getPrimaryKeyProperties();
+
+ if (primaryKeys.length == 0)
+ {
+ _log.warn("Cannot delete instances of type " + dynaClass + " because it has no primary keys");
+ return;
+ }
+
+ String sql = createDeleteSql(model, dynaClass, primaryKeys, null);
+
+ if (_log.isDebugEnabled())
+ {
+ _log.debug("About to execute SQL " + sql);
+ }
+
+ statement = connection.prepareStatement(sql);
+
+ for (int idx = 0; idx < primaryKeys.length; idx++)
+ {
+ setObject(statement, idx + 1, dynaBean, primaryKeys[idx]);
+ }
+
+ int count = statement.executeUpdate();
+
+ if (count != 1)
+ {
+ _log.warn("Attempted to delete a single row " + dynaBean +
+ " in table " + dynaClass.getTableName() +
+ " but changed " + count + " row(s).");
+ }
+ }
+ catch (SQLException ex)
+ {
+ throw new DynaSqlException("Error while deleting from the database", ex);
+ }
+ finally
+ {
+ closeStatement(statement);
+ }
+ }
+
+ /**
+ * Derives the column values for the given dyna properties from the dyna bean.
+ *
+ * @param properties The properties
+ * @param bean The bean
+ * @return The values indexed by the column names
+ */
+ protected HashMap toColumnValues(SqlDynaProperty[] properties, DynaBean bean)
+ {
+ HashMap result = new HashMap();
+
+ for (int idx = 0; idx < properties.length; idx++)
+ {
+ result.put(properties[idx].getName(),
+ bean == null ? null : bean.get(properties[idx].getName()));
+ }
+ return result;
+ }
+
+ /**
+ * Sets a parameter of the prepared statement based on the type of the column of the property.
+ *
+ * @param statement The statement
+ * @param sqlIndex The index of the parameter to set in the statement
+ * @param dynaBean The bean of which to take the value
+ * @param property The property of the bean, which also defines the corresponding column
+ */
+ protected void setObject(PreparedStatement statement, int sqlIndex, DynaBean dynaBean, SqlDynaProperty property) throws SQLException
+ {
+ int typeCode = property.getColumn().getTypeCode();
+ Object value = dynaBean.get(property.getName());
+
+ if (value == null)
+ {
+ statement.setNull(sqlIndex, typeCode);
+ }
+ else if (value instanceof String)
+ {
+ statement.setString(sqlIndex, (String)value);
+ }
+ else
+ {
+ statement.setObject(sqlIndex, value, typeCode);
+ }
+ }
+
+ /**
+ * Creates an iterator over the given result set.
+ *
+ * @param model The database model
+ * @param resultSet The result set to iterate over
+ */
+ protected DynaSqlIterator createResultSetIterator(Database model, ResultSet resultSet)
+ {
+ return new DynaSqlIterator(getPlatformInfo(), model, resultSet, true);
+ }
+}
Added: 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=232530&view=auto
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/PostgreSqlPlatform.java (added)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/PostgreSqlPlatform.java Sat Aug 13 14:36:44 2005
@@ -0,0 +1,83 @@
+package org.apache.ddlutils.platform;
+
+/*
+ * 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.sql.Types;
+
+import org.apache.ddlutils.PlatformInfo;
+import org.apache.ddlutils.builder.PostgreSqlBuilder;
+
+/**
+ * The platform implementation for PostgresSql.
+ *
+ * @author <a href="mailto:tomdz@apache.org">Thomas Dudziak</a>
+ * @version $Revision: 231306 $
+ */
+public class PostgreSqlPlatform extends PlatformImplBase
+{
+ /** Database name of this platform */
+ public static final String DATABASENAME = "PostgreSql";
+ /** The standard PostgreSQL jdbc driver */
+ public static final String JDBC_DRIVER = "org.postgresql.Driver";
+ /** The subprotocol used by the standard PostgreSQL driver */
+ public static final String JDBC_SUBPROTOCOL = "postgresql";
+
+ /**
+ * Creates a new platform instance.
+ */
+ public PostgreSqlPlatform()
+ {
+ PlatformInfo info = new PlatformInfo();
+
+ // this is the default length though it might be changed when building PostgreSQL
+ // in file src/include/postgres_ext.h
+ info.setMaxIdentifierLength(31);
+ info.setRequiringNullAsDefaultValue(false);
+ info.setPrimaryKeyEmbedded(true);
+ info.setForeignKeysEmbedded(false);
+ info.setIndicesEmbedded(false);
+ info.addNativeTypeMapping(Types.ARRAY, "BYTEA");
+ info.addNativeTypeMapping(Types.BINARY, "BYTEA");
+ info.addNativeTypeMapping(Types.BIT, "BOOLEAN");
+ info.addNativeTypeMapping(Types.BLOB, "BYTEA");
+ info.addNativeTypeMapping(Types.CLOB, "TEXT");
+ info.addNativeTypeMapping(Types.DECIMAL, "NUMERIC");
+ info.addNativeTypeMapping(Types.DISTINCT, "BYTEA");
+ info.addNativeTypeMapping(Types.DOUBLE, "DOUBLE PRECISION");
+ info.addNativeTypeMapping(Types.FLOAT, "DOUBLE PRECISION");
+ info.addNativeTypeMapping(Types.JAVA_OBJECT, "BYTEA");
+ info.addNativeTypeMapping(Types.LONGVARBINARY, "BYTEA");
+ info.addNativeTypeMapping(Types.LONGVARCHAR, "TEXT");
+ info.addNativeTypeMapping(Types.NULL, "BYTEA");
+ info.addNativeTypeMapping(Types.OTHER, "BYTEA");
+ info.addNativeTypeMapping(Types.REF, "BYTEA");
+ info.addNativeTypeMapping(Types.STRUCT, "BYTEA");
+ info.addNativeTypeMapping(Types.TINYINT, "SMALLINT");
+ info.addNativeTypeMapping(Types.VARBINARY, "BYTEA");
+ info.addNativeTypeMapping("DATALINK", "BYTEA");
+
+ setSqlBuilder(new PostgreSqlBuilder(info));
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.ddlutils.builder.Platform#getDatabaseName()
+ */
+ public String getDatabaseName()
+ {
+ return DATABASENAME;
+ }
+}
Added: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/SapDbPlatform.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/SapDbPlatform.java?rev=232530&view=auto
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/SapDbPlatform.java (added)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/SapDbPlatform.java Sat Aug 13 14:36:44 2005
@@ -0,0 +1,85 @@
+package org.apache.ddlutils.platform;
+
+/*
+ * Copyright 1999-2002,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.sql.Types;
+
+import org.apache.ddlutils.PlatformInfo;
+import org.apache.ddlutils.builder.SapDbBuilder;
+
+/**
+ * The SapDB platform implementation.
+ *
+ * @author <a href="mailto:tomdz@apache.org">Thomas Dudziak</a>
+ * @version $Revision: 231306 $
+ */
+public class SapDbPlatform extends PlatformImplBase
+{
+ /** Database name of this platform */
+ public static final String DATABASENAME = "SapDB";
+ /** The standard SapDB/MaxDB jdbc driver */
+ public static final String JDBC_DRIVER = "com.sap.dbtech.jdbc.DriverSapDB";
+ /** The subprotocol used by the standard SapDB/MaxDB driver */
+ public static final String JDBC_SUBPROTOCOL = "sapdb";
+
+ /**
+ * Creates a new platform instance.
+ */
+ public SapDbPlatform()
+ {
+ PlatformInfo info = new PlatformInfo();
+
+ info.setMaxIdentifierLength(32);
+ info.setRequiringNullAsDefaultValue(false);
+ info.setPrimaryKeyEmbedded(true);
+ info.setForeignKeysEmbedded(false);
+ info.setIndicesEmbedded(false);
+ info.setCommentPrefix("/*");
+ info.setCommentSuffix("*/");
+
+ info.addNativeTypeMapping(Types.ARRAY, "LONG BYTE");
+ info.addNativeTypeMapping(Types.BIGINT, "FIXED(38,0)");
+ info.addNativeTypeMapping(Types.BINARY, "LONG BYTE");
+ info.addNativeTypeMapping(Types.BIT, "BOOLEAN");
+ info.addNativeTypeMapping(Types.BLOB, "LONG BYTE");
+ info.addNativeTypeMapping(Types.CLOB, "LONG");
+ info.addNativeTypeMapping(Types.DISTINCT, "LONG BYTE");
+ info.addNativeTypeMapping(Types.DOUBLE, "DOUBLE PRECISION");
+ info.addNativeTypeMapping(Types.FLOAT, "DOUBLE PRECISION");
+ info.addNativeTypeMapping(Types.JAVA_OBJECT, "LONG BYTE");
+ info.addNativeTypeMapping(Types.LONGVARBINARY, "LONG BYTE");
+ info.addNativeTypeMapping(Types.LONGVARCHAR, "LONG VARCHAR");
+ info.addNativeTypeMapping(Types.NULL, "LONG BYTE");
+ info.addNativeTypeMapping(Types.NUMERIC, "DECIMAL");
+ info.addNativeTypeMapping(Types.OTHER, "LONG BYTE");
+ info.addNativeTypeMapping(Types.REF, "LONG BYTE");
+ info.addNativeTypeMapping(Types.STRUCT, "LONG BYTE");
+ info.addNativeTypeMapping(Types.TINYINT, "SMALLINT");
+ info.addNativeTypeMapping(Types.VARBINARY, "LONG BYTE");
+ info.addNativeTypeMapping("DATALINK", "LONG BYTE");
+
+ setSqlBuilder(new SapDbBuilder(info));
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.ddlutils.builder.Platform#getDatabaseName()
+ */
+ public String getDatabaseName()
+ {
+ return DATABASENAME;
+ }
+}
Added: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/SybasePlatform.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/SybasePlatform.java?rev=232530&view=auto
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/SybasePlatform.java (added)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/SybasePlatform.java Sat Aug 13 14:36:44 2005
@@ -0,0 +1,88 @@
+package org.apache.ddlutils.platform;
+
+/*
+ * 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.sql.Types;
+
+import org.apache.ddlutils.PlatformInfo;
+import org.apache.ddlutils.builder.SybaseBuilder;
+
+/**
+ * The platform implementation for Sybase.
+ *
+ * @author <a href="mailto:tomdz@apache.org">Thomas Dudziak</a>
+ * @version $Revision: 231306 $
+ */
+public class SybasePlatform extends PlatformImplBase
+{
+ /** Database name of this platform */
+ public static final String DATABASENAME = "Sybase";
+ /** The standard Sybase jdbc driver */
+ public static final String JDBC_DRIVER = "com.sybase.jdbc2.jdbc.SybDriver";
+ /** The old Sybase jdbc driver */
+ public static final String JDBC_DRIVER_OLD = "com.sybase.jdbc.SybDriver";
+ /** The subprotocol used by the standard Sybase driver */
+ public static final String JDBC_SUBPROTOCOL = "sybase:Tds";
+
+ /**
+ * Creates a new platform instance.
+ */
+ public SybasePlatform()
+ {
+ PlatformInfo info = new PlatformInfo();
+
+ info.setMaxIdentifierLength(30);
+ info.setRequiringNullAsDefaultValue(false);
+ info.setPrimaryKeyEmbedded(true);
+ info.setForeignKeysEmbedded(false);
+ info.setIndicesEmbedded(false);
+ info.setCommentPrefix("/*");
+ info.setCommentSuffix("*/");
+
+ info.addNativeTypeMapping(Types.ARRAY, "IMAGE");
+ info.addNativeTypeMapping(Types.BIGINT, "DECIMAL(19,0)");
+ info.addNativeTypeMapping(Types.BLOB, "IMAGE");
+ info.addNativeTypeMapping(Types.CLOB, "TEXT");
+ info.addNativeTypeMapping(Types.DATE, "DATETIME");
+ info.addNativeTypeMapping(Types.DISTINCT, "IMAGE");
+ info.addNativeTypeMapping(Types.DOUBLE, "DOUBLE PRECISION");
+ info.addNativeTypeMapping(Types.FLOAT, "DOUBLE PRECISION");
+ info.addNativeTypeMapping(Types.INTEGER, "INT");
+ info.addNativeTypeMapping(Types.JAVA_OBJECT, "IMAGE");
+ info.addNativeTypeMapping(Types.LONGVARBINARY, "IMAGE");
+ info.addNativeTypeMapping(Types.LONGVARCHAR, "TEXT");
+ info.addNativeTypeMapping(Types.NULL, "IMAGE");
+ info.addNativeTypeMapping(Types.OTHER, "IMAGE");
+ info.addNativeTypeMapping(Types.REF, "IMAGE");
+ info.addNativeTypeMapping(Types.STRUCT, "IMAGE");
+ info.addNativeTypeMapping(Types.TIME, "DATETIME");
+ info.addNativeTypeMapping(Types.TIMESTAMP, "DATETIME");
+ info.addNativeTypeMapping(Types.TINYINT, "SMALLINT");
+ info.addNativeTypeMapping("BOOLEAN", "BIT");
+ info.addNativeTypeMapping("DATALINK", "IMAGE");
+
+ setSqlBuilder(new SybaseBuilder(info));
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.ddlutils.builder.Platform#getDatabaseName()
+ */
+ public String getDatabaseName()
+ {
+ return DATABASENAME;
+ }
+}
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=232530&r1=232529&r2=232530&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 Sat Aug 13 14:36:44 2005
@@ -1,8 +1,9 @@
package org.apache.ddlutils.task;
import org.apache.commons.dbcp.BasicDataSource;
-import org.apache.ddlutils.builder.SqlBuilder;
-import org.apache.ddlutils.builder.SqlBuilderFactory;
+import org.apache.ddlutils.Platform;
+import org.apache.ddlutils.PlatformFactory;
+import org.apache.ddlutils.PlatformUtils;
import org.apache.tools.ant.BuildException;
/**
@@ -78,35 +79,38 @@
}
/**
- * Creates the sql builder for the configured database.
+ * Creates the platform for the configured database.
*
- * @return The sql builder
+ * @return The platform
*/
- protected SqlBuilder getSqlBuilder() throws BuildException
+ protected Platform getPlatform() throws BuildException
{
- // TODO: This should largely be deducable from the jdbc connection url
- if (getDatabaseType() == null)
- {
- throw new BuildException("The database type needs to be defined.");
- }
-
- SqlBuilder builder = null;
+ Platform platform = null;
try
{
- builder = SqlBuilderFactory.newSqlBuilder(getDatabaseType());
+ if (getDatabaseType() == null)
+ {
+ setDatabaseType(new PlatformUtils().determineDatabaseType(getDataSource()));
+ if (getDatabaseType() == null)
+ {
+ throw new BuildException("The database type needs to be defined.");
+ }
+ }
+ platform = PlatformFactory.createNewPlatformInstance(getDatabaseType());
}
catch (Exception ex)
{
- throw new BuildException("Database type "+getDatabaseType()+" is not supported.");
+ throw new BuildException("Database type "+getDatabaseType()+" is not supported.", ex);
}
- if (builder == null)
+ if (platform == null)
{
throw new BuildException("Database type "+getDatabaseType()+" is not supported.");
}
else
{
- return builder;
+ platform.setDataSource(getDataSource());
+ return platform;
}
}
}
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=232530&r1=232529&r2=232530&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 Sat Aug 13 14:36:44 2005
@@ -22,8 +22,8 @@
import javax.sql.DataSource;
-import org.apache.ddlutils.builder.SqlBuilder;
-import org.apache.ddlutils.builder.SqlBuilderFactory;
+import org.apache.ddlutils.Platform;
+import org.apache.ddlutils.PlatformFactory;
import org.apache.ddlutils.io.DataConverterRegistration;
import org.apache.ddlutils.io.DataReader;
import org.apache.ddlutils.io.DataToDatabaseSink;
@@ -96,10 +96,11 @@
{
try
{
- SqlBuilder builder = SqlBuilderFactory.newSqlBuilder(_databaseType);
- DataToDatabaseSink sink = new DataToDatabaseSink(_dataSource, model, builder);
- DataReader reader = new DataReader();
+ Platform platform = PlatformFactory.createNewPlatformInstance(_databaseType);
+ 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=232530&r1=232529&r2=232530&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 Sat Aug 13 14:36:44 2005
@@ -22,9 +22,8 @@
import javax.sql.DataSource;
-import org.apache.ddlutils.builder.SqlBuilder;
-import org.apache.ddlutils.builder.SqlBuilderFactory;
-import org.apache.ddlutils.dynabean.DynaSql;
+import org.apache.ddlutils.Platform;
+import org.apache.ddlutils.PlatformFactory;
import org.apache.ddlutils.io.DataWriter;
import org.apache.ddlutils.model.Database;
import org.apache.ddlutils.model.Table;
@@ -81,18 +80,18 @@
{
try
{
- SqlBuilder builder = SqlBuilderFactory.newSqlBuilder(_databaseType);
- DynaSql dynaSql = new DynaSql(builder, _dataSource, model);
- DataWriter writer = new DataWriter(model, new FileOutputStream(_outputFile), _encoding);
-
+ Platform platform = PlatformFactory.createNewPlatformInstance(_databaseType);
+ 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();)
{
Table table = (Table)tableIt.next();
- writer.write(dynaSql.query("select * from "+table.getName()));
+ writer.write(platform.query(model, "select * from "+table.getName()));
}
writer.writeDocumentEnd();
}
Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteDataToSpecifiedDatabaseCommand.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteDataToSpecifiedDatabaseCommand.java?rev=232530&r1=232529&r2=232530&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteDataToSpecifiedDatabaseCommand.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteDataToSpecifiedDatabaseCommand.java Sat Aug 13 14:36:44 2005
@@ -19,8 +19,9 @@
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
-import org.apache.ddlutils.builder.SqlBuilder;
-import org.apache.ddlutils.builder.SqlBuilderFactory;
+
+import org.apache.ddlutils.Platform;
+import org.apache.ddlutils.PlatformFactory;
import org.apache.ddlutils.io.DataReader;
import org.apache.ddlutils.io.DataToDatabaseSink;
import org.apache.ddlutils.model.Database;
@@ -36,9 +37,9 @@
public class WriteDataToSpecifiedDatabaseCommand extends DatabaseCommand
{
/** 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();
/**
* Adds a fileset.
@@ -72,10 +73,11 @@
try
{
- SqlBuilder builder = SqlBuilderFactory.newSqlBuilder(getDatabaseType());
- DataToDatabaseSink sink = new DataToDatabaseSink(getDataSource(), model, builder);
- DataReader reader = new DataReader();
+ Platform platform = PlatformFactory.createNewPlatformInstance(getDatabaseType());
+ DataToDatabaseSink sink = new DataToDatabaseSink(platform, model);
+ DataReader reader = new DataReader();
+ platform.setDataSource(getDataSource());
reader.setModel(model);
reader.setSink(sink);
if ((_singleDataFile != null) && !_fileSets.isEmpty())
Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteSchemaSqlToFileCommand.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteSchemaSqlToFileCommand.java?rev=232530&r1=232529&r2=232530&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteSchemaSqlToFileCommand.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/task/WriteSchemaSqlToFileCommand.java Sat Aug 13 14:36:44 2005
@@ -21,7 +21,8 @@
import java.sql.Connection;
import java.sql.SQLException;
-import org.apache.ddlutils.builder.SqlBuilder;
+import org.apache.ddlutils.Platform;
+import org.apache.ddlutils.io.JdbcModelReader;
import org.apache.ddlutils.model.Database;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
@@ -59,14 +60,14 @@
throw new BuildException("Cannot overwrite output file "+_outputFile.getAbsolutePath());
}
- SqlBuilder builder = getSqlBuilder();
+ Platform platform = getPlatform();
Connection connection = null;
try
{
FileWriter writer = new FileWriter(_outputFile);
- builder.setWriter(writer);
+ platform.getSqlBuilder().setWriter(writer);
if (isAlterDatabase())
{
if (getDataSource() == null)
@@ -74,11 +75,14 @@
throw new BuildException("No database specified.");
}
connection = getDataSource().getConnection();
- builder.alterDatabase(model, connection, true, true);
+
+ Database currentModel = new JdbcModelReader(connection).getDatabase();
+
+ platform.getSqlBuilder().alterDatabase(currentModel, model, true, true);
}
else
{
- builder.createDatabase(model);
+ platform.getSqlBuilder().createTables(model);
}
writer.close();
task.log("Written SQL to "+_outputFile.getAbsolutePath(), Project.MSG_INFO);
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=232530&r1=232529&r2=232530&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 Sat Aug 13 14:36:44 2005
@@ -17,12 +17,9 @@
*/
import java.io.StringWriter;
-import java.sql.Connection;
-import java.sql.SQLException;
-import org.apache.ddlutils.builder.SqlBuilder;
+import org.apache.ddlutils.Platform;
import org.apache.ddlutils.model.Database;
-import org.apache.ddlutils.util.DDLExecutor;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
@@ -42,43 +39,26 @@
throw new BuildException("No database specified.");
}
- SqlBuilder builder = getSqlBuilder();
+ Platform platform = getPlatform();
StringWriter writer = new StringWriter();
- Connection connection = null;
- builder.setWriter(writer);
+ platform.getSqlBuilder().setWriter(writer);
try
{
- connection = getDataSource().getConnection();
if (isAlterDatabase())
{
- builder.alterDatabase(model, connection, true, true);
+ platform.alterTables(model, true, true, true);
}
else
{
- builder.createDatabase(model);
+ platform.createTables(model, true, true);
}
-
- DDLExecutor executor = new DDLExecutor(getDataSource());
- executor.evaluateBatch(writer.toString());
task.log("Created database", Project.MSG_INFO);
}
catch (Exception ex)
{
throw new BuildException(ex);
- }
- finally
- {
- if (connection != null)
- {
- try
- {
- connection.close();
- }
- catch (SQLException ex)
- {}
- }
}
}
}
Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/util/JdbcSupport.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/util/JdbcSupport.java?rev=232530&r1=232529&r2=232530&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/util/JdbcSupport.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/util/JdbcSupport.java Sat Aug 13 14:36:44 2005
@@ -24,6 +24,7 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import org.apache.ddlutils.DynaSqlException;
/**
* JdbcSupport is an abstract base class for objects which need to
@@ -33,37 +34,53 @@
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
* @version $Revision$
*/
-public abstract class JdbcSupport {
+public abstract class JdbcSupport
+{
/** The Log to which logging calls will be made. */
- private final Log log = LogFactory.getLog(getClass());
-
- private DataSource dataSource;
-
- public JdbcSupport() {
+ private final Log _log = LogFactory.getLog(getClass());
+ /** The data source */
+ private DataSource _dataSource;
+
+ /**
+ * Creates a new instance without a data source.
+ */
+ public JdbcSupport()
+ {
}
- public JdbcSupport(DataSource dataSource) {
- this.dataSource = dataSource;
+ /**
+ * Creates a new instance that uses the given data source for talking to
+ * the database.
+ *
+ * @param dataSource The data source
+ */
+ public JdbcSupport(DataSource dataSource)
+ {
+ _dataSource = dataSource;
}
// Properties
//-------------------------------------------------------------------------
/**
- * Returns the DataSource used to pool JDBC Connections.
- * @return DataSource
+ * Returns the data source used for communicating with the database.
+ *
+ * @return The data source
*/
- public DataSource getDataSource() {
- return dataSource;
+ public DataSource getDataSource()
+ {
+ return _dataSource;
}
/**
- * Sets the DataSource used to pool JDBC Connections.
- * @param dataSource The dataSource to set
+ * Sets the DataSource used for communicating with the database.
+ *
+ * @param dataSource The data source
*/
- public void setDataSource(DataSource dataSource) {
- this.dataSource = dataSource;
+ public void setDataSource(DataSource dataSource)
+ {
+ _dataSource = dataSource;
}
// Implementation methods
@@ -72,15 +89,22 @@
/**
* Returns a (new) JDBC connection from the data source.
*
- * @return A JDBC connection
+ * @return The connection
*/
- public Connection borrowConnection() throws SQLException
+ public Connection borrowConnection() throws DynaSqlException
{
- return getDataSource().getConnection();
+ try
+ {
+ return getDataSource().getConnection();
+ }
+ catch (SQLException ex)
+ {
+ throw new DynaSqlException("Could not get a connection from the datasource", ex);
+ }
}
/**
- * Closes a JDBC connection (returns it back to pool if a poolable datasource).
+ * Closes the given JDBC connection (returns it back to the pool if the datasource is poolable).
*
* @param connection The connection
*/
@@ -95,7 +119,7 @@
}
catch (Exception e)
{
- log.error("Caught exception while returning connection to pool", e);
+ _log.warn("Caught exception while returning connection to pool", e);
}
}
@@ -120,7 +144,7 @@
}
catch (Exception e)
{
- log.warn("Ignoring exception closing statement", e);
+ _log.warn("Ignoring exception closing statement", e);
}
}
}
Modified: db/ddlutils/trunk/src/test/org/apache/ddlutils/TestDatabaseWriterBase.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/test/org/apache/ddlutils/TestDatabaseWriterBase.java?rev=232530&r1=232529&r2=232530&view=diff
==============================================================================
--- db/ddlutils/trunk/src/test/org/apache/ddlutils/TestDatabaseWriterBase.java (original)
+++ db/ddlutils/trunk/src/test/org/apache/ddlutils/TestDatabaseWriterBase.java Sat Aug 13 14:36:44 2005
@@ -17,7 +17,6 @@
*/
import java.io.StringReader;
-import java.sql.SQLException;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
@@ -28,17 +27,14 @@
import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.beanutils.DynaProperty;
import org.apache.commons.dbcp.BasicDataSource;
-import org.apache.ddlutils.builder.BuilderUtils;
-import org.apache.ddlutils.dynabean.DynaSqlException;
import org.apache.ddlutils.io.DataReader;
import org.apache.ddlutils.io.DataToDatabaseSink;
import org.apache.ddlutils.model.Database;
-import org.apache.ddlutils.util.DDLExecutor;
/**
* Base class for database writer tests.
*/
-public abstract class TestDatabaseWriterBase extends TestBuilderBase
+public abstract class TestDatabaseWriterBase extends TestPlatformBase
{
/** The name of the property that specifies properties file with the settings for the connection to test against */
public static final String JDBC_PROPERTIES_PROPERTY = "jdbc.properties.file";
@@ -49,8 +45,8 @@
/** The data source to test against */
private DataSource _dataSource;
- /** The platform */
- private String _platform;
+ /** The database name */
+ private String _databaseName;
/** The database model */
private Database _model;
@@ -96,19 +92,13 @@
{
throw new DynaSqlException(ex);
}
- try
- {
- _platform = new BuilderUtils().determineDatabaseType(_dataSource);
- }
- catch (SQLException ex)
- {
- throw new DynaSqlException(ex);
- }
- if (_platform == null)
+
+ _databaseName = new PlatformUtils().determineDatabaseType(_dataSource);
+ if (_databaseName == null)
{
// could not determine, perhaps the property has been set ?
- _platform = props.getProperty(PLATFORM_PROPERTY);
- if (_platform == null)
+ _databaseName = props.getProperty(PLATFORM_PROPERTY);
+ if (_databaseName == null)
{
throw new DynaSqlException("Could not determine platform from datasource, please specify it in the jdbc.properties via the ddlutils.platform property");
}
@@ -130,7 +120,7 @@
*/
protected String getDatabaseName()
{
- return _platform;
+ return _databaseName;
}
/**
@@ -143,17 +133,16 @@
return _model;
}
- /**
- * Returns a DDL executor instance.
- *
- * @return The executor instance
+
+ /* (non-Javadoc)
+ * @see org.apache.ddlutils.TestPlatformBase#setUp()
*/
- protected DDLExecutor getDDLExecutor()
+ protected void setUp() throws Exception
{
- return new DDLExecutor(getDataSource(), getBuilder());
+ super.setUp();
+ getPlatform().setDataSource(getDataSource());
}
-
/* (non-Javadoc)
* @see org.apache.ddlutils.TestBuilderBase#tearDown()
*/
@@ -179,7 +168,7 @@
{
_model = parseDatabaseFromString(schemaXml);
- getDDLExecutor().createDatabase(_model, true);
+ getPlatform().createTables(_model, true, true);
}
catch (Exception ex)
{
@@ -199,7 +188,7 @@
DataReader dataReader = new DataReader();
dataReader.setModel(_model);
- dataReader.setSink(new DataToDatabaseSink(getDataSource(), _model, getBuilder()));
+ dataReader.setSink(new DataToDatabaseSink(getPlatform(), _model));
dataReader.parse(new StringReader(dataXml));
}
catch (Exception ex)
@@ -215,7 +204,7 @@
{
try
{
- getDDLExecutor().dropDatabase(_model);
+ getPlatform().dropTables(_model, true);
}
catch (Exception ex)
{
@@ -233,7 +222,7 @@
*/
protected Object getPropertyValue(DynaBean bean, String propName)
{
- if (getBuilder().isCaseSensitive())
+ if (getPlatform().getPlatformInfo().isCaseSensitive())
{
return bean.get(propName);
}
Copied: db/ddlutils/trunk/src/test/org/apache/ddlutils/TestPlatformBase.java (from r226549, db/ddlutils/trunk/src/test/org/apache/ddlutils/TestBuilderBase.java)
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/test/org/apache/ddlutils/TestPlatformBase.java?p2=db/ddlutils/trunk/src/test/org/apache/ddlutils/TestPlatformBase.java&p1=db/ddlutils/trunk/src/test/org/apache/ddlutils/TestBuilderBase.java&r1=226549&r2=232530&rev=232530&view=diff
==============================================================================
--- db/ddlutils/trunk/src/test/org/apache/ddlutils/TestBuilderBase.java (original)
+++ db/ddlutils/trunk/src/test/org/apache/ddlutils/TestPlatformBase.java Sat Aug 13 14:36:44 2005
@@ -23,17 +23,14 @@
import junit.framework.TestCase;
-import org.apache.ddlutils.builder.SqlBuilder;
-import org.apache.ddlutils.builder.SqlBuilderFactory;
import org.apache.ddlutils.io.DatabaseReader;
import org.apache.ddlutils.model.Database;
-import org.apache.ddlutils.util.DDLExecutor;
import org.xml.sax.SAXException;
/**
* Base class for builder tests.
*/
-public abstract class TestBuilderBase extends TestCase
+public abstract class TestPlatformBase extends TestCase
{
/** The database schema for testing the column types */
public static final String COLUMN_TEST_SCHEMA =
@@ -74,9 +71,9 @@
" </table>\n"+
"</database>";
- /** The tested builder */
- private SqlBuilder _builder;
- /** The writer that the builder writes to */
+ /** The tested platform */
+ private Platform _platform;
+ /** The writer that the builder of the platform writes to */
private StringWriter _writer;
/* (non-Javadoc)
@@ -84,9 +81,9 @@
*/
protected void setUp() throws Exception
{
- _writer = new StringWriter();
- _builder = SqlBuilderFactory.newSqlBuilder(getDatabaseName());
- _builder.setWriter(_writer);
+ _writer = new StringWriter();
+ _platform = PlatformFactory.createNewPlatformInstance(getDatabaseName());
+ _platform.getSqlBuilder().setWriter(_writer);
}
/* (non-Javadoc)
@@ -94,18 +91,28 @@
*/
protected void tearDown() throws Exception
{
- _builder = null;
- _writer = null;
+ _platform = null;
+ _writer = null;
}
/**
- * Returns the tested sql builder.
+ * Returns the tested platform.
*
- * @return The builder
+ * @return The platform
*/
- protected SqlBuilder getBuilder()
+ protected Platform getPlatform()
{
- return _builder;
+ return _platform;
+ }
+
+ /**
+ * Returns the info object of the tested platform.
+ *
+ * @return The platform info object
+ */
+ protected PlatformInfo getPlatformInfo()
+ {
+ return getPlatform().getPlatformInfo();
}
/**
@@ -137,7 +144,22 @@
return (Database)reader.parse(new StringReader(dbDef));
}
-
+
+ /**
+ * Creates the database creation sql for the test database defined by {@link #COLUMN_TEST_SCHEMA}.
+ *
+ * @return The sql
+ */
+ protected String createTestDatabase() throws IntrospectionException, IOException, SAXException
+ {
+ Database testDb = parseDatabaseFromString(COLUMN_TEST_SCHEMA);
+
+ // we're turning the comment creation off to make testing easier
+ getPlatformInfo().setCommentsSupported(false);
+ getPlatform().getSqlBuilder().createTables(testDb);
+ return getBuilderOutput();
+ }
+
/**
* Compares the two strings but ignores any whitespace differences.
* It also recognizes special delimiter chars.