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

📄 qimport3.pas

📁 Advanced Data Import Component Suite for Borland Delphi and C++ Builder allows you to import your da
💻 PAS
📖 第 1 页 / 共 5 页
字号:
      begin
        if  i < StringGrid.ColCount then Result := i;
      end
      else begin
        if GridCaptionRow > -1 then
          for i := 0 to StringGrid.ColCount - 1 do
            if AnsiCompareStr(StringGrid.Cells[i, GridCaptionRow], ColName) = 0 then
            begin
              Result := i;
              Exit;
            end;
      end;
    end;
    {$ENDIF}
  end;
end;

procedure TQImport3.DestinationDisableControls;
begin
  if FIsCSV then Exit;
  case ImportDestination of
    qidDataSet: DataSet.DisableControls;
    {$IFNDEF NOGUI}
    qidDBGrid: DBGrid.DataSource.DataSet.DisableControls;
    qidListView: ListView.Items.BeginUpdate;
    {$ENDIF}
  end;
end;

procedure TQImport3.DestinationEnableControls;
begin
  if FIsCSV then Exit;
  case ImportDestination of
    qidDataSet: DataSet.EnableControls;
    {$IFNDEF NOGUI}
    qidDBGrid: DBGrid.DataSource.DataSet.EnableControls;
    qidListView: ListView.Items.EndUpdate;
    {$ENDIF}
  end;
end;

procedure TQImport3.CheckDestination;
begin
  QImportCheckDestination(IsCSV, ImportDestination, DataSet
    {$IFNDEF NOGUI}, DBGrid, ListView,
 StringGrid{$ENDIF});
end;

function TQImport3.DestinationFindByKey: boolean;
var
  Keys, strValue: string;
  Values: Variant;
  i{$IFNDEF NOGUI}, j{$ENDIF}: Integer;
  Col: TQImportCol;
  {$IFNDEF NOGUI}Flag: Boolean; {$ENDIF}
begin
  Result := false;
  if FIsCSV then Exit;
  case ImportDestination of
    qidDataSet:
      if KeyColumns.Count > 0 then
      begin
        Keys := EmptyStr;
        if KeyColumns.Count > 1 then
          Values := VarArrayCreate([0, KeyColumns.Count - 1], varVariant);
        for i := 0 to KeyColumns.Count - 1 do
        begin
          Col := FImportRow.ColByName(KeyColumns[i]);
          if Assigned(Col) then
          begin
            Keys := Keys + Col.Name;
            if KeyColumns.Count > 1 then
              Values[i] := Col.Value
            else
              Values := Col.Value;
          end;
          if i < KeyColumns.Count - 1 then
            Keys := Keys + ';';
        end;
        if Assigned(FOnDestinationLocate) then
          FOnDestinationLocate(Self, KeyColumns, FImportRow, Keys, Values);
        try
          Result := DataSet.Locate(Keys, Values, [loCaseInsensitive])
        except
          Result := False;
        end;
      end;
    {$IFNDEF NOGUI}
    qidDBGrid:
      if KeyColumns.Count > 0 then
      begin
        Keys := EmptyStr;
        if KeyColumns.Count > 1 then
          Values := VarArrayCreate([0, KeyColumns.Count - 1], varVariant);
        for i := 0 to KeyColumns.Count - 1 do
        begin
          Col := FImportRow.ColByName(KeyColumns[i]);
          if Assigned(Col) then
          begin
            Keys := Keys + DBGrid.Columns[Col.FColumnIndex].Field.FieldName;
            if KeyColumns.Count > 1 then
              Values[i] := Col.Value
            else
              Values := Col.Value;
          end;
          if i < KeyColumns.Count - 1 then Keys := Keys + ';';
        end;
        if Assigned(FOnDestinationLocate) then
          FOnDestinationLocate(Self, KeyColumns, FImportRow, Keys, Values);
        Result := DBGrid.DataSource.DataSet.Locate(Keys, Values, [loCaseInsensitive])
      end;
    qidListView: begin
      for i := 0 to ListView.Items.Count - 1 do
      begin
        Flag := true;
        for j := 0 to KeyColumns.Count - 1 do
        begin
          Col := FImportRow.ColByName(KeyColumns[j]);
          if Assigned(Col) then
          begin
            strValue := Col.Value;
            if Col.FColumnIndex = 0 then
              Flag := AnsiCompareText(strValue, ListView.Items[i].Caption) = 0
            else
              Flag := AnsiCompareText(strValue, ListView.Items[i].SubItems[Col.FColumnIndex - 1]) = 0;
            if not Flag then
              Break;
          end
          else
            Exit;
        end;
        if Flag then
        begin
          Result := true;
          FCurrListItem := ListView.Items[i];
          Exit;
        end;
      end;
    end;
    qidStringGrid: begin
      for i := 0 to StringGrid.RowCount - 1 do
      begin
        if (GridCaptionRow > -1) and (i = GridCaptionRow) then
          Continue;
        Flag := true;
        for j := 0 to KeyColumns.Count - 1 do
        begin
          Col := FImportRow.ColByName(KeyColumns[j]);
          if Assigned(Col) then
          begin
            strValue := Col.Value;
            Flag := AnsiCompareText(strValue, StringGrid.Cells[Col.FColumnIndex, i]) = 0
          end
          else
            Exit;
          if not Flag then
            Break;
        end;
        if Flag then
        begin
          Result := true;
          FCurrStrGrRow := i;
          Exit;
        end;
      end;
    end;
    {$ENDIF}
  end;
