开发者

Firebird constant value

Is it possible to have database wide constants? What I want is to define a constant like:

  • UPDATE_CONSTANT = 1
  • INSERT_CONSTANT = 2
  • DELETE_CONSTANT = 3

and then use it in for example a trigger like:

CREATE TRIGGER AD_PRJ_PROJECTS FOR PRJ_PROJECT
ACTIVE AFTER DELETE
POSITION 1
AS
BEGIN
   开发者_StackOverflow中文版EXECUTE PROCEDURE SP_ADD_HISTORY 'PRJ_PROJECT', DELETE_CONSTANT;
END;


You could use a generator:

SET GENERATOR DELETE_CONSTANT TO 3;

...

EXECUTE PROCEDURE SP_ADD_HISTORY 'PRJ_PROJECT', GEN_ID(DELETE_CONSTANT, 0);

Update: yes, using a generator for this purpose is dangerous, as they can be changed. However, in FireBird 3.0 Alpha 1 this risk can be eliminated using access rights: Grants access on generators.


I don't think there is an easy way for declaring constants.

I could be done by creating you own DLL for user defined function, and lmake a function for each constant.

I Think the Idea using generators as "global" constants is briliant.

But you can make a "local constant" to make your code a bit more readable:

CREATE TRIGGER AD_PRJ_PROJECTS FOR PRJ_PROJECT
ACTIVE AFTER DELETE
POSITION 1
AS
  DECLARE VARIABLE DELETE_CONSTANT INTEGER;
BEGIN
   DELETE_CONSTANT = 1;
   EXECUTE PROCEDURE SP_ADD_HISTORY 'PRJ_PROJECT', DELETE_CONSTANT;
END;


Use a single row table with triggers that prevent insertion and deletion from it. Having to read from it certainly does not makes code clearer but it helps to implement such "constants". Also remove write permissions from everybody but sysdba


You can implement some simple preprocesor of yours scripts that converts constants to values..

triggers.presql

@DELETE_CONSTANT = 1
CREATE TRIGGER AD_PRJ_PROJECTS FOR PRJ_PROJECT
ACTIVE AFTER DELETE
POSITION 1
BEGIN
   EXECUTE PROCEDURE SP_ADD_HISTORY 'PRJ_PROJECT', DELETE_CONSTANT;
END;

triggers.sql

CREATE TRIGGER AD_PRJ_PROJECTS FOR PRJ_PROJECT
ACTIVE AFTER DELETE
POSITION 1
BEGIN
   EXECUTE PROCEDURE SP_ADD_HISTORY 'PRJ_PROJECT', 1;
END;
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