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