Browse Source

* FCL-DB: fix for MSSQL/Sybase db test framework errors when existing FPDEV table present.
Thanks to Lacak2 for the fix idea!

git-svn-id: trunk@22898 -

reiniero 12 years ago
parent
commit
c26abcebe7
1 changed files with 27 additions and 4 deletions
  1. 27 4
      packages/fcl-db/tests/sqldbtoolsunit.pas

+ 27 - 4
packages/fcl-db/tests/sqldbtoolsunit.pas

@@ -408,17 +408,40 @@ end;
 
 procedure TSQLDBConnector.TryDropIfExist(ATableName: String);
 begin
-  // This makes live soo much easier, since it avoids the exception if the table already
+  // This makes life soo much easier, since it avoids the exception if the table already
   // exists. And while this exeption is in a try..except statement, the debugger
-  // always shows the exception. Which is pretty annoying
-  // It only works with Firebird 2, though.
+  // always shows the exception, which is pretty annoying.
   try
     if SQLDbType = INTERBASE then
       begin
+      // This only works with Firebird 2+
       FConnection.ExecuteDirect('execute block as begin if (exists (select 1 from rdb$relations where rdb$relation_name=''' + ATableName + ''')) '+
-             'then execute statement ''drop table ' + ATAbleName + ';'';end');
+        'then execute statement ''drop table ' + ATAbleName + ';'';end');
       FTransaction.CommitRetaining;
       end;
+    if SQLDBType = mssql then
+      begin
+      // Checking is needed here to avoid getting "auto rollback" of a subsequent CREATE TABLE statement
+      // which leads to the rollback not referring to the right transaction=>SQL error
+      // Use SQL92 ISO standard INFORMATION_SCHEMA:
+      FConnection.ExecuteDirect(
+        'if exists (select * from INFORMATION_SCHEMA.TABLES where TABLE_TYPE=''BASE TABLE'' AND TABLE_NAME=''' + ATableName + ''') '+
+        'begin '+
+        'drop table ' + ATAbleName + ' '+
+        'end');
+      FTransaction.CommitRetaining;
+      end;
+    if SQLDbType = sybase then
+      begin
+      // Checking is needed here to avoid getting "auto rollback" of a subsequent CREATE TABLE statement
+      // which leads to the rollback not referring to the right transaction=>SQL error
+      // Can't use SQL standard information_schema; instead query sysobjects for User tables
+      FConnection.ExecuteDirect(
+        'if exists (select * from sysobjects where type = ''U'' and name=''' + ATableName + ''') '+
+        'begin '+
+        'drop table ' + ATAbleName + ' '+
+        'end');
+      end;
   except
     FTransaction.RollbackRetaining;
   end;