]> granicus.if.org Git - php/commitdiff
tweaks and optimizations
authorAndrey Hristov <andrey@php.net>
Mon, 8 May 2006 15:06:51 +0000 (15:06 +0000)
committerAndrey Hristov <andrey@php.net>
Mon, 8 May 2006 15:06:51 +0000 (15:06 +0000)
#tested on 32b and 64b

ext/mysqli/mysqli_api.c

index e480d5d17b05db476585a0c508eab34e86aed4ce..43eb19347c838ac048f0bba6d57922bd72b31527 100644 (file)
@@ -89,11 +89,6 @@ PHP_FUNCTION(mysqli_stmt_bind_param)
        unsigned long   rc;
 
        /* calculate and check number of parameters */
-       num_vars = argc - 1;
-       if (!getThis()) {
-               /* ignore handle parameter in procedural interface*/
-               --num_vars; 
-       }
        if (argc < 2) {
                /* there has to be at least one pair */
                WRONG_PARAM_COUNT;
@@ -105,8 +100,12 @@ PHP_FUNCTION(mysqli_stmt_bind_param)
 
        MYSQLI_FETCH_RESOURCE(stmt, MY_STMT *, &mysql_stmt, "mysqli_stmt", MYSQLI_STATUS_VALID); 
 
+       num_vars = argc - 1;
        if (getThis()) {
                start = 1;
+       } else {
+               /* ignore handle parameter in procedural interface*/
+               --num_vars;
        }
 
        if (typelen != argc - start) {
@@ -154,23 +153,19 @@ PHP_FUNCTION(mysqli_stmt_bind_param)
 
                        case 'b': /* Blob (send data) */
                                bind[ofs].buffer_type = MYSQL_TYPE_LONG_BLOB;
-                               bind[ofs].is_null = 0;
-                               bind[ofs].length = 0;
+                               /* don't initialize is_null and length to 0 because we use ecalloc */
                                break;
 
                        case 's': /* string */
                                bind[ofs].buffer_type = MYSQL_TYPE_VAR_STRING;
-                               bind[ofs].buffer = NULL; 
-                               bind[ofs].buffer_length = 0;
+                               /* don't initialize buffer and buffer_length because we use ecalloc */
                                bind[ofs].is_null = &stmt->param.is_null[ofs];
                                break;
 
                        default:
                                php_error_docref(NULL TSRMLS_CC, E_WARNING, "Undefined fieldtype %c (parameter %d)", types[ofs], i+1);
-                               efree(args);
-                               efree(bind);
-                               RETURN_FALSE; 
-                               break;
+                               RETVAL_FALSE;
+                               goto end;
                }
                ofs++;
        }
@@ -178,9 +173,8 @@ PHP_FUNCTION(mysqli_stmt_bind_param)
        MYSQLI_REPORT_STMT_ERROR(stmt->stmt);
 
        if (rc) {
-               efree(args);
-               efree(bind);
-               RETURN_FALSE;
+               RETVAL_FALSE;
+               goto end;
        }
 
        stmt->param.var_cnt = num_vars;
@@ -193,10 +187,10 @@ PHP_FUNCTION(mysqli_stmt_bind_param)
                        stmt->param.vars[i] = NULL;
                }
        }
+       RETVAL_TRUE;
+end:
        efree(args);
        efree(bind);
-
-       RETURN_TRUE;
 }
 /* }}} */
 
@@ -279,12 +273,16 @@ PHP_FUNCTION(mysqli_stmt_bind_result)
                                break;
 
                        case MYSQL_TYPE_NULL:
-                               stmt->result.buf[ofs].type = IS_NULL; 
-                               stmt->result.buf[ofs].buflen = 0;
+                               stmt->result.buf[ofs].type = IS_NULL;
+                               /*
+                                 don't initialize to 0 :
+                                 1. stmt->result.buf[ofs].buflen
+                                 2. bind[ofs].buffer
+                                 3. bind[ofs].buffer_length
+                                 because memory was allocated with ecalloc
+                               */
                                bind[ofs].buffer_type = MYSQL_TYPE_NULL;
-                               bind[ofs].buffer = 0;
                                bind[ofs].is_null = &stmt->result.is_null[ofs];
-                               bind[ofs].buffer_length = 0;
                                break;
 
                        case MYSQL_TYPE_SHORT:
@@ -294,7 +292,7 @@ PHP_FUNCTION(mysqli_stmt_bind_result)
                        case MYSQL_TYPE_YEAR:
                                convert_to_long_ex(args[i]);
                                stmt->result.buf[ofs].type = IS_LONG;
-                               stmt->result.buf[ofs].buflen = 0;
+                               /* don't set stmt->result.buf[ofs].buflen to 0, we used ecalloc */
                                stmt->result.buf[ofs].val = (char *)emalloc(sizeof(int));
                                bind[ofs].buffer_type = MYSQL_TYPE_LONG;
                                bind[ofs].buffer = stmt->result.buf[ofs].val;
@@ -369,30 +367,27 @@ PHP_FUNCTION(mysqli_stmt_bind_result)
        MYSQLI_REPORT_STMT_ERROR(stmt->stmt);
 
        if (rc) {
-               efree(bind);
-               efree(args);
                /* dont close the statement or subsequent usage (for example ->execute()) will lead to crash */
                for (i=0; i < var_cnt ; i++) {
-                       if (stmt->result.buf[i].val)        
+                       if (stmt->result.buf[i].val) {
                                efree(stmt->result.buf[i].val);
+                       }
                }
                efree(stmt->result.buf);
                efree(stmt->result.is_null);
-               RETURN_FALSE;
-       }
-
-       stmt->result.var_cnt = var_cnt;
-       stmt->result.vars = (zval **)safe_emalloc((var_cnt), sizeof(zval), 0);
-       for (i = start; i < var_cnt+start; i++) {
-               ofs = i-start;
-               ZVAL_ADDREF(*args[i]);
-               stmt->result.vars[ofs] = *args[i];
+               RETVAL_FALSE;
+       } else {
+               stmt->result.var_cnt = var_cnt;
+               stmt->result.vars = (zval **)safe_emalloc((var_cnt), sizeof(zval), 0);
+               for (i = start; i < var_cnt+start; i++) {
+                       ofs = i-start;
+                       ZVAL_ADDREF(*args[i]);
+                       stmt->result.vars[ofs] = *args[i];
+               }
+               RETVAL_TRUE;
        }
-
        efree(args);
        efree(bind);
-
-       RETURN_TRUE;
 }
 /* }}} */
 
@@ -687,7 +682,7 @@ PHP_FUNCTION(mysqli_stmt_fetch)
                                                        ZVAL_LONG(stmt->result.vars[i], *(unsigned int *)stmt->result.buf[i].val);
                                                } else {
                                                        ZVAL_LONG(stmt->result.vars[i], *(int *)stmt->result.buf[i].val);
-                                               };
+                                               }
                                                break;
                                        case IS_DOUBLE:
                                                ZVAL_DOUBLE(stmt->result.vars[i], *(double *)stmt->result.buf[i].val);
@@ -972,8 +967,6 @@ PHP_FUNCTION(mysqli_free_result)
 
        mysql_free_result(result);
        MYSQLI_CLEAR_RESOURCE(&mysql_result);   
-
-       return;
 }
 /* }}} */
 
@@ -1136,7 +1129,7 @@ PHP_FUNCTION(mysqli_kill)
 }
 /* }}} */
 
-/* {{{ proto mysqli_set_local_infile_default(object link)
+/* {{{ proto void mysqli_set_local_infile_default(object link)
    unsets user defined handler for load local infile command */
 PHP_FUNCTION(mysqli_set_local_infile_default)
 {
@@ -1184,6 +1177,8 @@ PHP_FUNCTION(mysqli_set_local_infile_handler)
        /* save callback function */
        ALLOC_ZVAL(mysql->li_read);     
        ZVAL_STRING(mysql->li_read, callback_func->value.str.val, 1);
+
+       RETURN_TRUE;
 }
 /* }}} */
 
@@ -1199,10 +1194,7 @@ PHP_FUNCTION(mysqli_more_results)
        }
        MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
 
-       if (!mysql_more_results(mysql->mysql)) {
-               RETURN_FALSE;
-       }       
-       RETURN_TRUE;
+       RETURN_BOOL(mysql_more_results(mysql->mysql));
 }
 /* }}} */
 
@@ -1217,10 +1209,7 @@ PHP_FUNCTION(mysqli_next_result) {
        }
        MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
 
-       if (mysql_next_result(mysql->mysql)) {
-               RETURN_FALSE;
-       }       
-       RETURN_TRUE;
+       RETURN_BOOL(!mysql_next_result(mysql->mysql));
 }
 /* }}} */
 
@@ -1288,11 +1277,7 @@ PHP_FUNCTION(mysqli_options)
                        break;
        }
 
-       if (ret != 0) {
-               RETURN_FALSE;
-       } else {
-               RETURN_TRUE;
-       }
+       RETURN_BOOL(!ret);
 }   
 /* }}} */
 
@@ -1311,10 +1296,8 @@ PHP_FUNCTION(mysqli_ping)
        MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
        rc = mysql_ping(mysql->mysql);
        MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql);
-       if (rc) {
-               RETURN_FALSE;
-       }
-       RETURN_TRUE;
+
+       RETURN_BOOL(!rc);
 }
 /* }}} */
 
@@ -1359,8 +1342,8 @@ PHP_FUNCTION(mysqli_prepare)
                        memcpy(mysql->mysql->net.last_error, last_error, MYSQL_ERRMSG_SIZE);
                        memcpy(mysql->mysql->net.sqlstate, sqlstate, SQLSTATE_LENGTH+1);
                }
-       } 
-       
+       }
+       /* don't joing to the previous if because it won't work if mysql_stmt_prepare_fails */
        if (!stmt->stmt) {
                MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql);
                efree(stmt);
@@ -1370,8 +1353,13 @@ PHP_FUNCTION(mysqli_prepare)
 
        mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE));
        mysqli_resource->ptr = (void *)stmt;
-       stmt->query = (query_len) ? (char *)emalloc(query_len + 1) : NULL;
-       strcpy(stmt->query, query);
+       /* don't initialize stmt->query with NULL, we ecalloc()-ed the memory */
+       /* Get performance boost if reporting is switched off */
+       if (query_len && (MyG(report_mode) & MYSQLI_REPORT_INDEX)) {
+               stmt->query = (char *)emalloc(query_len + 1);
+               memcpy(stmt->query, query, query_len);
+               stmt->query[query_len] = '\0';
+       }
 
        /* change status */
        mysqli_resource->status = MYSQLI_STATUS_VALID;
@@ -1497,7 +1485,7 @@ PHP_FUNCTION(mysqli_real_escape_string) {
        newstr_len = mysql_real_escape_string(mysql->mysql, newstr, escapestr, escapestr_len);
        newstr = erealloc(newstr, newstr_len + 1);
                
-       RETURN_STRING(newstr, 0);
+       RETURN_STRINGL(newstr, newstr_len, 0);
 }
 /* }}} */
 
@@ -1608,7 +1596,6 @@ PHP_FUNCTION(mysqli_stmt_data_seek)
        MYSQLI_FETCH_RESOURCE(stmt, MY_STMT *, &mysql_stmt, "mysqli_stmt", MYSQLI_STATUS_VALID);
 
        mysql_stmt_data_seek(stmt->stmt, offset);
-       return;
 }
 /* }}} */
 
@@ -1642,8 +1629,6 @@ PHP_FUNCTION(mysqli_stmt_free_result)
        MYSQLI_FETCH_RESOURCE(stmt, MY_STMT *, &mysql_stmt, "mysqli_stmt", MYSQLI_STATUS_VALID);
 
        mysql_stmt_free_result(stmt->stmt);
-
-       return;
 }
 /* }}} */
 
@@ -1805,7 +1790,7 @@ PHP_FUNCTION(mysqli_stat)
 
 /* }}} */
  
-/* {{{ proto int mysqli_stmt_attr_set(object stmt, long attr, bool mode)
+/* {{{ proto int mysqli_stmt_attr_set(object stmt, long attr, long mode)
 */
 PHP_FUNCTION(mysqli_stmt_attr_set)
 {
@@ -1833,7 +1818,11 @@ PHP_FUNCTION(mysqli_stmt_attr_get)
 {
        MY_STMT *stmt;
        zval    *mysql_stmt;
+#if MYSQL_VERSION_ID > 50099
+       my_bool value;
+#else
        ulong   value = 0;
+#endif
        ulong   attr;
        int             rc;
 
@@ -1845,7 +1834,7 @@ PHP_FUNCTION(mysqli_stmt_attr_get)
        if ((rc = mysql_stmt_attr_get(stmt->stmt, attr, &value))) {
                RETURN_FALSE;
        }
-       RETURN_LONG(value);
+       RETURN_LONG((long)value);
 }
 /* }}} */