Annotation of embedaddon/php/ext/mysqli/mysqli_nonapi.c, revision 1.1.1.2
1.1 misho 1: /*
2: +----------------------------------------------------------------------+
3: | PHP Version 5 |
4: +----------------------------------------------------------------------+
5: | Copyright (c) 1997-2012 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: Georg Richter <georg@php.net> |
16: | Andrey Hristov <andrey@php.net> |
17: | Ulf Wendel <uw@php.net> |
18: +----------------------------------------------------------------------+
19:
1.1.1.2 ! misho 20: $Id$
1.1 misho 21: */
22:
23: #ifdef HAVE_CONFIG_H
24: #include "config.h"
25: #endif
26:
27: #include <signal.h>
28:
29: #include "php.h"
30: #include "php_ini.h"
31: #include "ext/standard/info.h"
32: #include "php_mysqli_structs.h"
33: #include "mysqli_priv.h"
34:
35: #define SAFE_STR(a) ((a)?a:"")
36:
37: #ifndef zend_parse_parameters_none
38: #define zend_parse_parameters_none() \
39: zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "")
40: #endif
41:
42: /* {{{ php_mysqli_set_error
43: */
44: static void php_mysqli_set_error(long mysql_errno, char *mysql_err TSRMLS_DC)
45: {
46: MyG(error_no) = mysql_errno;
47: if (MyG(error_msg)) {
48: efree(MyG(error_msg));
49: }
50: if(mysql_err && *mysql_err) {
51: MyG(error_msg) = estrdup(mysql_err);
52: } else {
53: MyG(error_msg) = NULL;
54: }
55: }
56: /* }}} */
57:
58:
59: void mysqli_common_connect(INTERNAL_FUNCTION_PARAMETERS, zend_bool is_real_connect, zend_bool in_ctor)
60: {
61: MY_MYSQL *mysql = NULL;
62: MYSQLI_RESOURCE *mysqli_resource = NULL;
63: zval *object = getThis();
64: char *hostname = NULL, *username=NULL, *passwd=NULL, *dbname=NULL, *socket=NULL;
65: int hostname_len = 0, username_len = 0, passwd_len = 0, dbname_len = 0, socket_len = 0;
66: zend_bool persistent = FALSE;
67: long port = 0, flags = 0;
68: uint hash_len;
69: char *hash_key = NULL;
70: zend_bool new_connection = FALSE;
71: zend_rsrc_list_entry *le;
72: mysqli_plist_entry *plist = NULL;
73: zend_bool self_alloced = 0;
74:
75:
76: #if !defined(MYSQL_USE_MYSQLND)
77: if ((MYSQL_VERSION_ID / 100) != (mysql_get_client_version() / 100)) {
78: php_error_docref(NULL TSRMLS_CC, E_WARNING,
79: "Headers and client library minor version mismatch. Headers:%d Library:%ld",
80: MYSQL_VERSION_ID, mysql_get_client_version());
81: }
82: #endif
83:
84: if (getThis() && !ZEND_NUM_ARGS() && in_ctor) {
85: php_mysqli_init(INTERNAL_FUNCTION_PARAM_PASSTHRU);
86: return;
87: }
88: hostname = username = dbname = passwd = socket = NULL;
89:
90: if (!is_real_connect) {
91: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ssssls", &hostname, &hostname_len, &username, &username_len,
92: &passwd, &passwd_len, &dbname, &dbname_len, &port, &socket, &socket_len) == FAILURE) {
93: return;
94: }
95:
96: if (object && instanceof_function(Z_OBJCE_P(object), mysqli_link_class_entry TSRMLS_CC)) {
97: mysqli_resource = ((mysqli_object *) zend_object_store_get_object(object TSRMLS_CC))->ptr;
98: if (mysqli_resource && mysqli_resource->ptr) {
99: mysql = (MY_MYSQL*) mysqli_resource->ptr;
100: }
101: }
102: if (!mysql) {
103: mysql = (MY_MYSQL *) ecalloc(1, sizeof(MY_MYSQL));
104: self_alloced = 1;
105: }
106: flags |= CLIENT_MULTI_RESULTS; /* needed for mysql_multi_query() */
107: } else {
108: /* We have flags too */
109: if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O|sssslsl", &object, mysqli_link_class_entry,
110: &hostname, &hostname_len, &username, &username_len, &passwd, &passwd_len, &dbname, &dbname_len, &port, &socket, &socket_len,
111: &flags) == FAILURE) {
112: return;
113: }
114:
115: mysqli_resource = ((mysqli_object *) zend_object_store_get_object(object TSRMLS_CC))->ptr;
116: MYSQLI_FETCH_RESOURCE_CONN(mysql, &object, MYSQLI_STATUS_INITIALIZED);
117:
118: /* set some required options */
119: flags |= CLIENT_MULTI_RESULTS; /* needed for mysql_multi_query() */
120: /* remove some insecure options */
121: flags &= ~CLIENT_MULTI_STATEMENTS; /* don't allow multi_queries via connect parameter */
122: if (PG(open_basedir) && PG(open_basedir)[0] != '\0') {
123: flags &= ~CLIENT_LOCAL_FILES;
124: }
125: }
126:
127: if (!socket_len || !socket) {
128: socket = MyG(default_socket);
129: }
130: if (!port){
131: port = MyG(default_port);
132: }
133: if (!passwd) {
134: passwd = MyG(default_pw);
135: passwd_len = strlen(SAFE_STR(passwd));
136: }
137: if (!username){
138: username = MyG(default_user);
139: }
140: if (!hostname || !hostname_len) {
141: hostname = MyG(default_host);
142: }
143:
144: if (mysql->mysql && mysqli_resource &&
145: (mysqli_resource->status > MYSQLI_STATUS_INITIALIZED))
146: {
147: /* already connected, we should close the connection */
148: php_mysqli_close(mysql, MYSQLI_CLOSE_IMPLICIT, mysqli_resource->status TSRMLS_CC);
149: }
150:
151: if (strlen(SAFE_STR(hostname)) > 2 && !strncasecmp(hostname, "p:", 2)) {
152: hostname += 2;
153: if (!MyG(allow_persistent)) {
154: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Persistent connections are disabled. Downgrading to normal");
155: } else {
156: mysql->persistent = persistent = TRUE;
157:
158: hash_len = spprintf(&hash_key, 0, "mysqli_%s_%s%ld%s%s%s", SAFE_STR(hostname), SAFE_STR(socket),
159: port, SAFE_STR(username), SAFE_STR(dbname),
160: SAFE_STR(passwd));
161:
162: mysql->hash_key = hash_key;
163:
164: /* check if we can reuse exisiting connection ... */
165: if (zend_hash_find(&EG(persistent_list), hash_key, hash_len + 1, (void **)&le) == SUCCESS) {
166: if (Z_TYPE_P(le) == php_le_pmysqli()) {
167: plist = (mysqli_plist_entry *) le->ptr;
168:
169: do {
170: if (zend_ptr_stack_num_elements(&plist->free_links)) {
171: mysql->mysql = zend_ptr_stack_pop(&plist->free_links);
172:
173: MyG(num_inactive_persistent)--;
174: /* reset variables */
175:
176: #ifndef MYSQLI_NO_CHANGE_USER_ON_PCONNECT
1.1.1.2 ! misho 177: if (!mysqli_change_user_silent(mysql->mysql, username, passwd, dbname, passwd_len)) {
1.1 misho 178: #else
179: if (!mysql_ping(mysql->mysql)) {
180: #endif
181: #ifdef MYSQLI_USE_MYSQLND
182: mysqlnd_restart_psession(mysql->mysql);
183: #endif
184: MyG(num_active_persistent)++;
185: goto end;
186: } else {
187: mysqli_close(mysql->mysql, MYSQLI_CLOSE_IMPLICIT);
188: mysql->mysql = NULL;
189: }
190: }
191: } while (0);
192: }
193: } else {
194: zend_rsrc_list_entry le;
195: le.type = php_le_pmysqli();
196: le.ptr = plist = calloc(1, sizeof(mysqli_plist_entry));
197:
198: zend_ptr_stack_init_ex(&plist->free_links, 1);
199: zend_hash_update(&EG(persistent_list), hash_key, hash_len + 1, (void *)&le, sizeof(le), NULL);
200: }
201: }
202: }
203: if (MyG(max_links) != -1 && MyG(num_links) >= MyG(max_links)) {
204: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Too many open links (%ld)", MyG(num_links));
205: goto err;
206: }
207:
208: if (persistent && MyG(max_persistent) != -1 &&
209: (MyG(num_active_persistent) + MyG(num_inactive_persistent))>= MyG(max_persistent))
210: {
211: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Too many open persistent links (%ld)",
212: MyG(num_active_persistent) + MyG(num_inactive_persistent));
213: goto err;
214: }
215: if (!mysql->mysql) {
216: #if !defined(MYSQLI_USE_MYSQLND)
217: if (!(mysql->mysql = mysql_init(NULL))) {
218: #else
219: if (!(mysql->mysql = mysqlnd_init(persistent))) {
220: #endif
221: goto err;
222: }
223: new_connection = TRUE;
224: }
225:
226: #ifdef HAVE_EMBEDDED_MYSQLI
227: if (hostname_len) {
228: unsigned int external=1;
229: mysql_options(mysql->mysql, MYSQL_OPT_USE_REMOTE_CONNECTION, (char *)&external);
230: } else {
231: mysql_options(mysql->mysql, MYSQL_OPT_USE_EMBEDDED_CONNECTION, 0);
232: }
233: #endif
234:
235: #if !defined(MYSQLI_USE_MYSQLND)
236: /* BC for prior to bug fix #53425 */
237: flags |= CLIENT_MULTI_RESULTS;
238:
239: if (mysql_real_connect(mysql->mysql, hostname, username, passwd, dbname, port, socket, flags) == NULL)
240: #else
241: if (mysqlnd_connect(mysql->mysql, hostname, username, passwd, passwd_len, dbname, dbname_len,
242: port, socket, flags TSRMLS_CC) == NULL)
243: #endif
244: {
245: /* Save error messages - for mysqli_connect_error() & mysqli_connect_errno() */
246: php_mysqli_set_error(mysql_errno(mysql->mysql), (char *) mysql_error(mysql->mysql) TSRMLS_CC);
247: php_mysqli_throw_sql_exception((char *)mysql_sqlstate(mysql->mysql), mysql_errno(mysql->mysql) TSRMLS_CC,
248: "%s", mysql_error(mysql->mysql));
249: if (!is_real_connect) {
250: /* free mysql structure */
251: mysqli_close(mysql->mysql, MYSQLI_CLOSE_DISCONNECTED);
252: mysql->mysql = NULL;
253: }
254: goto err;
255: }
256:
257: /* clear error */
258: php_mysqli_set_error(mysql_errno(mysql->mysql), (char *) mysql_error(mysql->mysql) TSRMLS_CC);
259:
260: #if !defined(MYSQLI_USE_MYSQLND)
261: mysql->mysql->reconnect = MyG(reconnect);
262:
263: /* set our own local_infile handler */
264: php_set_local_infile_handler_default(mysql);
265: #endif
266:
267: mysql_options(mysql->mysql, MYSQL_OPT_LOCAL_INFILE, (char *)&MyG(allow_local_infile));
268:
269: end:
270: if (!mysqli_resource) {
271: mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE));
272: mysqli_resource->ptr = (void *)mysql;
273: }
274: mysqli_resource->status = MYSQLI_STATUS_VALID;
275:
276: /* store persistent connection */
277: if (persistent && (new_connection || is_real_connect)) {
278: MyG(num_active_persistent)++;
279: }
280:
281: MyG(num_links)++;
282:
283: mysql->multi_query = 0;
284:
285: if (!object || !instanceof_function(Z_OBJCE_P(object), mysqli_link_class_entry TSRMLS_CC)) {
286: MYSQLI_RETURN_RESOURCE(mysqli_resource, mysqli_link_class_entry);
287: } else {
288: ((mysqli_object *) zend_object_store_get_object(object TSRMLS_CC))->ptr = mysqli_resource;
289: }
290: if (!is_real_connect) {
291: return;
292: } else {
293: RETURN_TRUE;
294: }
295:
296: err:
297: if (mysql->hash_key) {
298: efree(mysql->hash_key);
299: mysql->hash_key = NULL;
300: mysql->persistent = FALSE;
301: }
302: if (!is_real_connect && self_alloced) {
303: efree(mysql);
304: }
305: RETVAL_FALSE;
306: }
307:
308:
309: /* {{{ proto object mysqli_connect([string hostname [,string username [,string passwd [,string dbname [,int port [,string socket]]]]]])
310: Open a connection to a mysql server */
311: PHP_FUNCTION(mysqli_connect)
312: {
313: mysqli_common_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, FALSE, FALSE);
314: }
315: /* }}} */
316:
317:
318: /* {{{ proto object mysqli_link_construct()
319: */
320: PHP_FUNCTION(mysqli_link_construct)
321: {
322: mysqli_common_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, FALSE, TRUE);
323: }
324: /* }}} */
325:
326:
327: /* {{{ proto int mysqli_connect_errno(void)
328: Returns the numerical value of the error message from last connect command */
329: PHP_FUNCTION(mysqli_connect_errno)
330: {
331: RETURN_LONG(MyG(error_no));
332: }
333: /* }}} */
334:
335: /* {{{ proto string mysqli_connect_error(void)
336: Returns the text of the error message from previous MySQL operation */
337: PHP_FUNCTION(mysqli_connect_error)
338: {
339: if (MyG(error_msg)) {
340: RETURN_STRING(MyG(error_msg),1);
341: } else {
342: RETURN_NULL();
343: }
344: }
345: /* }}} */
346:
347:
348: /* {{{ proto mixed mysqli_fetch_array (object result [,int resulttype])
349: Fetch a result row as an associative array, a numeric array, or both */
350: PHP_FUNCTION(mysqli_fetch_array)
351: {
352: php_mysqli_fetch_into_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, 0);
353: }
354: /* }}} */
355:
356: /* {{{ proto mixed mysqli_fetch_assoc (object result)
357: Fetch a result row as an associative array */
358: PHP_FUNCTION(mysqli_fetch_assoc)
359: {
360: php_mysqli_fetch_into_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, MYSQLI_ASSOC, 0);
361: }
362: /* }}} */
363:
364:
365: /* {{{ proto mixed mysqli_fetch_all (object result [,int resulttype])
366: Fetches all result rows as an associative array, a numeric array, or both */
367: #if defined(MYSQLI_USE_MYSQLND)
368: PHP_FUNCTION(mysqli_fetch_all)
369: {
370: MYSQL_RES *result;
371: zval *mysql_result;
372: long mode = MYSQLND_FETCH_NUM;
373:
374: if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O|l", &mysql_result, mysqli_result_class_entry, &mode) == FAILURE) {
375: return;
376: }
377: MYSQLI_FETCH_RESOURCE(result, MYSQL_RES *, &mysql_result, "mysqli_result", MYSQLI_STATUS_VALID);
378:
379: if (!mode || (mode & ~MYSQLND_FETCH_BOTH)) {
380: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Mode can be only MYSQLI_FETCH_NUM, "
381: "MYSQLI_FETCH_ASSOC or MYSQLI_FETCH_BOTH");
382: RETURN_FALSE;
383: }
384:
385: mysqlnd_fetch_all(result, mode, return_value);
386: }
387: /* }}} */
388:
389:
390:
391: /* {{{ proto array mysqli_get_client_stats(void)
392: Returns statistics about the zval cache */
393: PHP_FUNCTION(mysqli_get_client_stats)
394: {
395: if (zend_parse_parameters_none() == FAILURE) {
396: return;
397: }
398: mysqlnd_get_client_stats(return_value);
399: }
400: /* }}} */
401:
402:
403: /* {{{ proto array mysqli_get_connection_stats(void)
404: Returns statistics about the zval cache */
405: PHP_FUNCTION(mysqli_get_connection_stats)
406: {
407: MY_MYSQL *mysql;
408: zval *mysql_link;
409:
410: if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
411: &mysql_link, mysqli_link_class_entry) == FAILURE) {
412: return;
413: }
414: MYSQLI_FETCH_RESOURCE_CONN(mysql, &mysql_link, MYSQLI_STATUS_VALID);
415:
416: mysqlnd_get_connection_stats(mysql->mysql, return_value);
417: }
418: #endif
419: /* }}} */
420:
1.1.1.2 ! misho 421: /* {{{ proto mixed mysqli_error_list (object connection)
! 422: Fetches all client errors */
! 423: PHP_FUNCTION(mysqli_error_list)
! 424: {
! 425: MY_MYSQL *mysql;
! 426: zval *mysql_link;
! 427:
! 428: if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
! 429: return;
! 430: }
! 431: MYSQLI_FETCH_RESOURCE_CONN(mysql, &mysql_link, MYSQLI_STATUS_VALID);
! 432: array_init(return_value);
! 433: #if defined(MYSQLI_USE_MYSQLND)
! 434: if (mysql->mysql->data->error_info->error_list) {
! 435: MYSQLND_ERROR_LIST_ELEMENT * message;
! 436: zend_llist_position pos;
! 437: for (message = (MYSQLND_ERROR_LIST_ELEMENT *) zend_llist_get_first_ex(mysql->mysql->data->error_info->error_list, &pos);
! 438: message;
! 439: message = (MYSQLND_ERROR_LIST_ELEMENT *) zend_llist_get_next_ex(mysql->mysql->data->error_info->error_list, &pos))
! 440: {
! 441: zval * single_error;
! 442: MAKE_STD_ZVAL(single_error);
! 443: array_init(single_error);
! 444: add_assoc_long_ex(single_error, "errno", sizeof("errno"), message->error_no);
! 445: add_assoc_string_ex(single_error, "sqlstate", sizeof("sqlstate"), message->sqlstate, 1);
! 446: add_assoc_string_ex(single_error, "error", sizeof("error"), message->error, 1);
! 447: add_next_index_zval(return_value, single_error);
! 448: }
! 449: }
! 450: #else
! 451: if (mysql_errno(mysql->mysql)) {
! 452: zval * single_error;
! 453: MAKE_STD_ZVAL(single_error);
! 454: array_init(single_error);
! 455: add_assoc_long_ex(single_error, "errno", sizeof("errno"), mysql_errno(mysql->mysql));
! 456: add_assoc_string_ex(single_error, "sqlstate", sizeof("sqlstate"), mysql_sqlstate(mysql->mysql), 1);
! 457: add_assoc_string_ex(single_error, "error", sizeof("error"), mysql_error(mysql->mysql), 1);
! 458: add_next_index_zval(return_value, single_error);
! 459: }
! 460: #endif
! 461: }
! 462: /* }}} */
! 463:
! 464:
! 465: /* {{{ proto string mysqli_stmt_error_list(object stmt)
! 466: */
! 467: PHP_FUNCTION(mysqli_stmt_error_list)
! 468: {
! 469: MY_STMT *stmt;
! 470: zval *mysql_stmt;
! 471:
! 472: if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_stmt, mysqli_stmt_class_entry) == FAILURE) {
! 473: return;
! 474: }
! 475: MYSQLI_FETCH_RESOURCE_STMT(stmt, &mysql_stmt, MYSQLI_STATUS_INITIALIZED);
! 476: array_init(return_value);
! 477: #if defined(MYSQLI_USE_MYSQLND)
! 478: if (stmt->stmt && stmt->stmt->data && stmt->stmt->data->error_info->error_list) {
! 479: MYSQLND_ERROR_LIST_ELEMENT * message;
! 480: zend_llist_position pos;
! 481: for (message = (MYSQLND_ERROR_LIST_ELEMENT *) zend_llist_get_first_ex(stmt->stmt->data->error_info->error_list, &pos);
! 482: message;
! 483: message = (MYSQLND_ERROR_LIST_ELEMENT *) zend_llist_get_next_ex(stmt->stmt->data->error_info->error_list, &pos))
! 484: {
! 485: zval * single_error;
! 486: MAKE_STD_ZVAL(single_error);
! 487: array_init(single_error);
! 488: add_assoc_long_ex(single_error, "errno", sizeof("errno"), message->error_no);
! 489: add_assoc_string_ex(single_error, "sqlstate", sizeof("sqlstate"), message->sqlstate, 1);
! 490: add_assoc_string_ex(single_error, "error", sizeof("error"), message->error, 1);
! 491: add_next_index_zval(return_value, single_error);
! 492: }
! 493: }
! 494: #else
! 495: if (mysql_stmt_errno(stmt->stmt)) {
! 496: zval * single_error;
! 497: MAKE_STD_ZVAL(single_error);
! 498: array_init(single_error);
! 499: add_assoc_long_ex(single_error, "errno", sizeof("errno"), mysql_stmt_errno(stmt->stmt));
! 500: add_assoc_string_ex(single_error, "sqlstate", sizeof("sqlstate"), mysql_stmt_sqlstate(stmt->stmt), 1);
! 501: add_assoc_string_ex(single_error, "error", sizeof("error"), mysql_stmt_error(stmt->stmt), 1);
! 502: add_next_index_zval(return_value, single_error);
! 503: }
! 504: #endif
! 505: }
! 506: /* }}} */
! 507:
1.1 misho 508:
509: /* {{{ proto mixed mysqli_fetch_object (object result [, string class_name [, NULL|array ctor_params]])
510: Fetch a result row as an object */
511: PHP_FUNCTION(mysqli_fetch_object)
512: {
513: php_mysqli_fetch_into_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, MYSQLI_ASSOC, 1);
514: }
515: /* }}} */
516:
517: /* {{{ proto bool mysqli_multi_query(object link, string query)
518: allows to execute multiple queries */
519: PHP_FUNCTION(mysqli_multi_query)
520: {
521: MY_MYSQL *mysql;
522: zval *mysql_link;
523: char *query = NULL;
524: int query_len;
525:
526: if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os", &mysql_link, mysqli_link_class_entry, &query, &query_len) == FAILURE) {
527: return;
528: }
529: MYSQLI_FETCH_RESOURCE_CONN(mysql, &mysql_link, MYSQLI_STATUS_VALID);
530:
531: MYSQLI_ENABLE_MQ;
532: if (mysql_real_query(mysql->mysql, query, query_len)) {
533: #ifndef MYSQLI_USE_MYSQLND
534: char s_error[MYSQL_ERRMSG_SIZE], s_sqlstate[SQLSTATE_LENGTH+1];
535: unsigned int s_errno;
536: /* we have to save error information, cause
537: MYSQLI_DISABLE_MQ will reset error information */
538: strcpy(s_error, mysql_error(mysql->mysql));
539: strcpy(s_sqlstate, mysql_sqlstate(mysql->mysql));
540: s_errno = mysql_errno(mysql->mysql);
541: #else
1.1.1.2 ! misho 542: MYSQLND_ERROR_INFO error_info = *mysql->mysql->data->error_info;
1.1 misho 543: #endif
544: MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql);
545: MYSQLI_DISABLE_MQ;
546:
547: #ifndef MYSQLI_USE_MYSQLND
548: /* restore error information */
549: strcpy(mysql->mysql->net.last_error, s_error);
550: strcpy(mysql->mysql->net.sqlstate, s_sqlstate);
551: mysql->mysql->net.last_errno = s_errno;
552: #else
1.1.1.2 ! misho 553: *mysql->mysql->data->error_info = error_info;
1.1 misho 554: #endif
555: RETURN_FALSE;
556: }
557: RETURN_TRUE;
558: }
559: /* }}} */
560:
561: /* {{{ proto mixed mysqli_query(object link, string query [,int resultmode]) */
562: PHP_FUNCTION(mysqli_query)
563: {
564: MY_MYSQL *mysql;
565: zval *mysql_link;
566: MYSQLI_RESOURCE *mysqli_resource;
567: MYSQL_RES *result;
568: char *query = NULL;
569: int query_len;
570: long resultmode = MYSQLI_STORE_RESULT;
571:
572: if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os|l", &mysql_link, mysqli_link_class_entry, &query, &query_len, &resultmode) == FAILURE) {
573: return;
574: }
575:
576: if (!query_len) {
577: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty query");
578: RETURN_FALSE;
579: }
580: if ((resultmode & ~MYSQLI_ASYNC) != MYSQLI_USE_RESULT && (resultmode & ~MYSQLI_ASYNC) != MYSQLI_STORE_RESULT) {
581: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid value for resultmode");
582: RETURN_FALSE;
583: }
584:
585: MYSQLI_FETCH_RESOURCE_CONN(mysql, &mysql_link, MYSQLI_STATUS_VALID);
586:
587: MYSQLI_DISABLE_MQ;
588:
589:
590: #ifdef MYSQLI_USE_MYSQLND
591: if (resultmode & MYSQLI_ASYNC) {
592: if (mysqli_async_query(mysql->mysql, query, query_len)) {
593: MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql);
594: RETURN_FALSE;
595: }
596: mysql->async_result_fetch_type = resultmode & ~MYSQLI_ASYNC;
597: RETURN_TRUE;
598: }
599: #endif
600:
601: if (mysql_real_query(mysql->mysql, query, query_len)) {
602: MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql);
603: RETURN_FALSE;
604: }
605:
606: if (!mysql_field_count(mysql->mysql)) {
607: /* no result set - not a SELECT */
608: if (MyG(report_mode) & MYSQLI_REPORT_INDEX) {
609: php_mysqli_report_index(query, mysqli_server_status(mysql->mysql) TSRMLS_CC);
610: }
611: RETURN_TRUE;
612: }
613:
614: switch (resultmode) {
615: case MYSQLI_STORE_RESULT:
616: result = mysql_store_result(mysql->mysql);
617: break;
618: case MYSQLI_USE_RESULT:
619: result = mysql_use_result(mysql->mysql);
620: break;
621: }
622: if (!result) {
623: php_mysqli_throw_sql_exception((char *)mysql_sqlstate(mysql->mysql), mysql_errno(mysql->mysql) TSRMLS_CC,
624: "%s", mysql_error(mysql->mysql));
625: RETURN_FALSE;
626: }
627:
628: if (MyG(report_mode) & MYSQLI_REPORT_INDEX) {
629: php_mysqli_report_index(query, mysqli_server_status(mysql->mysql) TSRMLS_CC);
630: }
631:
632: mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE));
633: mysqli_resource->ptr = (void *)result;
634: mysqli_resource->status = MYSQLI_STATUS_VALID;
635: MYSQLI_RETURN_RESOURCE(mysqli_resource, mysqli_result_class_entry);
636: }
637: /* }}} */
638:
639:
640: #if defined(MYSQLI_USE_MYSQLND)
641: #include "php_network.h"
642: /* {{{ mysqlnd_zval_array_to_mysqlnd_array functions */
643: static int mysqlnd_zval_array_to_mysqlnd_array(zval *in_array, MYSQLND ***out_array TSRMLS_DC)
644: {
645: zval **elem;
646: int i = 0, current = 0;
647:
648: if (Z_TYPE_P(in_array) != IS_ARRAY) {
649: return 0;
650: }
651: *out_array = ecalloc(zend_hash_num_elements(Z_ARRVAL_P(in_array)) + 1, sizeof(MYSQLND *));
652: for (zend_hash_internal_pointer_reset(Z_ARRVAL_P(in_array));
653: zend_hash_get_current_data(Z_ARRVAL_P(in_array), (void **) &elem) == SUCCESS;
654: zend_hash_move_forward(Z_ARRVAL_P(in_array))) {
655: i++;
656: if (Z_TYPE_PP(elem) != IS_OBJECT ||
657: !instanceof_function(Z_OBJCE_PP(elem), mysqli_link_class_entry TSRMLS_CC)) {
658: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Parameter %d not a mysqli object", i);
659: } else {
660: MY_MYSQL *mysql;
661: MYSQLI_RESOURCE *my_res;
662: mysqli_object *intern = (mysqli_object *)zend_object_store_get_object(*elem TSRMLS_CC);
663: if (!(my_res = (MYSQLI_RESOURCE *)intern->ptr)) {
664: php_error_docref(NULL TSRMLS_CC, E_WARNING, "[%d] Couldn't fetch %s", i, intern->zo.ce->name);
665: continue;
666: }
667: mysql = (MY_MYSQL*) my_res->ptr;
668: if (MYSQLI_STATUS_VALID && my_res->status < MYSQLI_STATUS_VALID) {
669: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid object %d or resource %s", i, intern->zo.ce->name);
670: continue;
671: }
672: (*out_array)[current++] = mysql->mysql;
673: }
674: }
675: return 0;
676: }
677: /* }}} */
678:
679:
680: /* {{{ mysqlnd_zval_array_from_mysqlnd_array */
681: static int mysqlnd_zval_array_from_mysqlnd_array(MYSQLND **in_array, zval *out_array TSRMLS_DC)
682: {
683: MYSQLND **p = in_array;
684: HashTable *new_hash;
685: zval **elem, **dest_elem;
686: int ret = 0;
687:
688: ALLOC_HASHTABLE(new_hash);
689: zend_hash_init(new_hash, zend_hash_num_elements(Z_ARRVAL_P(out_array)), NULL, ZVAL_PTR_DTOR, 0);
690:
691: for (zend_hash_internal_pointer_reset(Z_ARRVAL_P(out_array));
692: zend_hash_get_current_data(Z_ARRVAL_P(out_array), (void **) &elem) == SUCCESS;
693: zend_hash_move_forward(Z_ARRVAL_P(out_array)))
694: {
695: if (Z_TYPE_PP(elem) != IS_OBJECT || !instanceof_function(Z_OBJCE_PP(elem), mysqli_link_class_entry TSRMLS_CC)) {
696: continue;
697: }
698: {
699: MY_MYSQL *mysql;
700: mysqli_object *intern = (mysqli_object *)zend_object_store_get_object(*elem TSRMLS_CC);
701: mysql = (MY_MYSQL *) ((MYSQLI_RESOURCE *)intern->ptr)->ptr;
702: if (mysql->mysql == *p) {
703: zend_hash_next_index_insert(new_hash, (void *)elem, sizeof(zval *), (void **)&dest_elem);
704: if (dest_elem) {
705: zval_add_ref(dest_elem);
706: }
707: ret++;
708: p++;
709: }
710: }
711: }
712:
713: /* destroy old array and add new one */
714: zend_hash_destroy(Z_ARRVAL_P(out_array));
715: efree(Z_ARRVAL_P(out_array));
716:
717: zend_hash_internal_pointer_reset(new_hash);
718: Z_ARRVAL_P(out_array) = new_hash;
719:
720: return 0;
721: }
722: /* }}} */
723:
724:
725: /* {{{ mysqlnd_dont_poll_zval_array_from_mysqlnd_array */
726: static int mysqlnd_dont_poll_zval_array_from_mysqlnd_array(MYSQLND **in_array, zval *in_zval_array, zval *out_array TSRMLS_DC)
727: {
728: MYSQLND **p = in_array;
729: HashTable *new_hash;
730: zval **elem, **dest_elem;
731: int ret = 0;
732:
733: ALLOC_HASHTABLE(new_hash);
734: zend_hash_init(new_hash, zend_hash_num_elements(Z_ARRVAL_P(in_zval_array)), NULL, ZVAL_PTR_DTOR, 0);
735: if (in_array) {
736: for (zend_hash_internal_pointer_reset(Z_ARRVAL_P(in_zval_array));
737: zend_hash_get_current_data(Z_ARRVAL_P(in_zval_array), (void **) &elem) == SUCCESS;
738: zend_hash_move_forward(Z_ARRVAL_P(in_zval_array)))
739: {
740: MY_MYSQL *mysql;
741: mysqli_object *intern = (mysqli_object *)zend_object_store_get_object(*elem TSRMLS_CC);
742: mysql = (MY_MYSQL *) ((MYSQLI_RESOURCE *)intern->ptr)->ptr;
743: if (mysql->mysql == *p) {
744: zend_hash_next_index_insert(new_hash, (void *)elem, sizeof(zval *), (void **)&dest_elem);
745: if (dest_elem) {
746: zval_add_ref(dest_elem);
747: }
748: ret++;
749: p++;
750: }
751: }
752: }
753:
754: /* destroy old array and add new one */
755: zend_hash_destroy(Z_ARRVAL_P(out_array));
756: efree(Z_ARRVAL_P(out_array));
757:
758: zend_hash_internal_pointer_reset(new_hash);
759: Z_ARRVAL_P(out_array) = new_hash;
760:
761: return 0;
762: }
763: /* }}} */
764:
765:
766: /* {{{ proto int mysqli_poll(array read, array write, array error, long sec [, long usec]) U
767: Poll connections */
768: PHP_FUNCTION(mysqli_poll)
769: {
770: zval *r_array, *e_array, *dont_poll_array;
771: MYSQLND **new_r_array = NULL, **new_e_array = NULL, **new_dont_poll_array = NULL;
772: long sec = 0, usec = 0;
773: enum_func_status ret;
774: uint desc_num;
775:
776: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a!a!al|l", &r_array, &e_array, &dont_poll_array, &sec, &usec) == FAILURE) {
777: return;
778: }
779: if (sec < 0 || usec < 0) {
780: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Negative values passed for sec and/or usec");
781: RETURN_FALSE;
782: }
783:
784: if (r_array != NULL) {
785: mysqlnd_zval_array_to_mysqlnd_array(r_array, &new_r_array TSRMLS_CC);
786: }
787: if (e_array != NULL) {
788: mysqlnd_zval_array_to_mysqlnd_array(e_array, &new_e_array TSRMLS_CC);
789: }
790:
791: ret = mysqlnd_poll(new_r_array, new_e_array, &new_dont_poll_array, sec, usec, &desc_num);
792:
793: mysqlnd_dont_poll_zval_array_from_mysqlnd_array(r_array != NULL ? new_dont_poll_array:NULL, r_array, dont_poll_array TSRMLS_CC);
794:
795: if (r_array != NULL) {
796: mysqlnd_zval_array_from_mysqlnd_array(new_r_array, r_array TSRMLS_CC);
797: }
798: if (e_array != NULL) {
799: mysqlnd_zval_array_from_mysqlnd_array(new_e_array, e_array TSRMLS_CC);
800: }
801:
802: if (new_dont_poll_array) {
803: efree(new_dont_poll_array);
804: }
805: if (new_r_array) {
806: efree(new_r_array);
807: }
808: if (new_e_array) {
809: efree(new_e_array);
810: }
811: if (ret == PASS) {
812: RETURN_LONG(desc_num);
813: } else {
814: RETURN_FALSE;
815: }
816: }
817: /* }}} */
818:
819:
820: /* {{{ proto int mysqli_reap_async_query(object link) U
821: Poll connections */
822: PHP_FUNCTION(mysqli_reap_async_query)
823: {
824: MY_MYSQL *mysql;
825: zval *mysql_link;
826: MYSQLI_RESOURCE *mysqli_resource;
827: MYSQL_RES *result;
828:
829: if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
830: return;
831: }
832:
833: MYSQLI_FETCH_RESOURCE_CONN(mysql, &mysql_link, MYSQLI_STATUS_VALID);
834:
835: if (FAIL == mysqlnd_reap_async_query(mysql->mysql)) {
836: RETURN_FALSE;
837: }
838:
839: if (!mysql_field_count(mysql->mysql)) {
840: /* no result set - not a SELECT */
841: if (MyG(report_mode) & MYSQLI_REPORT_INDEX) {
842: /* php_mysqli_report_index("n/a", mysqli_server_status(mysql->mysql) TSRMLS_CC); */
843: }
844: RETURN_TRUE;
845: }
846:
847: switch (mysql->async_result_fetch_type) {
848: case MYSQLI_STORE_RESULT:
849: result = mysql_store_result(mysql->mysql);
850: break;
851: case MYSQLI_USE_RESULT:
852: result = mysql_use_result(mysql->mysql);
853: break;
854: }
855:
856: if (!result) {
857: php_mysqli_throw_sql_exception((char *)mysql_sqlstate(mysql->mysql), mysql_errno(mysql->mysql) TSRMLS_CC,
858: "%s", mysql_error(mysql->mysql));
859: RETURN_FALSE;
860: }
861:
862: if (MyG(report_mode) & MYSQLI_REPORT_INDEX) {
863: /* php_mysqli_report_index("n/a", mysqli_server_status(mysql->mysql) TSRMLS_CC); */
864: }
865:
866: mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE));
867: mysqli_resource->ptr = (void *)result;
868: mysqli_resource->status = MYSQLI_STATUS_VALID;
869: MYSQLI_RETURN_RESOURCE(mysqli_resource, mysqli_result_class_entry);
870: }
871: /* }}} */
872:
873:
874: /* {{{ proto object mysqli_stmt_get_result(object link) U
875: Buffer result set on client */
876: PHP_FUNCTION(mysqli_stmt_get_result)
877: {
878: MYSQL_RES *result;
879: MYSQLI_RESOURCE *mysqli_resource;
880: MY_STMT *stmt;
881: zval *mysql_stmt;
882:
883: if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_stmt, mysqli_stmt_class_entry) == FAILURE) {
884: return;
885: }
886: MYSQLI_FETCH_RESOURCE_STMT(stmt, &mysql_stmt, MYSQLI_STATUS_VALID);
887:
888: if (!(result = mysqlnd_stmt_get_result(stmt->stmt))) {
889: MYSQLI_REPORT_STMT_ERROR(stmt->stmt);
890: RETURN_FALSE;
891: }
892:
893: mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE));
894: mysqli_resource->ptr = (void *)result;
895: mysqli_resource->status = MYSQLI_STATUS_VALID;
896: MYSQLI_RETURN_RESOURCE(mysqli_resource, mysqli_result_class_entry);
897: }
898: /* }}} */
899: #endif
900:
901:
902: /* {{{ proto object mysqli_get_warnings(object link) */
903: PHP_FUNCTION(mysqli_get_warnings)
904: {
905: MY_MYSQL *mysql;
906: zval *mysql_link;
907: MYSQLI_RESOURCE *mysqli_resource;
908: MYSQLI_WARNING *w;
909:
910: if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
911: return;
912: }
913: MYSQLI_FETCH_RESOURCE_CONN(mysql, &mysql_link, MYSQLI_STATUS_VALID);
914:
915: if (mysql_warning_count(mysql->mysql)) {
1.1.1.2 ! misho 916: #ifdef MYSQLI_USE_MYSQLND
! 917: w = php_get_warnings(mysql->mysql->data TSRMLS_CC);
! 918: #else
1.1 misho 919: w = php_get_warnings(mysql->mysql TSRMLS_CC);
1.1.1.2 ! misho 920: #endif
1.1 misho 921: } else {
922: RETURN_FALSE;
923: }
924: mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE));
925: mysqli_resource->ptr = mysqli_resource->info = (void *)w;
926: mysqli_resource->status = MYSQLI_STATUS_VALID;
927: MYSQLI_RETURN_RESOURCE(mysqli_resource, mysqli_warning_class_entry);
928: }
929: /* }}} */
930:
1.1.1.2 ! misho 931:
1.1 misho 932: /* {{{ proto object mysqli_stmt_get_warnings(object link) */
933: PHP_FUNCTION(mysqli_stmt_get_warnings)
934: {
935: MY_STMT *stmt;
936: zval *stmt_link;
937: MYSQLI_RESOURCE *mysqli_resource;
938: MYSQLI_WARNING *w;
939:
940: if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &stmt_link, mysqli_stmt_class_entry) == FAILURE) {
941: return;
942: }
943: MYSQLI_FETCH_RESOURCE_STMT(stmt, &stmt_link, MYSQLI_STATUS_VALID);
944:
945: if (mysqli_stmt_warning_count(stmt->stmt)) {
946: w = php_get_warnings(mysqli_stmt_get_connection(stmt->stmt) TSRMLS_CC);
947: } else {
948: RETURN_FALSE;
949: }
950: mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE));
951: mysqli_resource->ptr = mysqli_resource->info = (void *)w;
952: mysqli_resource->status = MYSQLI_STATUS_VALID;
953: MYSQLI_RETURN_RESOURCE(mysqli_resource, mysqli_warning_class_entry);
954: }
955: /* }}} */
956:
1.1.1.2 ! misho 957:
1.1 misho 958: #ifdef HAVE_MYSQLI_SET_CHARSET
959: /* {{{ proto bool mysqli_set_charset(object link, string csname)
960: sets client character set */
961: PHP_FUNCTION(mysqli_set_charset)
962: {
963: MY_MYSQL *mysql;
964: zval *mysql_link;
965: char *cs_name;
966: int csname_len;
967:
968: if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os", &mysql_link, mysqli_link_class_entry, &cs_name, &csname_len) == FAILURE) {
969: return;
970: }
971: MYSQLI_FETCH_RESOURCE_CONN(mysql, &mysql_link, MYSQLI_STATUS_VALID);
972:
973: if (mysql_set_character_set(mysql->mysql, cs_name)) {
974: RETURN_FALSE;
975: }
976: RETURN_TRUE;
977: }
978: /* }}} */
979: #endif
980:
981: #ifdef HAVE_MYSQLI_GET_CHARSET
982: /* {{{ proto object mysqli_get_charset(object link) U
983: returns a character set object */
984: PHP_FUNCTION(mysqli_get_charset)
985: {
986: MY_MYSQL *mysql;
987: zval *mysql_link;
988: const char *name = NULL, *collation = NULL, *dir = NULL, *comment = NULL;
989: uint minlength, maxlength, number, state;
990: #if !defined(MYSQLI_USE_MYSQLND)
991: MY_CHARSET_INFO cs;
992: #else
993: const MYSQLND_CHARSET *cs;
994: #endif
995:
996: if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
997: return;
998: }
999: MYSQLI_FETCH_RESOURCE_CONN(mysql, &mysql_link, MYSQLI_STATUS_VALID);
1000:
1001:
1002: #if !defined(MYSQLI_USE_MYSQLND)
1003: mysql_get_character_set_info(mysql->mysql, &cs);
1004: name = (char *)cs.csname;
1005: collation = (char *)cs.name;
1006: dir = (char *)cs.dir;
1007: minlength = cs.mbminlen;
1008: maxlength = cs.mbmaxlen;
1009: number = cs.number;
1010: state = cs.state;
1011: comment = cs.comment;
1012: #else
1.1.1.2 ! misho 1013: cs = mysql->mysql->data->charset;
1.1 misho 1014: if (!cs) {
1015: php_error_docref(NULL TSRMLS_CC, E_WARNING, "The connection has no charset associated");
1016: RETURN_NULL();
1017: }
1018: name = cs->name;
1019: collation = cs->collation;
1020: minlength = cs->char_minlen;
1021: maxlength = cs->char_maxlen;
1022: number = cs->nr;
1023: comment = cs->comment;
1024: state = 1; /* all charsets are compiled in */
1025: #endif
1026: object_init(return_value);
1027:
1028: add_property_string(return_value, "charset", (name) ? (char *)name : "", 1);
1029: add_property_string(return_value, "collation",(collation) ? (char *)collation : "", 1);
1030: add_property_string(return_value, "dir", (dir) ? (char *)dir : "", 1);
1031: add_property_long(return_value, "min_length", minlength);
1032: add_property_long(return_value, "max_length", maxlength);
1033: add_property_long(return_value, "number", number);
1034: add_property_long(return_value, "state", state);
1035: add_property_string(return_value, "comment", (comment) ? (char *)comment : "", 1);
1036: }
1037: /* }}} */
1038: #endif
1039:
1040: /*
1041: * Local variables:
1042: * tab-width: 4
1043: * c-basic-offset: 4
1044: * End:
1045: * vim600: noet sw=4 ts=4 fdm=marker
1046: * vim<600: noet sw=4 ts=4
1047: */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>