1--TEST--
2MySQL PDOStatement->bindParam() - SQL column types
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--FILE--
11<?php
12	require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
13	$db = MySQLPDOTest::factory();
14	MySQLPDOTest::createTestTable($db);
15
16	function pdo_mysql_stmt_bindparam_types_do($db, $offset, $native, $sql_type, $value) {
17
18			if ($native)
19				$db->setAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY, 0);
20			else
21				$db->setAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY, 1);
22
23			$db->exec('DROP TABLE IF EXISTS test');
24			$sql = sprintf('CREATE TABLE test(id INT, label %s) ENGINE=%s', $sql_type, MySQLPDOTest::getTableEngine());
25			if ((!$stmt = @$db->prepare($sql)) || (!@$stmt->execute()))
26				// Server might not support column type - skip it
27				return true;
28
29			$stmt = $db->prepare('INSERT INTO test(id, label) VALUES (1, ?)');
30			if (!$stmt->bindParam(1, $value)) {
31				printf("[%03d/%s + 1] %s\n", $offset, ($native) ? 'native' : 'emulated',
32					var_export($stmt->errorInfo(), true));
33				return false;
34			}
35			if (!$stmt->execute()) {
36				printf("[%03d/%s + 2] %s\n", $offset, ($native) ? 'native' : 'emulated',
37					var_export($stmt->errorInfo(), true));
38				return false;
39			}
40
41			$stmt = $db->query('SELECT id, label FROM test');
42			$id = $label = null;
43			if (!$stmt->bindColumn(1, $id)) {
44				printf("[%03d/%s + 3] %s\n", $offset, ($native) ? 'native' : 'emulated',
45					var_export($stmt->errorInfo(), true));
46				return false;
47			}
48			if (!$stmt->bindColumn(2, $label)) {
49				printf("[%03d/%s + 4] %s\n", $offset, ($native) ? 'native' : 'emulated',
50					var_export($stmt->errorInfo(), true));
51				return false;
52			}
53
54			if (!$stmt->fetch(PDO::FETCH_BOUND)) {
55				printf("[%03d/%s + 5] %s\n", $offset, ($native) ? 'native' : 'emulated',
56					var_export($stmt->errorInfo(), true));
57				return false;
58			}
59			$stmt->closeCursor();
60
61			if ($label != $value) {
62				printf("[%03d/%s + 6] Got %s expecting %s - plase check manually\n",
63					$offset, ($native) ? 'native' : 'emulated',
64					var_export($label, true), var_export($value, true));
65				// fall through
66			}
67
68			$stmt->execute();
69			$row = $stmt->fetch(PDO::FETCH_ASSOC);
70			if (empty($row)) {
71				printf("[%03d/%s + 7] %s\n", $offset, ($native) ? 'native' : 'emulated',
72					var_export($stmt->errorInfo(), true));
73				return false;
74			}
75
76			if ($row['label'] != $value) {
77				printf("[%03d/%s + 8] Got %s expecting %s - plase check manually\n",
78					$offset, ($native) ? 'native' : 'emulated',
79					var_export($row['label'], true), var_export($value, true));
80				return false;
81			}
82
83			if ($row['label'] != $label) {
84				printf("[%03d/%s + 9] Got %s from FETCH_ASSOC and %s from FETCH_BOUND- plase check manually\n",
85					$offset, ($native) ? 'native' : 'emulated',
86					var_export($row['label'], true), var_export($value, true));
87				return false;
88			}
89
90			$db->exec('DROP TABLE IF EXISTS test');
91			return true;
92	}
93
94	function pdo_mysql_stmt_bindparam_types($db, $offset, $sql_type, $value) {
95
96		pdo_mysql_stmt_bindparam_types_do($db, $offset, true, $sql_type, $value);
97		pdo_mysql_stmt_bindparam_types_do($db, $offset, false, $sql_type, $value);
98
99	}
100
101	try {
102
103		// pdo_mysql_stmt_bindparam_types($db, 2, 'BIT(8)', 1);
104		pdo_mysql_stmt_bindparam_types($db, 3, 'TINYINT', -127);
105		pdo_mysql_stmt_bindparam_types($db, 4, 'TINYINT UNSIGNED', 255);
106		pdo_mysql_stmt_bindparam_types($db, 5, 'BOOLEAN', 1);
107		pdo_mysql_stmt_bindparam_types($db, 6, 'SMALLINT', -32768);
108		pdo_mysql_stmt_bindparam_types($db, 7, 'SMALLINT UNSIGNED', 65535);
109		pdo_mysql_stmt_bindparam_types($db, 8, 'MEDIUMINT', -8388608);
110		pdo_mysql_stmt_bindparam_types($db, 9, 'MEDIUMINT UNSIGNED', 16777215);
111		pdo_mysql_stmt_bindparam_types($db, 10, 'INT', -2147483648);
112		pdo_mysql_stmt_bindparam_types($db, 11, 'INT UNSIGNED', 4294967295);
113		pdo_mysql_stmt_bindparam_types($db, 12, 'BIGINT',  -1000);
114		pdo_mysql_stmt_bindparam_types($db, 13, 'BIGINT UNSIGNED', 1000);
115		pdo_mysql_stmt_bindparam_types($db, 14, 'REAL', -1000);
116		pdo_mysql_stmt_bindparam_types($db, 15, 'REAL UNSIGNED', 1000);
117		pdo_mysql_stmt_bindparam_types($db, 16, 'REAL ZEROFILL', '0000000000000000000000');
118		pdo_mysql_stmt_bindparam_types($db, 17, 'REAL UNSIGNED ZEROFILL', '0000000000000000000010');
119		pdo_mysql_stmt_bindparam_types($db, 18, 'DOUBLE', -1000);
120		pdo_mysql_stmt_bindparam_types($db, 19, 'DOUBLE UNSIGNED', 1000);
121		pdo_mysql_stmt_bindparam_types($db, 20, 'DOUBLE ZEROFILL', '000000000000');
122		pdo_mysql_stmt_bindparam_types($db, 21, 'DOUBLE ZEROFILL UNSIGNED', '000000001000');
123		pdo_mysql_stmt_bindparam_types($db, 22, 'FLOAT', -1000);
124		pdo_mysql_stmt_bindparam_types($db, 23, 'FLOAT UNSIGNED', 1000);
125		pdo_mysql_stmt_bindparam_types($db, 24, 'FLOAT ZEROFILL', '000000000000');
126		pdo_mysql_stmt_bindparam_types($db, 25, 'FLOAT ZEROFILL UNSIGNED', '000000001000');
127		pdo_mysql_stmt_bindparam_types($db, 26, 'DECIMAL', -1000);
128		pdo_mysql_stmt_bindparam_types($db, 27, 'DECIMAL UNSIGNED', 1000);
129		pdo_mysql_stmt_bindparam_types($db, 28, 'DECIMAL ZEROFILL', '000000000000');
130		pdo_mysql_stmt_bindparam_types($db, 29, 'DECIMAL ZEROFILL UNSIGNED', '000000001000');
131		pdo_mysql_stmt_bindparam_types($db, 30, 'NUMERIC', -1000);
132		pdo_mysql_stmt_bindparam_types($db, 31, 'NUMERIC UNSIGNED', 1000);
133		pdo_mysql_stmt_bindparam_types($db, 32, 'NUMERIC ZEROFILL', '000000000000');
134		pdo_mysql_stmt_bindparam_types($db, 33, 'NUMERIC ZEROFILL UNSIGNED', '000000001000');
135		pdo_mysql_stmt_bindparam_types($db, 34, 'DATE', '2008-04-23');
136		pdo_mysql_stmt_bindparam_types($db, 35, 'TIME', '16:43:12');
137		pdo_mysql_stmt_bindparam_types($db, 36, 'TIMESTAMP', '2008-04-23 16:44:53');
138		pdo_mysql_stmt_bindparam_types($db, 37, 'DATETIME', '2008-04-23 16:44:53');
139		pdo_mysql_stmt_bindparam_types($db, 38, 'YEAR', '2008');
140		pdo_mysql_stmt_bindparam_types($db, 39, 'CHAR(1)', 'a');
141		pdo_mysql_stmt_bindparam_types($db, 40, 'CHAR(255)', 'abc');
142		pdo_mysql_stmt_bindparam_types($db, 41, 'VARCHAR(255)', str_repeat('a', 255));
143		pdo_mysql_stmt_bindparam_types($db, 42, 'BINARY(255)', str_repeat('a', 255));
144		pdo_mysql_stmt_bindparam_types($db, 43, 'VARBINARY(255)', str_repeat('a', 255));
145		pdo_mysql_stmt_bindparam_types($db, 44, 'TINYBLOB', str_repeat('a', 255));
146		pdo_mysql_stmt_bindparam_types($db, 45, 'BLOB', str_repeat('b', 300));
147		pdo_mysql_stmt_bindparam_types($db, 46, 'MEDIUMBLOB', str_repeat('b', 300));
148		pdo_mysql_stmt_bindparam_types($db, 47, 'LONGBLOB', str_repeat('b', 300));
149		pdo_mysql_stmt_bindparam_types($db, 48, 'TINYTEXT', str_repeat('c', 255));
150		pdo_mysql_stmt_bindparam_types($db, 49, 'TINYTEXT BINARY', str_repeat('c', 255));
151		pdo_mysql_stmt_bindparam_types($db, 50, 'TEXT', str_repeat('d', 300));
152		pdo_mysql_stmt_bindparam_types($db, 51, 'TEXT BINARY', str_repeat('d', 300));
153		pdo_mysql_stmt_bindparam_types($db, 52, 'MEDIUMTEXT', str_repeat('d', 300));
154		pdo_mysql_stmt_bindparam_types($db, 53, 'MEDIUMTEXT BINARY', str_repeat('d', 300));
155		pdo_mysql_stmt_bindparam_types($db, 54, 'LONGTEXT', str_repeat('d', 300));
156		pdo_mysql_stmt_bindparam_types($db, 55, 'LONGTEXT BINARY', str_repeat('d', 300));
157		pdo_mysql_stmt_bindparam_types($db, 56, "ENUM('yes', 'no') DEFAULT 'yes'", "no");
158		pdo_mysql_stmt_bindparam_types($db, 57, "SET('yes', 'no') DEFAULT 'yes'", "no");
159
160	} catch (PDOException $e) {
161		printf("[001] %s [%s] %s\n",
162			$e->getMessage(), $db->errorCode(), implode(' ', $db->errorInfo()));
163	}
164
165	print "done!";
166?>
167--CLEAN--
168<?php
169require dirname(__FILE__) . '/mysql_pdo_test.inc';
170MySQLPDOTest::dropTestTable();
171?>
172--EXPECTF--
173done!