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

📄 changenotification.pas

📁 odac for oralce 8i,10g,11g easy to connect to oralce from delphi
💻 PAS
字号:
unit ChangeNotification;

interface

uses
  Windows, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  DBCtrls, ExtCtrls, StdCtrls, ComCtrls, Buttons, Grids, DBGrids, OdacVcl,
  DB, MemUtils, MemDS, DBAccess, Ora, OraClasses, OraCall,
  OdacDemoForm, OdacDemoFrame;

type
  TChangeNotificationFrame = class(TOdacDemoFrame)
    ToolBar: TPanel;
    Panel1: TPanel;
    Query1: TOraQuery;
    DataSource1: TDataSource;
    btOpen: TSpeedButton;
    btClose: TSpeedButton;
    OraSession2: TOraSession;
    Panel2: TPanel;
    DBGrid1: TDBGrid;
    ToolBar1: TPanel;
    Label6: TLabel;
    Panel8: TPanel;
    DBNavigator2: TDBNavigator;
    Splitter1: TSplitter;
    Panel9: TPanel;
    DBGrid2: TDBGrid;
    ToolBar2: TPanel;
    Label7: TLabel;
    Panel10: TPanel;
    DBNavigator3: TDBNavigator;
    Query2: TOraQuery;
    DataSource2: TDataSource;
    meLog: TMemo;
    Splitter2: TSplitter;
    Panel3: TPanel;
    cbReflect: TCheckBox;
    ChangeNotification: TOraChangeNotification;
    procedure btOpenClick(Sender: TObject);
    procedure btCloseClick(Sender: TObject);
    procedure ChangeNotificationChange(Sender: TObject;
      NotifyType: TChangeNotifyEventType;
      TableChanges: TNotifyTableChanges);
    procedure cbReflectClick(Sender: TObject);
  private
    function ChangeNotifyOperationsToString(Ops: TChangeNotifyOperations): string;
    function ChangeNotifyEventTypeToString(Event: TChangeNotifyEventType): string;
  public
    procedure Initialize; override;
    procedure SetDebug(Value: boolean); override;
    procedure SetDirect(Value: boolean); override;
  end;

implementation

{$IFDEF CLR}
{$R *.nfm}
{$ENDIF}
{$IFDEF WIN32}
{$R *.dfm}
{$ENDIF}

procedure TChangeNotificationFrame.Initialize;
begin
  inherited;

  Query1.Connection := Connection;
  cbReflect.Checked := Query1.Options.ReflectChangeNotify;
end;

procedure TChangeNotificationFrame.SetDebug(Value: boolean);
begin
  Query1.Debug := Value;
  Query2.Debug := Value;
end;

procedure TChangeNotificationFrame.SetDirect(Value: boolean);
begin
  OraSession2.Options.Direct := Value;
end;

procedure TChangeNotificationFrame.btOpenClick(Sender: TObject);
begin
  if Application.MessageBox('CHANGE NOTIFICATION privilege must be granted before using change notification. Continue?',
    PChar(Application.Title), MB_ICONQUESTION or MB_YESNO) <> IDYES
  then
    Exit;
    
  Query1.Open;
  if not OraSession2.Connected then begin
    AssignConnectionTo(OraSession2);
    OraSession2.Connect;
  end;
  Query2.Open;
end;

procedure TChangeNotificationFrame.btCloseClick(Sender: TObject);
begin
  Query1.Close;
  Query2.Close;
end;

function TChangeNotificationFrame.ChangeNotifyOperationsToString(Ops: TChangeNotifyOperations): string;
begin
  if cnoInsert in Ops then
    Result := Result + 'Insert' + ', ';
  if cnoUpdate in Ops then
    Result := Result + 'Update' + ', ';
  if cnoDelete in Ops then
    Result := Result + 'Delete' + ', ';
  if cnoAllRows in Ops then
    Result := Result + 'AllRows' + ', ';
  if cnoAlter in Ops then
    Result := Result + 'Alter' + ', ';
  if cnoDrop in Ops then
    Result := Result + 'Drop';
end;

function TChangeNotificationFrame.ChangeNotifyEventTypeToString(Event: TChangeNotifyEventType): string;
begin
  case Event of
    cneNone:
      Result := 'None';
    cneStartup:
      Result := 'Startup';
    cneShutdown:
      Result := 'Shutdown';
    cneShutdownAny:
      Result := 'ShutdownAny';
    cneDropDB:
      Result := 'DropDB';
    cneDereg:
      Result := 'Dereg';
    cneObjChange:
      Result := 'ObjChange';
  end;
end;

procedure TChangeNotificationFrame.ChangeNotificationChange(Sender: TObject;
  NotifyType: TChangeNotifyEventType; TableChanges: TNotifyTableChanges);
var
  i, j: integer;
begin
  meLog.Lines.Add('EventType: ' + ChangeNotifyEventTypeToString(NotifyType));

  if NotifyType <> cneObjChange then
    Exit;

  meLog.Lines.Add('Table Count: ' + IntToStr(TableChanges.Count));

  for i := 0 to TableChanges.Count - 1 do begin
    meLog.Lines.Add(TableChanges[i].TableName);
    meLog.Lines.Add('Operations: ' + ChangeNotifyOperationsToString(TableChanges[i].Operations));

    if cnoAllRows in TableChanges[i].Operations then
      Continue;

    for j := 0 to TableChanges[i].RowChanges.Count - 1 do begin
      meLog.Lines.Add('RowId: ' + TableChanges[i].RowChanges[j].RowId);
      meLog.Lines.Add('Row operations: ' +
        ChangeNotifyOperationsToString(TableChanges[i].RowChanges[j].Operations));
    end;
  end;

  meLog.Lines.Add('');
end;

procedure TChangeNotificationFrame.cbReflectClick(Sender: TObject);
begin
  try
    Query1.Options.ReflectChangeNotify := cbReflect.Checked;
  except
    cbReflect.Checked := Query1.Options.ReflectChangeNotify;
    raise;
  end;
end;

end.

⌨️ 快捷键说明

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