浏览代码

Add forgotten ApplyUpdates; simplify code/queries

Reinier Olislagers 10 年之前
父节点
当前提交
423050ef47
共有 1 个文件被更改,包括 50 次插入46 次删除
  1. 50 46
      importtable.pas

+ 50 - 46
importtable.pas

@@ -149,24 +149,26 @@ begin
       begin
       begin
         if fmReg.TestConnection(RegRec.DatabaseName, fmEnterPass.edUser.Text, fmEnterPass.edPassword.Text,
         if fmReg.TestConnection(RegRec.DatabaseName, fmEnterPass.edUser.Text, fmEnterPass.edPassword.Text,
           RegRec.Charset) then
           RegRec.Charset) then
+        begin
           with fmMain do
           with fmMain do
           begin
           begin
             RegisteredDatabases[FDestIndex].RegRec.UserName:= fmEnterPass.edUser.Text;
             RegisteredDatabases[FDestIndex].RegRec.UserName:= fmEnterPass.edUser.Text;
             RegisteredDatabases[FDestIndex].RegRec.Password:= fmEnterPass.edPassword.Text;
             RegisteredDatabases[FDestIndex].RegRec.Password:= fmEnterPass.edPassword.Text;
             RegisteredDatabases[FDestIndex].RegRec.Role:= fmEnterPass.cbRole.Text;
             RegisteredDatabases[FDestIndex].RegRec.Role:= fmEnterPass.cbRole.Text;
-          end
-          else
-          begin
-            Exit;
           end;
           end;
-      end
+        end
+        else
+        begin
+          exit;
+        end;
+      end;
     end;
     end;
-
     if not(assigned(FDestinationQuery)) then
     if not(assigned(FDestinationQuery)) then
       FDestinationQuery:=TSQLQuery.Create(nil);
       FDestinationQuery:=TSQLQuery.Create(nil);
     FDestinationQuery.Close;
     FDestinationQuery.Close;
     FDestinationQuery.DataBase:=IBConnection;
     FDestinationQuery.DataBase:=IBConnection;
     FDestinationQuery.Transaction:=SQLTrans;
     FDestinationQuery.Transaction:=SQLTrans;
+    FDestinationQuery.ParseSQL; //belts and braces - generate InsertSQL
     FDestinationQuery.SQL.Text:='select * from '+FDestTable;
     FDestinationQuery.SQL.Text:='select * from '+FDestTable;
     FDestinationQuery.Open;
     FDestinationQuery.Open;
   end;
   end;
@@ -177,7 +179,6 @@ var
   i: integer;
   i: integer;
   MappingCount: integer;
   MappingCount: integer;
 begin
 begin
-
   // MappingCount will map fields if necessary so we need destination fields
   // MappingCount will map fields if necessary so we need destination fields
   if FImporter.DestinationFields.Count=0 then
   if FImporter.DestinationFields.Count=0 then
   begin
   begin
@@ -276,83 +277,86 @@ procedure TfmImportTable.bbImportClick(Sender: TObject);
 var
 var
   DestColumn: string;
   DestColumn: string;
   i: Integer;
   i: Integer;
-  SQLTarget: TSQLQuery;
   Num: Integer;
   Num: Integer;
 begin
 begin
   if not(assigned(FDestinationQuery)) and (FDestinationQuery.Active=false) then
   if not(assigned(FDestinationQuery)) and (FDestinationQuery.Active=false) then
     exit; //no destination fields
     exit; //no destination fields
 
 
-  // Skip first row if necessary
-  if chkSkipFirstRow.Checked then
-  begin
-    if not(FImporter.ReadRow) then
-      exit; //error: end of file?
-  end;
+  Screen.Cursor:=crHourGlass;
+  bbClose.Enabled:=false;
+  bbImport.Enabled:=false;
+  try
+    // Skip first row if necessary
+    if chkSkipFirstRow.Checked then
+    begin
+      if not(FImporter.ReadRow) then
+        exit; //error: end of file?
+    end;
 
 
-  // Enter password if it is not saved... and we're not connected to an embedded
-  // database
-  with fmMain.RegisteredDatabases[FDestIndex] do
-  begin
-    if (IBConnection.HostName<>'') and (IBConnection.Password = '') then
+    // Enter password if it is not saved... and we're not connected to an embedded
+    // database
+    with fmMain.RegisteredDatabases[FDestIndex] do
     begin
     begin
