Pergunta

É possível ter constantes de largura banco de dados? O que eu quero é definir uma constante como:

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

e, em seguida, usá-lo em, por exemplo, um gatilho como:

CREATE TRIGGER AD_PRJ_PROJECTS FOR PRJ_PROJECT
ACTIVE AFTER DELETE
POSITION 1
AS
BEGIN
   EXECUTE PROCEDURE SP_ADD_HISTORY 'PRJ_PROJECT', DELETE_CONSTANT;
END;
Foi útil?

Solução

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.

Outras dicas

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;
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top