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]