mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-09-08 10:39:29 +02:00
* AutoCommit option
git-svn-id: trunk@29121 -
This commit is contained in:
parent
4fe1ec9c51
commit
411e798495
@ -60,7 +60,7 @@ type
|
||||
TDBEventTypes = set of TDBEventType;
|
||||
TDBLogNotifyEvent = Procedure (Sender : TSQLConnection; EventType : TDBEventType; Const Msg : String) of object;
|
||||
|
||||
TSQLQueryOption = (sqoDisconnected, sqoAutoApplyUpdates);
|
||||
TSQLQueryOption = (sqoDisconnected, sqoAutoApplyUpdates, sqoAutoCommit);
|
||||
TSQLQueryOptions = Set of TSQLQueryOption;
|
||||
|
||||
TSQLHandle = Class(TObject)
|
||||
@ -317,6 +317,7 @@ type
|
||||
FTransaction: TSQLTransaction;
|
||||
FParseSQL: Boolean;
|
||||
FDataLink : TDataLink;
|
||||
FRowsAffected : TRowsCount;
|
||||
procedure SetDatabase(AValue: TSQLConnection);
|
||||
procedure SetParams(AValue: TParams);
|
||||
procedure SetSQL(AValue: TStrings);
|
||||
@ -463,6 +464,7 @@ type
|
||||
public
|
||||
constructor Create(AOwner : TComponent); override;
|
||||
destructor Destroy; override;
|
||||
Procedure ApplyUpdates(MaxErrors: Integer); override; overload;
|
||||
procedure Prepare; virtual;
|
||||
procedure UnPrepare; virtual;
|
||||
procedure ExecSQL; virtual;
|
||||
@ -869,6 +871,7 @@ end;
|
||||
|
||||
Procedure TCustomSQLStatement.DoExecute;
|
||||
begin
|
||||
FRowsAffected:=-1;
|
||||
If (FParams.Count>0) and Assigned(DataSource) then
|
||||
CopyParamsFromMaster(False);
|
||||
If LogEvent(detExecute) then
|
||||
@ -1074,10 +1077,12 @@ end;
|
||||
|
||||
function TCustomSQLStatement.RowsAffected: TRowsCount;
|
||||
begin
|
||||
Result := -1;
|
||||
if not Assigned(Database) then
|
||||
Exit;
|
||||
Result:=Database.RowsAffected(FCursor);
|
||||
if FRowsAffected=-1 then
|
||||
begin
|
||||
if Assigned(Database) then
|
||||
FRowsAffected:=Database.RowsAffected(FCursor);
|
||||
end;
|
||||
Result:=FRowsAffected;
|
||||
end;
|
||||
|
||||
{ TSQLConnection }
|
||||
@ -2067,6 +2072,17 @@ begin
|
||||
inherited Destroy;
|
||||
end;
|
||||
|
||||
Procedure TCustomSQLQuery.ApplyUpdates(MaxErrors: Integer);
|
||||
begin
|
||||
inherited ApplyUpdates(MaxErrors);
|
||||
If sqoAutoCommit in QueryOptions then
|
||||
begin
|
||||
// Retrieve rows affected for last update.
|
||||
FStatement.RowsAffected;
|
||||
SQLTransaction.Commit;
|
||||
end;
|
||||
end;
|
||||
|
||||
function TCustomSQLQuery.ParamByName(Const AParamName: String): TParam;
|
||||
|
||||
begin
|
||||
@ -2401,6 +2417,12 @@ begin
|
||||
try
|
||||
Prepare;
|
||||
Execute;
|
||||
If sqoAutoCommit in QueryOptions then
|
||||
begin
|
||||
// Retrieve rows affected
|
||||
FStatement.RowsAffected;
|
||||
SQLTransaction.Commit;
|
||||
end;
|
||||
finally
|
||||
// Cursor has to be assigned, or else the prepare went wrong before PrepareStatment was
|
||||
// called, so UnPrepareStatement shoudn't be called either
|
||||
|
@ -46,6 +46,7 @@ type
|
||||
Procedure TestAutoApplyUpdatesPost;
|
||||
Procedure TestAutoApplyUpdatesDelete;
|
||||
Procedure TestCheckRowsAffected;
|
||||
Procedure TestAutoCOmmit;
|
||||
end;
|
||||
|
||||
{ TTestTSQLConnection }
|
||||
@ -183,11 +184,7 @@ begin
|
||||
// Test also that an edit still works.
|
||||
with SQLDBConnector do
|
||||
begin
|
||||
try
|
||||
ExecuteDirect('DROP table testdiscon');
|
||||
except
|
||||
// Ignore
|
||||
end;
|
||||
TryDropIfExist('testdiscon');
|
||||
ExecuteDirect('create table testdiscon (id integer not null, a varchar(10), constraint pk_testdiscon primary key(id))');
|
||||
Transaction.COmmit;
|
||||
for I:=1 to 20 do
|
||||
@ -250,11 +247,7 @@ begin
|
||||
// Check that we can only set QueryOptions when the query is inactive.
|
||||
with SQLDBConnector do
|
||||
begin
|
||||
try
|
||||
ExecuteDirect('DROP table testdiscon');
|
||||
except
|
||||
// Ignore
|
||||
end;
|
||||
TryDropIfExist('testdiscon');
|
||||
ExecuteDirect('create table testdiscon (id integer not null, a varchar(10), constraint pk_testdiscon primary key(id))');
|
||||
Transaction.COmmit;
|
||||
ExecuteDirect(Format('INSERT INTO testdiscon values (%d,''%.6d'')',[1,1]));
|
||||
@ -275,11 +268,7 @@ begin
|
||||
// Test also that POST afterpost event is backwards compatible.
|
||||
with SQLDBConnector do
|
||||
begin
|
||||
try
|
||||
ExecuteDirect('DROP table testdiscon');
|
||||
except
|
||||
// Ignore
|
||||
end;
|
||||
TryDropIfExist('testdiscon');
|
||||
ExecuteDirect('create table testdiscon (id integer not null, a varchar(10), constraint pk_testdiscon primary key(id))');
|
||||
Transaction.COmmit;
|
||||
for I:=1 to 2 do
|
||||
@ -315,11 +304,7 @@ begin
|
||||
// Test that if sqoAutoApplyUpdates is in QueryOptions, then Delete automatically does an ApplyUpdates
|
||||
with SQLDBConnector do
|
||||
begin
|
||||
try
|
||||
ExecuteDirect('DROP table testdiscon');
|
||||
except
|
||||
// Ignore
|
||||
end;
|
||||
TryDropIfExist('testdiscon');
|
||||
ExecuteDirect('create table testdiscon (id integer not null, a varchar(10), constraint pk_testdiscon primary key(id))');
|
||||
Transaction.COmmit;
|
||||
for I:=1 to 2 do
|
||||
@ -357,11 +342,7 @@ begin
|
||||
// Test that if sqoAutoApplyUpdates is in QueryOptions, then Delete automatically does an ApplyUpdates
|
||||
with SQLDBConnector do
|
||||
begin
|
||||
try
|
||||
ExecuteDirect('DROP table testdiscon');
|
||||
except
|
||||
// Ignore
|
||||
end;
|
||||
TryDropIfExist('testdiscon');
|
||||
ExecuteDirect('create table testdiscon (id integer not null, a varchar(10), constraint pk_testdiscon primary key(id))');
|
||||
Transaction.COmmit;
|
||||
for I:=1 to 2 do
|
||||
@ -380,6 +361,45 @@ begin
|
||||
end;
|
||||
end;
|
||||
|
||||
Procedure TTestTSQLQuery.TestAutoCOmmit;
|
||||
var
|
||||
Q: TSQLQuery;
|
||||
T : TSQLTransaction;
|
||||
I, J : Integer;
|
||||
begin
|
||||
with SQLDBConnector do
|
||||
begin
|
||||
TryDropIfExist('testdiscon');
|
||||
ExecuteDirect('create table testdiscon (id integer not null, a varchar(10), constraint pk_testdiscon primary key(id))');
|
||||
if Transaction.Active then
|
||||
Transaction.Commit;
|
||||
end;
|
||||
Q:=SQLDBConnector.Query;
|
||||
Q.QueryOptions:=[sqoAutoCommit];
|
||||
for I:=1 to 2 do
|
||||
begin
|
||||
Q.SQL.Text:=Format('INSERT INTO testdiscon values (%d,''%.6d'');',[i,i]);
|
||||
Q.Prepare;
|
||||
Q.ExecSQL;
|
||||
// We do not commit anything explicitly.
|
||||
end;
|
||||
Q:=Nil;
|
||||
T:=Nil;
|
||||
try
|
||||
T:=TSQLTransaction.Create(Nil);
|
||||
Q:=TSQLQuery.Create(Nil);
|
||||
Q.Transaction:=T;
|
||||
Q.Database:=SQLDBConnector.Connection;
|
||||
T.Database:=SQLDBConnector.Connection;
|
||||
Q.SQL.text:='SELECT COUNT(*) from testdiscon';
|
||||
Q.Open;
|
||||
AssertEquals('Records have been committed to database',2,Q.Fields[0].AsInteger);
|
||||
finally
|
||||
Q.Free;
|
||||
T.Free;
|
||||
end;
|
||||
end;
|
||||
|
||||
|
||||
{ TTestTSQLConnection }
|
||||
|
||||
@ -437,11 +457,7 @@ var
|
||||
begin
|
||||
with SQLDBConnector do
|
||||
begin
|
||||
try
|
||||
TryDropIfExist('testdiscon');
|
||||
except
|
||||
// Ignore
|
||||
end;
|
||||
TryDropIfExist('testdiscon');
|
||||
ExecuteDirect('create table testdiscon (id integer not null, a varchar(10), constraint pk_testdiscon primary key(id))');
|
||||
if Transaction.Active then
|
||||
Transaction.Commit;
|
||||
|
Loading…
Reference in New Issue
Block a user