Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Yet another Unique Key generator
Message
De
21/11/1999 11:17:43
 
Information générale
Forum:
Visual FoxPro
Catégorie:
Base de données, Tables, Vues, Index et syntaxe SQL
Divers
Thread ID:
00293537
Message ID:
00293643
Vues:
23
This is for SINGLE USER only. In multiuser environment you would have to lock the highest key until after you inserted and updated the new record.
Why don't you just have a primary key table with tablename C and nextid I fields, look up the tablename you want to append to, lock it, add one to nextid and unlock it and then use this as the primary key for your data table.
Peter


>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]
Peter Cortiel
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform