cvs: pecl /pdo pdo_sql_parser.re pdo_stmt.c php_pdo_driver.h
| From: | Wez Furlong | Date: | Sun, 13 Feb 2005 06:29:36 +0000 |
| Subject: | cvs: pecl /pdo pdo_sql_parser.re pdo_stmt.c php_pdo_driver.h | ||
| Groups: | php.pecl.cvs | ||
| Request: | Send a blank email to pecl-cvs+get-2536@lists.php.net to get a copy of this message | ||
wez Sun Feb 13 01:29:36 2005 EDT
Modified files:
/pecl/pdo pdo_sql_parser.re pdo_stmt.c php_pdo_driver.h
Log:
implement mapping of :name to ? parameters for drivers that only support ?
placeholders.
The current restriction is that you may not use the same named parameter
more than one in a given query, as there is a danger of scary things happen
with the zval if it gets bound multiple times.
http://cvs.php.net/diff.php/pecl/pdo/pdo_sql_parser.re?r1=1.18&r2=1.19&ty=u
Index: pecl/pdo/pdo_sql_parser.re
diff -u pecl/pdo/pdo_sql_parser.re:1.18 pecl/pdo/pdo_sql_parser.re:1.19
--- pecl/pdo/pdo_sql_parser.re:1.18 Sat Feb 12 23:00:22 2005
+++ pecl/pdo/pdo_sql_parser.re Sun Feb 13 01:29:35 2005
@@ -16,7 +16,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: pdo_sql_parser.re,v 1.18 2005/02/13 04:00:22 wez Exp $ */
+/* $Id: pdo_sql_parser.re,v 1.19 2005/02/13 06:29:35 wez Exp $ */
#include "php.h"
#include "php_pdo_driver.h"
@@ -259,10 +259,25 @@
} else {
/* rewrite :name to ? */
+
+ newbuffer_len = inquery_len;
+
+ if (stmt->bound_param_map == NULL) {
+ ALLOC_HASHTABLE(stmt->bound_param_map);
+ zend_hash_init(stmt->bound_param_map, 13, NULL, NULL, 0);
+ }
+
+ for (plc = placeholders; plc; plc = plc->next) {
+ char *name;
+
+ name = estrndup(plc->pos, plc->len);
+ zend_hash_index_update(stmt->bound_param_map, plc->bindno, name, plc->len + 1, NULL);
+ efree(name);
+ plc->quoted = "?";
+ plc->qlen = 1;
+ }
- /* HARD!. We need to remember the mapping and bind those positions. */
- pdo_raise_impl_error(stmt->dbh, stmt, "IM001", "cannot map :name to ? in this
version" TSRMLS_CC);
- ret = -1;
+ goto rewrite;
}
clean_up:
http://cvs.php.net/diff.php/pecl/pdo/pdo_stmt.c?r1=1.68&r2=1.69&ty=u
Index: pecl/pdo/pdo_stmt.c
diff -u pecl/pdo/pdo_stmt.c:1.68 pecl/pdo/pdo_stmt.c:1.69
--- pecl/pdo/pdo_stmt.c:1.68 Wed Feb 9 10:56:27 2005
+++ pecl/pdo/pdo_stmt.c Sun Feb 13 01:29:35 2005
@@ -18,7 +18,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: pdo_stmt.c,v 1.68 2005/02/09 15:56:27 iliaa Exp $ */
+/* $Id: pdo_stmt.c,v 1.69 2005/02/13 06:29:35 wez Exp $ */
/* The PDO Statement Handle Class */
@@ -73,6 +73,37 @@
}
/* }}} */
+static inline int rewrite_name_to_position(pdo_stmt_t *stmt, struct pdo_bound_param_data *param
TSRMLS_DC)
+{
+ if (stmt->bound_param_map) {
+ /* rewriting :name to ? style.
+ * We need to fixup the parameter numbers on the parameters.
+ * If we find that a given named parameter has been used twice,
+ * we will raise an error, as we can't be sure that it is safe
+ * to bind multiple parameters onto the same zval in the underlying
+ * driver */
+ char **name;
+ int position = 0;
+ zend_hash_internal_pointer_reset(stmt->bound_param_map);
+ while (SUCCESS == zend_hash_get_current_data(stmt->bound_param_map, (void**)&name)) {
+ if (strcmp(name, param->name)) {
+ position++;
+ zend_hash_move_forward(stmt->bound_param_map);
+ continue;
+ }
+ if (param->paramno >= 0) {
+ pdo_raise_impl_error(stmt->dbh, stmt, "IM001", "PDO refuses to handle
repeating the same :named parameter for multiple positions with this driver, as it might be unsafe
to do so. Consider using a separate name for each parameter instead" TSRMLS_CC);
+ return -1;
+ }
+ param->paramno = position;
+ return 1;
+ }
+ pdo_raise_impl_error(stmt->dbh, stmt, "HY093", "parameter was not defined"
TSRMLS_CC);
+ return 0;
+ }
+ return 1;
+}
+
/* trigger callback hook for parameters */
static int dispatch_param_event(pdo_stmt_t *stmt, enum pdo_param_event event_type TSRMLS_DC)
{
@@ -83,14 +114,13 @@
if (!stmt->methods->param_hook) {
return 1;
}
-
+
ht = stmt->bound_params;
iterate:
if (ht) {
zend_hash_internal_pointer_reset(ht);
while (SUCCESS == zend_hash_get_current_data(ht, (void**)¶m)) {
-
if (!stmt->methods->param_hook(stmt, param, event_type TSRMLS_CC)) {
ret = 0;
break;
@@ -141,6 +171,7 @@
}
}
+#if 0
/* update the column index on named bound parameters */
if (stmt->bound_params) {
struct pdo_bound_param_data *param;
@@ -150,6 +181,7 @@
param->paramno = col;
}
}
+#endif
if (stmt->bound_columns) {
struct pdo_bound_param_data *param;
@@ -253,6 +285,10 @@
zend_hash_index_update(hash, param->paramno, param, sizeof(*param), (void**)&pparam);
}
+ if (!rewrite_name_to_position(stmt, pparam TSRMLS_CC)) {
+ return 0;
+ }
+
/* tell the driver we just created a parameter */
if (stmt->methods->param_hook) {
return stmt->methods->param_hook(stmt, pparam, PDO_PARAM_EVT_ALLOC TSRMLS_CC);
@@ -1232,6 +1268,10 @@
zend_hash_destroy(stmt->bound_params);
FREE_HASHTABLE(stmt->bound_params);
}
+ if (stmt->bound_param_map) {
+ zend_hash_destroy(stmt->bound_param_map);
+ FREE_HASHTABLE(stmt->bound_param_map);
+ }
if (stmt->bound_columns) {
zend_hash_destroy(stmt->bound_columns);
FREE_HASHTABLE(stmt->bound_columns);
http://cvs.php.net/diff.php/pecl/pdo/php_pdo_driver.h?r1=1.44&r2=1.45&ty=u
Index: pecl/pdo/php_pdo_driver.h
diff -u pecl/pdo/php_pdo_driver.h:1.44 pecl/pdo/php_pdo_driver.h:1.45
--- pecl/pdo/php_pdo_driver.h:1.44 Tue Feb 8 19:38:11 2005
+++ pecl/pdo/php_pdo_driver.h Sun Feb 13 01:29:35 2005
@@ -16,7 +16,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: php_pdo_driver.h,v 1.44 2005/02/09 00:38:11 wez Exp $ */
+/* $Id: php_pdo_driver.h,v 1.45 2005/02/13 06:29:35 wez Exp $ */
#ifndef PHP_PDO_DRIVER_H
#define PHP_PDO_DRIVER_H
@@ -35,7 +35,7 @@
# define FALSE 0
#endif
-#define PDO_DRIVER_API 20050206
+#define PDO_DRIVER_API 20050213
enum pdo_param_type {
PDO_PARAM_NULL,
@@ -502,6 +502,8 @@
/* keep track of bound input parameters. Some drivers support
* input/output parameters, but you can't rely on that working */
HashTable *bound_params;
+ /* When rewriting from named to positional, this maps positions to names */
+ HashTable *bound_param_map;
/* keep track of PHP variables bound to named (or positional) columns
* in the result set */
HashTable *bound_columns;