⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qimport2.pas

📁 EMS Advanced.Import.Component.Suite.v2.43
💻 PAS
📖 第 1 页 / 共 5 页
字号:

            ftFloat,
            ftCurrency,
            ftBCD: begin
              for j := Length(str2) downto 1 do
                if str2[j] = SysUtils.ThousandSeparator then
                  Delete(str2, j, 1)
                else if str2[j] = SysUtils.DecimalSeparator then begin
                  Delete(str2, j, 1);
                  Insert('.', str2, j);
                end;
            end;

            ftDate,
            ftTime,
            ftDateTime: str2 := FormatDateTime('yyyy-mm-dd', StrToDate(str2));
          end;
      end;

      j := 0;
      case Row.QImport.LastAction of
        qiaInsert: begin
          str := str + FormatIdentifier(Row[i].Name, CurrentDatabase);
          str1 := str1 + str2;
          if i < Row.Count - 1 then begin
            str := str + ', ';
            str1 := str1 + ', ';
          end;
        end;
        qiaUpdate: begin
          if Row.QImport.KeyColumns.IndexOf(Row[i].Name) > - 1 then begin
            str1 := str1 + FormatIdentifier(Row[i].Name, CurrentDatabase) + ' = ' + str2;
            Inc(j);
            if j < Row.QImport.KeyColumns.Count then
              str1 := str1 + LF + 'AND    ';
          end
          else begin
            if str <> EmptyStr then str := str + '       ';
            str := str + FormatIdentifier(Row[i].Name, CurrentDatabase) + ' = ' + str2 ;
            Inc(k);
            if k < Row.Count - Row.QImport.KeyColumns.Count then
              str := str + ',' + LF;
          end;
        end;
        qiaDelete: begin
          if Row.QImport.KeyColumns.IndexOf(Row[i].Name) > - 1 then begin
            str := str + FormatIdentifier(Row[i].Name, CurrentDatabase) + ' = ' + str2;
            Inc(j);
            if j < Row.QImport.KeyColumns.Count then
              str := str + LF + 'AND    ';
          end
        end;
      end;
    end;
    case Row.QImport.LastAction of
      qiaInsert: str := Format(sINSERT,
        [FormatIdentifier(TableName, CurrentDatabase), str, str1]) + LF + LF;
      qiaUpdate: str := Format(sUPDATE,
        [FormatIdentifier(TableName, CurrentDatabase), str, str1]) + LF + LF;
      qiaDelete: str := Format(sDELETE,
        [FormatIdentifier(TableName, CurrentDatabase), str]) + LF + LF;
    end;
      FSQLLog.Write(str[1], Length(str));
  end;}

  if Assigned(FOnAfterPost) then FOnAfterPost(Self, FImportRow);
end;

procedure TQImport2.DoImportRecord;
begin
  Inc(FImportedRecs);
  if Assigned(FOnImportRecord) then
    FOnImportRecord(Self);
end;

procedure TQImport2.DoImportError(Error: Exception);
var
  lastError: string;
begin
  if FErrorLog then
  begin
    lastError := Format({$IFDEF WIN32}QImportLoadStr(QIW_ImportErrorFormat){$ENDIF}
                  {$IFDEF LINUX}QIW_ImportErrorFormat{$ENDIF},
                  [FormatDateTime(Formats.ShortDateFormat + ' ' +
                   Formats.ShortTimeFormat, Now), FCurrentLineNumber, Error.Message]);
    FErrors.Add(lastError);
    if Assigned(FErrorLogFS) then
    begin
      lastError := lastError + LF;
      FErrorLogFS.Write(lastError[1], Length(lastError));
    end;
  end;

  Inc(FCurrentLineNumber);

  if Assigned(FOnImportError) then
    FOnImportError(Self);
end;

procedure TQImport2.WriteErrorLog(const ErrorMsg: string);
var
  lastError: string;
begin
  if FErrorLog then
  begin
    lastError := Format({$IFDEF WIN32}QImportLoadStr(QIW_ImportErrorFormat){$ENDIF}
                  {$IFDEF LINUX}QIW_ImportErrorFormat{$ENDIF},
                  [FormatDateTime(Formats.ShortDateFormat + ' ' +
                   Formats.ShortTimeFormat, Now), FCurrentLineNumber, ErrorMsg]);
    
    if Assigned(FErrorLogFS) then
    begin
      lastError := lastError + LF;
      FErrorLogFS.Write(lastError[1], Length(lastError));
    end;
  end;

  if Assigned(FOnImportErrorAdv) then
    FOnImportErrorAdv(Self);
end;

procedure TQImport2.DoNeedCommit;
begin
  if Assigned(FOnNeedCommit) then
    FOnNeedCommit(Self);
end;

