unit UpdateDataBase;

interface

uses ScAutoUpdateUnit, ADODB, SysUtils, DataBaseTables, Classes, Connections;

type
  TUpdateDB = class
  private
    procedure UpdateAllTables(AConnection: TADOConnection); virtual;
  public
    constructor Create; virtual;
    destructor Destroy; virtual;

    procedure Update(AConnection: TEncryptConnection);
  end;

  TUpdateManagerDB = class(TUpdateDB)
  private
    Procedure UpdateAllTables(AConnection: TADOConnection); override;
  public
    procedure Update(AConnection: TConnection); overload;
  end;

  TUpdateProjectDB = class(TUpdateDB)
  private
    procedure UpdateAllTables(AConnection: TADOConnection); override;
  public
    procedure ForceUpdate(AConnection: TADOConnection);
  end;

  TUpdatePhaseDB = class(TUpdateDB)
  private
    FAuditList: TStringList;

    procedure UpdateAllTables(AConnection: TADOConnection); override;
    procedure GetAuditList(AConnection: TADOConnection);
    function GetNewAuditTableName(AIndex: Integer): String;
  public
    constructor Create; override;
    destructor Destroy; override;

    procedure AddAuditData(AConnection: TADOConnection; AIndex: Integer);
  end;

implementation

{ TUpdateDB }

constructor TUpdateDB.Create;
begin

end;

destructor TUpdateDB.Destroy;
begin

end;

procedure TUpdateDB.Update(AConnection: TEncryptConnection);
begin
  if AConnection.NeedUpdate then
  begin
    UpdateAllTables(AConnection.Connection);
    AConnection.UpdateFileVersion;
  end;
end;

procedure TUpdateDB.UpdateAllTables(AConnection: TADOConnection);
begin
  // Nothing
end;

{ TUpdateProjectDB }

procedure TUpdateProjectDB.ForceUpdate(AConnection: TADOConnection);
begin
  UpdateAllTables(AConnection);
end;

procedure TUpdateProjectDB.UpdateAllTables(AConnection: TADOConnection);
var
  Updater: TScUpdater;
  iIndex: Integer;
begin
  Updater := TScUpdater.Create;
  try
    Updater.ForceUpdate := True;
    Updater.Open('', AConnection, '', '');
    Updater.AddTableDef(SBills, @tdBills, Length(tdBills), False, False);
    Updater.AddTableDef(SProjProperties, @tdProjProperties, Length(tdProjProperties), False, False);
    Updater.AddTableDef(SDealPayment, @tdDealPayment, Length(tdDealPayment), False, False);
    Updater.AddTableDef(SBGL, @tdBGL, length(tdBGL), False, False);
    Updater.AddTableDef(SBGBills, @tdBGBills, length(tdBGBills), False, False);
    Updater.AddTableDef(SStaff, @tdStaff, Length(tdStaff), False, False);
    Updater.AddTableDef(SDealBills, @tdDealBills, Length(tdDealBills), False, False);
    Updater.AddTableDef(SMainDataList, @tdMainDataList, Length(tdMainDataList), False, False);
    Updater.AddTableDef(SProjectGL, @tdProjectGL, Length(tdProjectGL), False, False);
    Updater.AddTableDef(SGLPrice, @tdGLPrice, Length(tdGLPrice), False, False);
    Updater.AddTableDef(SDetailGL, @tdDetailGL, Length(tdDetailGL), False, False);
    Updater.AddTableDef(SOtherMeasureOnce, @tdOtherMeasureOnce, Length(tdOtherMeasureOnce), False, False);
    Updater.AddTableDef(SOtherMeasurePhase, @tdOtherMeasurePhase, Length(tdOtherMeasurePhase), False, False);
    Updater.AddTableDef(SOMPhaseDetail, @tdOMPhaseDetail, Length(tdOMPhaseDetail), False, False);
    Updater.ExcuteUpdate;
  finally
    Updater.Free;
  end;
end;

{ TUpdatePhaseDB }

procedure TUpdatePhaseDB.AddAuditData(AConnection: TADOConnection;
  AIndex: Integer);
var
  Updater: TScUpdater;
begin
  Updater := TScUpdater.Create;
  try
    Updater.ForceUpdate := True;
    Updater.Open('', AConnection, '', '');
    Updater.AddTableDef(GetNewAuditTableName(AIndex), @tdRefer_Audit, Length(tdRefer_Audit), False, False);
    Updater.ExcuteUpdate;
  finally
    Updater.Free;
  end;
end;

constructor TUpdatePhaseDB.Create;
begin
  inherited;
  FAuditList := TStringList.Create;
end;

destructor TUpdatePhaseDB.Destroy;
begin
  FAuditList.Free;
  inherited;
end;

procedure TUpdatePhaseDB.GetAuditList(AConnection: TADOConnection);
var
  iIndex: Integer;
begin
  AConnection.GetTableNames(FAuditList);
  iIndex := 0;
  while iIndex < FAuditList.Count do
  begin
    if Pos(SAudit, FAuditList.Strings[iIndex]) = 1 then
      Inc(iIndex)
    else
      FAuditList.Delete(iIndex);
  end;
end;

function TUpdatePhaseDB.GetNewAuditTableName(AIndex: Integer): String;
begin
  Result := SAudit + IntToStr(AIndex);
end;

procedure TUpdatePhaseDB.UpdateAllTables(AConnection: TADOConnection);
var
  Updater: TScUpdater;
  iIndex: Integer;
begin
  Updater := TScUpdater.Create;
  try
    GetAuditList(AConnection);

    Updater.ForceUpdate := True;
    Updater.Open('', AConnection, '', '');
    Updater.AddTableDef(SRefer, @tdRefer_Audit, Length(tdRefer_Audit), False, False);
    for iIndex := 0 to FAuditList.Count - 1 do
      Updater.AddTableDef(FAuditList.Strings[iIndex], @tdRefer_Audit, Length(tdRefer_Audit), False, False);
    Updater.AddTableDef(SPhaseProperty, @tdPhaseProperty, Length(tdPhaseProperty), False, False);
    Updater.AddTableDef(SPhasePay, @tdPhasePay, Length(tdPhasePay), False, False);
    Updater.AddTableDef(SZJJL, @tdZJJL, Length(tdZJJL), False, False);
    Updater.AddTableDef(SZJJL_History, @tdZJJL_History, length(tdZJJL_History), False, False);
    Updater.AddTableDef(SZJJL_Detail, @tdZJJL_Detail, length(tdZJJL_Detail), False, False);
    Updater.AddTableDef(SReportData, @tdReportData, Length(tdReportData), False, False);
    Updater.ExcuteUpdate;
  finally
    Updater.Free;
  end;
end;

{ TUpdateManagerDB }

procedure TUpdateManagerDB.Update(AConnection: TConnection);
begin
  UpdateAllTables(AConnection.Connection);
end;

procedure TUpdateManagerDB.UpdateAllTables(AConnection: TADOConnection);
var
  Updater: TScUpdater;
  iIndex: Integer;
begin
  Updater := TScUpdater.Create;
  try
    Updater.ForceUpdate := True;
    Updater.Open('', AConnection, '', '');
    Updater.AddTableDef(SProjectsInfo, @tdProjectsInfo, Length(tdProjectsInfo), False, False);
    Updater.AddTableDef(STenderProperty, @tdTenderProperty, Length(tdTenderProperty), False, False);
    Updater.AddTableDef(STenderBackup, @tdTenderBackup, Length(tdTenderBackup), False, False);
    Updater.ExcuteUpdate;
  finally
    Updater.Free;
  end;
end;

end.
