From ed3edc2f05c7be9c498aee64de2ad5a796456135 Mon Sep 17 00:00:00 2001 From: Adam Baratz Date: Fri, 26 Feb 2016 12:01:59 -0500 Subject: [PATCH] fix bug #71667 (emulate how mssql extension names "computed" columns) --- ext/pdo_dblib/dblib_driver.c | 1 + ext/pdo_dblib/dblib_stmt.c | 20 +++++++++++++----- ext/pdo_dblib/php_pdo_dblib_int.h | 1 + ext/pdo_dblib/tests/bug_71667.phpt | 34 ++++++++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 ext/pdo_dblib/tests/bug_71667.phpt diff --git a/ext/pdo_dblib/dblib_driver.c b/ext/pdo_dblib/dblib_driver.c index 9d9424a8b2..ad54ae57b3 100644 --- a/ext/pdo_dblib/dblib_driver.c +++ b/ext/pdo_dblib/dblib_driver.c @@ -101,6 +101,7 @@ static int dblib_handle_preparer(pdo_dbh_t *dbh, const char *sql, size_t sql_len stmt->driver_data = S; stmt->methods = &dblib_stmt_methods; stmt->supports_placeholders = PDO_PLACEHOLDER_NONE; + S->computed_column_name_count = 0; S->err.sqlstate = stmt->error_code; return 1; diff --git a/ext/pdo_dblib/dblib_stmt.c b/ext/pdo_dblib/dblib_stmt.c index 6098ae515d..a1055434d3 100644 --- a/ext/pdo_dblib/dblib_stmt.c +++ b/ext/pdo_dblib/dblib_stmt.c @@ -218,17 +218,27 @@ static int pdo_dblib_stmt_describe(pdo_stmt_t *stmt, int colno) return FAILURE; } + if (colno == 0) { + S->computed_column_name_count = 0; + } + col = &stmt->columns[colno]; fname = (char*)dbcolname(H->link, colno+1); if (fname && *fname) { col->name = zend_string_init(fname, strlen(fname), 0); } else { - char buf[16]; - int len; - - len = snprintf(buf, sizeof(buf), "computed%d", colno); - col->name = zend_string_init(buf, len, 0); + if (S->computed_column_name_count > 0) { + char buf[16]; + int len; + + len = snprintf(buf, sizeof(buf), "computed%d", S->computed_column_name_count); + col->name = zend_string_init(buf, len, 0); + } else { + col->name = zend_string_init("computed", strlen("computed"), 0); + } + + S->computed_column_name_count++; } col->maxlen = dbcollen(H->link, colno+1); diff --git a/ext/pdo_dblib/php_pdo_dblib_int.h b/ext/pdo_dblib/php_pdo_dblib_int.h index 5b47922abe..01586881d5 100644 --- a/ext/pdo_dblib/php_pdo_dblib_int.h +++ b/ext/pdo_dblib/php_pdo_dblib_int.h @@ -118,6 +118,7 @@ typedef struct { typedef struct { pdo_dblib_db_handle *H; pdo_dblib_err err; + unsigned int computed_column_name_count; } pdo_dblib_stmt; typedef struct { diff --git a/ext/pdo_dblib/tests/bug_71667.phpt b/ext/pdo_dblib/tests/bug_71667.phpt new file mode 100644 index 0000000000..1c5005fd6a --- /dev/null +++ b/ext/pdo_dblib/tests/bug_71667.phpt @@ -0,0 +1,34 @@ +--TEST-- +PDO_DBLIB: Emulate how mssql extension names "computed" columns +--SKIPIF-- + +--FILE-- +prepare("SELECT 1, 2 AS named, 3"); +$stmt->execute(); +var_dump($stmt->fetchAll()); + +?> +--EXPECT-- +array(1) { + [0]=> + array(6) { + ["computed"]=> + string(1) "1" + [0]=> + string(1) "1" + ["named"]=> + string(1) "2" + [1]=> + string(1) "2" + ["computed1"]=> + string(1) "3" + [2]=> + string(1) "3" + } +} -- 2.40.0