使用TUniSQLMonitor监视SQL语句执行的耗时

//代码来自官方论坛
type
TUniSqlMonitorEx = class(TUniSqlMonitor) private FExecuteDuration: Cardinal; FFinalEvent: Boolean; protected procedure InternalSQLExecute(Obj: TObject; const SQL: string; Params: TDAParams; const Caption: string; BeforeEvent: boolean; var MessageID: Cardinal); override; public property ExecuteDuration: Cardinal read FExecuteDuration; Property IsFinalEvent: Boolean read FFinalEvent; end; TForm1 = class(TForm) UniConnection1: TUniConnection; UniQuery1: TUniQuery; OracleUniProvider1: TOracleUniProvider; procedure FormCreate(Sender: TObject); private UniSQLMonitor: TUniSqlMonitorEx; procedure onSQL(Sender: TObject; Text: String; Flag: TDATraceFlag); { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} { TUniSqlMonitorEx } procedure TUniSqlMonitorEx.InternalSQLExecute(Obj: TObject; const SQL: string; Params: TDAParams; const Caption: string; BeforeEvent: boolean; var MessageID: Cardinal); begin if BeforeEvent then begin FFinalEvent := False; FExecuteDuration := GetTickCount; end; inherited; if not BeforeEvent then begin FFinalEvent := True; FExecuteDuration := (GetTickCount - FExecuteDuration); FOnSQLEvent(Obj, SQL, tfQExecute); end; end; procedure TForm1.FormCreate(Sender: TObject); begin UniSQLMonitor := TUniSqlMonitorEx.Create(Application.Owner); UniSQLMonitor.OnSQL := Self.onSQL; UniQuery1.Execute; end;
procedure TForm1.onSQL(Sender: TObject; Text: String; Flag: TDATraceFlag);
begin
  if (Sender is TUniQuery) and (Flag = tfQExecute) then
  begin
    if UniSQLMonitor.IsFinalEvent then
      ShowMessage(FloatToStr(UniSQLMonitor.ExecuteDuration / 100))
    else
      FCurrentSQL := Text;
  end
  else if (Flag = tfError) then
      ShowMessage(FCurrentSQL + ', Error:' + Text);
end;

 

posted @ 2022-12-09 14:26  lybingyu  阅读(77)  评论(0编辑  收藏  举报