开发者

Is it possible to use arguments with firebird events : POST_EVENT 'event_name' + string args?

I've a trigger that detects a change on a field PHONE_EXT and POSTs an EVENT. I would like to post the Phone_ID with the event in order to use this ID in the client. Is this possible? How?

CREATE TRIGGER tr2 FOR employee
ACTIVE AFTER UPDATE POSITION 0
AS
BEGIN
    IF (new.PHONE_EXT <> old.PHONE_EXT) THEN
      开发者_JAVA技巧    POST_EVENT 'phone_ext_changed'; <-- I would like to pass a string parameter with record ID
END


AFAIK, you cannot pass parameters, but you can get what you want with one of this ideas:

  • If in your client you're interested in events over specific records, you can append the ID of the changing record and post that event. The clients register the events in which are interested using the specific ID's of interest. See example 1.
  • if your front-end are interested in all changes but you want to know which particular records changed, you can "flag" the records as "recently changed" (using another field on the same record, or a detail table, for example). Upon client notification and action it reverts or clears the flag. This approach may be powered, for example, using auxiliary tables to track missing records from specific clients, it depends on your needs.

Example 1

begin
  if (new.phone_ext <> old.phone_ext) then
    post_event 'phone_ext_changed_'||new.ID;
end

Example 2

begin
  if (new.phone_ext <> old.phone_ext) then
  begin
    new.recent_ext_change = 1;
    /* or maybe */
    new.last_ext_change = cast('now' as DateTime);
    /* or maybe */
    insert into changed_phone_ext values (gen_id(some_generator, 1), New.ID, 'now');
    /* finally, post the event */
    post_event 'phone_ext_changed_';
  end
end

I'm using both with success in different applications/situations.


You can use it as follows:

  1. Set a context variable in the trigger and place the desired information in it.

Ex.:

Create trigger evento_ai0 for evento
active after insert position 0
AS
BEGIN
 Post_Event 'Evento_inserido';

"Creating the context variavble"

rdb$set_context('USER_SESSION', 'REGISTRO' , 'Registro inserido: '||new.eve_id);
END
  1. To capture the saved information use:

    Select rdb$get_context('USER_SESSION', 'REGISTRO') from rdb$database;


This is not possible. The event is a name only, if you add ids or other qualifiers, it simply becomes a different event because it has a different name. When subscribing to events, you can only subscribe by name, you can't use wildcards, and it is not possible to include parameters.

Events are for simple and cheap notification, and Firebird can even coalesce multiple 'posts' of the same event into a single notification to a client, so parameters or values are not supported.

The basic idea is that a client subscribe to events, and then determines what changed and what it needs to react to. You can 'help' the client by - for example - populating a support table that is cheap to query.

Also consider reading the article "The Power of Firebird Events", it is a bit old, but a lot of it still applies as Firebird events haven't changed much.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