Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Understanding CursorAdapter methods
Message
 
 
To
All
General information
Forum:
Visual FoxPro
Category:
Other
Title:
Understanding CursorAdapter methods
Environment versions
Visual FoxPro:
VFP 9 SP1
Miscellaneous
Thread ID:
01475311
Message ID:
01475311
Views:
194
I can’t understand some (actually many) things in the code posted by Aleksey Tsingauz a couple of years ago on how to retrieve IDENTITY value using SCOPE_IDENTITY(). Here is the copy of the message ( Message #1111814 )
**************************************      
    CLOSE DATABASES all      
    SQLDISCONNECT(0)      
    CLEAR      
    SET MULTILOCKS ON      
        
    LOCAL nAutoRefreshConn as Integer      
        
  * Change connection string to connect to any available SQL Server      
    nAutoRefreshConn=SQLSTRINGCONNECT("DRIVER=SQL Server;SERVER=(local);Trusted_Connection=Yes;")      
    IF (nAutoRefreshConn <1)      
     	? "Failed to connect!"      
       AERROR(aerrs)      
       DISPLAY MEMORY LIKE aerrs      
       return      
    ENDIF      
        
    TEXT TO cSQL NOSHOW      
       CREATE TABLE #CAAutoRefreshDemo      
       (      
        f_IDENTITY int NOT NULL IDENTITY PRIMARY KEY,      
        f1 int NOT NULL UNIQUE,      
        f_TIMESTAMP timestamp      
       )      
    ENDTEXT      
        
    IF SQLEXEC(nAutoRefreshConn ,cSQL)!=1      
       ? "Failed to create demo table!"      
       AERROR(aerrs)      
       DISPLAY MEMORY LIKE aerrs      
    ENDIF      
        
    LOCAL oCA as CursorAdapter    
        
    oCA=CREATEOBJECT("CA_SCOPE_IDENTITY")    
        
    oCA.Alias = "CATest"      
    oCA.BufferModeOverride= 5     
    oCA.DataSource= nAutoRefreshConn      
    oCA.DataSourceType="ODBC"      
    oCA.SelectCmd="select * from #CAAutoRefreshDemo"      
    oCA.CursorSchema="f_IDENTITY I, f1 I, f_TIMESTAMP Q(8)"        
    oCA.UseCursorSchema= .T.    
    oCA.Tables="#CAAutoRefreshDemo"      
    oCA.UpdatableFieldList="f1"      
    oCA.UpdateNameList="f1 #CAAutoRefreshDemo.f1, f_TIMESTAMP #CAAutoRefreshDemo.f_TIMESTAMP, f_IDENTITY #CAAutoRefreshDemo.f_IDENTITY"      
    oCA.KeyFieldList="f_IDENTITY"  
        
    oCA.CursorFill()    
        
    oCA.IDENTITY_Field="f_IDENTITY"    
    oCA.RefreshTimestamp= .F. && just for demo purposes will include f_TIMESTAMP into InsertCmdRefreshFieldList    
    oCA.InsertCmdRefreshFieldList="f_TIMESTAMP "    
        
    INSERT INTO CATest (f1) VALUES (100)    
    INSERT INTO CATest (f1) VALUES (200)    
    INSERT INTO CATest (f1) VALUES (300)    
    INSERT INTO CATest (f1) VALUES (400)    
    INSERT INTO CATest (f1) VALUES (500)    
    INSERT INTO CATest (f1) VALUES (600)    
    INSERT INTO CATest (f1) VALUES (700)    
    INSERT INTO CATest (f1) VALUES (800)    
    INSERT INTO CATest (f1) VALUES (900)    
        
    IF !TABLEUPDATE(.T.)    
     	? "TABLEUPDATE is failed!"      
       AERROR(aerrs)      
       DISPLAY MEMORY LIKE aerrs      
    ELSE    
    	LIST     
    ENDIF    
        
        
    TABLEREVERT(.T.)    
    SQLDISCONNECT(0)    
    RETURN     
    
    
    DEFINE CLASS CA_SCOPE_IDENTITY AS CursorAdapter    
      IDENTITY_Field=""  
        
     PROCEDURE DataSource_ASSIGN   
        LPARAMETERS tAssign    
        this.DataSource = tAssign      
        TRY  
        SQLEXEC(this.DataSource, ;  
        		"create procedure #Get_ValHelper @in Int, @out int OUTPUT " + ;  
        		"AS SET NOCOUNT ON " + ;  
        		"SELECT @out=@in")   
  	  CATCH  
  	  ENDTRY  
     ENDPROC  
        
    	PROCEDURE BeforeInsert     
    		LPARAMETERS cFldState, lForce, cInsertCmd    
    		IF LEN(ALLTRIM(this.IDENTITY_Field))>0  
    			cInsertCmd = cInsertCmd + ;  
    				"; DECLARE @id int; SELECT @id = SCOPE_IDENTITY()" + ;  
    				"; EXEC #Get_ValHelper @id, ?@" + ;  
    				this.Alias + "." + ALLTRIM(this.IDENTITY_Field)  
    		ENDIF  
    		?    
    		? PROGRAM()    
    		? "cInsertCmd=",cInsertCmd    
    	ENDPROC     
        
    ENDDEFINE    
Here is just one thing I don't understand: Is there a "link" or some kind of "relation" between methods DataSource_ASSIGN and BeforeInsert()?
"The creative process is nothing but a series of crises." Isaac Bashevis Singer
"My experience is that as soon as people are old enough to know better, they don't know anything at all." Oscar Wilde
"If a nation values anything more than freedom, it will lose its freedom; and the irony of it is that if it is comfort or money that it values more, it will lose that too." W.Somerset Maugham
Next
Reply
Map
View

Click here to load this message in the networking platform