Skip to content

Commit 567096d

Browse files
committed
Refactor bound_param_map to point to a zend_string*
1 parent 7547f18 commit 567096d

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

ext/pdo/pdo_sql_parser.re

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ struct placeholder {
7777
};
7878

7979
static void free_param_name(zval *el) {
80-
efree(Z_PTR_P(el));
80+
zend_string_release_ex(Z_PTR_P(el), 0);
8181
}
8282

8383
PDO_API int pdo_parse_params(pdo_stmt_t *stmt, zend_string *inquery, zend_string **outquery)
@@ -359,20 +359,22 @@ rewrite:
359359

360360
for (plc = placeholders; plc; plc = plc->next) {
361361
int skip_map = 0;
362-
char *p;
362+
zend_string *p;
363363
zend_string *idxbuf;
364+
zend_string *name_str;
364365

365366
if (plc->bindno == PDO_PARSER_BINDNO_ESCAPED_CHAR) {
366367
continue;
367368
}
368369

369370
name = estrndup(plc->pos, plc->len);
371+
name_str = zend_string_init(name, plc->len, 0);
370372

371373
/* check if bound parameter is already available */
372-
if (!strcmp(name, "?") || (p = zend_hash_str_find_ptr(stmt->bound_param_map, name, plc->len)) == NULL) {
374+
if (!strcmp(name, "?") || (p = zend_hash_find_ptr(stmt->bound_param_map, name_str)) == NULL) {
373375
idxbuf = zend_strpprintf(0, tmpl, bind_no++);
374376
} else {
375-
idxbuf = zend_string_init(p, strlen(p), 0);
377+
idxbuf = p;
376378
skip_map = 1;
377379
}
378380

@@ -382,11 +384,11 @@ rewrite:
382384

383385
if (!skip_map && stmt->named_rewrite_template) {
384386
/* create a mapping */
385-
zend_hash_str_update_mem(stmt->bound_param_map, name, plc->len, ZSTR_VAL(plc->quoted), ZSTR_LEN(plc->quoted) + 1);
387+
zend_hash_update_ptr(stmt->bound_param_map, name_str, plc->quoted);
386388
}
387389

388390
/* map number to name */
389-
zend_hash_index_update_mem(stmt->bound_param_map, plc->bindno, ZSTR_VAL(plc->quoted), ZSTR_LEN(plc->quoted) + 1);
391+
zend_hash_index_update_ptr(stmt->bound_param_map, plc->bindno, plc->quoted);
390392

391393
efree(name);
392394
}
@@ -405,8 +407,11 @@ rewrite:
405407

406408
for (plc = placeholders; plc; plc = plc->next) {
407409
char *name;
410+
zend_string *name_str;
408411
name = estrndup(plc->pos, plc->len);
409-
zend_hash_index_update_mem(stmt->bound_param_map, plc->bindno, name, plc->len + 1);
412+
name_str = zend_string_init(name, plc->len, 0);
413+
//zend_hash_index_update_mem(stmt->bound_param_map, plc->bindno, name, plc->len + 1);
414+
zend_hash_index_update_ptr(stmt->bound_param_map, plc->bindno, name_str);
410415
efree(name);
411416
plc->quoted = ZSTR_CHAR('?');
412417
newbuffer_len -= plc->len - 1;

ext/pdo/pdo_stmt.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ static inline bool rewrite_name_to_position(pdo_stmt_t *stmt, struct pdo_bound_p
5050
* we will raise an error, as we can't be sure that it is safe
5151
* to bind multiple parameters onto the same zval in the underlying
5252
* driver */
53-
char *name;
53+
zend_string *name;
5454
int position = 0;
5555

5656
if (stmt->named_rewrite_template) {
@@ -60,7 +60,7 @@ static inline bool rewrite_name_to_position(pdo_stmt_t *stmt, struct pdo_bound_p
6060
if (!param->name) {
6161
/* do the reverse; map the parameter number to the name */
6262
if ((name = zend_hash_index_find_ptr(stmt->bound_param_map, param->paramno)) != NULL) {
63-
param->name = zend_string_init(name, strlen(name), 0);
63+
param->name = name;
6464
return 1;
6565
}
6666
/* TODO Error? */
@@ -69,7 +69,7 @@ static inline bool rewrite_name_to_position(pdo_stmt_t *stmt, struct pdo_bound_p
6969
}
7070

7171
ZEND_HASH_FOREACH_PTR(stmt->bound_param_map, name) {
72-
if (strncmp(name, ZSTR_VAL(param->name), ZSTR_LEN(param->name) + 1)) {
72+
if (zend_string_equals(name, param->name)) {
7373
position++;
7474
continue;
7575
}

ext/pdo_pgsql/pgsql_statement.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,11 +275,11 @@ static int pgsql_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_data *
275275
ZEND_ATOL(param->paramno, ZSTR_VAL(param->name) + 1);
276276
} else {
277277
/* resolve parameter name to rewritten name */
278-
char *namevar;
278+
zend_string *namevar;
279279

280280
if (stmt->bound_param_map && (namevar = zend_hash_find_ptr(stmt->bound_param_map,
281281
param->name)) != NULL) {
282-
ZEND_ATOL(param->paramno, namevar + 1);
282+
param->paramno = ZEND_STRTOL(ZSTR_VAL(namevar), NULL, 10);
283283
param->paramno--;
284284
} else {
285285
pdo_pgsql_error_stmt_msg(stmt, 0, "HY093", ZSTR_VAL(param->name));

0 commit comments

Comments
 (0)