Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to know records were edited or deletion by Other use
Message
 
To
18/10/2000 10:46:32
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Miscellaneous
Thread ID:
00430813
Message ID:
00430984
Views:
8
John,

Contrary to what someone else has said there is no reason to lock a record while someone works on it. That approach is called a pessimistic strategy and is a valid way to do things. However, there is also the optimistic strategy which doesn't detect conflicts until you attempt to save the edit. If you choose to use the optimistic strategy then you have to deal with possible conflicts. You have three basic approaches you can follow;

Last One In Wins
In this approach you set the second argument of TableUpdate to .T. and force the write past any update conflicts.

First one in wins
In this approach you set the second argument to .F. adn if the Tableupdate fials you do a table revert and tell the user "Too bad buddy".

Attempt to resolve to a field level
IN this approach you detect the failed TableUpdate with teh second argument set to .F. and then execute code to compare the record on disk with your buffer and what your buffer started with.

Here's some sample code from a form class's Resolve method that I created for the AppDev training classes that implements the third approach. It is heavily commented so you should be able to figure out what it is doing;
LPARAMETERS pcAlias, pcDatabase

* Define constants for the arrays
#DEFINE BUFFERNEW laFields( lnCnt, 2 )
#DEFINE TABLENEW laFields( lnCnt, 3 )

LOCAL llRet, laFields(1), lnCnt, lnNext, llGotOne, llView, lcUser, lcTable

DIMENSION laFields(1)

llRet = .T.

llView = ( CursorGetProp( "SOURCETYPE", pcAlias ) = 1 )
IF llView
   * Fail the resoution for updateable views
   RETURN .F.
ENDIF

SET DATABASE TO (pcDatabase)
* Attempts to resolve a failed TableUpdate()
*
* Compares OldVal() and Buffer values to find the
* changed values, then checks CurVal() to see if
* these are among the fields that are different
* in the file.  If the changed fields are not
* among the different file values, the buffer
* is updated from the table and the TableUpdate()
* is forced.  Otherwise the TableUpdate is allowed
* to fail and the SaveChanges will also fail.

* Select the work area being resolved
SELECT (pcAlias)

* Build an array of fields names
AFIELDS(laFields)

* Get the first modified record
lnNext = GETNEXTMODIFIED(0,pcAlias)
* As long as we have a modified record and we have not failed
DO WHILE lnNext <> 0 AND llRet
   * Set for no conflicts
   llGotOne = .F.
   * Move to the modified record
   GOTO lnNext

   * Check buffer against OLDVAL() to build a list of changed fields
   * result of comparison is stored in column 2 of laFields
   FOR lnCnt = 1 TO ALEN(laFields,1)
      * Ignore the update and create fields
      BUFFERNEW = OLDVAL( laFields( lnCnt, 1 ) ) <> ;
         EVALUATE(pcAlias+"."+laFields(lnCnt,1))
   ENDFOR

   * Check changed fields against CURVAL() to find any conflicts
   * Store conflict detection result in column 3 of laFields
   FOR lnCnt = 1 TO ALEN(laFields,1)
      TABLENEW = OLDVAL( laFields( lnCnt, 1 ) ) <> ;
         CURVAL( laFields( lnCnt, 1 ) )
      IF BUFFERNEW AND TABLENEW AND ;
         EVALUATE(pcAlias+"."+laFields(lnCnt,1)) <> ;
         CURVAL( laFields( lnCnt, 1 ) )
         * If both the buffer and the curval() are new from the oldval()
         * and they are not the same value
         * Set conflict flag
         llGotOne = .T.
      ENDIF
   ENDFOR

   * If any conflicts
   IF llGotOne
      * Set to fail
      llRet = .F.
      * LOOP back to exit DO WHILE
      LOOP
   ELSE
      * There are no field collisions so we will fix the buffered field
      * values to match the disk image for the unedited fields
      FOR lnCnt = 1 TO ALEN(laFields,1)
         IF TABLENEW
            REPLACE (pcAlias+"."+laFields(lnCnt,1)) WITH ;
               CURVAL( pcAlias+"."+laFields( lnCnt, 1 ) )
         ENDIF
      ENDFOR
	
   ENDIF

   * Force TableUpdate()
   IF NOT TableUpdate( .F., .T., pcAlias )
      llRet = .F.
      LOOP
   ENDIF
	

   * Get the next modified record
   lnNext = GETNEXTMODIFIED(lnNext,pcAlias)

ENDDO

RETURN llRet
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform