diff options
Diffstat (limited to 'ext/pdo/pdo_sql_parser.c')
-rw-r--r-- | ext/pdo/pdo_sql_parser.c | 180 |
1 files changed, 34 insertions, 146 deletions
diff --git a/ext/pdo/pdo_sql_parser.c b/ext/pdo/pdo_sql_parser.c index 573d5865a0..f34d36999f 100644 --- a/ext/pdo/pdo_sql_parser.c +++ b/ext/pdo/pdo_sql_parser.c @@ -554,40 +554,54 @@ safe: } plc->freeq = 1; } else { - zval tmp_param; - ZVAL_DUP(&tmp_param, parameter); - switch (Z_TYPE(tmp_param)) { - case IS_NULL: - plc->quoted = "NULL"; - plc->qlen = sizeof("NULL")-1; + enum pdo_param_type param_type = param->param_type; + zend_string *buf = NULL; + + /* assume all types are nullable */ + if (Z_TYPE_P(parameter) == IS_NULL) { + param_type = PDO_PARAM_NULL; + } + + switch (param_type) { + case PDO_PARAM_BOOL: + plc->quoted = zend_is_true(parameter) ? "1" : "0"; + plc->qlen = sizeof("1")-1; plc->freeq = 0; break; - case IS_FALSE: - case IS_TRUE: - convert_to_long(&tmp_param); - /* fall through */ - case IS_LONG: - case IS_DOUBLE: - convert_to_string(&tmp_param); - plc->qlen = Z_STRLEN(tmp_param); - plc->quoted = estrdup(Z_STRVAL(tmp_param)); + case PDO_PARAM_INT: + buf = zend_long_to_str(zval_get_long(parameter)); + + plc->qlen = ZSTR_LEN(buf); + plc->quoted = estrdup(ZSTR_VAL(buf)); plc->freeq = 1; break; + case PDO_PARAM_NULL: + plc->quoted = "NULL"; + plc->qlen = sizeof("NULL")-1; + plc->freeq = 0; + break; + default: - convert_to_string(&tmp_param); - if (!stmt->dbh->methods->quoter(stmt->dbh, Z_STRVAL(tmp_param), - Z_STRLEN(tmp_param), &plc->quoted, &plc->qlen, - param->param_type)) { + buf = zval_get_string(parameter); + if (!stmt->dbh->methods->quoter(stmt->dbh, ZSTR_VAL(buf), + ZSTR_LEN(buf), &plc->quoted, &plc->qlen, + param_type)) { /* bork */ ret = -1; strncpy(stmt->error_code, stmt->dbh->error_code, 6); + if (buf) { + zend_string_release(buf); + } goto clean_up; } plc->freeq = 1; } - zval_dtor(&tmp_param); + + if (buf) { + zend_string_release(buf); + } } } else { zval *parameter; @@ -717,132 +731,6 @@ clean_up: return ret; } -#if 0 -int old_pdo_parse_params(pdo_stmt_t *stmt, char *inquery, int inquery_len, char **outquery, - int *outquery_len) -{ - Scanner s; - char *ptr; - int t; - int bindno = 0; - int newbuffer_len; - int padding; - HashTable *params = stmt->bound_params; - struct pdo_bound_param_data *param; - /* allocate buffer for query with expanded binds, ptr is our writing pointer */ - newbuffer_len = inquery_len; - - /* calculate the possible padding factor due to quoting */ - if(stmt->dbh->max_escaped_char_length) { - padding = stmt->dbh->max_escaped_char_length; - } else { - padding = 3; - } - if(params) { - ZEND_HASH_FOREACH_PTR(params, param) { - if(param->parameter) { - convert_to_string(param->parameter); - /* accommodate a string that needs to be fully quoted - bind placeholders are at least 2 characters, so - the accommodate their own "'s - */ - newbuffer_len += padding * Z_STRLEN_P(param->parameter); - } - } ZEND_HASH_FOREACH_END(); - } - *outquery = (char *) emalloc(newbuffer_len + 1); - *outquery_len = 0; - - ptr = *outquery; - s.cur = inquery; - while((t = scan(&s)) != PDO_PARSER_EOI) { - if(t == PDO_PARSER_TEXT) { - memcpy(ptr, s.tok, s.cur - s.tok); - ptr += (s.cur - s.tok); - *outquery_len += (s.cur - s.tok); - } - else if(t == PDO_PARSER_BIND) { - if(!params) { - /* error */ - efree(*outquery); - *outquery = NULL; - return (int) (s.cur - inquery); - } - /* lookup bind first via hash and then index */ - /* stupid keys need to be null-terminated, even though we know their length */ - if((NULL != (param = zend_hash_str_find_ptr(params, s.tok, s.cur-s.tok)) - || - NULL != (params = zend_hash_index_find_ptr(params, bindno))) - { - char *quotedstr; - int quotedstrlen; - /* restore the in-string key, doesn't need null-termination here */ - /* currently everything is a string here */ - - /* quote the bind value if necessary */ - if(stmt->dbh->methods->quoter(stmt->dbh, Z_STRVAL_P(param->parameter), - Z_STRLEN_P(param->parameter), "edstr, "edstrlen)) - { - memcpy(ptr, quotedstr, quotedstrlen); - ptr += quotedstrlen; - *outquery_len += quotedstrlen; - efree(quotedstr); - } else { - memcpy(ptr, Z_STRVAL_P(param->parameter), Z_STRLEN_P(param->parameter)); - ptr += Z_STRLEN_P(param->parameter); - *outquery_len += (Z_STRLEN_P(param->parameter)); - } - } - else { - /* error and cleanup */ - efree(*outquery); - *outquery = NULL; - return (int) (s.cur - inquery); - } - bindno++; - } - else if(t == PDO_PARSER_BIND_POS) { - if(!params) { - /* error */ - efree(*outquery); - *outquery = NULL; - return (int) (s.cur - inquery); - } - /* lookup bind by index */ - if(NULL != (params = zend_hash_index_find_ptr(params, bindno))) - { - char *quotedstr; - int quotedstrlen; - /* currently everything is a string here */ - - /* quote the bind value if necessary */ - if(stmt->dbh->methods->quoter(stmt->dbh, Z_STRVAL_P(param->parameter), - Z_STRLEN_P(param->parameter), "edstr, "edstrlen)) - { - memcpy(ptr, quotedstr, quotedstrlen); - ptr += quotedstrlen; - *outquery_len += quotedstrlen; - efree(quotedstr); - } else { - memcpy(ptr, Z_STRVAL_P(param->parameter), Z_STRLEN_P(param->parameter)); - ptr += Z_STRLEN_P(param->parameter); - *outquery_len += (Z_STRLEN_P(param->parameter)); - } - } - else { - /* error and cleanup */ - efree(*outquery); - *outquery = NULL; - return (int) (s.cur - inquery); - } - bindno++; - } - } - *ptr = '\0'; - return 0; -} -#endif - /* * Local variables: * tab-width: 4 |