summaryrefslogtreecommitdiff
path: root/ext/pdo_pgsql/pgsql_driver.c
diff options
context:
space:
mode:
authorWez Furlong <wez@php.net>2005-07-20 02:37:57 +0000
committerWez Furlong <wez@php.net>2005-07-20 02:37:57 +0000
commit79f3cb9856abe0537f3fbd6004c7e2ed82aaeb84 (patch)
tree58bbf8973b24d53221ac91780ab884ce8c91eed7 /ext/pdo_pgsql/pgsql_driver.c
parent9438584d76a6ac7d3bef3535511d3251c6a0ada6 (diff)
downloadphp-git-79f3cb9856abe0537f3fbd6004c7e2ed82aaeb84.tar.gz
Add PDO_PGSQL_ATTR_DISABLE_NATIVE_PREPARED_STATEMENT attribute, which, when set
to true, forces the driver to use PDO's own emulated prepared statement support. Why would you want that, considering that native prepared statements are supposed to be the best thing ever? "Often postgresql will have to plan the query without knowing the parameters - and it will choose a bad plan. In some cases it will plan based on the first parameters you send. " Ugh. So now we have a way to let you decide that you know better than the pgsql query planner.
Diffstat (limited to 'ext/pdo_pgsql/pgsql_driver.c')
-rw-r--r--ext/pdo_pgsql/pgsql_driver.c95
1 files changed, 49 insertions, 46 deletions
diff --git a/ext/pdo_pgsql/pgsql_driver.c b/ext/pdo_pgsql/pgsql_driver.c
index 038b0e99c7..3d0be6d233 100644
--- a/ext/pdo_pgsql/pgsql_driver.c
+++ b/ext/pdo_pgsql/pgsql_driver.c
@@ -153,55 +153,58 @@ static int pgsql_handle_preparer(pdo_dbh_t *dbh, const char *sql, long sql_len,
}
#if HAVE_PQPREPARE
- stmt->supports_placeholders = PDO_PLACEHOLDER_NAMED;
- stmt->named_rewrite_template = "$%d";
- ret = pdo_parse_params(stmt, (char*)sql, sql_len, &nsql, &nsql_len TSRMLS_CC);
-
- if (ret == 1) {
- /* query was re-written */
- sql = nsql;
- } else if (ret == -1) {
- /* couldn't grok it */
- strcpy(dbh->error_code, stmt->error_code);
- return 0;
- }
-
- spprintf(&S->stmt_name, 0, "pdo_pgsql_stmt_%08x", (unsigned int)stmt);
- res = PQprepare(H->server, S->stmt_name, sql, 0, NULL);
- if (nsql) {
- efree(nsql);
- }
- if (!res) {
- pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
- return 0;
- }
-
- /* check if the connection is using protocol version 2.0.
- * if that is the reason that the prepare failed, we want to fall
- * through and let PDO emulate it for us */
- status = PQresultStatus(res);
- switch (status) {
- case PGRES_COMMAND_OK:
- case PGRES_TUPLES_OK:
- /* it worked */
- PQclear(res);
- return 1;
-
- case PGRES_BAD_RESPONSE:
- /* server is probably too old; fall through and let
- * PDO emulate it */
- efree(S->stmt_name);
- S->stmt_name = NULL;
- PQclear(res);
- break;
+ if (!driver_options || pdo_attr_lval(driver_options,
+ PDO_PGSQL_ATTR_DISABLE_NATIVE_PREPARED_STATEMENT, 0 TSRMLS_CC) == 0) {
+ stmt->supports_placeholders = PDO_PLACEHOLDER_NAMED;
+ stmt->named_rewrite_template = "$%d";
+ ret = pdo_parse_params(stmt, (char*)sql, sql_len, &nsql, &nsql_len TSRMLS_CC);
+
+ if (ret == 1) {
+ /* query was re-written */
+ sql = nsql;
+ } else if (ret == -1) {
+ /* couldn't grok it */
+ strcpy(dbh->error_code, stmt->error_code);
+ return 0;
+ }
- default:
- /* protocol 3.0 and above; hard error */
- pdo_pgsql_error(dbh, status, pdo_pgsql_sqlstate(res));
- PQclear(res);
+ spprintf(&S->stmt_name, 0, "pdo_pgsql_stmt_%08x", (unsigned int)stmt);
+ res = PQprepare(H->server, S->stmt_name, sql, 0, NULL);
+ if (nsql) {
+ efree(nsql);
+ }
+ if (!res) {
+ pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
return 0;
+ }
+
+ /* check if the connection is using protocol version 2.0.
+ * if that is the reason that the prepare failed, we want to fall
+ * through and let PDO emulate it for us */
+ status = PQresultStatus(res);
+ switch (status) {
+ case PGRES_COMMAND_OK:
+ case PGRES_TUPLES_OK:
+ /* it worked */
+ PQclear(res);
+ return 1;
+
+ case PGRES_BAD_RESPONSE:
+ /* server is probably too old; fall through and let
+ * PDO emulate it */
+ efree(S->stmt_name);
+ S->stmt_name = NULL;
+ PQclear(res);
+ break;
+
+ default:
+ /* protocol 3.0 and above; hard error */
+ pdo_pgsql_error(dbh, status, pdo_pgsql_sqlstate(res));
+ PQclear(res);
+ return 0;
+ }
+ /* fall through */
}
- /* fall through */
#endif
stmt->supports_placeholders = PDO_PLACEHOLDER_NONE;