{**
Posts updates to database.
@param Sender a cached result set object.
@param UpdateType a type of updates.
@param OldRowAccessor an accessor object to old column values.
@param NewRowAccessor an accessor object to new column values.
}
procedure TZGenericCachedResolver.PostUpdates(Sender: IZCachedResultSet;
UpdateType: TZRowUpdateType; OldRowAccessor, NewRowAccessor: TZRowAccessor);
var
Statement : IZPreparedStatement;
SQL : string;
SQLParams : TObjectList;
lUpdateCount : Integer;
lValidateUpdateCount : Boolean;
TempKey : IZAnyValue;
SenderStatement : IZStatement;
begin
if (UpdateType = utDeleted) and (OldRowAccessor.RowBuffer.UpdateType = utInserted) then
Exit;
case UpdateType of
utInserted:
begin
if InsertStatement = nil then begin
SQL := FormInsertStatement(FInsertParams, NewRowAccessor);
InsertStatement := CreateResolverStatement(SQL);
Statement := InsertStatement;
end;
Statement := InsertStatement;
SQLParams := FInsertParams;
end;
utDeleted:
begin
if not FWhereAll then begin
If DeleteStatement = nil then begin
SQL := FormDeleteStatement(FDeleteParams, OldRowAccessor);
DeleteStatement := CreateResolverStatement(SQL);
end;
Statement := DeleteStatement;
SQLParams := FDeleteParams;
end else begin
FDeleteParams.Clear; //EH: where columns propably are cached after 1. call
SQL := FormDeleteStatement(FDeleteParams, OldRowAccessor);
if SQL = '' then Exit;
TempKey := TZAnyValue.CreateWithInteger(Hash(SQL));
Statement := FStatements.Get(TempKey) as IZPreparedStatement;
If Statement = nil then begin
Statement := CreateResolverStatement(SQL);
FStatements.Put(TempKey, Statement);
end;
SQLParams := FDeleteParams;
end;
end;
utModified:
begin
FUpdateParams.Clear; //EH: where columns propably are cached after 1. call
//now what's faster?: caching stmts too by using a hashmap or recreate always
//first of all: we need the new command-stmt
SQL := FormUpdateStatement(FUpdateParams, OldRowAccessor, NewRowAccessor);
If SQL = '' then exit;// no fields have been changed
TempKey := TZAnyValue.CreateWithInteger(Hash(SQL));
UpdateStatement := FStatements.Get(TempKey) as IZPreparedStatement;
If UpdateStatement = nil then begin
UpdateStatement := CreateResolverStatement(SQL);
FStatements.Put(TempKey, UpdateStatement);
end;
Statement := UpdateStatement;
SQLParams := FUpdateParams;
end;
else
Exit;
end;
FillStatement(Statement, SQLParams, OldRowAccessor, NewRowAccessor);
// if Property ValidateUpdateCount isn't set : assume it's true
SenderStatement := Sender.GetStatement;
if Assigned(SenderStatement) then begin
SQL := SenderStatement.GetParameters.Values['ValidateUpdateCount'];
lValidateUpdateCount := (SQL = '') or StrToBoolEx(SQL);
end else begin
lValidateUpdateCount := true;
end;
lUpdateCount := Statement.ExecuteUpdatePrepared;
{$IFDEF WITH_VALIDATE_UPDATE_COUNT}
if (lValidateUpdateCount) and (lUpdateCount <> 1 ) then
raise EZSQLException.Create(Format(SInvalidUpdateCount, [lUpdateCount]));
{$ENDIF}
end;