See sample.
Code: Select all
program OdacRefreshBeforeEditSample;
{$APPTYPE CONSOLE}
uses
DB, Ora, OraScript, OraSmart, DBAccess, sysutils;
procedure CreateOracleTables(ASession: TOraSession);
var
OraScript: TOraScript;
begin
//script for creation of Oracle tables
OraScript := TOraScript.Create(nil);
try
OraScript.Session := ASession;
OraScript.SQL.BeginUpdate;
OraScript.SQL.Add('DROP TABLE A;');
OraScript.SQL.Add('CREATE TABLE A (');
OraScript.SQL.Add(' ID NUMBER(9) NOT NULL,');
OraScript.SQL.Add(' FIELD1 VARCHAR2(40));');
OraScript.SQL.Add('ALTER TABLE A ADD (CONSTRAINT PK_A_ID PRIMARY KEY (ID));');
OraScript.SQL.Add('INSERT INTO A (ID, FIELD1) VALUES (1, ''Test'');');
OraScript.SQL.Add('COMMIT;');
OraScript.SQL.EndUpdate;
OraScript.Execute;
finally
OraScript.Free;
end;
end;
procedure RunDemonstration(Session: TOraSession);
var
qr1,qr2: TSmartQuery;
begin
qr1 := TSmartQuery.Create(nil);
qr2 := TSmartQuery.Create(nil);
try
qr1.Session := Session;
qr1.SQL.Text := 'SELECT * FROM A';
qr1.KeyFields := 'ID';
qr1.LockMode := lmLockImmediate;
qr1.RefreshOptions := [roAfterInsert,roAfterUpdate,roBeforeEdit];
qr1.Open;
qr2.Session := Session;
qr2.SQL.Text := 'SELECT * FROM A';
qr2.KeyFields := 'ID';
qr2.LockMode := lmLockImmediate;
qr2.RefreshOptions := [roAfterInsert,roAfterUpdate,roBeforeEdit];
//qr2.LockMode := Ora.lmNone;
qr2.RefreshOptions := [roAfterInsert,roAfterUpdate,roBeforeEdit];
qr2.Open;
//Step 1: Both queries are open and show the same record. Now we update the record in only one query and post it.
qr1.Edit;
qr1.FieldByName('FIELD1').AsString := 'Updated';
qr1.Post;
//Step 2: Now we edit the same record in the other query
qr2.Edit;
if qr1.FieldByName('FIELD1').AsString <> qr2.FieldByName('FIELD1').AsString then //check if the record is refreshed
begin
Writeln('No RefreshBeforeEdit was performed!!!!');
Readln;
//RefreshOption contain [roBeforeEdit] but no RefreshRecord is performed before edit.
//If the LockMode of qr2 is set from lmLockImmediate to lmNone the RefreshBeforeEdit works.
//So I have to decide if I want to lock or if I want a RefreshBeforeEdit.
end else
begin
Writeln('RefreshBeforeEdit works.');
Readln;
end;
finally
qr1.Free;
qr2.Free;
end;
end;
var
UserName,Password,Server: String;
OraSession: TOraSession;
begin
//Logon information - Please fill out !!!!
UserName := '';
Password := '';
Server := '';
OraSession := TOraSession.Create(nil);
try
//logon to Database
OraSession.LoginPrompt := False;
OraSession.UserName := UserName;
OraSession.Password := Password;
OraSession.Server := Server;
OraSession.AutoCommit := True;
OraSession.Open;
//creation of needed Oracle tables
CreateOracleTables(OraSession);
//run test
RunDemonstration(OraSession);
finally
OraSession.Free;
end;
end.