summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDmitry Stogov <dmitry@zend.com>2015-05-05 16:19:51 +0300
committerDmitry Stogov <dmitry@zend.com>2015-05-05 16:19:51 +0300
commit70c86732cb14bad0b2539a54c16e8e845f740117 (patch)
tree102cc8c5265f03b4f0bbd28cd13f29a101893480
parent275afbacd11c2f4d0dac7d450ffdf500d5eab104 (diff)
downloadphp-git-70c86732cb14bad0b2539a54c16e8e845f740117.tar.gz
Use zend_string to represent pdo_column_data.name and avoid duplication.
-rw-r--r--Zend/zend_API.c17
-rw-r--r--Zend/zend_API.h1
-rw-r--r--ext/pdo/pdo_stmt.c56
-rw-r--r--ext/pdo/php_pdo_driver.h3
-rw-r--r--ext/pdo_dblib/dblib_stmt.c9
-rw-r--r--ext/pdo_firebird/firebird_statement.c4
-rw-r--r--ext/pdo_mysql/mysql_statement.c7
-rw-r--r--ext/pdo_oci/oci_statement.c3
-rw-r--r--ext/pdo_odbc/odbc_stmt.c3
-rw-r--r--ext/pdo_pgsql/pgsql_statement.c7
-rw-r--r--ext/pdo_sqlite/sqlite_statement.c5
11 files changed, 66 insertions, 49 deletions
diff --git a/Zend/zend_API.c b/Zend/zend_API.c
index e55ec45af6..7f8587ab4f 100644
--- a/Zend/zend_API.c
+++ b/Zend/zend_API.c
@@ -3828,6 +3828,23 @@ ZEND_API int zend_declare_class_constant_string(zend_class_entry *ce, const char
}
/* }}} */
+ZEND_API void zend_update_property_ex(zend_class_entry *scope, zval *object, zend_string *name, zval *value) /* {{{ */
+{
+ zval property;
+ zend_class_entry *old_scope = EG(scope);
+
+ EG(scope) = scope;
+
+ if (!Z_OBJ_HT_P(object)->write_property) {
+ zend_error_noreturn(E_CORE_ERROR, "Property %s of class %s cannot be updated", name, Z_OBJCE_P(object)->name->val);
+ }
+ ZVAL_STR(&property, name);
+ Z_OBJ_HT_P(object)->write_property(object, &property, value, NULL);
+
+ EG(scope) = old_scope;
+}
+/* }}} */
+
ZEND_API void zend_update_property(zend_class_entry *scope, zval *object, const char *name, size_t name_length, zval *value) /* {{{ */
{
zval property;
diff --git a/Zend/zend_API.h b/Zend/zend_API.h
index e0860a45f4..f61091ae72 100644
--- a/Zend/zend_API.h
+++ b/Zend/zend_API.h
@@ -329,6 +329,7 @@ ZEND_API int zend_declare_class_constant_string(zend_class_entry *ce, const char
ZEND_API int zend_update_class_constants(zend_class_entry *class_type);
+ZEND_API void zend_update_property_ex(zend_class_entry *scope, zval *object, zend_string *name, zval *value);
ZEND_API void zend_update_property(zend_class_entry *scope, zval *object, const char *name, size_t name_length, zval *value);
ZEND_API void zend_update_property_null(zend_class_entry *scope, zval *object, const char *name, size_t name_length);
ZEND_API void zend_update_property_bool(zend_class_entry *scope, zval *object, const char *name, size_t name_length, zend_long value);
diff --git a/ext/pdo/pdo_stmt.c b/ext/pdo/pdo_stmt.c
index 3387da0f2d..6383abb925 100644
--- a/ext/pdo/pdo_stmt.c
+++ b/ext/pdo/pdo_stmt.c
@@ -209,7 +209,7 @@ int pdo_stmt_describe_columns(pdo_stmt_t *stmt) /* {{{ */
/* if we are applying case conversions on column names, do so now */
if (stmt->dbh->native_case != stmt->dbh->desired_case && stmt->dbh->desired_case != PDO_CASE_NATURAL) {
- char *s = stmt->columns[col].name;
+ char *s = stmt->columns[col].name->val;
switch (stmt->dbh->desired_case) {
case PDO_CASE_UPPER:
@@ -243,8 +243,8 @@ int pdo_stmt_describe_columns(pdo_stmt_t *stmt) /* {{{ */
if (stmt->bound_columns) {
struct pdo_bound_param_data *param;
- if ((param = zend_hash_str_find_ptr(stmt->bound_columns,
- stmt->columns[col].name, stmt->columns[col].namelen)) != NULL) {
+ if ((param = zend_hash_find_ptr(stmt->bound_columns,
+ stmt->columns[col].name)) != NULL) {
param->paramno = col;
}
}
@@ -345,7 +345,8 @@ static int really_register_bound_param(struct pdo_bound_param_data *param, pdo_s
int i;
for (i = 0; i < stmt->column_count; i++) {
- if (strncmp(stmt->columns[i].name, param->name->val, param->name->len + 1) == 0) {
+ if (stmt->columns[i].name->len == param->name->len &&
+ strncmp(stmt->columns[i].name->val, param->name->val, param->name->len + 1) == 0) {
param->paramno = i;
break;
}
@@ -1025,7 +1026,7 @@ static int do_fetch(pdo_stmt_t *stmt, int do_bind, zval *return_value, enum pdo_
switch (how) {
case PDO_FETCH_ASSOC:
- add_assoc_zval(return_value, stmt->columns[i].name, &val);
+ zend_symtable_update(Z_ARRVAL_P(return_value), stmt->columns[i].name, &val);
break;
case PDO_FETCH_KEY_PAIR:
@@ -1046,19 +1047,18 @@ static int do_fetch(pdo_stmt_t *stmt, int do_bind, zval *return_value, enum pdo_
case PDO_FETCH_USE_DEFAULT:
case PDO_FETCH_BOTH:
- add_assoc_zval(return_value, stmt->columns[i].name, &val);
+ zend_symtable_update(Z_ARRVAL_P(return_value), stmt->columns[i].name, &val);
if (Z_REFCOUNTED(val)) {
Z_ADDREF(val);
}
- add_next_index_zval(return_value, &val);
+ zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &val);
break;
case PDO_FETCH_NAMED:
/* already have an item with this name? */
{
zval *curr_val;
- if ((curr_val = zend_hash_str_find(Z_ARRVAL_P(return_value), stmt->columns[i].name,
- strlen(stmt->columns[i].name)))) {
+ if ((curr_val = zend_hash_find(Z_ARRVAL_P(return_value), stmt->columns[i].name))) {
zval arr;
if (Z_TYPE_P(curr_val) != IS_ARRAY) {
/* a little bit of black magic here:
@@ -1077,33 +1077,33 @@ static int do_fetch(pdo_stmt_t *stmt, int do_bind, zval *return_value, enum pdo_
ZVAL_COPY_VALUE(&cur, curr_val);
ZVAL_COPY_VALUE(curr_val, &arr);
- add_next_index_zval(&arr, &cur);
+ zend_hash_next_index_insert_new(Z_ARRVAL(arr), &cur);
} else {
ZVAL_COPY_VALUE(&arr, curr_val);
}
- add_next_index_zval(&arr, &val);
+ zend_hash_next_index_insert_new(Z_ARRVAL(arr), &val);
} else {
- add_assoc_zval(return_value, stmt->columns[i].name, &val);
+ zend_hash_update(Z_ARRVAL_P(return_value), stmt->columns[i].name, &val);
}
}
break;
case PDO_FETCH_NUM:
- add_next_index_zval(return_value, &val);
+ zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &val);
break;
case PDO_FETCH_OBJ:
case PDO_FETCH_INTO:
- zend_update_property(NULL, return_value,
- stmt->columns[i].name, stmt->columns[i].namelen,
+ zend_update_property_ex(NULL, return_value,
+ stmt->columns[i].name,
&val);
zval_ptr_dtor(&val);
break;
case PDO_FETCH_CLASS:
if ((flags & PDO_FETCH_SERIALIZE) == 0 || idx) {
- zend_update_property(ce, return_value,
- stmt->columns[i].name, stmt->columns[i].namelen,
+ zend_update_property_ex(ce, return_value,
+ stmt->columns[i].name,
&val);
zval_ptr_dtor(&val);
} else {
@@ -1194,16 +1194,16 @@ static int do_fetch(pdo_stmt_t *stmt, int do_bind, zval *return_value, enum pdo_
if (return_all) {
if ((flags & PDO_FETCH_UNIQUE) == PDO_FETCH_UNIQUE) {
- add_assoc_zval(return_all, Z_STRVAL(grp_val), return_value);
+ zend_symtable_update(Z_ARRVAL_P(return_all), Z_STR(grp_val), return_value);
} else {
zval grp;
if ((pgrp = zend_symtable_find(Z_ARRVAL_P(return_all), Z_STR(grp_val))) == NULL) {
array_init(&grp);
- add_assoc_zval(return_all, Z_STRVAL(grp_val), &grp);
+ zend_symtable_update(Z_ARRVAL_P(return_all), Z_STR(grp_val), &grp);
} else {
ZVAL_COPY_VALUE(&grp, pgrp);
}
- add_next_index_zval(&grp, return_value);
+ zend_hash_next_index_insert(Z_ARRVAL(grp), return_value);
}
zval_dtor(&grp_val);
}
@@ -1518,7 +1518,7 @@ static PHP_METHOD(PDOStatement, fetchAll)
} else {
array_init(return_value);
do {
- add_next_index_zval(return_value, &data);
+ zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &data);
} while (do_fetch(stmt, 1, &data, how | flags, PDO_FETCH_ORI_NEXT, 0, 0));
}
}
@@ -1824,7 +1824,7 @@ static PHP_METHOD(PDOStatement, getColumnMeta)
/* add stock items */
col = &stmt->columns[colno];
- add_assoc_string(return_value, "name", col->name);
+ add_assoc_str(return_value, "name", zend_string_copy(col->name));
add_assoc_long(return_value, "len", col->maxlen); /* FIXME: unsigned ? */
add_assoc_long(return_value, "precision", col->precision);
if (col->param_type != PDO_PARAM_ZVAL) {
@@ -2027,7 +2027,7 @@ static int pdo_stmt_do_next_rowset(pdo_stmt_t *stmt)
struct pdo_column_data *cols = stmt->columns;
for (i = 0; i < stmt->column_count; i++) {
- efree(cols[i].name);
+ zend_string_release(cols[i].name);
}
efree(stmt->columns);
stmt->columns = NULL;
@@ -2329,7 +2329,7 @@ PDO_API void php_pdo_free_statement(pdo_stmt_t *stmt)
for (i = 0; i < stmt->column_count; i++) {
if (cols[i].name) {
- efree(cols[i].name);
+ zend_string_release(cols[i].name);
cols[i].name = NULL;
}
}
@@ -2506,7 +2506,8 @@ static zval *row_prop_read(zval *object, zval *member, int type, void **cache_sl
/* TODO: replace this with a hash of available column names to column
* numbers */
for (colno = 0; colno < stmt->column_count; colno++) {
- if (strcmp(stmt->columns[colno].name, Z_STRVAL_P(member)) == 0) {
+ if (stmt->columns[colno].name->len == Z_STRLEN_P(member) &&
+ strncmp(stmt->columns[colno].name->val, Z_STRVAL_P(member), Z_STRLEN_P(member)) == 0) {
fetch_value(stmt, rv, colno, NULL);
//???
//Z_SET_REFCOUNT_P(rv, 0);
@@ -2565,7 +2566,8 @@ static int row_prop_exists(zval *object, zval *member, int check_empty, void **c
/* TODO: replace this with a hash of available column names to column
* numbers */
for (colno = 0; colno < stmt->column_count; colno++) {
- if (strcmp(stmt->columns[colno].name, Z_STRVAL_P(member)) == 0) {
+ if (stmt->columns[colno].name->len == Z_STRLEN_P(member) &&
+ strncmp(stmt->columns[colno].name->val, Z_STRVAL_P(member), Z_STRLEN_P(member)) == 0) {
return 1;
}
}
@@ -2606,7 +2608,7 @@ static HashTable *row_get_properties(zval *object)
zval val;
fetch_value(stmt, &val, i, NULL);
- zend_hash_str_update(stmt->std.properties, stmt->columns[i].name, stmt->columns[i].namelen, &val);
+ zend_hash_update(stmt->std.properties, stmt->columns[i].name, &val);
}
return stmt->std.properties;
diff --git a/ext/pdo/php_pdo_driver.h b/ext/pdo/php_pdo_driver.h
index 407d1baa82..efa21a4e14 100644
--- a/ext/pdo/php_pdo_driver.h
+++ b/ext/pdo/php_pdo_driver.h
@@ -527,11 +527,10 @@ static inline pdo_dbh_object_t *php_pdo_dbh_fetch_object(zend_object *obj) {
/* describes a column */
struct pdo_column_data {
- char *name;
+ zend_string *name;
size_t maxlen;
zend_ulong precision;
enum pdo_param_type param_type;
- size_t namelen;
/* don't touch this unless your name is dbdo */
void *dbdo_data;
diff --git a/ext/pdo_dblib/dblib_stmt.c b/ext/pdo_dblib/dblib_stmt.c
index 4da5f71531..97776548b2 100644
--- a/ext/pdo_dblib/dblib_stmt.c
+++ b/ext/pdo_dblib/dblib_stmt.c
@@ -191,16 +191,17 @@ static int pdo_dblib_stmt_describe(pdo_stmt_t *stmt, int colno)
{
pdo_dblib_stmt *S = (pdo_dblib_stmt*)stmt->driver_data;
pdo_dblib_db_handle *H = S->H;
+ struct pdo_column_data *col;
+ zend_string *str;
if(colno >= stmt->column_count || colno < 0) {
return FAILURE;
}
- struct pdo_column_data *col = &stmt->columns[colno];
-
- col->name = estrdup(dbcolname(H->link, colno+1));
+ col = &stmt->columns[colno];
+ str = dbcolname(H->link, colno+1);
+ col->name = zend_string_init(str, strlen(str), 0);
col->maxlen = dbcollen(H->link, colno+1);
- col->namelen = strlen(col->name);
col->param_type = PDO_PARAM_STR;
return 1;
diff --git a/ext/pdo_firebird/firebird_statement.c b/ext/pdo_firebird/firebird_statement.c
index 50901841c8..337ce3fb66 100644
--- a/ext/pdo_firebird/firebird_statement.c
+++ b/ext/pdo_firebird/firebird_statement.c
@@ -197,8 +197,8 @@ static int firebird_stmt_describe(pdo_stmt_t *stmt, int colno) /* {{{ */
: (var->aliasname_length);
col->precision = -var->sqlscale;
col->maxlen = var->sqllen;
- col->namelen = colname_len;
- col->name = cp = emalloc(colname_len + 1);
+ col->name = zend_string_alloc(colname_len, 0);
+ cp = col->name->val;
if (colname_len > var->aliasname_length) {
memmove(cp, var->relname, var->relname_length);
cp += var->relname_length;
diff --git a/ext/pdo_mysql/mysql_statement.c b/ext/pdo_mysql/mysql_statement.c
index 56728ed92d..551960560e 100644
--- a/ext/pdo_mysql/mysql_statement.c
+++ b/ext/pdo_mysql/mysql_statement.c
@@ -696,14 +696,11 @@ static int pdo_mysql_stmt_describe(pdo_stmt_t *stmt, int colno) /* {{{ */
PDO_DBG_RETURN(1);
}
for (i = 0; i < stmt->column_count; i++) {
- int namelen;
if (S->H->fetch_table_names) {
- namelen = spprintf(&cols[i].name, 0, "%s.%s", S->fields[i].table, S->fields[i].name);
- cols[i].namelen = namelen;
+ cols[i].name = strpprintf(0, "%s.%s", S->fields[i].table, S->fields[i].name);
} else {
- cols[i].namelen = S->fields[i].name_length;
- cols[i].name = estrndup(S->fields[i].name, S->fields[i].name_length);
+ cols[i].name = zend_string_init(S->fields[i].name, S->fields[i].name_length, 0);
}
cols[i].precision = S->fields[i].decimals;
diff --git a/ext/pdo_oci/oci_statement.c b/ext/pdo_oci/oci_statement.c
index e47fba513b..9efb371f6d 100644
--- a/ext/pdo_oci/oci_statement.c
+++ b/ext/pdo_oci/oci_statement.c
@@ -525,8 +525,7 @@ static int oci_stmt_describe(pdo_stmt_t *stmt, int colno) /* {{{ */
col->precision = scale;
col->maxlen = data_size;
- col->namelen = namelen;
- col->name = estrndup((char *)colname, namelen);
+ col->name = zend_string_init((char *)colname, namelen, 0);
S->cols[colno].dtype = dtype;
diff --git a/ext/pdo_odbc/odbc_stmt.c b/ext/pdo_odbc/odbc_stmt.c
index 5d4ff3af96..6421cb6701 100644
--- a/ext/pdo_odbc/odbc_stmt.c
+++ b/ext/pdo_odbc/odbc_stmt.c
@@ -594,8 +594,7 @@ static int odbc_stmt_describe(pdo_stmt_t *stmt, int colno)
colsize = displaysize;
col->maxlen = S->cols[colno].datalen = colsize;
- col->namelen = colnamelen;
- col->name = estrdup(S->cols[colno].colname);
+ col->name = zend_string_init(S->cols[colno].colname, colnamelen, 0);
S->cols[colno].is_unicode = pdo_odbc_sqltype_is_unicode(S, S->cols[colno].coltype);
/* returning data as a string */
diff --git a/ext/pdo_pgsql/pgsql_statement.c b/ext/pdo_pgsql/pgsql_statement.c
index 435781ecf1..60553b36ce 100644
--- a/ext/pdo_pgsql/pgsql_statement.c
+++ b/ext/pdo_pgsql/pgsql_statement.c
@@ -438,13 +438,14 @@ static int pgsql_stmt_describe(pdo_stmt_t *stmt, int colno)
pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
struct pdo_column_data *cols = stmt->columns;
struct pdo_bound_param_data *param;
+ char *str;
if (!S->result) {
return 0;
}
- cols[colno].name = estrdup(PQfname(S->result, colno));
- cols[colno].namelen = strlen(cols[colno].name);
+ str = PQfname(S->result, colno);
+ cols[colno].name = zend_string_init(str, strlen(str), 0);
cols[colno].maxlen = PQfsize(S->result, colno);
cols[colno].precision = PQfmod(S->result, colno);
S->cols[colno].pgsql_type = PQftype(S->result, colno);
@@ -459,7 +460,7 @@ static int pgsql_stmt_describe(pdo_stmt_t *stmt, int colno)
/* did the user bind the column as a LOB ? */
if (stmt->bound_columns && (
(param = zend_hash_index_find_ptr(stmt->bound_columns, colno)) != NULL ||
- (param = zend_hash_str_find_ptr(stmt->bound_columns, cols[colno].name, cols[colno].namelen)) != NULL)) {
+ (param = zend_hash_find_ptr(stmt->bound_columns, cols[colno].name)) != NULL)) {
if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_LOB) {
cols[colno].param_type = PDO_PARAM_LOB;
diff --git a/ext/pdo_sqlite/sqlite_statement.c b/ext/pdo_sqlite/sqlite_statement.c
index 29300c198a..f42ad05329 100644
--- a/ext/pdo_sqlite/sqlite_statement.c
+++ b/ext/pdo_sqlite/sqlite_statement.c
@@ -233,6 +233,7 @@ static int pdo_sqlite_stmt_fetch(pdo_stmt_t *stmt,
static int pdo_sqlite_stmt_describe(pdo_stmt_t *stmt, int colno)
{
pdo_sqlite_stmt *S = (pdo_sqlite_stmt*)stmt->driver_data;
+ char *str;
if(colno >= sqlite3_column_count(S->stmt)) {
/* error invalid column */
@@ -240,8 +241,8 @@ static int pdo_sqlite_stmt_describe(pdo_stmt_t *stmt, int colno)
return 0;
}
- stmt->columns[colno].name = estrdup(sqlite3_column_name(S->stmt, colno));
- stmt->columns[colno].namelen = strlen(stmt->columns[colno].name);
+ str = sqlite3_column_name(S->stmt, colno);
+ stmt->columns[colno].name = zend_string_init(str, strlen(str), 0);
stmt->columns[colno].maxlen = 0xffffffff;
stmt->columns[colno].precision = 0;