]> granicus.if.org Git - php/commitdiff
Fixed Bug #36602: persistent connections don't work with MySQL 5.0.3+.
authorWez Furlong <wez@php.net>
Sun, 9 Apr 2006 08:11:31 +0000 (08:11 +0000)
committerWez Furlong <wez@php.net>
Sun, 9 Apr 2006 08:11:31 +0000 (08:11 +0000)
Fixed PECL Bug #6262; correctly fallback to emulated statements
when the server version is too old.

Also include code to default to using emulated prepared statements, as these
can take advantage of the query cache.  Since it's a bit late in the release
process to spring this on the masses, the default has not been changed.  The
code is included so that it doesn't get lost.

ext/pdo_mysql/mysql_driver.c

index 9ffa80ad5dd4d00d56264434648aa9358aa054c1..51e502ccaa80f7687dbd6c01364692ccaf8050fa 100755 (executable)
@@ -163,6 +163,7 @@ static int mysql_handle_preparer(pdo_dbh_t *dbh, const char *sql, long sql_len,
        char *nsql = NULL;
        int nsql_len = 0;
        int ret;
+       int server_version;
 #endif
        
        S->H = H;
@@ -173,11 +174,11 @@ static int mysql_handle_preparer(pdo_dbh_t *dbh, const char *sql, long sql_len,
                goto end;
        }
 
-       /* TODO: add runtime check to determine if the server we are talking to supports
-        * prepared statements; if it doesn't, we should set stmt->supports_placeholders
-        * to PDO_PLACEHOLDER_NONE, and have the rest of the code look at S->stmt to
-        * determine if we're using real prepared statements or the PDO emulated version */
 #if HAVE_MYSQL_STMT_PREPARE
+       server_version = mysql_get_server_version(H->server);
+       if (server_version < 40100) {
+               goto fallback;
+       }
        stmt->supports_placeholders = PDO_PLACEHOLDER_POSITIONAL;
        ret = pdo_parse_params(stmt, (char*)sql, sql_len, &nsql, &nsql_len TSRMLS_CC);
 
@@ -318,6 +319,7 @@ static int pdo_mysql_set_attribute(pdo_dbh_t *dbh, long attr, zval *val TSRMLS_D
                ((pdo_mysql_db_handle *)dbh->driver_data)->buffered = Z_BVAL_P(val);
                return 1;
        case PDO_MYSQL_ATTR_DIRECT_QUERY:
+       case PDO_ATTR_EMULATE_PREPARES:
                ((pdo_mysql_db_handle *)dbh->driver_data)->emulate_prepare = Z_BVAL_P(val);
                return 1;
        default:
@@ -431,6 +433,17 @@ static int pdo_mysql_handle_factory(pdo_dbh_t *dbh, zval *driver_options TSRMLS_
        H->einfo.errcode = 0;
        H->einfo.errmsg = NULL;
 
+       /* at the time of writing, the mysql documentation states:
+        *      http://mysql.localhost.net.ar/doc/refman/5.0/en/query-cache-how.html
+        *      "A query also is not cached under these conditions:
+        *      ...
+        *      It was issued as a prepared statement, even if no placeholders were employed."
+        *
+        * We default to emulating prepared statements
+        * in order to take advantage of the query cache
+FIXME: H->emulate_prepare = 1;   a bit risky to do this so late in the RC, so defer it.
+       */
+
        /* allocate an environment */
        
        /* handle for the server */
@@ -449,7 +462,12 @@ static int pdo_mysql_handle_factory(pdo_dbh_t *dbh, zval *driver_options TSRMLS_
                char *init_cmd = NULL, *default_file = NULL, *default_group = NULL;
 
                H->buffered = pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_USE_BUFFERED_QUERY, 0 TSRMLS_CC);
-               H->emulate_prepare = pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_DIRECT_QUERY, 0 TSRMLS_CC);
+
+               H->emulate_prepare = pdo_attr_lval(driver_options,
+                       PDO_MYSQL_ATTR_DIRECT_QUERY, H->emulate_prepare TSRMLS_CC);
+               H->emulate_prepare = pdo_attr_lval(driver_options, 
+                       PDO_ATTR_EMULATE_PREPARES, H->emulate_prepare TSRMLS_CC);
+
                H->max_buffer_size = pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_MAX_BUFFER_SIZE, H->max_buffer_size TSRMLS_CC);
 
                if (mysql_options(H->server, MYSQL_OPT_CONNECT_TIMEOUT, (const char *)&connect_timeout)) {
@@ -462,6 +480,15 @@ static int pdo_mysql_handle_factory(pdo_dbh_t *dbh, zval *driver_options TSRMLS_
                        goto cleanup;
                }
 
+#ifdef MYSQL_OPT_RECONNECT
+               /* since 5.0.3, the default for this option is 0 if not specified.
+                * we want the old behaviour */
+               {
+                       long reconnect = 1;
+                       mysql_options(H->server, MYSQL_OPT_RECONNECT, (const char*)&reconnect);
+               }
+#endif
+
                init_cmd = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_INIT_COMMAND, NULL TSRMLS_CC);
                if (init_cmd) {
                        if (mysql_options(H->server, MYSQL_INIT_COMMAND, (const char *)init_cmd)) {