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 2006/01/21 14:11:17 UTC
svn commit: r371026 - in /db/ddlutils/trunk/src:
java/org/apache/ddlutils/platform/firebird/
java/org/apache/ddlutils/platform/mysql/ test/org/apache/ddlutils/io/
Author: tomdz
Date: Sat Jan 21 05:10:40 2006
New Revision: 371026
URL: http://svn.apache.org/viewcvs?rev=371026&view=rev
Log:
Javadoc fixes & code layout cleanup
Minor changes to the firebird platform:
* runBatch method replaces the evaluateBatch method
* added back mappings to the native type mappings
Modified:
db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/firebird/FirebirdBuilder.java
db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/firebird/FirebirdModelReader.java
db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/firebird/FirebirdPlatform.java
db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mysql/MySql50ModelReader.java
db/ddlutils/trunk/src/test/org/apache/ddlutils/io/RoundtripTestBase.java
db/ddlutils/trunk/src/test/org/apache/ddlutils/io/TestConstraints.java
Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/firebird/FirebirdBuilder.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/firebird/FirebirdBuilder.java?rev=371026&r1=371025&r2=371026&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/firebird/FirebirdBuilder.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/firebird/FirebirdBuilder.java Sat Jan 21 05:10:40 2006
@@ -1,7 +1,7 @@
package org.apache.ddlutils.platform.firebird;
/*
- * Copyright 1999-2006 The Apache Software Foundation.
+ * Copyright 2006 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.
@@ -34,6 +34,9 @@
*/
public class FirebirdBuilder extends SqlBuilder
{
+ /** Denotes the string used via SET TERM for delimiting commands that need to be executed in one go. */
+ public static final String TERM_COMMAND = "--TERM--";
+
/**
* Creates a new builder instance.
*
@@ -59,7 +62,7 @@
print("CREATE GENERATOR ");
printIdentifier(getConstraintName("gen", table, columns[idx].getName(), null));
printEndOfStatement();
- print("--TERM--");
+ print(TERM_COMMAND);
printEndOfStatement();
print("CREATE TRIGGER ");
printIdentifier(getConstraintName("trg", table, columns[idx].getName(), null));
@@ -76,7 +79,8 @@
printIdentifier(getConstraintName("gen", table, columns[idx].getName(), null));
println(", 1);");
println("END;");
- print("--TERM--;");
+ print(TERM_COMMAND);
+ print(";");
}
}
@@ -107,7 +111,7 @@
/**
* {@inheritDoc}
- * @todo : we are kinf of stuck here, since last insert id needs the database name..
+ * @todo : we are kind of stuck here, since last insert id needs the database name..
*/
public String getSelectLastInsertId(Table table)
{
@@ -142,9 +146,12 @@
protected String getNativeDefaultValue(Column column)
{
String defaultValue = column.getDefaultValue();
- if (column.getTypeCode() == Types.BIT || column.getTypeCode() == Types.BOOLEAN)
+
+ if ((column.getTypeCode() == Types.BIT) || (column.getTypeCode() == Types.BOOLEAN))
{
- defaultValue = getDefaultValueHelper().convert(column.getDefaultValue(), column.getTypeCode(), Types.SMALLINT).toString();
+ defaultValue = getDefaultValueHelper().convert(column.getDefaultValue(),
+ column.getTypeCode(),
+ Types.SMALLINT).toString();
}
return defaultValue;
}
Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/firebird/FirebirdModelReader.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/firebird/FirebirdModelReader.java?rev=371026&r1=371025&r2=371026&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/firebird/FirebirdModelReader.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/firebird/FirebirdModelReader.java Sat Jan 21 05:10:40 2006
@@ -1,5 +1,7 @@
+package org.apache.ddlutils.platform.firebird;
+
/*
- * Copyright 1999-2006 The Apache Software Foundation.
+ * Copyright 2006 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.
@@ -14,8 +16,6 @@
* limitations under the License.
*/
-package org.apache.ddlutils.platform.firebird;
-
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
@@ -32,31 +32,36 @@
import org.apache.ddlutils.platform.JdbcModelReader;
/**
- * The Jdbc Model Reader for FireBird.
+ * The Jdbc Model Reader for Firebird.
*
* @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
* @version $Revision: $
*/
-public class FirebirdModelReader extends JdbcModelReader {
-
+public class FirebirdModelReader extends JdbcModelReader
+{
/**
- * @param platformInfo the platformInfo
+ * Creates a new model reader for Firebird databases.
+ *
+ * @param platformInfo The platform specific settings
*/
- public FirebirdModelReader(PlatformInfo platformInfo) {
+ public FirebirdModelReader(PlatformInfo platformInfo)
+ {
super(platformInfo);
}
/**
- * (@inheritDoc)
+ * {@inheritDoc}
*/
- protected void readIndex(DatabaseMetaDataWrapper metaData, Map values, Map knownIndices) throws SQLException {
+ protected void readIndex(DatabaseMetaDataWrapper metaData, Map values, Map knownIndices) throws SQLException
+ {
super.readIndex(metaData, values, knownIndices);
+
Iterator indexNames = knownIndices.keySet().iterator();
-// printIdentifier(getConstraintName(null, table, "PK", null));
while (indexNames.hasNext())
{
- String indexName = (String) indexNames.next();
+ String indexName = (String)indexNames.next();
+
if (indexName.indexOf("PRIMARY") != -1)
{
// we have hit a primary key, remove it..
@@ -64,6 +69,7 @@
}
}
}
+
/**
* {@inheritDoc}
* @todo This needs some more work, since table names can be case sensitive or lowercase
@@ -72,7 +78,7 @@
*/
protected Table readTable(DatabaseMetaDataWrapper metaData, Map values) throws SQLException
{
- Table table = super.readTable(metaData, values);
+ Table table = super.readTable(metaData, values);
determineAutoIncrementFromResultSetMetaData(table, table.getColumns());
// fix the indexes.
@@ -90,21 +96,21 @@
*/
protected void determineAutoIncrementFromResultSetMetaData(Table table, Column[] columnsToCheck) throws SQLException
{
- StringBuffer query = new StringBuffer();
-
- String prefix = ("gen_"+table.getName()+"_").toUpperCase();
+ StringBuffer query = new StringBuffer();
+ String prefix = ("gen_"+table.getName()+"_").toUpperCase();
query.append("SELECT RDB$GENERATOR_NAME FROM RDB$GENERATORS WHERE RDB$GENERATOR_NAME STARTING WITH '");
query.append(prefix);
query.append("'");
- Statement stmt = getConnection().createStatement();
- ResultSet rs = stmt.executeQuery(query.toString());
-
+
+ Statement stmt = getConnection().createStatement();
+ ResultSet rs = stmt.executeQuery(query.toString());
while(rs.next())
{
String generatorName = rs.getString(1).substring(prefix.length()).trim();
- Column column = table.findColumn(generatorName, false);
+ Column column = table.findColumn(generatorName, false);
+
if (column != null)
{
column.setAutoIncrement(true);
@@ -121,23 +127,20 @@
*/
private void fixIndexes(Table table)
{
- if (table.getIndexCount() == 0 || table.getForeignKeyCount() == 0)
+ // we don't do anything when there are no indexes or foreignkeys.
+ if ((table.getIndexCount() > 0) && (table.getForeignKeyCount() > 0))
{
- // we don't do anything when there are no indexes or foreignkeys.
- return;
- }
- for (int i = 0; i < table.getForeignKeyCount(); i++)
- {
- ForeignKey fk = table.getForeignKey(i);
- Index index = table.findIndex(fk.getName());
- if (index != null)
+ for (int idx = 0; idx < table.getForeignKeyCount(); idx++)
{
- // remove it from the indexes..
- table.removeIndex(index);
+ ForeignKey fk = table.getForeignKey(idx);
+ Index index = table.findIndex(fk.getName());
+
+ if (index != null)
+ {
+ // remove it from the indexes..
+ table.removeIndex(index);
+ }
}
}
- // filter
}
-
-
}
Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/firebird/FirebirdPlatform.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/firebird/FirebirdPlatform.java?rev=371026&r1=371025&r2=371026&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/firebird/FirebirdPlatform.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/firebird/FirebirdPlatform.java Sat Jan 21 05:10:40 2006
@@ -1,7 +1,7 @@
package org.apache.ddlutils.platform.firebird;
/*
- * Copyright 1999-2006 The Apache Software Foundation.
+ * Copyright 2005-2006 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.
@@ -27,7 +27,6 @@
import org.apache.commons.logging.LogFactory;
import org.apache.ddlutils.DynaSqlException;
import org.apache.ddlutils.PlatformInfo;
-import org.apache.ddlutils.model.Database;
import org.apache.ddlutils.platform.PlatformImplBase;
/**
@@ -40,16 +39,20 @@
*/
public class FirebirdPlatform extends PlatformImplBase
{
- /** The log for this platform. */
- private final Log _log = LogFactory.getLog(getClass());
/** Database name of this platform. */
public static final String DATABASENAME = "Firebird";
/** The standard Firebird jdbc driver. */
public static final String JDBC_DRIVER = "org.firebirdsql.jdbc.FBDriver";
/** The subprotocol used by the standard Firebird driver. */
public static final String JDBC_SUBPROTOCOL = "firebirdsql";
+ /** The log for this platform. */
+ private final Log _log = LogFactory.getLog(getClass());
- public FirebirdPlatform() {
+ /**
+ * Creates a new Firebird platform instance.
+ */
+ public FirebirdPlatform()
+ {
PlatformInfo info = new PlatformInfo();
info.setMaxIdentifierLength(31);
@@ -62,39 +65,44 @@
info.setSupportingDelimitedIdentifiers(false);
// BINARY and VARBINARY are also handled by the InterbaseBuilder.getSqlType method
- info.addNativeTypeMapping(Types.ARRAY, "BLOB");
- info.addNativeTypeMapping(Types.BINARY, "BLOB", Types.LONGVARBINARY);
- info.addNativeTypeMapping(Types.BIT, "SMALLINT", Types.BIT);
+ info.addNativeTypeMapping(Types.ARRAY, "BLOB", Types.LONGVARBINARY);
+ info.addNativeTypeMapping(Types.BINARY, "BLOB", Types.LONGVARBINARY);
+ info.addNativeTypeMapping(Types.BIT, "SMALLINT", Types.SMALLINT);
info.addNativeTypeMapping(Types.CLOB, "BLOB SUB_TYPE TEXT", Types.LONGVARCHAR);
- info.addNativeTypeMapping(Types.DISTINCT, "BLOB", Types.LONGVARBINARY);
- info.addNativeTypeMapping(Types.BLOB, "BLOB", Types.LONGVARBINARY);
+ info.addNativeTypeMapping(Types.DISTINCT, "BLOB", Types.LONGVARBINARY);
+ info.addNativeTypeMapping(Types.BLOB, "BLOB", Types.LONGVARBINARY);
info.addNativeTypeMapping(Types.DOUBLE, "DOUBLE PRECISION");
- info.addNativeTypeMapping(Types.FLOAT, "DOUBLE PRECISION");
- info.addNativeTypeMapping(Types.JAVA_OBJECT, "BLOB", Types.LONGVARBINARY);
- info.addNativeTypeMapping(Types.LONGVARBINARY, "BLOB", Types.LONGVARBINARY);
+ info.addNativeTypeMapping(Types.FLOAT, "DOUBLE PRECISION", Types.DOUBLE);
+ info.addNativeTypeMapping(Types.JAVA_OBJECT, "BLOB", Types.LONGVARBINARY);
+ info.addNativeTypeMapping(Types.LONGVARBINARY, "BLOB", Types.LONGVARBINARY);
info.addNativeTypeMapping(Types.LONGVARCHAR, "BLOB SUB_TYPE TEXT");
- info.addNativeTypeMapping(Types.NULL, "BLOB");
- info.addNativeTypeMapping(Types.OTHER, "BLOB");
+ info.addNativeTypeMapping(Types.NULL, "BLOB", Types.LONGVARBINARY);
+ info.addNativeTypeMapping(Types.OTHER, "BLOB", Types.LONGVARBINARY);
info.addNativeTypeMapping(Types.REAL, "FLOAT");
- info.addNativeTypeMapping(Types.TINYINT, "SMALLINT");
- info.addNativeTypeMapping(Types.REF, "BLOB");
- info.addNativeTypeMapping(Types.STRUCT, "BLOB");
- info.addNativeTypeMapping(Types.VARBINARY, "BLOB", Types.LONGVARBINARY);
- info.addNativeTypeMapping(Types.BOOLEAN, "SMALLINT");
- info.addNativeTypeMapping("DATALINK", "BLOB");
+ info.addNativeTypeMapping(Types.TINYINT, "SMALLINT", Types.SMALLINT);
+ info.addNativeTypeMapping(Types.REF, "BLOB", Types.LONGVARBINARY);
+ info.addNativeTypeMapping(Types.STRUCT, "BLOB", Types.LONGVARBINARY);
+ info.addNativeTypeMapping(Types.VARBINARY, "BLOB", Types.LONGVARBINARY);
+
+ info.addNativeTypeMapping("BOOLEAN", "SMALLINT", "SMALLINT");
+ info.addNativeTypeMapping("DATALINK", "BLOB", "LONGVARBINARY");
- /**
+ /*
* This value is set to 128, to give multiple column index a chance
* to stay below the maximum key size of 256.
* If you use different encodings, you most likely need to decrease this value.
*/
+ // TODO: Why should we care - after all this is a database problem and not a
+ // DdlUtils one ? This value should be the one specified by JDBC which is 254
+ // Also, do we really need to specify a default size for SMALLINT ?
info.addDefaultSize(Types.VARCHAR, 128);
- info.addDefaultSize(Types.CHAR, 128);
+ info.addDefaultSize(Types.CHAR, 128);
info.addDefaultSize(Types.SMALLINT, 5);
setSqlBuilder(new FirebirdBuilder(info));
setModelReader(new FirebirdModelReader(info));
}
+
/**
* {@inheritDoc}
*/
@@ -102,69 +110,76 @@
{
return DATABASENAME;
}
-
- /**
- * {@inheritDoc}
- */
- public void createTables(Connection connection, Database model, boolean dropTablesFirst, boolean continueOnError) throws DynaSqlException
- {
- String sql = getCreateTablesSql(model, dropTablesFirst, continueOnError);
- runBatch(connection, sql, continueOnError);
- }
/**
- * Runbatch is a replacement for evaluateBatch.
- * Especially the <code>SET TERM !!;</code> blocks need to be executed in one go.
+ * Firebird-specific replacement for
+ * {@link org.apache.ddlutils.platform.PlatformImplBase#evaluateBatch(Connection, String, boolean)}
+ * that executes blocks delimited by the term {@link FirebirdBuilder#TERM_COMMAND}
+ * (defined in the database via <code>SET TERM !!;</code>), in one go.
*
- * @param connection the connection to use
- * @param sql the sql to process
- * @param continueOnError needs to continue when an error occurs.
+ * @param connection The connection to use
+ * @param sql The sql to process
+ * @param continueOnError Whether to continue when an error occurs
+ * @return The number of errors
*/
- public int runBatch(Connection connection, String sql, boolean continueOnError) throws DynaSqlException
+ public int evaluateBatch(Connection connection, String sql, boolean continueOnError) throws DynaSqlException
{
- int errors = 0;
Statement statement = null;
- int commandCount = 0;
+ int commandCount = 0;
+ int errors = 0;
- try {
+ try
+ {
statement = connection.createStatement();
+
StringTokenizer tokenizer = new StringTokenizer(sql, ";");
+ StringBuffer command = new StringBuffer();
while (tokenizer.hasMoreTokens())
{
- String command = tokenizer.nextToken().trim();
- if (command.equals("--TERM--"))
+ String token = tokenizer.nextToken().trim();
+
+ command.setLength(0);
+
+ if (token.equals(FirebirdBuilder.TERM_COMMAND))
{
- command = "";
- while(tokenizer.hasMoreTokens())
+ while (tokenizer.hasMoreTokens())
{
- String termSql = tokenizer.nextToken().trim();
- if(termSql.equals("--TERM--"))
- {
- break;
- }
- if (termSql.length() > 0)
+ token = tokenizer.nextToken().trim();
+
+ if (token.length() > 0)
{
- command+=termSql+";";
+ if (token.equals(FirebirdBuilder.TERM_COMMAND))
+ {
+ break;
+ }
+ else
+ {
+ command.append(token);
+ command.append(";");
+ }
}
}
}
+ else
+ {
+ command.append(token);
+ }
if (command.length() == 0)
{
continue;
}
- System.err.println("SQL Command :\n" + command);
commandCount++;
if (_log.isDebugEnabled())
{
- _log.debug("About to execute SQL " + command);
+ _log.debug("About to execute SQL " + command.toString());
}
try
{
- int results = statement.executeUpdate(command);
+ int results = statement.executeUpdate(command.toString());
if (_log.isDebugEnabled())
{
@@ -175,7 +190,7 @@
{
if (continueOnError)
{
- System.err.println("SQL Command " + command + " failed with " + ex.getMessage());
+ _log.error("SQL Command " + command.toString() + " failed", ex);
errors++;
}
else
@@ -194,7 +209,10 @@
}
connection.clearWarnings();
}
- _log.info("Executed "+ commandCount + " SQL command(s) with " + errors + " error(s)");
+ if (_log.isInfoEnabled())
+ {
+ _log.info("Executed "+ commandCount + " SQL command(s) with " + errors + " error(s)");
+ }
}
catch (SQLException ex)
{
Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mysql/MySql50ModelReader.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mysql/MySql50ModelReader.java?rev=371026&r1=371025&r2=371026&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mysql/MySql50ModelReader.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mysql/MySql50ModelReader.java Sat Jan 21 05:10:40 2006
@@ -24,7 +24,7 @@
import org.apache.ddlutils.platform.DatabaseMetaDataWrapper;
/**
- * Reads a database model from a MySql50 database.
+ * Reads a database model from a MySql 5 database.
*
* @author Martin van den Bemt
* @version $Revision: $
@@ -32,7 +32,7 @@
public class MySql50ModelReader extends MySqlModelReader
{
/**
- * Creates a new model reader for PostgreSql databases.
+ * Creates a new model reader for MySql 5 databases.
*
* @param platformInfo The platform specific settings
*/
@@ -42,11 +42,12 @@
}
/**
- * (@inheritDoc)
+ * {@inheritDoc}
*/
protected Column readColumn(DatabaseMetaDataWrapper metaData, Map values) throws SQLException
{
Column column = super.readColumn(metaData, values);
+
// make sure the defaultvalue is null when an empty is returned.
if ("".equals(column.getDefaultValue()))
{
Modified: db/ddlutils/trunk/src/test/org/apache/ddlutils/io/RoundtripTestBase.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/test/org/apache/ddlutils/io/RoundtripTestBase.java?rev=371026&r1=371025&r2=371026&view=diff
==============================================================================
--- db/ddlutils/trunk/src/test/org/apache/ddlutils/io/RoundtripTestBase.java (original)
+++ db/ddlutils/trunk/src/test/org/apache/ddlutils/io/RoundtripTestBase.java Sat Jan 21 05:10:40 2006
@@ -110,7 +110,7 @@
private boolean _useDelimitedIdentifiers;
/**
- * Specifies whether the test shall use delimited identifirs
+ * Specifies whether the test shall use delimited identifiers.
*
* @param useDelimitedIdentifiers Whether to use delimited identifiers
*/
Modified: db/ddlutils/trunk/src/test/org/apache/ddlutils/io/TestConstraints.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/test/org/apache/ddlutils/io/TestConstraints.java?rev=371026&r1=371025&r2=371026&view=diff
==============================================================================
--- db/ddlutils/trunk/src/test/org/apache/ddlutils/io/TestConstraints.java (original)
+++ db/ddlutils/trunk/src/test/org/apache/ddlutils/io/TestConstraints.java Sat Jan 21 05:10:40 2006
@@ -1,9 +1,7 @@
package org.apache.ddlutils.io;
-import junit.framework.Test;
-
/*
- * Copyright 1999-2006 The Apache Software Foundation.
+ * Copyright 2005-2006 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.
@@ -17,6 +15,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+
+import junit.framework.Test;
/**
* Performs the constraint tests.