Check UpdateCount on TZUpdateSQL.PostUpdates

In this forum we will discuss things relating the ZEOSLib 6.6.x stable versions

Moderators: gto, EgonHugeist

Post Reply
ddantoni
Fresh Boarder
Fresh Boarder
Posts: 14
Joined: 17.10.2005, 15:15

Check UpdateCount on TZUpdateSQL.PostUpdates

Post by ddantoni »

Hi,
it should be very interesting if the TZUpdateSQL.PostUpdates method included a parameter which turn on a validation of updates.

In the TZUpdateSQL.PostUpdates method, replace the usual "Statement.ExecutePrepared;" by

Code: Select all

lValidateUpdateCount := StrToBoolEx(
          Sender.GetStatement.GetParameters.Values['ValidateUpdateCount']);

        lUpdateCount := Statement.ExecuteUpdatePrepared;
        if  (lValidateUpdateCount) and (lUpdateCount <> 1) then
          raise EZSQLException.CreateWithCode(2802, Format('%d record(s) updated. Only one record should have been updated.', [lUpdateCount]));
(or pass the lUpdateCount value to an event)

Thanks,
David
User avatar
mdaems
Zeos Project Manager
Zeos Project Manager
Posts: 2766
Joined: 20.09.2005, 15:28
Location: Brussels, Belgium
Contact:

Post by mdaems »

Depends on what zeoslib version you are using, but since 6.6.3 this has been added to the changelist:
Automatically generated updates could update more than 1 row at once or no row at all. Query
property ‘ValidateUpdateCount=true/false’ added (http://zeos.firmos.at/viewtopic.php?t=1757)
Default value is false. In Zeoslib 7 it will be enabled by default.

Mark
Image
ddantoni
Fresh Boarder
Fresh Boarder
Posts: 14
Joined: 17.10.2005, 15:15

Post by ddantoni »

Hi Mark,
thank for this quick answer.

I use the version "6.6.4-stable" and after a search for "ValidateUpdateCount", I found the modification was only in ZDbcGenericResolver.pas.

The PostUpdates method of TZUpdateSQL is not modified yet.

(I use this purpose to check if another client hasn't modified the same record before I apply the update. I do this thing using a field named LastModifiedTime and I added "WHERE LastModifiedTime=:OLD_LastModifiedTime" in my update query. By this way, if LastModifiedTime has been changed, an exception is raised.)

David
User avatar
mdaems
Zeos Project Manager
Zeos Project Manager
Posts: 2766
Joined: 20.09.2005, 15:28
Location: Brussels, Belgium
Contact:

Post by mdaems »

Sorry, didn't read your first post correctly, I'm afraid.

Experimenting now...

Mark
Image
User avatar
mdaems
Zeos Project Manager
Zeos Project Manager
Posts: 2766
Joined: 20.09.2005, 15:28
Location: Brussels, Belgium
Contact:

Post by mdaems »

SVN commit 597. (Testing branch)

Attention : this test will only be done when ValidateUpdateCount is explicitly set in the query properties. This is a different behaviour as in case of the automatically generated queries. There the check will be done always unless the property is explicitly set to false in Zeoslib 7. The reason : TZUpdateSQL is intended for manual intervention by a programmer. When he writes queries for updating he should be aware of possible multiple or zero updates. There checks are only needed when the programmers explicitly wants them.

In 6.6 the property must also be set explicitly for automatically generated updates. We had to do this because the feature was only added very late and we didn't want to break existing applications.

Mark
Image
ddantoni
Fresh Boarder
Fresh Boarder
Posts: 14
Joined: 17.10.2005, 15:15

Post by ddantoni »

Thank you !!!
trupka
Expert Boarder
Expert Boarder
Posts: 140
Joined: 26.08.2007, 22:10

Patch causes duplicate updates

Post by trupka »

Current patch causes big problem with duplicate updates and indirectly, violations of primary keys. This completely breaks application. Problem is in ZSQLUpdate.pas , line 789:

Code: Select all

      if ExecuteStatement then
      begin
       Statement.ExecutePrepared; //<-- EXECUTES STATEMENT FIRST TIME
       lValidateUpdateCount := StrToBoolEx( 
          Sender.GetStatement.GetParameters.Values['ValidateUpdateCount']); 

       lUpdateCount := Statement.ExecuteUpdatePrepared; //<-- EXECUTES STATEMENT AGAIN
       if  (lValidateUpdateCount) and (lUpdateCount <> 1) then 
         raise EZSQLException.Create(Format(SInvalidUpdateCount, [lUpdateCount]));
In attachment is small patch against svn://zeos.firmos.at/zeos/branches/6.6-patches rev 624 that fixes this issue.
You do not have the required permissions to view the files attached to this post.
User avatar
mdaems
Zeos Project Manager
Zeos Project Manager
Posts: 2766
Joined: 20.09.2005, 15:28
Location: Brussels, Belgium
Contact:

Post by mdaems »

Oops Trupka. I suppose that's my fault... merge error... Problem was automatical merge didn't work because of other differences between testing and stable versions in this file.

I'll try to fix this tonight...

Mark
Image
User avatar
mdaems
Zeos Project Manager
Zeos Project Manager
Posts: 2766
Joined: 20.09.2005, 15:28
Location: Brussels, Belgium
Contact:

Post by mdaems »

Should be alright now.
Image
trupka
Expert Boarder
Expert Boarder
Posts: 140
Joined: 26.08.2007, 22:10

Post by trupka »

mdaems,
And it is .. :D
Thank You for effort
Post Reply