unit BidLotDM; interface uses SysUtils, Classes, DB, ConstTypeUnit, ADODB; type TBidLotDataModule = class(TDataModule) atBidLot: TADOTable; atBidLotID: TIntegerField; atBidLotProjName: TWideStringField; atBidLotAliasName: TWideStringField; atBidLotFullName: TWideStringField; atBidLotFlag: TIntegerField; procedure atBidLotAfterPost(DataSet: TDataSet); private FProject: TObject; function MaxBidLotID: Integer; function CheckBidLot(const aProjName, aFullName: string): Boolean; procedure BeginRefresh; procedure EndRefresh; procedure ExecuteSQL(const aSQL: string); procedure AddBidLot(const aProjName, aFullName: string); procedure DeleteBidLot(const aProjName, aFullName: string); procedure ReNameBidLot(const aProjName, aFullName: string); function GetBidLotDB: TDataSet; procedure SetProject(const Value: TObject); public procedure Notify(aOperation: TBidLotOperation; const aProjName, aFullName: string); procedure RefreshBidLot(aStrings: TStrings); procedure SyncProjectView; property Project: TObject read FProject write SetProject; property BidLotDB: TDataSet read GetBidLotDB; end; implementation uses ScProjectManager; {$R *.dfm} { TBidLotDataModule } procedure TBidLotDataModule.AddBidLot(const aProjName, aFullName: string); begin if not atBidLot.Active then Exit; atBidLot.Append; atBidLotID.Value := MaxBidLotID; atBidLotProjName.Value := aProjName; atBidLotAliasName.Value := aProjName; atBidLotFullName.Value := aFullName; atBidLotFlag.Value := 1; atBidLot.Post; end; procedure TBidLotDataModule.BeginRefresh; var sSQL: string; begin sSQL := 'Update BidLot Set Flag = 0'; ExecuteSQL(sSQL); end; function TBidLotDataModule.CheckBidLot(const aProjName, aFullName: string): Boolean; var aqQuery: TADOQuery; begin aqQuery := TADOQuery.Create(nil); try aqQuery.Connection := TProject(FProject).Connection; aqQuery.SQL.Text := Format('Select * From BidLot Where ' + '(ProjName = ''%s'') and (FullName = ''%s'')', [aProjName, aFullName]); aqQuery.Open; Result := aqQuery.RecordCount > 0; if Result then begin aqQuery.Edit; aqQuery.FieldByName('Flag').AsInteger := 1; aqQuery.Post; end; finally aqQuery.Free; end; end; procedure TBidLotDataModule.DeleteBidLot(const aProjName, aFullName: string); var sSQL: string; begin sSQL := Format('Delete * From BidLot Where ' + '(ProjName = ''%s'') and (FullName = ''%s'')', [aProjName, aFullName]); ExecuteSQL(sSQL); // atBidLot.Refresh; end; procedure TBidLotDataModule.EndRefresh; var sSQL: string; begin sSQL := 'Delete * From BidLot Where Flag = 0'; ExecuteSQL(sSQL); end; procedure TBidLotDataModule.ExecuteSQL(const aSQL: string); var aqQuery: TADOQuery; begin aqQuery := TADOQuery.Create(nil); try aqQuery.Connection := TProject(FProject).Connection; aqQuery.SQL.Text := aSQL; aqQuery.ExecSQL; finally aqQuery.Free; end; end; function TBidLotDataModule.GetBidLotDB: TDataSet; begin Result := atBidLot; end; function TBidLotDataModule.MaxBidLotID: Integer; var aqQuery: TADOQuery; begin aqQuery := TADOQuery.Create(nil); try aqQuery.Connection := TProject(FProject).Connection; aqQuery.SQL.Text := 'Select Max(ID) As ID From BidLot'; aqQuery.Open; Result := aqQuery.FieldByName('ID').AsInteger + 1; finally aqQuery.Free; end; end; procedure TBidLotDataModule.Notify(aOperation: TBidLotOperation; const aProjName, aFullName: string); begin if FProject = nil then Exit; case aOperation of boAdd : AddBidLot(aProjName, aFullName); boDelete : DeleteBidLot(aProjName, aFullName); boReName : ReNameBidLot(aProjName, aFullName); end; TProject(FProject).Changed := True; end; procedure TBidLotDataModule.RefreshBidLot(aStrings: TStrings); var I: Integer; sProjName, sFullName: string; begin BeginRefresh; for I := 0 to aStrings.Count - 1 do begin sProjName := aStrings[I]; sFullName := string(aStrings.Objects[I]); if not CheckBidLot(sProjName, sFullName) then AddBidLot(sProjName, sFullName); end; EndRefresh; atBidLot.Active := False; atBidLot.Active := True; atBidLot.First; end; procedure TBidLotDataModule.ReNameBidLot(const aProjName, aFullName: string); var sSQL: string; begin sSQL := Format('Update BidLot Set ProjName = ''%s'' Where ' + 'FullName = ''%s''', [aProjName, aFullName]); ExecuteSQL(sSQL); end; procedure TBidLotDataModule.SetProject(const Value: TObject); begin FProject := Value; if Assigned(FProject) then begin atBidLot.Connection := TProject(FProject).Connection; atBidLot.Active := True; end; end; procedure TBidLotDataModule.SyncProjectView; var sgsBidLot: TStrings; begin atBidLot.Active := False; atBidLot.Active := True; sgsBidLot := TStringList.Create; try atBidLot.First; while not atBidLot.Eof do begin sgsBidLot.Add(atBidLotAliasName.Value); atBidLot.Next; end; TProject(FProject).BidLotList := sgsBidLot; finally sgsBidLot.Free; end; end; procedure TBidLotDataModule.atBidLotAfterPost(DataSet: TDataSet); begin TProject(FProject).Changed := True; end; end.