README.context_variables2 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. Returned value:
  14. INTEGER for RDB$SET_CONTEXT
  15. VARCHAR(32765) for RDB$GET_CONTEXT
  16. Usage:
  17. RDB$SET_CONTEXT and RDB$GET_CONTEXT set and retrieve current value for the
  18. context variables. Namespace name identifies a group of context variables with
  19. similar properties. Access rules such as the fact if variables may be read and
  20. written to and by whom are determined by namespace which they belong to.
  21. Namespace and variable names are case-sensitive.
  22. Value of a variable is stored as a string, its length is limited by 32765 bytes.
  23. RDB$GET_CONTEXT retrieves current value of a variable. If variable does not
  24. exist in namespace return value for the function is NULL.
  25. RDB$SET_CONTEXT sets a value for specific variable. Function returns value of
  26. 1 if variable existed before the call and 0 otherwise. To delete variable from
  27. context set its value to NULL.
  28. Currently, there is a fixed number of pre-defined namespaces you may use.
  29. USER_SESSION namespace offers access to session-specific user-defined
  30. variables. You can define and set value for variable with any name in this
  31. context. USER_TRANSACTION namespace offers the same possibilities for
  32. individual transactions.
  33. SYSTEM namespace provides read-only access to the following variables.
  34. Variable name Value
  35. ------------------------------------------------------------------------------
  36. NETWORK_PROTOCOL | The network protocol used by client to connect. Currently
  37. | used values: "TCPv4", "TCPv6", "XNET" and NULL.
  38. |
  39. WIRE_COMPRESSED | Compression status of current connection.
  40. | If connection is compressed - returns "TRUE", if it is
  41. | not compressed - returns "FALSE".
  42. | If connection is embedded - returns NULL.
  43. |
  44. WIRE_ENCRYPTED | Encryption status of current connection.
  45. | Value is the same as for compression status above.
  46. |
  47. WIRE_CRYPT_PLUGIN | If connection is encrypted - returns name of current plugin,
  48. | otherwise NULL.
  49. |
  50. CLIENT_ADDRESS | The wire protocol address and port number of remote client
  51. | represented as string. Value is IP address concatenated with
  52. | port number using the '/' separator character. Value is
  53. | returned for TCPv4 and TCPv6 protocols only, for all other
  54. | protocols NULL is returned.
  55. |
  56. CLIENT_HOST | The wire protocol host name of remote client. Value is
  57. | returned for all supported protocols.
  58. |
  59. CLIENT_OS_USER | Remote OS user name
  60. |
  61. CLIENT_PID | Process ID of remote client application
  62. |
  63. CLIENT_PROCESS | Process name of remote client application
  64. |
  65. CLIENT_VERSION | Version of the client library used by client application
  66. |
  67. DB_NAME | Canonical name of current database. It is either alias
  68. | name if connectivity via file names is not allowed or
  69. | fully expanded database file name otherwise.
  70. |
  71. DB_GUID | GUID of the current database
  72. |
  73. DB_FILE_ID | Unique filesystem-level ID of the current database
  74. |
  75. ISOLATION_LEVEL | Isolation level for current transaction. Returned values
  76. | are "READ COMMITTED", "CONSISTENCY", "SNAPSHOT".
  77. |
  78. TRANSACTION_ID | Numeric ID for current transaction. Returned value is the
  79. | same as of CURRENT_TRANSACTION pseudo-variable.
  80. |
  81. LOCK_TIMEOUT | Lock timeout value specified for current transaction
  82. |
  83. READ_ONLY | Returns "TRUE" if current transaction is read-only and
  84. | "FALSE" otherwise
  85. |
  86. SESSION_ID | Numeric ID for current session. Returned value is the
  87. | same as of CURRENT_CONNECTION pseudo-variable.
  88. |
  89. CURRENT_USER | Current user for the connection. Returned value is the
  90. | same as of CURRENT_USER pseudo-variable.
  91. |
  92. EFFECTIVE_USER | Effective user for now. It indicates privileges of
  93. | which user is currently used to execute function, procedure, trigger.
  94. |
  95. CURRENT_ROLE | Current role for the connection. Returned value is the
  96. | same as of CURRENT_ROLE pseudo-variable.
  97. |
  98. ENGINE_VERSION | Engine version number, e.g. "2.1.0" (since V2.1)
  99. |
  100. GLOBAL_CN | Most current value of global Commit Number counter
  101. |
  102. SNAPSHOT_NUMBER | Value of Snapshot Number of currently database snapshot: either
  103. | transaction level (for SNAPSHOT or CONSISTENCY transaction),
  104. | or request level (for READ COMMITTED READ CONSISTENCY
  105. | transaction). NULL, if snapshot is not exist.
  106. |
  107. SESSION_IDLE_TIMEOUT | Current value of idle connection timeout
  108. |
  109. STATEMENT_TIMEOUT | Current value of statement execution timeout
  110. |
  111. REPLICATION_SEQUENCE | Current replication sequence (number of the latest segment
  112. | written to the replication journal)
  113. |
  114. REPLICA_MODE | Replica mode of the database. Possible values are
  115. | "READ-ONLY", "READ-WRITE" and NULL.
  116. |
  117. EXT_CONN_POOL_SIZE | Pool size (number of connections inside the pool)
  118. |
  119. EXT_CONN_POOL_IDLE_COUNT | Count of currently inactive connections in the pool
  120. |
  121. EXT_CONN_POOL_ACTIVE_COUNT | Count of active connections, associated with pool
  122. |
  123. EXT_CONN_POOL_LIFETIME | Idle connection lifetime, in seconds
  124. |
  125. SESSION_TIMEZONE | Current session time zone.
  126. |
  127. DECFLOAT_ROUND | Rounding mode used in operations with DECFLOAT values.
  128. |
  129. DECFLOAT_TRAPS | Exceptional conditions in operations with DECFLOAT
  130. | values that cause a trap.
  131. |
  132. PARALLEL_WORKERS | Number of parallel workers that could be used by attachment.
  133. |
  134. PAGES_ALLOCATED | Current value of allocated pages in the database.
  135. |
  136. PAGES_USED | Current value of allocated pages in the database.
  137. |
  138. PAGES_FREE | Current value of free pages in the database.
  139. Notes:
  140. To prevent DoS attacks against Firebird Server you are not allowed to have
  141. more than 1000 variables stored in each transaction or session context.
  142. Example(s):
  143. create procedure set_context(User_ID varchar(40), Trn_ID integer) as
  144. begin
  145. RDB$SET_CONTEXT('USER_TRANSACTION', 'Trn_ID', Trn_ID);
  146. RDB$SET_CONTEXT('USER_TRANSACTION', 'User_ID', User_ID);
  147. end;
  148. create table journal (
  149. jrn_id integer not null primary key,
  150. jrn_lastuser varchar(40),
  151. jrn_lastaddr varchar(255),
  152. jrn_lasttransaction integer
  153. );
  154. CREATE TRIGGER UI_JOURNAL FOR JOURNAL BEFORE INSERT OR UPDATE
  155. as
  156. begin
  157. new.jrn_lastuser = rdb$get_context('USER_TRANSACTION', 'User_ID');
  158. new.jrn_lastaddr = rdb$get_context('SYSTEM', 'CLIENT_ADDRESS');
  159. new.jrn_lasttransaction = rdb$get_context('USER_TRANSACTION', 'Trn_ID');
  160. end;
  161. execute procedure set_context('skidder', 1);
  162. insert into journal(jrn_id) values(0);
  163. commit;