procedure TQImport2.DoEndImport;
begin
  if Assigned(FOnAfterImport) then FOnAfterImport(Self);
end;

procedure TQImport2.Notification(AComponent: TComponent; Operation: TOperation);
begin
  inherited;
  if (AComponent = FDataSet) and (Operation = opRemove)
    then FDataSet := nil;
  {$IFNDEF NOGUI}
  if (AComponent = FDBGrid) and (Operation = opRemove)
    then FDBGrid := nil;
  if (AComponent = FListView) and (Operation = opRemove)
    then FListView := nil;
  if (AComponent = FStringGrid) and (Operation = opRemove)
    then FStringGrid := nil;
  {$ENDIF}
end;

procedure TQImport2.InitializeImportRow;

  function IsKeyColumn(const AColumnName: string): Boolean;
  var
    i: Integer;
  begin
    Result := False;
    for i := 0 to KeyColumns.Count - 1 do
    begin
      if CompareStr(KeyColumns[i], AColumnName) = 0 then
      begin
        Result := True;
        Break;
      end;
    end;
  end;

var
  i, k: integer;
  Col: TQImportCol;
begin
  FImportRow.Clear;
  FImportRow.FIndex.Clear;
  FMappedColumns.Clear;

  for i := 0 to FMap.Count - 1 do begin
    k := DestinationFindColumn(FMap.Names[i]);
    if (k > -1) or IsCSV or (FImportDestination = qidUserDefined) then begin
      with FImportRow.Add(FMap.Names[i]) do FColumnIndex := k;
      FImportRow.FIndex.AddObject(FMap.Names[i], TObject(i));
      if not IsKeyColumn(FMap.Names[i]) then
        FMappedColumns.Add(FMap.Names[i]);
    end;
    {if Assigned(FDataSet.FindField(FMap.Names[i])) then begin
      FImportRow.Add(FMap.Names[i]);
      FImportRow.FIndex.AddObject(FMap.Names[i], TObject(i));
    end;}
  end;
  FImportRow.FIndex.Sort;

  for i := 0 to FFieldFormats.Count - 1 do
  begin
    Col := FImportRow.ColByName(FFieldFormats.Items[i].FieldName);
    if not Assigned(Col) {ab}and ((FFieldFormats[i].GeneratorStep <> 0) or
      (FFieldFormats[i].ConstantValue <> '')){/ab} then
    begin
      k := DestinationFindColumn(FFieldFormats.Items[i].FieldName);
      if k > -1 then
      begin
        Col := FImportRow.Add(FFieldFormats.Items[i].FieldName);
        Col.FColumnIndex := k;
      end;
    end;
    {if not Assigned(Col) and
       Assigned(FDataSet.FindField(FFieldFormats.Items[i].FieldName)) then
      FImportRow.Add(FFieldFormats.Items[i].FieldName);}

    // ab Col := FImportRow.ColByName(FFieldFormats.Items[i].FieldName);
    if Assigned(Col) and (FFieldFormats[i].GeneratorStep <> 0) then
      FImportGenerators.Add(FFieldFormats.Items[i].FieldName,
        FFieldFormats[i].GeneratorValue, FFieldFormats[i].GeneratorStep);
  end;
end;

procedure TQImport2.BeforeImport;
var
  ErrorLogMode: word;
  str: string;
  ELFN: string;
{var
  SQLLogMode: word;}
begin
  if (FImportRecCount > 0)  and (ImportRecCount < FTotalRecCount)
    then FTotalRecCount := FImportRecCount;

  FLastAction := qiaNone;
  FImportedRecs := 0;
  FErrors.Clear;
  FCanceled := false;
  FFormats.StoreFormats;
  FFormats.ApplyParams;
  DestinationDisableControls;
  FCurrentLineNumber := 1;

  {$IFNDEF NOGUI}
  FCurrListItem := nil;
  FCurrStrGrRow := FGridStartRow;
  {$ENDIF}

  InitializeImportRow;

  if FErrorLog and (FErrorLogFileName <> EmptyStr) then
  begin
    if FRewriteErrorLogFile
      then ErrorLogMode := fmCreate
      else ErrorLogMode := fmOpenReadWrite;

    ELFN := FErrorLogFileName;
    str := ExtractFilePath(ELFN);
    if str = EmptyStr then
      ELFN := ExtractFilePath(ParamStr(0)) + ELFN
    else if not DirectoryExists(str) then
      ForceDirectories(str);

    FErrorLogFS := TFileStream.Create(ELFN, ErrorLogMode);
    FErrorLogFS.Position := FErrorLogFS.Size;

    str := Format({$IFDEF WIN32}QImportLoadStr(QIW_ErrorLogStarted){$ENDIF}
                  {$IFDEF LINUX}QIW_ErrorLogStarted{$ENDIF},
      [FormatDateTime(Formats.ShortDateFormat + ' ' + Formats.ShortTimeFormat,
        Now)]) + LF + LF;
    FErrorLogFS.Write(str[1], Length(str));
  end;

