Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Anything wrong with this INSERT() wrapper ?
Message
From
24/04/2002 23:00:57
 
 
To
24/04/2002 04:07:07
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00648352
Message ID:
00648964
Views:
19
>>I got tired of the syntax for INSERT INTO and having to check to see if the record already existed, so I wrote this wrapper for the whole process.
>>Anyone see a problem with it?
>>
>>
>>PROCEDURE _Insert( tcTable, tcTag, tu1, tu2, tu3, tu4, tu5 )
>>
>>*********************************
>>***** searches tcTag (candidate tag) for values corresponing to tu? values
>>*****    positions pointer on record if found
>>*****    inserts record if not found
>>*****
>>***** tu1, tu2 etc. must be in order as they appear in tcTag
>>*****    Example:
>>*****        select Invoice
>>*****        Index on BINTOC(PK) + DTOS(date) Tag Invoice_order
>>*****        if _Insert( 'Invoice, 'Invoice_Order', <PK>, date() )
>>*****          && record pointer on existing or new record
>>*****        else
>>*****          && error
>>*****        endif
>>*********************************
>>
>>IF EMPTY(tcTable)
>>  tcTable = ALIAS()
>>ENDIF
>>
>>IF EMPTY(tcTag)
>>  tcTag = DBGETPROP(tcTable,"TABLE","PRIMARYKEY")
>>ENDIF
>>
>>LOCAL ln, lc, lu, lcType, laFields(1), lcKey, lnCnt, lnField, lcMemVar, lcSeek
>>
>>***** Build string to INDEXSEEK()
>>lcSeek = ''
>>FOR ln = 1 TO PCOUNT() - 2
>>  lu = EVALUATE('tu'+TRANSFORM(ln))
>>  lcType = VARTYPE(lu)
>>  DO CASE
>>    CASE lcType = 'N'
>>      lc = BINTOC(lu)
>>    CASE lcType = 'C'
>>      lc = lu
>>    CASE lcType = 'D'
>>      lc = DTOS(lu)
>>    CASE lcType = 'T'
>>      lc = TTOC(lu)
>>  ENDCASE
>>  lcSeek = lcSeek + lc
>>ENDFOR
>>
>>***** determine if String currently in table
>>IF !INDEXSEEK(lcSeek,.T.,tcTable,tcTag)
>>  ***** determine order of fields in Key()
>>  = AFIELDS(laFields,tcTable)
>>  lcKey = KEY(TAGNO(tcTag,tcTable,tcTable),tcTable)
>>  FOR ln = 1 TO ALEN(laFields,1)
>>    laFields(ln,2) = AT(laFields(ln,1),lcKey)
>>  ENDFOR
>>  = ASORT(laFields,2)
>>
>>  ***** assign value to fields in Key()
>>  lnCnt = 0
>>  FOR lnField = 1 TO ALEN(laFields,1)
>>    lcMemVar = laFields(lnField,1)
>>    PRIVATE (lcMemVar)    && make sure no values leak thru from calling prog()
>>    IF !EMPTY( laFields(lnField,2) )
>>      ** only assign values to fields in tcTag
>>      lnCnt = lnCnt + 1
>>      STORE EVALUATE('tu'+TRANSFORM(lnCnt)) TO (lcMemVar)
>>    ENDIF
>>  ENDFOR
>>  INSERT INTO (tcTable) FROM MEMVAR
>>ENDIF
>>
>
>Bill,
>I couldn't see something obvious. But wouldn't it be better if you used seek() instead of indexseek(). If you're using buffer (likely) :
>-Inserted a new record
>-Didn't move off the record yet and didn't tableupdated
>-Called the same routine with same values (matching to current record keys)
>-Indexseek would return .f. causing an insert
>Cetin

Hi Cetin,
Thanks, I didn't realize that INDEXSEEK() would return .f. if the table was buffered and the current record was the one you were looking for and it hadn't been TABLEUPDATE()d yet. I did discover that once you move off the record, then INDEXSEEK() finds it, even without TABLEUPDATE(). I liked the idea of not moving the pointer if it was already on the correct record, but now I'll have to rethink it.

BTW, since you appear to be so good at buffers, is there any way I can get a SELECT .... to query buffered data? That's one of my achilles heels to remember that SELECT ... ignores the buffer.

Thanks again.
Bill Morris
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform