oci8_failover.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2018 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Stig Sæther Bakken <ssb@php.net> |
  16. | Thies C. Arntzen <thies@thieso.net> |
  17. | |
  18. | Collection support by Andy Sautins <asautins@veripost.net> |
  19. | Temporary LOB support by David Benson <dbenson@mancala.com> |
  20. | ZTS per process OCIPLogon by Harald Radi <harald.radi@nme.at> |
  21. | |
  22. | Redesigned by: Antony Dovgal <antony@zend.com> |
  23. | Andi Gutmans <andi@php.net> |
  24. | Wez Furlong <wez@omniti.com> |
  25. +----------------------------------------------------------------------+
  26. */
  27. #ifdef HAVE_CONFIG_H
  28. #include "config.h"
  29. #endif
  30. #include "php.h"
  31. #include "ext/standard/info.h"
  32. #include "php_ini.h"
  33. #if HAVE_OCI8
  34. #include "php_oci8.h"
  35. #include "php_oci8_int.h"
  36. /* {{{ callback_fn()
  37. OCI TAF callback function, calling userspace function */
  38. sb4 callback_fn(void *svchp, void *envhp, void *fo_ctx, ub4 fo_type, ub4 fo_event)
  39. {
  40. /* Create zval */
  41. zval retval, params[3];
  42. php_oci_connection *connection = (php_oci_connection*)fo_ctx;
  43. /* Default return value */
  44. sb4 returnValue = 0;
  45. /* Check if userspace callback function was unregistered */
  46. if (Z_ISUNDEF(connection->taf_callback) || Z_ISNULL(connection->taf_callback)) {
  47. return 0;
  48. }
  49. /* Initialize zval */
  50. ZVAL_RES(&params[0], connection->id);
  51. ZVAL_LONG(&params[1], fo_event);
  52. ZVAL_LONG(&params[2], fo_type);
  53. /* Call user function (if possible) */
  54. if (call_user_function(EG(function_table), NULL, &connection->taf_callback, &retval, 3, params) == FAILURE) {
  55. php_error_docref(NULL, E_WARNING, "Unable to call Oracle TAF callback function");
  56. }
  57. /* Set return value */
  58. if (Z_TYPE(retval) == IS_LONG) {
  59. returnValue = (sb4) Z_LVAL(retval);
  60. }
  61. /* Setting params[0] to null so resource isn't destroyed on zval_dtor */
  62. ZVAL_NULL(&params[0]);
  63. /* Cleanup */
  64. zval_ptr_dtor(&retval);
  65. zval_ptr_dtor(&params[0]);
  66. zval_ptr_dtor(&params[1]);
  67. zval_ptr_dtor(&params[2]);
  68. return returnValue;
  69. }
  70. /* }}} */
  71. /* {{{ php_oci_unregister_taf_callback()
  72. Unregister the userspace callback function for Oracle TAF,
  73. while keeping the OCI callback alive */
  74. int php_oci_unregister_taf_callback(php_oci_connection *connection)
  75. {
  76. return php_oci_register_taf_callback(connection, NULL);
  77. }
  78. /* }}} */
  79. /* {{{ php_oci_register_taf_callback()
  80. Register a callback function for Oracle TAF */
  81. int php_oci_register_taf_callback(php_oci_connection *connection, zval *callback)
  82. {
  83. sword errstatus;
  84. int registered = 0;
  85. /* temporary failover callback structure */
  86. OCIFocbkStruct failover;
  87. if (!callback) {
  88. /* Unregister callback */
  89. if (Z_ISUNDEF(connection->taf_callback) || Z_ISNULL(connection->taf_callback)) {
  90. return 0; // Nothing to unregister
  91. }
  92. registered = 1;
  93. zval_ptr_dtor(&connection->taf_callback);
  94. ZVAL_NULL(&connection->taf_callback);
  95. } else {
  96. if (!Z_ISUNDEF(connection->taf_callback)) {
  97. registered = 1;
  98. if (!Z_ISNULL(connection->taf_callback)) {
  99. zval_ptr_dtor(&connection->taf_callback);
  100. ZVAL_NULL(&connection->taf_callback);
  101. }
  102. }
  103. /* Set userspace callback function */
  104. ZVAL_COPY(&connection->taf_callback, callback);
  105. }
  106. /* OCI callback function already registered */
  107. if (registered) {
  108. return 0;
  109. }
  110. /* set context */
  111. failover.fo_ctx = connection;
  112. /* set callback function */
  113. failover.callback_function = &callback_fn;
  114. /* do the registration */
  115. PHP_OCI_CALL_RETURN(errstatus, OCIAttrSet, (connection->server, (ub4) OCI_HTYPE_SERVER, (void *) &failover, (ub4) 0, (ub4) OCI_ATTR_FOCBK, connection->err));
  116. if (errstatus != OCI_SUCCESS) {
  117. zval_ptr_dtor(&connection->taf_callback);
  118. ZVAL_UNDEF(&connection->taf_callback);
  119. connection->errcode = php_oci_error(connection->err, errstatus);
  120. return 1;
  121. }
  122. /* successful conclusion */
  123. return 0;
  124. }
  125. /* }}} */
  126. #endif /* HAVE_OCI8 */
  127. /*
  128. * Local variables:
  129. * tab-width: 4
  130. * c-basic-offset: 4
  131. * End:
  132. * vim600: noet sw=4 ts=4 fdm=marker
  133. * vim<600: noet sw=4 ts=4
  134. */