1 : /*
2 : +----------------------------------------------------------------------+
3 : | PHP Version 5 |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) 1997-2009 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 : | Author: Georg Richter <georg@php.net> |
16 : +----------------------------------------------------------------------+
17 :
18 : */
19 : #ifdef HAVE_CONFIG_H
20 : #include "config.h"
21 : #endif
22 :
23 : #include <signal.h>
24 :
25 : #include "php.h"
26 : #include "php_ini.h"
27 : #include "ext/standard/info.h"
28 : #include "php_mysqli.h"
29 :
30 : /* {{{ void php_clear_warnings() */
31 : void php_clear_warnings(MYSQLI_WARNING *w)
32 2 : {
33 : MYSQLI_WARNING *n;
34 :
35 6 : while (w) {
36 2 : n = w;
37 2 : efree(w->reason);
38 2 : w = w->next;
39 2 : efree(n);
40 : }
41 2 : }
42 : /* }}} */
43 :
44 : /* {{{ MYSQLI_WARNING *php_new_warning */
45 : MYSQLI_WARNING *php_new_warning(char *reason, char *sqlstate, int errorno)
46 2 : {
47 : MYSQLI_WARNING *w;
48 :
49 2 : w = (MYSQLI_WARNING *)ecalloc(1, sizeof(MYSQLI_WARNING));
50 :
51 2 : w->reason = safe_estrdup(reason);
52 2 : if (sqlstate) {
53 2 : strcpy(w->sqlstate, sqlstate);
54 : } else {
55 0 : strcpy(w->sqlstate, "00000");
56 : }
57 2 : w->errorno = errorno;
58 :
59 2 : return w;
60 : }
61 : /* }}} */
62 :
63 : /* {{{ MYSQLI_WARNING *php_get_warnings(MYSQL *mysql) */
64 : MYSQLI_WARNING *php_get_warnings(MYSQL *mysql)
65 2 : {
66 2 : MYSQLI_WARNING *w, *first = NULL, *prev = NULL;
67 : MYSQL_RES *result;
68 : MYSQL_ROW row;
69 :
70 2 : if (mysql_query(mysql, "SHOW WARNINGS")) {
71 0 : return NULL;
72 : }
73 :
74 2 : result = mysql_store_result(mysql);
75 6 : while ((row = mysql_fetch_row(result))) {
76 2 : w = php_new_warning(row[2], "HY000", atoi(row[1]));
77 2 : if (!first) {
78 2 : first = w;
79 : }
80 2 : if (prev) {
81 0 : prev->next = (void *)w;
82 : }
83 2 : prev = w;
84 : }
85 2 : mysql_free_result(result);
86 2 : return first;
87 : }
88 : /* }}} */
89 :
90 : /* {{{ bool mysqli_warning::next() */
91 : PHP_METHOD(mysqli_warning, next)
92 1 : {
93 : MYSQLI_WARNING *w;
94 : zval *mysqli_warning;
95 1 : mysqli_object *obj = (mysqli_object *)zend_objects_get_address(getThis() TSRMLS_CC);
96 :
97 1 : if (obj->ptr) {
98 1 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
99 : &mysqli_warning, mysqli_warning_class_entry) == FAILURE) {
100 0 : return;
101 : }
102 :
103 1 : MYSQLI_FETCH_RESOURCE(w, MYSQLI_WARNING *, &mysqli_warning, "mysqli_warning", MYSQLI_STATUS_VALID);
104 :
105 1 : if (w->next) {
106 0 : w = w->next;
107 0 : ((MYSQLI_RESOURCE *)(obj->ptr))->ptr = w;
108 0 : RETURN_TRUE;
109 : }
110 : }
111 1 : RETURN_FALSE;
112 : }
113 : /* }}} */
114 :
115 : /* {{{ property mysqli_warning_message */
116 : int mysqli_warning_message(mysqli_object *obj, zval **retval TSRMLS_DC)
117 0 : {
118 : MYSQLI_WARNING *w;
119 :
120 0 : if (!obj->ptr || !((MYSQLI_RESOURCE *)(obj->ptr))->ptr) {
121 0 : return FAILURE;
122 : }
123 :
124 0 : w = (MYSQLI_WARNING *)((MYSQLI_RESOURCE *)(obj->ptr))->ptr;
125 0 : ALLOC_ZVAL(*retval);
126 0 : if (w->reason) {
127 0 : ZVAL_STRING(*retval, w->reason, 1);
128 : } else {
129 0 : ZVAL_NULL(*retval);
130 : }
131 0 : return SUCCESS;
132 : }
133 : /* }}} */
134 :
135 : /* {{{ property mysqli_warning_sqlstate */
136 : int mysqli_warning_sqlstate(mysqli_object *obj, zval **retval TSRMLS_DC)
137 0 : {
138 : MYSQLI_WARNING *w;
139 :
140 0 : if (!obj->ptr || !((MYSQLI_RESOURCE *)(obj->ptr))->ptr) {
141 0 : return FAILURE;
142 : }
143 :
144 0 : w = (MYSQLI_WARNING *)((MYSQLI_RESOURCE *)(obj->ptr))->ptr;
145 0 : ALLOC_ZVAL(*retval);
146 0 : ZVAL_STRING(*retval, w->sqlstate, 1);
147 0 : return SUCCESS;
148 : }
149 : /* }}} */
150 :
151 : /* {{{ property mysqli_warning_error */
152 : int mysqli_warning_errno(mysqli_object *obj, zval **retval TSRMLS_DC)
153 0 : {
154 : MYSQLI_WARNING *w;
155 :
156 0 : if (!obj->ptr || !((MYSQLI_RESOURCE *)(obj->ptr))->ptr) {
157 0 : return FAILURE;
158 : }
159 0 : w = (MYSQLI_WARNING *)((MYSQLI_RESOURCE *)(obj->ptr))->ptr;
160 0 : ALLOC_ZVAL(*retval);
161 0 : ZVAL_LONG(*retval, w->errorno);
162 0 : return SUCCESS;
163 : }
164 : /* }}} */
165 :
166 : /* {{{ mysqli_warning_construct(object obj) */
167 : PHP_METHOD(mysqli_warning, __construct)
168 2 : {
169 : zval *z;
170 : mysqli_object *obj;
171 : MYSQL *hdl;
172 : MYSQLI_WARNING *w;
173 : MYSQLI_RESOURCE *mysqli_resource;
174 :
175 2 : if (ZEND_NUM_ARGS() != 1) {
176 0 : WRONG_PARAM_COUNT;
177 : }
178 2 : if (zend_parse_parameters(1 TSRMLS_CC, "o", &z)==FAILURE) {
179 0 : return;
180 : }
181 2 : obj = (mysqli_object *)zend_object_store_get_object(z TSRMLS_CC);\
182 :
183 2 : if (obj->zo.ce == mysqli_link_class_entry) {
184 : MY_MYSQL *mysql;
185 2 : MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &z, "mysqli_link", MYSQLI_STATUS_VALID);
186 2 : hdl = mysql->mysql;
187 0 : } else if (obj->zo.ce == mysqli_stmt_class_entry) {
188 : MY_STMT *stmt;
189 0 : MYSQLI_FETCH_RESOURCE(stmt, MY_STMT *, &z, "mysqli_stmt", MYSQLI_STATUS_VALID);
190 0 : hdl = stmt->stmt->mysql;
191 : } else {
192 0 : RETURN_FALSE;
193 : }
194 :
195 2 : if (mysql_warning_count(hdl)) {
196 2 : w = php_get_warnings(hdl);
197 : } else {
198 0 : RETURN_FALSE;
199 : }
200 :
201 2 : mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE));
202 2 : mysqli_resource->status = MYSQLI_STATUS_VALID;
203 2 : mysqli_resource->ptr = mysqli_resource->info = (void *)w;
204 :
205 5 : if (!getThis() || !instanceof_function(Z_OBJCE_P(getThis()), mysqli_warning_class_entry TSRMLS_CC)) {
206 1 : MYSQLI_RETURN_RESOURCE(mysqli_resource, mysqli_warning_class_entry);
207 : } else {
208 1 : ((mysqli_object *) zend_object_store_get_object(getThis() TSRMLS_CC))->ptr = mysqli_resource;
209 : }
210 :
211 : }
212 : /* }}} */
213 :
214 : zend_function_entry mysqli_warning_methods[] = {
215 : PHP_ME(mysqli_warning, __construct, NULL, ZEND_ACC_PROTECTED)
216 : PHP_ME(mysqli_warning, next, NULL, ZEND_ACC_PUBLIC)
217 : {NULL, NULL, NULL}
218 : };
219 :
220 : mysqli_property_entry mysqli_warning_property_entries[] = {
221 : {"message", mysqli_warning_message, NULL},
222 : {"sqlstate", mysqli_warning_sqlstate, NULL},
223 : {"errno", mysqli_warning_errno, NULL},
224 : {NULL, NULL, NULL}
225 : };
226 :
227 : /*
228 : * Local variables:
229 : * tab-width: 4
230 : * c-basic-offset: 4
231 : * indent-tabs-mode: t
232 : * End:
233 : * vim600: noet sw=4 ts=4 fdm=marker
234 : * vim<600: noet sw=4 ts=4
235 : */
|