Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Yet another Unique Key generator
Message
From
20/11/1999 18:37:53
 
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Miscellaneous
Thread ID:
00293537
Message ID:
00293556
Views:
22
>Before I commit to using this code, I want it to pass peer review. Does anyone see any problems with it?
>
>if !file( "test.dbc" )
> create database test
> create table KeyCtr ;
> ( kKeyCtr c(30) primary key, ;
> nCtr i )
> create table test ;
> ( kTest i primary key )
>* ( kTest i primary key default MkKey( "test.kTest" ) )
> close database
>endif
>
>use test!test shared
>do while .t.
> insert into test values ( MkKey( "test.kTest" ) )
> ? test.kTest
>enddo
>return
>
>function MkKey( tcFld )
>
>local ;
> llDone
>
>llDone = .f.
>do while !llDone
>
> * Get the current value of the Counter for this key
> select nCtr ;
> from KeyCtr ;
> where kKeyCtr = tcFld ;
> into array laCtr
>
> if _tally = 0
> * If we didn't get one, there is no counter yet.
> * So make one and try again
> debugout "Added Key Counter: " + tcFld
> insert into KeyCtr ( kKeyCtr, nCtr ) values ( tcFld, 0 )
> loop
> endif
>
> * Bump the counter.
> * The counter holds the next key value, we use the original value
> * The kKeyCtr identifies which counter,
> * the nCtr make sure noone else has bumped it allready
> update KeyCtr ;
> set nCtr = laCtr[1] + 1 ;
> where kKeyCtr = tcFld and nCtr = laCtr[1]
>
> if _tally = 0
> * The update failed.
> * Probably because someone other process has grabbed the same key,
> * and bumped the counter first.
> * So we will try again.
> debugout "Counter assignment contention at " + transform( laCtr[1] )
> * Close the KeyCtr table to assure a fresh version of the key counter data.
> * Otherwise, we get the same key from the buffer, not a new one from disk.
> use in KeyCtr
> loop
> endif
>
> * Once we get to here, we have a good new key.
> llDone = .t.
>
>enddo
>
>return laCtr[1]


Yes. There is nothing to prevent two people from getting the same key value.
Craig Berntson
MCSD, Microsoft .Net MVP, Grape City Community Influencer
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform