README.context_variables2 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. -----------------------------------------
  2. Generic user and system context variables
  3. -----------------------------------------
  4. Function:
  5. New built-in functions give access to some information about current
  6. connection and current transaction. Also they provide means to associate
  7. and retrieve user context data with transaction or connection.
  8. Author:
  9. Nickolay Samofatov <nickolay at broadviewsoftware dot com>
  10. Format:
  11. RDB$SET_CONTEXT( <namespace>, <variable>, <value> )
  12. RDB$GET_CONTEXT( <namespace>, <variable> )
  13. Declared as:
  14. DECLARE EXTERNAL FUNCTION RDB$GET_CONTEXT
  15. VARCHAR(80),
  16. VARCHAR(80)
  17. RETURNS VARCHAR(255) FREE_IT;
  18. DECLARE EXTERNAL FUNCTION RDB$SET_CONTEXT
  19. VARCHAR(80),
  20. VARCHAR(80),
  21. VARCHAR(255)
  22. RETURNS INTEGER BY VALUE;
  23. Usage:
  24. RDB$SET_CONTEXT and RDB$GET_CONTEXT set and retrieve current value for the
  25. context variables. Namespace name identifies a group of context variables with
  26. similar properties. Access rules such as the fact if variables may be read and
  27. written to and by whom are determined by namespace which they belong to.
  28. Namespace and variable names are case-sensitive.
  29. RDB$GET_CONTEXT retrieves current value of a variable. If variable does not
  30. exist in namespace return value for the function is NULL.
  31. RDB$SET_CONTEXT sets a value for specific variable. Function returns value of
  32. 1 if variable existed before the call and 0 otherwise. To delete variable from
  33. context set its value to NULL.
  34. Currently, there is a fixed number of pre-defined namespaces you may use.
  35. USER_SESSION namespace offers access to session-specific user-defined
  36. variables. You can define and set value for variable with any name in this
  37. context. USER_TRANSACTION namespace offers the same possibilities for
  38. individual transactions.
  39. SYSTEM namespace provides read-only access to the following variables.
  40. Variable name Value
  41. ------------------------------------------------------------------------------
  42. NETWORK_PROTOCOL | The network protocol used by client to connect. Currently
  43. | used values: "TCPv4", "WNET", "XNET" and NULL
  44. |
  45. CLIENT_ADDRESS | The wire protocol address of remote client represented as
  46. | string. Value is IP address in form "xxx.xxx.xxx.xxx" for
  47. | TCPv4 protocol. Local process ID for XNET protocol and
  48. | NULL for all other protocols
  49. |
  50. DB_NAME | Canonical name of current database. It is either alias
  51. | name if connectivity via file names is not allowed or
  52. | fully expanded database file name otherwise.
  53. |
  54. ISOLATION_LEVEL | Isolation level for current transaction. Returned values
  55. | are "READ COMMITTED", "CONSISTENCY", "SNAPSHOT"
  56. |
  57. TRANSACTION_ID | Numeric ID for current transaction. Returned value is the
  58. | same as of CURRENT_TRANSACTION pseudo-variable
  59. |
  60. SESSION_ID | Numeric ID for current session. Returned value is the
  61. | same as of CURRENT_CONNECTION pseudo-variable
  62. |
  63. CURRENT_USER | Current user for the connection. Returned value is the
  64. | same as of CURRENT_USER pseudo-variable
  65. |
  66. CURRENT_ROLE | Current role for the connection. Returned value is the
  67. | same as of CURRENT_ROLE pseudo-variable
  68. Notes:
  69. To prevent DoS attacks against Firebird Server you are not allowed to have
  70. more than 1000 variables stored in each transaction or session context.
  71. Example(s):
  72. create procedure set_context(User_ID varchar(40), Trn_ID integer) as
  73. begin
  74. RDB$SET_CONTEXT('USER_TRANSACTION', 'Trn_ID', Trn_ID);
  75. RDB$SET_CONTEXT('USER_TRANSACTION', 'User_ID', User_ID);
  76. end;
  77. create table journal (
  78. jrn_id integer not null primary key,
  79. jrn_lastuser varchar(40),
  80. jrn_lastaddr varchar(255),
  81. jrn_lasttransaction integer
  82. );
  83. CREATE TRIGGER UI_JOURNAL FOR JOURNAL BEFORE INSERT OR UPDATE
  84. as
  85. begin
  86. new.jrn_lastuser = rdb$get_context('USER_TRANSACTION', 'User_ID');
  87. new.jrn_lastaddr = rdb$get_context('SYSTEM', 'CLIENT_ADDRESS');
  88. new.jrn_lasttransaction = rdb$get_context('USER_TRANSACTION', 'Trn_ID');
  89. end;
  90. execute procedure set_context('skidder', 1);
  91. insert into journal(jrn_id) values(0);
  92. commit;