Fixed some Sql Server platform bugs
git-svn-id: https://svn.apache.org/repos/asf/db/ddlutils/trunk@711742 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/main/java/org/apache/ddlutils/platform/mssql/MSSqlBuilder.java b/src/main/java/org/apache/ddlutils/platform/mssql/MSSqlBuilder.java
index f13c267..0888bf3 100644
--- a/src/main/java/org/apache/ddlutils/platform/mssql/MSSqlBuilder.java
+++ b/src/main/java/org/apache/ddlutils/platform/mssql/MSSqlBuilder.java
@@ -351,9 +351,16 @@
{
// Sql Server per default does not allow us to insert values explicitly into
// identity columns. However, we can change this behavior
- boolean hasIdentityColumns = targetTable.getAutoIncrementColumns().length > 0;
+ // We need to this only if
+ // - there is a column in both tables that is auto increment only in the target table, or
+ // - there is a column in both tables that is auto increment in both tables
+ Column[] targetIdentityColumns = targetTable.getAutoIncrementColumns();
- if (hasIdentityColumns)
+ // Sql Server allows only one identity column, so let's take a shortcut here
+ boolean needToAllowIdentityInsert = (targetIdentityColumns.length > 0) &&
+ (sourceTable.findColumn(targetIdentityColumns[0].getName(), getPlatform().isDelimitedIdentifierModeOn()) != null);
+
+ if (needToAllowIdentityInsert)
{
print("SET IDENTITY_INSERT ");
printIdentifier(getTableName(targetTable));
@@ -362,7 +369,7 @@
}
super.copyData(sourceTable, targetTable);
// We have to turn it off ASAP because it can be on only for one table per session
- if (hasIdentityColumns)
+ if (needToAllowIdentityInsert)
{
print("SET IDENTITY_INSERT ");
printIdentifier(getTableName(targetTable));
diff --git a/src/test/java/org/apache/ddlutils/io/TestMisc.java b/src/test/java/org/apache/ddlutils/io/TestMisc.java
index c19e3ff..2745c2f 100644
--- a/src/test/java/org/apache/ddlutils/io/TestMisc.java
+++ b/src/test/java/org/apache/ddlutils/io/TestMisc.java
@@ -37,6 +37,7 @@
import org.apache.ddlutils.model.Table;
import org.apache.ddlutils.platform.derby.DerbyPlatform;
import org.apache.ddlutils.platform.hsqldb.HsqlDbPlatform;
+import org.apache.ddlutils.platform.mssql.MSSqlPlatform;
import org.apache.ddlutils.platform.mysql.MySql50Platform;
import org.apache.ddlutils.platform.mysql.MySqlPlatform;
import org.apache.ddlutils.platform.postgresql.PostgreSqlPlatform;
@@ -1268,7 +1269,8 @@
if (MySqlPlatform.DATABASENAME.equals(getPlatform().getName()) ||
MySql50Platform.DATABASENAME.equals(getPlatform().getName()) ||
PostgreSqlPlatform.DATABASENAME.equals(getPlatform().getName()) ||
- DerbyPlatform.DATABASENAME.equals(getPlatform().getName()))
+ DerbyPlatform.DATABASENAME.equals(getPlatform().getName()) ||
+ MSSqlPlatform.DATABASENAME.equals(getPlatform().getName()))
{
query.append(" AS ");
if (getPlatform().isDelimitedIdentifierModeOn())