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