{  if FSQLLog and (SQLLogFileName <> EmptyStr) then begin
    if FSQLLogFileRewrite
      then SQLLogMode := fmCreate
      else SQLLogMode := fmOpenReadWrite;
    FSQL := TFileStream.Create(SQLLogFileName, SQLLogMode);
    FSQL.Position := FSQL.Size;
  end;}
end;

procedure TQImport2.DoImport;
var
  ImpRes: TQImportResult;
begin
  StartImport;
  try
    Inc(FCurrentLineNumber, FSkipFirstRows);
    while CheckCondition do
    begin
      FLastAction := qiaNone;
      if not Skip then
      begin
        FillImportRow;
        if FImportDestination = qidUserDefined then
        begin
          if Assigned(FOnUserDefinedImport) then
            FOnUserDefinedImport(Self, FImportRow);
        end
        else begin
          if Assigned(FOnImportRowComplete) then
            FOnImportRowComplete(Self, FImportRow);
          ImpRes := ImportData;
          case ImpRes of
            qirBreak: Break;
            qirContinue:
              begin
                ChangeCondition;
                Continue;
              end
          end;
        end;
      end
      else
        Inc(FCurrentLineNumber);
      ChangeCondition;
{$IFDEF WIN32}
      Sleep(0);
{$ENDIF}
    end;
  finally
    FinishImport;
  end;
end;

procedure TQImport2.DataManipulation;
begin
  if IsCSV then
  begin
    DestinationSetValues;
  end
  else begin
    case ImportMode of
      qimInsertAll:
        if DoBeforePost then
          DestinationInsert;
      qimInsertNew:
        if not DestinationFindByKey and
           not DestinationFindByFields and DoBeforePost then
          DestinationInsert;
      qimUpdate:
        if DestinationFindByKey and DoBeforePost then
          DestinationEdit;
      qimUpdateOrInsert:
        if DestinationFindByKey then
        begin
          if DoBeforePost then
            DestinationEdit
        end
        else begin
          if not DestinationFindByFields and DoBeforePost then
            DestinationInsert;
        end;
      qimDelete:
        if DestinationFindByKey and DoBeforePost then
          DestinationDelete;
      qimDeleteOrInsert:
        if DestinationFindByKey then
        begin
          if DoBeforePost then
            DestinationDelete
        end
        else begin
          if not DestinationFindByFields and DoBeforePost then
            DestinationInsert;
        end;
    end;

    if LastAction in [qiaInsert, qiaUpdate] then
    begin
      DestinationSetValues;
      DestinationPost;
    end;
    DoAfterPost;
  end;
  DoImportRecord;
  Inc(FCurrentLineNumber);
end;

procedure TQImport2.AfterImport;
var
  i: integer;
  str: string;
//  Path, FLogName: string;
begin
  FFormats.RestoreFormats;
  for i := FImportRow.Count - 1 downto 0 do FImportRow.Delete(i);
//  if FImportGenerators.Count > 0 then
    for i := FImportGenerators.Count - 1 downto 0 do FImportGenerators.Delete(i);
  DestinationEnableControls;

  // Saving ErrorLog To File
{  if (FErrors.Count > 0) and (FErrorLogFileName <> EmptyStr) then begin
    FLogName := FErrorLogFileName;
    Path := ExtractFilePath(FLogName);
    if Path = EmptyStr then begin
      GetDir(0, Path);
      Path := IncludeTrailingBackSlash(Path);
      FLogName := Path + FLogName;
    end;
    if not DirectoryExists(Path) then
      ForceDirectories(Path);
    if DirectoryExists(Path) then
      FErrors.SaveToFile(FLogName);
  end;}

  if Assigned(FErrorLogFS) then begin
    str := Format({$IFDEF WIN32}QImportLoadStr(QIW_ErrorLogFinished){$ENDIF}
                  {$IFDEF LINUX}QIW_ErrorLogFinished{$ENDIF},
      [FormatDateTime(Formats.ShortDateFormat + ' ' + Formats.ShortTimeFormat,
        Now)]) + LF;
    if FErrors.Count > 0 then
      str := LF + str;
    FErrorLogFS.Write(str[1], Length(str));

    if FErrors.Count > 0
      then str := Format({$IFDEF WIN32}QImportLoadStr(QIW_SomeErrorsFound){$ENDIF}
                         {$IFDEF LINUX}QIW_SomeErrorsFound{$ENDIF},
                         [FErrors.Count]) + LF + LF
      else str := {$IFDEF WIN32}QImportLoadStr(QIW_NoErrorsFound){$ENDIF}
               

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -