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

只愿长相守 提交于 2021-02-18 08:19:45

问题


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
          POST_EVENT 'phone_ext_changed'; <-- I would like to pass a string parameter with record ID
END

回答1:


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.




回答2:


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;




回答3:


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.



来源:https://stackoverflow.com/questions/4338077/is-it-possible-to-use-arguments-with-firebird-events-post-event-event-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!