-      if fmEnterPass.ShowModal = mrOk then
+      if (IBConnection.HostName<>'') and (IBConnection.Password = '') then
       begin
       begin
-        if fmReg.TestConnection(RegRec.DatabaseName, fmEnterPass.edUser.Text, fmEnterPass.edPassword.Text,
-          RegRec.Charset) then
-          with fmMain do
+        if fmEnterPass.ShowModal = mrOk then
+        begin
+          if fmReg.TestConnection(RegRec.DatabaseName, fmEnterPass.edUser.Text, fmEnterPass.edPassword.Text,
+            RegRec.Charset) then
           begin
           begin
-            RegisteredDatabases[FDestIndex].RegRec.UserName:= fmEnterPass.edUser.Text;
-            RegisteredDatabases[FDestIndex].RegRec.Password:= fmEnterPass.edPassword.Text;
-            RegisteredDatabases[FDestIndex].RegRec.Role:= fmEnterPass.cbRole.Text;
+            with fmMain do
+            begin
+              RegisteredDatabases[FDestIndex].RegRec.UserName:= fmEnterPass.edUser.Text;
+              RegisteredDatabases[FDestIndex].RegRec.Password:= fmEnterPass.edPassword.Text;
+              RegisteredDatabases[FDestIndex].RegRec.Role:= fmEnterPass.cbRole.Text;
+            end
           end
           end
           else
           else
           begin
           begin
-            Exit;
+            exit;
           end;
           end;
-      end
-    end;
-
-    SQLTarget:=TSQLQuery.Create(nil);
-    try
-      SQLTarget.DataBase:=IBConnection;
-      SQLTarget.Transaction:=SQLTrans;
-      SQLTarget.SQL.Text:='select * from '+FDestTable;
-      SQLTarget.ParseSQL:=true;
+        end;
+      end;
 
 
       // Start import
       // Start import
       if SQLTrans.Active then
       if SQLTrans.Active then
         SQLTrans.RollBack;
         SQLTrans.RollBack;
       SQLTrans.StartTransaction;
       SQLTrans.StartTransaction;
-      SQLTarget.Open;
+      FDestinationQuery.Open;
       Num:=0;
       Num:=0;
       try
       try
         while FImporter.ReadRow do
         while FImporter.ReadRow do
         begin
         begin
-          SQLTarget.Insert;
+          FDestinationQuery.Insert;
           for I:=0 to FImporter.MappingCount-1 do
           for I:=0 to FImporter.MappingCount-1 do
           begin
           begin
-            DestColumn := FImporter.Mapping[I].DestinationField;
+            DestColumn:=FImporter.Mapping[I].DestinationField;
             // Note: csv import sees everything as strings so let the db convert if possible
             // Note: csv import sees everything as strings so let the db convert if possible
-            SQLTarget.Fields.FieldByName(DestColumn).AsString:=FImporter.GetData(i);
+            FDestinationQuery.Fields.FieldByName(DestColumn).AsString:=FImporter.GetData(i);
           end;
           end;
-          SQLTarget.Post;
+          FDestinationQuery.Post;
           Inc(Num);
           Inc(Num);
         end;
         end;
+        FDestinationQuery.ApplyUpdates;
+        // could be also done after e.g. every 1000 records for
+        // higher performance
         SQLTrans.Commit;
         SQLTrans.Commit;
-        SQLTarget.Close;
+        FDestinationQuery.Close;
+        Screen.Cursor:=crDefault; // for message
         ShowMessage(IntToStr(Num) + ' record(s) have been imported');
         ShowMessage(IntToStr(Num) + ' record(s) have been imported');
       except
       except
         on E: Exception do
         on E: Exception do
         begin
         begin
-          MessageDlg('Error while copy: ' + e.Message, mtError, [mbOk], 0);
+          MessageDlg('Error while importing: ' + e.Message, mtError, [mbOk], 0);
           SQLTrans.Rollback;
           SQLTrans.Rollback;
         end;
         end;
       end;
       end;
-    finally
-      SQLTarget.Free;
     end;
     end;
+  finally
+    bbClose.Enabled:=true;
+    bbImport.Enabled:=true;
+    Screen.Cursor:=crDefault;
   end;
   end;
-  FDestinationQuery.Close;
 end;
 end;
 
 
 procedure TfmImportTable.Init(DestinationIndex: Integer; DestinationTableName: string);
 procedure TfmImportTable.Init(DestinationIndex: Integer; DestinationTableName: string);