Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Anything wrong with this INSERT() wrapper ?
Message
From
24/04/2002 04:07:07
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
 
To
23/04/2002 14:48:53
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00648352
Message ID:
00648535
Views:
23
>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
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform