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 */
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20
21 #include <signal.h>
22
23 #include "php.h"
24 #include "php_ini.h"
25 #include "ext/standard/info.h"
26 #include "php_mysqli_structs.h"
27 #include "mysqli_priv.h"
28 #include "zend_exceptions.h"
29
php_mysqli_throw_sql_exception(char * sqlstate,int errorno,char * format,...)30 void php_mysqli_throw_sql_exception(char *sqlstate, int errorno, char *format, ...)
31 {
32 zval sql_ex;
33 va_list arg;
34 char *message;
35
36 va_start(arg, format);
37 vspprintf(&message, 0, format, arg);
38 va_end(arg);
39
40 if (!(MyG(report_mode) & MYSQLI_REPORT_STRICT)) {
41 php_error_docref(NULL, E_WARNING, "(%s/%d): %s", sqlstate, errorno, message);
42 efree(message);
43 return;
44 }
45
46 object_init_ex(&sql_ex, mysqli_exception_class_entry);
47
48 if (message) {
49 zend_update_property_string(
50 mysqli_exception_class_entry, Z_OBJ(sql_ex), "message", sizeof("message") - 1, message);
51 }
52
53 if (sqlstate) {
54 zend_update_property_string(
55 mysqli_exception_class_entry, Z_OBJ(sql_ex), "sqlstate", sizeof("sqlstate") - 1, sqlstate);
56 } else {
57 zend_update_property_string(
58 mysqli_exception_class_entry, Z_OBJ(sql_ex), "sqlstate", sizeof("sqlstate") - 1, "00000");
59 }
60
61 efree(message);
62 zend_update_property_long(mysqli_exception_class_entry, Z_OBJ(sql_ex), "code", sizeof("code") - 1, errorno);
63
64 zend_throw_exception_object(&sql_ex);
65 }
66
PHP_METHOD(mysqli_sql_exception,getSqlState)67 PHP_METHOD(mysqli_sql_exception, getSqlState)
68 {
69 zval *prop;
70 zval rv;
71
72 ZEND_PARSE_PARAMETERS_NONE();
73
74 prop = zend_read_property(mysqli_exception_class_entry, Z_OBJ_P(ZEND_THIS), "sqlstate", sizeof("sqlstate")-1, 1, &rv);
75 ZVAL_DEREF(prop);
76 zend_string *str = zval_get_string(prop);
77
78 RETURN_STR(str);
79 }
80