-
-
Save edwinyzh/6562502f98907d4639198e7994811d7f to your computer and use it in GitHub Desktop.
The concept of batch updating with conditions - mORMot (Delphi/Pascal)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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