Skip to content

Instantly share code, notes, and snippets.

@edwinyzh
Created October 15, 2018 06:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edwinyzh/6562502f98907d4639198e7994811d7f to your computer and use it in GitHub Desktop.
Save edwinyzh/6562502f98907d4639198e7994811d7f to your computer and use it in GitHub Desktop.
The concept of batch updating with conditions - mORMot (Delphi/Pascal)
program Concept_mORMotBatchUpdateWithConditionsPrj;
uses
System.SysUtils, SynCommons, mORMot, mORMotHttpClient;
{$APPTYPE CONSOLE}
{$R *.res}
type
TSQLPerson = class(TSQLRecord)
private
FName: String;
FVersionNum: Int64;
published
property Name: String read FName write FName;
property VersionNum: Int64 read FVersionNum write FVersionNum;
end;
procedure CheckBatchResults(const aBatchUpdateResultArray: TIDDynArray);
begin
end;
const
cMrJohnsRecordId = 1;
var
dbCnn: TSQLHttpClient;
personRec: TSQLPerson;
batchUpdater: TSQLRestBatch;
batResults: TIDDynArray;
begin
// dbCnn := TSQLHttpClient.Create('localhost', 888, ...
personRec := TSQLPerson.Create;
batchUpdater := TSQLRestBatch.Create(dbCnn, TSQLPerson, MaxInt);
dbCnn.Retrieve(cMrJohnsRecordId, personRec);
personRec.Name := UpperCase(personRec.Name);
// As we know we CAN do this:
batchUpdater.Update(personRec);
// BUT, how to do this:
batchUpdater.Update(personRec, ' WHERE VersionNum = ' + IntToStr(personRec.VersionNum));
// The idea is using a VersionNum field to avoid the update conflict in a multi-client environment.
// In other words, the update operation should only succeed if the VersionNum has not been changed
// by other clients.
// Of course, many SQL engine, including SQLite, supports the 'UPDATE...WHERE' statement:
// https://www.sqlite.org/lang_update.html
// Now check if the update was a success by iterating the batResults array values, and take further action
// accordingly.
CheckBatchResults(batResults);
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment