mysqli_exception.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Author: Georg Richter <georg@php.net> |
  14. +----------------------------------------------------------------------+
  15. */
  16. #ifdef HAVE_CONFIG_H
  17. #include "config.h"
  18. #endif
  19. #include <signal.h>
  20. #include "php.h"
  21. #include "php_ini.h"
  22. #include "ext/standard/info.h"
  23. #include "php_mysqli_structs.h"
  24. #include "mysqli_priv.h"
  25. #include "zend_exceptions.h"
  26. void php_mysqli_throw_sql_exception(char *sqlstate, int errorno, char *format, ...)
  27. {
  28. zval sql_ex;
  29. va_list arg;
  30. char *message;
  31. va_start(arg, format);
  32. vspprintf(&message, 0, format, arg);
  33. va_end(arg);
  34. if (!(MyG(report_mode) & MYSQLI_REPORT_STRICT)) {
  35. php_error_docref(NULL, E_WARNING, "(%s/%d): %s", sqlstate, errorno, message);
  36. efree(message);
  37. return;
  38. }
  39. object_init_ex(&sql_ex, mysqli_exception_class_entry);
  40. if (message) {
  41. zend_update_property_string(
  42. mysqli_exception_class_entry, Z_OBJ(sql_ex), "message", sizeof("message") - 1, message);
  43. }
  44. if (sqlstate) {
  45. zend_update_property_string(
  46. mysqli_exception_class_entry, Z_OBJ(sql_ex), "sqlstate", sizeof("sqlstate") - 1, sqlstate);
  47. } else {
  48. zend_update_property_string(
  49. mysqli_exception_class_entry, Z_OBJ(sql_ex), "sqlstate", sizeof("sqlstate") - 1, "00000");
  50. }
  51. efree(message);
  52. zend_update_property_long(mysqli_exception_class_entry, Z_OBJ(sql_ex), "code", sizeof("code") - 1, errorno);
  53. zend_throw_exception_object(&sql_ex);
  54. }
  55. PHP_METHOD(mysqli_sql_exception, getSqlState)
  56. {
  57. zval *prop;
  58. zval rv;
  59. ZEND_PARSE_PARAMETERS_NONE();
  60. prop = zend_read_property(mysqli_exception_class_entry, Z_OBJ_P(ZEND_THIS), "sqlstate", sizeof("sqlstate")-1, 1, &rv);
  61. ZVAL_DEREF(prop);
  62. zend_string *str = zval_get_string(prop);
  63. RETURN_STR(str);
  64. }