Marc Vanzegbroeck wrote:Reinaldo,
It seems that SQLite also have database notification.
Can you give me an example how you you this with ADS.
Regards,
Marc
oAdsQuery:cSql += ;
"CREATE TRIGGER [SignalEvent AfterUpdate] ON encounters AFTER UPDATE BEGIN \n" + ;
" DECLARE @EncTotChrgs MONEY; \n" +;
" DECLARE @OldTotChrgs MONEY; \n" +;
" DECLARE @cEncounter CHAR( 13 ); \n" +;
" \n" +;
" SET @EncTotChrgs = ( SELECT EncTotChrgs FROM __new ); \n" +;
" SET @OldTotChrgs = ( SELECT EncTotChrgs FROM __old ); \n" +;
" SET @cEncounter = ( SELECT encounter FROM __new ); \n" +;
" \n" +;
" IF @EncTotChrgs <> @OldTotChrgs THEN \n" +;
" EXECUTE PROCEDURE sp_SignalEvent( \n" +;
" 'EncChrgsUpdated', \n" +;
" True, 2, \n" +;
" @cEncounter + ',' +CAST( @EncTotChrgs AS SQL_CHAR ) );\n" +;
" END; \n" +;
" \n" +;
"END NO MEMOS PRIORITY 1; \n" +;
" \n"
oAdsQuery:Run()
//create the event notification using stored procedure sp_createevent()
"EXECUTE PROCEDURE sp_CreateEvent( 'EncChrgsUpdated', 2 ); \n"
...
//wait for events using stored procedure
//this script gets executed inside ListenForMyEventOnBckgrnd().
//We are only initializing here.
::oEventQuery:cSql := " \n" +;
"EXECUTE PROCEDURE sp_WaitForEvent( 'EncChrgsUpdated', 0, 0, 0 ); \n"
...
::nIdle := hb_IdleAdd( {|| hb_BackGroundRun() } )
::hTask := hb_BackGroundAdd( {|| ::ListenForMyEventOnBckgrnd() }, _WAIT, .T. )
...
//this method gets executed on the background by hb_backgroundAdd().
/* This method will listen for a specific ADS notification from the server on the
background. oEventQuery sql statment is defined at the beginin in method :New(). The
specific notification we are interested on is called: EncChrgsUpdated
The only notification from the server that we are interested here
are notifications on new charges begin posted so that the StatusPanel xbrowse
as well as the graphs may be updated on real time.
*/
METHOD ListenForMyEventOnBckgrnd() CLASS StatusPanel
LOCAL aParms := {}
LOCAL nPos := 0
LOCAL lChanged := .F.
LOCAL aLine, nAdd, aRow
::oEventQuery:aResultSet := {}
//Listening timeout is set to zero. Everytime this background method runs
//it will check if a notification has been issued since last check.
::oEventQuery:cAlias := NIL
::oEventQuery:Run()
/*Uncomment the line below to monitor this backgound task working
DEBUG ::oEventQuery:aResultSet /**/
//update and refresh graph + xbrowse grid with new info. :aResultSet contains
//the encounter number and EncTotChrgs amount separated by a comma.
FOR EACH aLine IN ::oEventQuery:aResultSet
aParms := hb_aTokens( aLine[ 3 ], "," )
IF aLine[ 2 ] < 1 .OR. LEN( aParms ) < 2 ;LOOP ;ENDIF
::oUpdtQry:aParameters := { aparms[ 1 ] }
::FetchSPData( ::oUpdtQry )
//::oUpdtQry:aResultSet should never be empty. This is just a precaution.
IF EMPTY( ::oUpdtQry:aResultSet ) ;LOOP ;ENDIF
IF ( nPos := AScan( ::oAdsQuery:aResultSet, { |e| e[ COL_ENCOUNTERID ] == aParms[ 1 ] } ) ) > 0
::OnUpdateRow( nPos, ::oUpdtQry:aResultSet[ 1 ] ) //, Val( aParms[ 2 ] ) )
ELSE //new Encounter has been inserted into Encounters.adt
::OnInsertRow( AClone( ::oUpdtQry:aResultSet[ 1 ] ) )
ENDIF
lChanged := .T.
NEXT
IF lChanged
::oBrw:Refresh()
::UpdateGraph( .t. )
ENDIF
RETURN NIL
CREATE TRIGGER planning_update UPDATE ON planning BEGIN UPDATE refresh SET nr = nr + 1 where ID = 1;END;
#define _WAIT 10000 //10 Seconds
Event notifications are a mechanism that allows an action at the server to proactively notify clients that an event they are interested in has occurred.
Clients use the canned procedure sp_CreateEvent to register for event notifications. Once registered, the client can call sp_WaitForEvent or sp_WaitForAnyEvent to efficiently wait for the event to be signaled. Events are signaled using the sp_SignalEvent procedure.
The signal function sp_SignalEvent has a parameter that controls whether or not the event is signaled immediately if in a transaction or if it is signaled when the transaction is committed.
Advantage supports many-to-one event delivery. If an event is signaled X times between calls to one of the wait procedures, rather than receiving each signal individually, the wait function returns a count of how many times the event has been signaled since the last wait operation.
If an event is created with the ADS_EVENT_WITH_DATA option, the event can be signaled with a data string which will be returned when the signal is received. Currently only string data is allowed. The typical use of this string data is to provide a method of locating the record or table for which a signal is sent, however any string data can be used.
Return to FiveWin for Harbour/xHarbour
Users browsing this forum: Google [Bot] and 46 guests