1--TEST--
2MySQL Prepared Statements and different column counts
3--SKIPIF--
4<?php
5require_once(__DIR__ . DIRECTORY_SEPARATOR . 'skipif.inc');
6require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
7MySQLPDOTest::skip();
8$db = MySQLPDOTest::factory();
9
10$row = $db->query('SELECT VERSION() as _version')->fetch(PDO::FETCH_ASSOC);
11$matches = array();
12if (!preg_match('/^(\d+)\.(\d+)\.(\d+)/ismU', $row['_version'], $matches))
13    die(sprintf("skip Cannot determine MySQL Server version\n"));
14
15$version = $matches[1] * 10000 + $matches[2] * 100 + $matches[3];
16if ($version < 50000)
17    die(sprintf("skip Need MySQL Server 5.0.0+, found %d.%02d.%02d (%d)\n",
18        $matches[1], $matches[2], $matches[3], $version));
19?>
20--FILE--
21<?php
22    require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
23    $db = MySQLPDOTest::factory();
24
25    function check_result($offset, $stmt, $columns) {
26        $row = $stmt->fetch(PDO::FETCH_ASSOC);
27        $stmt->nextRowSet();
28
29        if (!isset($row['one']) || ($row['one'] != 1)) {
30                printf("[%03d + 1] Expecting array('one' => 1), got %s\n", $offset, var_export($row, true));
31                return false;
32        }
33
34        if (($columns == 2) &&
35            (!isset($row['two']) || ($row['two'] != 2))) {
36                printf("[%03d + 2] Expecting array('one' => 1, 'two' => 2), got %s\n", $offset, var_export($row, true));
37                return false;
38        } else if (($columns == 1) && isset($row['two'])) {
39                printf("[%03d + 3] Expecting one array element got two\n", $offset);
40                return false;
41        }
42
43        return true;
44    }
45
46    try {
47
48        // What will happen if a PS returns a different number of result set column upon each execution?
49        // Lets try with a SP accepting parameters...
50        $db->exec('DROP PROCEDURE IF EXISTS p');
51        $db->exec('CREATE PROCEDURE p(IN cols INT) BEGIN IF cols < 2 THEN SELECT cols AS "one"; ELSE SELECT 1 AS "one", cols AS "two"; END IF; END;');
52
53        // Emulates PS first
54        $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, 1);
55        $stmt = $db->prepare('CALL p(?)');
56
57        $columns = null;
58        $stmt->bindParam(1, $columns);
59        for ($i = 0; $i < 5; $i++) {
60            $columns = ($i % 2) + 1;
61            $stmt->execute();
62            check_result($i, $stmt, $columns);
63        }
64
65        if (MySQLPDOTest::isPDOMySQLnd()) {
66            // Native PS
67            // Libmysql cannot handle such a stored procedure. You will see leaks with libmysql
68            $db = MySQLPDOTest::factory();
69            $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, 0);
70            $db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, 1);
71            $stmt = $db->prepare('CALL p(?)');
72            $stmt->bindParam(1, $columns);
73            for ($i = 5; $i < 10; $i++) {
74                $columns = ($i % 2) + 1;
75                $stmt->execute();
76                check_result($i, $stmt, $columns);
77            }
78        }
79
80        // And now without parameters... - this gives a different control flow inside PDO
81        $db->exec('DROP PROCEDURE IF EXISTS p');
82        $db->exec('CREATE PROCEDURE p() BEGIN DECLARE cols INT; SELECT @numcols INTO cols; IF cols < 2 THEN SET @numcols = 2; SELECT cols AS "one"; ELSE SET @numcols = 1; SELECT 1 AS "one", cols AS "two"; END IF; END;');
83
84                // Emulates PS first
85        $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, 1);
86        $db->exec('SET @numcols = 1');
87        $stmt = $db->prepare('CALL p()');
88        $stmt->execute();
89        check_result(11, $stmt, 1);
90        $stmt->execute();
91        check_result(12, $stmt, 2);
92        $db->exec('SET @numcols = 1');
93        $stmt->execute();
94        check_result(13, $stmt, 1);
95
96        if (MySQLPDOTest::isPDOMySQLnd()) {
97            // Native PS
98            // Libmysql cannot handle such a stored procedure. You will see leaks with libmysql
99            $db = MySQLPDOTest::factory();
100            $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, 0);
101            $db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, 1);
102            $db->exec('SET @numcols = 1');
103            $stmt = $db->prepare('CALL p()');
104            $stmt->execute();
105            check_result(14, $stmt, 1);
106            $stmt->execute();
107            check_result(15, $stmt, 2);
108            $db->exec('SET @numcols = 1');
109            $stmt->execute();
110            check_result(16, $stmt, 1);
111        }
112
113    } catch (PDOException $e) {
114        printf("[99] %s [%s] %s\n",
115            $e->getMessage(), $db->errorCode(), implode(' ', $db->errorInfo()));
116    }
117
118    print "done!";
119?>
120--EXPECT--
121done!
122