#1 2018-10-15 07:00:43

edwinsn
Member
Registered: 2010-07-02
Posts: 1,215

How to batch update with conditions?

The concept's illustrated with this example:

https://gist.github.com/edwinyzh/656250 … 7994811d7f

In short, to implement the 'UPDATE...WHERE...' mechanism in mORMot's batch-update.

Do you like this suggestion, ab?


Delphi XE4 Pro on Windows 7 64bit.
Lazarus trunk built with fpcupdelux on Windows with cross-compile for Linux 64bit.

Offline

#2 2018-10-15 07:04:47

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,258
Website

Re: How to batch update with conditions?

The Batch update is only by ID.
So first retrieve the IDs then apply the Batch.

Offline

#3 2018-10-15 09:02:10

edwinsn
Member
Registered: 2010-07-02
Posts: 1,215

Re: How to batch update with conditions?

ab wrote:

So first retrieve the IDs then apply the Batch.

What if the record has been changed in the database by **other clients**, in between 'first retrieve the ID' and 'apply the Batch'?


Delphi XE4 Pro on Windows 7 64bit.
Lazarus trunk built with fpcupdelux on Windows with cross-compile for Linux 64bit.

Offline

#4 2018-10-15 10:56:18

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,258
Website

Re: How to batch update with conditions?

Then you should use a regular transaction on the server side, using a persistence service.

I hope you do encapsulate all your data access in a persistence service, and never call directly the ORM from the client, right? wink

Offline

#5 2018-10-15 12:35:00

edwinsn
Member
Registered: 2010-07-02
Posts: 1,215

Re: How to batch update with conditions?

OK, maybe I have a misunderstanding about SQLite transactions -  can the following situation (step 3) happen on the server side?

1 - start transaction.
2 - read a record from db.
3 - the record read in step 2 is modified by another client.
4 - update the record.
5 - commit the transaction.


Delphi XE4 Pro on Windows 7 64bit.
Lazarus trunk built with fpcupdelux on Windows with cross-compile for Linux 64bit.

Offline

#6 2018-10-15 12:52:31

Ehab
Member
Registered: 2016-09-07
Posts: 15

Re: How to batch update with conditions?

Hi edwinsn,
I'm new to SQLite, but I know that you can't update any record inside the transaction.

But still that another user can save his modified version of record after the current user end his transaction.

Offline

#7 2018-10-15 13:21:15

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,258
Website

Re: How to batch update with conditions?

You CAN update any record inside a transaction.
This is what transactions are for.

Offline

#8 2018-10-15 13:23:06

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,258
Website

Re: How to batch update with conditions?

You CAN update any record inside a transaction.
This is what transactions are for, and base of https://en.wikipedia.org/wiki/ACID_(computer_science)

If another user save his modified version in another transaction, the last one will be taken in account.

If you expect something else, a higher level mechanism (e.g. as locking, or versioning) may be used, but mainly in the persistence or even the business logic layer.

Offline

#9 2018-10-15 21:22:59

mpv
Member
From: Ukraine
Registered: 2012-03-24
Posts: 1,547
Website

Re: How to batch update with conditions?

The feature @edwinsn request is known as "Optimistic locking", and as @ab note it usually should be implemented in the business logic layer.
I have it in my apps (every row store a modification date). Before run update on server side I compare modification date passed from client side with date stored in DB (yes, one more select, but it is quick) and in case it's not equal  return error to client "Record is modified by another user".

Offline

#10 2018-10-16 03:33:56

edwinsn
Member
Registered: 2010-07-02
Posts: 1,215

Re: How to batch update with conditions?

mpv wrote:

The feature @edwinsn request is known as "Optimistic locking", and as @ab note it usually should be implemented in the business logic layer.
I have it in my apps (every row store a modification date). Before run update on server side I compare modification date passed from client side with date stored in DB (yes, one more select, but it is quick) and in case it's not equal  return error to client "Record is modified by another user".

I'm doing the similar thing - the logic is the same, except that  I do it on the client-side before submitting the batch-update, so it's not wrapped by a DB transaction.


Delphi XE4 Pro on Windows 7 64bit.
Lazarus trunk built with fpcupdelux on Windows with cross-compile for Linux 64bit.

Offline

#11 2018-10-16 12:19:01

edwinsn
Member
Registered: 2010-07-02
Posts: 1,215

Re: How to batch update with conditions?

BTW, I use mORMot's TModTime field type for the so-called 'modification date', and it's a perfect fit smile


Delphi XE4 Pro on Windows 7 64bit.
Lazarus trunk built with fpcupdelux on Windows with cross-compile for Linux 64bit.

Offline

Board footer

Powered by FluxBB