]> granicus.if.org Git - php/commitdiff
fix bug #71667 (emulate how mssql extension names "computed" columns)
authorAdam Baratz <adam.baratz@gmail.com>
Fri, 26 Feb 2016 17:01:59 +0000 (12:01 -0500)
committerAnatol Belski <ab@php.net>
Wed, 6 Apr 2016 12:41:55 +0000 (14:41 +0200)
ext/pdo_dblib/dblib_driver.c
ext/pdo_dblib/dblib_stmt.c
ext/pdo_dblib/php_pdo_dblib_int.h
ext/pdo_dblib/tests/bug_71667.phpt [new file with mode: 0644]

index 9d9424a8b20521a4234643257a74ec98df133407..ad54ae57b3e2d7be9903ab0a035a4fb850a389c3 100644 (file)
@@ -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;
index 6098ae515ddd4676f1ab145d77214ff64bbc373d..a1055434d37d7b956290c0b8663e88c2f72dc992 100644 (file)
@@ -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);
index 5b47922abefcf8ba184af46c7a8dd6a98b77a4db..01586881d55f1e3df4ae57eeb15c10b0c6ba3707 100644 (file)
@@ -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 (file)
index 0000000..1c5005f
--- /dev/null
@@ -0,0 +1,34 @@
+--TEST--
+PDO_DBLIB: Emulate how mssql extension names "computed" columns
+--SKIPIF--
+<?php
+if (!extension_loaded('pdo_dblib')) die('skip not loaded');
+require dirname(__FILE__) . '/config.inc';
+?>
+--FILE--
+<?php
+require dirname(__FILE__) . '/config.inc';
+
+$stmt = $db->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"
+  }
+}