>Having the declares fire every time you want to generate a guid is slow.
>
>Look at the bottom of this:
>
>
http://fox.wikis.com/wc.dll?Wiki~GUIDGenerationCode~WikiOoww.. that is bad design. Relying on VFP specific principles and breaking several other rules that is only known to very few is very, very bad.
Much better is to write a class that can be instantiated where the declaration of the API occurs only once in the declaration. Keep it simple.
Unless you want to win a performance design contest this example should never be practiced. The following is only 15% slower. But hey, who cares about < 0.3 seconds for a million keys? Certainly not the programmer who has to maintain the code.
m.lnLoopSize = 1000000
m.lnStart=SECONDS()
oGuid = CREATEOBJECT("Guid")
STORE REPLICATE(CHR(0),16) TO m.gcuuidcreatesequential
FOR m.lnLoop = 1 to m.lnLoopSize
m.lcJunk = oGuid.uidCreateSequential(@m.gcuuidcreatesequential)
ENDFOR m.lnLoop
?SECONDS()-m.lnStart,"my guids"
DEFINE CLASS Guid AS Custom
FUNCTION Init
DECLARE INTEGER UuidCreateSequential IN 'RPCRT4.dll' STRING @ gcuuidcreatesequential
ENDFUNC
FUNCTION uidcreatesequential(m.gcuuidcreatesequential)
RETURN EVL(UuidCreateSequential(@m.gcuuidcreatesequential),"GUID Error")
ENDFUNC
ENDDEFINE
Walter,
>
>>Hi Cetin,
>>
>>Worked like a charm! Thanks Tore, Gregory and Mike for your inputs!
>>
>>Actually what Tore shared also worked. Unfortunately, I can only tag one as "the solution".
>>
>>I will be using this for my "child tables", e.g. Invoice Details. I used to put an autoinc column to make the invoice details always shown in the sequence they were entered. Using UUID 4 and without either a timestamp or autoinc column, it jumbles up the order the invoice detail rows. With this, I can do away with the additional autoinc or datetime columns because it ensures rows are created in the proper sequence.
>>
>>Long live the Fox!
>>
>>
>>
>>>
>>>*uniqueIDSequential.prg
>>>Declare Integer CoCreateGuid In 'OLE32.dll' ;
>>> string @pguid
>>>Declare Integer StringFromGUID2 In 'OLE32.dll' ;
>>> string rguid, String @lpsz, Integer cchMax
>>>Declare Integer UuidCreateSequential In 'RPCRT4.dll' String @ Uuid
>>>
>>>Local pguid,rguid, lcOldError, lnResult
>>>pguid=Replicate(Chr(0),16)
>>>rguid=Replicate(Chr(0),80)
>>>lcOldError = On('error')
>>>On Error lnResult = CoCreateGuid(@pguid)
>>>lnResult = UuidCreateSequential(@pguid)
>>>On Error &lcOldError
>>>Return ;
>>> Iif( InList(m.lnResult,0,1824) And ;
>>> StringFromGUID2(pguid,@rguid,40) # 0, ;
>>> StrConv(Left(rguid,76),10), "" )
>>>