end;

function TQImport3.DestinationColCount: integer;
begin
  Result := QImportDestinationColCount(IsCSV, ImportDestination, DataSet
    {$IFNDEF NOGUI}, DBGrid, ListView, StringGrid{$ENDIF});
end;

function TQImport3.DestinationColName(Index: integer): string;
begin
  Result := QImportDestinationColName(IsCSV, ImportDestination, DataSet,
    {$IFNDEF NOGUI}DBGrid, ListView, StringGrid, GridCaptionRow,{$ENDIF} Index);
end;

procedure TQImport3.DoBeginImport;
begin
  if Assigned(FOnBeforeImport) then
    FOnBeforeImport(Self);
end;

function TQImport3.DoBeforePost: Boolean;
begin
  Result := true;
  if Assigned(FOnBeforePost) then
    FOnBeforePost(Self, FImportRow, Result);
end;

procedure TQImport3.DoAfterPost;
begin
  if Assigned(FOnAfterPost) then
    FOnAfterPost(Self, FImportRow);
end;

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

procedure TQImport3.DoImportError(Error: Exception);
begin
  FCustomImportError := True;
  LastError := AnsiString(Format(QImportLoadStr(QIW_ImportErrorFormat),
                [FormatDateTime(Formats.ShortDateFormat + ' ' +
                 Formats.ShortTimeFormat, Now), FCurrentLineNumber, Error.Message]));

  if FErrorLog then
  begin
    FErrors.Add( string(LastError));
    if Assigned(FErrorLogFS) then
    begin
      lastError := lastError + ansiString(LF);
      FErrorLogFS.Write( AnsiString(lastError)[1], Length(LastError)*SizeOf(AnsiChar));
    end;
  end;

  Inc(FCurrentLineNumber);

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

procedure TQImport3.WriteErrorLog(const ErrorMsg: string);
begin
  LastError := ANsiString(Format(QImportLoadStr(QIW_ImportErrorFormat),
                  [FormatDateTime(Formats.ShortDateFormat + ' ' +
                   Formats.ShortTimeFormat, Now), FCurrentLineNumber, ErrorMsg]));
  if FErrorLog then
  begin
    if Assigned(FErrorLogFS) then
    begin
      lastError := lastError + ansiString(LF);
      FErrorLogFS.Write( AnsiString(lastError)[1], Length(lastError)*SizeOf(AnsiChar));
    end;
  end;

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

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

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

procedure TQImport3.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 TQImport3.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.MapNameIdxHash.TableSize := FMap.Count;
  FMappedColumns.Clear;

  for i := 0 to FMap.Count - 1 do
  begin
    k := DestinationFindColumn(FMap.Names[i]);
    if (k = -1) and (FImportDestination <> qidUserDefined) then
      raise EQImportError.CreateFmt(QImportLoadStr(QIE_FieldNotFound), [FMap.Names[i]]);
    with FImportRow.Add(FMap.Names[i]) do
      FColumnIndex := k;
    FImportRow.MapNameIdxHash.Insert(FMap.Names[i], Pointer(i));
    if not IsKeyColumn(FMap.Names[i]) then
      FMappedColumns.Add(FMap.Names[i]);
  end;

  for i := 0 to FFieldFormats.Count - 1 do
  begin
    Col := FImportRow.ColByName(FFieldFormats.Items[i].FieldName);
    if not Assigned(Col) and ((FFieldFormats[i].GeneratorStep <> 0) or
      (FFieldFormats[i].ConstantValue <> '')) 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 Assigned(Col) and (FFieldFormats[i].GeneratorStep <> 0) then
      FImportGenerators.Add(FFieldFormats.Items[i].FieldName,
        FFieldFormats[i].GeneratorValue, FFieldFormats[i].GeneratorStep);
  end;
end;

procedure TQImport3.BeforeImport;
var
  ErrorLogMode: word;
  str: AnsiString;
  path, 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;
    path := ExtractFilePath(ELFN);
    if path = EmptyStr then
      ELFN := ExtractFilePath(ParamStr(0)) + ELFN
    else if not DirectoryExists(path) then
      ForceDirectories(path);

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

    str := AnsiString(Format(QImportLoadStr(QIW_ErrorLogStarted),
      [FormatDateTime(Formats.ShortDateFormat + ' ' + Formats.ShortTimeFormat,
        Now)]) + LF + LF);
    FErrorLogFS.Write( AnsiString(str)[1], Length(str)*SizeOf(AnsiChar));
  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 TQImport3.DoImport;
var
  ImpRes: TQImportResult;
begin
  if FIsCSV then
  begin
    if Assigned(FOnSetCharsetType) then
      FOnSetCharsetType(Self, FTempFileCharset);
  end;

  StartImport;
  try
    while CheckCondition do
    begin
      FLastAction := qiaNone;
      FCustomImportError := False;
      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

⌨️ 快捷键说明

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