1--TEST--
2MySQL: PDOStatement->getColumnMeta()
3--EXTENSIONS--
4pdo_mysql
5--SKIPIF--
6<?php
7require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
8MySQLPDOTest::skip();
9// Too many differences among MySQL version - run only with a recent one
10$db = MySQLPDOTest::factory();
11$stmt = $db->query('SELECT VERSION() as _version');
12$row = $stmt->fetch(PDO::FETCH_ASSOC);
13$version = ((int)substr($row['_version'], 0, 1) * 10) + (int)substr($row['_version'], 2, 1);
14if ($version < 51)
15    die("skip Test needs MySQL 5.1+");
16?>
17--FILE--
18<?php
19require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
20$db = MySQLPDOTest::factory();
21$db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, true);
22MySQLPDOTest::createTestTable($db);
23
24try {
25
26    $stmt = $db->prepare('SELECT id FROM test ORDER BY id ASC');
27
28    // execute() has not been called yet
29    // NOTE: no warning
30    if (false !== ($tmp = $stmt->getColumnMeta(0)))
31        printf("[002] Expecting false got %s\n", var_export($tmp, true));
32
33    $stmt->execute();
34
35    // invalid offset
36    try {
37        $stmt->getColumnMeta(-1);
38    } catch (\ValueError $e) {
39        echo $e->getMessage(), \PHP_EOL;
40    }
41
42    $emulated =  $stmt->getColumnMeta(0);
43
44    printf("Testing native PS...\n");
45    $db->setAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY, 0);
46        if (0 != $db->getAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY))
47            printf("[007] Unable to turn off emulated prepared statements\n");
48
49    $stmt = $db->prepare('SELECT id FROM test ORDER BY id ASC');
50    $stmt->execute();
51    $native = $stmt->getColumnMeta(0);
52    if (count($native) == 0) {
53        printf("[008] Meta data seems wrong, %s / %s\n",
54            var_export($native, true), var_export($emulated, true));
55    }
56
57    // invalid offset
58    if (false !== ($tmp = $stmt->getColumnMeta(1)))
59        printf("[009] Expecting false because of invalid offset got %s\n", var_export($tmp, true));
60
61
62    function test_meta(&$db, $offset, $sql_type, $value, $native_type, $pdo_type) {
63
64        $db->exec('DROP TABLE IF EXISTS test');
65
66        $sql = sprintf('CREATE TABLE test(id INT, label %s) ENGINE=%s', $sql_type, MySQLPDOTest::getTableEngine());
67        if (!($stmt = @$db->prepare($sql)) || (!@$stmt->execute())) {
68            // Some engines and/or MySQL server versions might not support the data type
69            return true;
70        }
71
72        if (!$db->exec(sprintf("INSERT INTO test(id, label) VALUES (1, '%s')", $value))) {
73            printf("[%03d] + 1] Insert failed, %d - %s\n", $offset,
74                $db->errorCode(), var_export($db->errorInfo(), true));
75            return false;
76        }
77
78        $stmt = $db->prepare('SELECT id, label FROM test');
79        $stmt->execute();
80        $meta = $stmt->getColumnMeta(1);
81        $row = $stmt->fetch(PDO::FETCH_ASSOC);
82
83        if (empty($meta)) {
84            printf("[%03d + 2] getColumnMeta() failed, %d - %s\n", $offset,
85                $stmt->errorCode(), var_export($stmt->errorInfo(), true));
86            return false;
87        }
88
89        $elements = array('flags', 'table', 'name', 'len', 'precision', 'pdo_type');
90        foreach ($elements as $k => $element)
91            if (!isset($meta[$element])) {
92                printf("[%03d + 3] Element %s missing, %s\n", $offset,
93                    $element, var_export($meta, true));
94                return false;
95            }
96
97        if (($meta['table'] != 'test') || ($meta['name'] != 'label')) {
98            printf("[%03d + 4] Table or field name is wrong, %s\n", $offset,
99                var_export($meta, true));
100            return false;
101        }
102
103        if (!is_null($native_type)) {
104            if (!isset($meta['native_type'])) {
105                printf("[%03d + 5] Element native_type missing, %s\n", $offset,
106                    var_export($meta, true));
107                return false;
108            }
109
110            if (!is_array($native_type))
111                $native_type = array($native_type);
112
113            $found = false;
114            foreach ($native_type as $k => $type) {
115                if ($meta['native_type'] == $type) {
116                    $found = true;
117                    break;
118                }
119            }
120
121            if (!$found) {
122                printf("[%03d + 6] Expecting native type %s, %s\n", $offset,
123                    var_export($native_type, true), var_export($meta, true));
124                return false;
125            }
126        }
127
128        if (!is_null($pdo_type) && ($meta['pdo_type'] != $pdo_type)) {
129            printf("[%03d + 6] Expecting PDO type %s got %s (%s)\n", $offset,
130                $pdo_type, var_export($meta, true), var_export($meta['native_type']));
131            return false;
132        }
133
134        return true;
135    }
136
137    $stmt = $db->prepare('SELECT @@sql_mode AS _mode');
138    $stmt->execute();
139    $row = $stmt->fetch(PDO::FETCH_ASSOC);
140    $real_as_float = (false === stristr($row['_mode'], "REAL_AS_FLOAT")) ? false : true;
141
142    $db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
143    test_meta($db, 20, 'BIT(8)', 1, 'BIT', PDO::PARAM_INT);
144    test_meta($db, 30, 'TINYINT', -127, 'TINY', PDO::PARAM_INT);
145    test_meta($db, 40, 'TINYINT UNSIGNED', 255, 'TINY', PDO::PARAM_INT);
146    test_meta($db, 50, 'BOOLEAN', 1, NULL, PDO::PARAM_INT);
147
148    test_meta($db, 60, 'SMALLINT', -32768, 'SHORT', PDO::PARAM_INT);
149    test_meta($db, 70, 'SMALLINT UNSIGNED', 65535, 'SHORT', PDO::PARAM_INT);
150
151    test_meta($db, 80, 'MEDIUMINT', -8388608, 'INT24', PDO::PARAM_INT);
152    test_meta($db, 90, 'MEDIUMINT UNSIGNED', 16777215, 'INT24', PDO::PARAM_INT);
153
154    test_meta($db, 100, 'INT', -2147483648, 'LONG', PDO::PARAM_INT);
155    test_meta($db, 110, 'INT UNSIGNED', 4294967295, 'LONG', PDO::PARAM_INT);
156
157    test_meta($db, 120, 'BIGINT', '-9223372036854775808', 'LONGLONG', (PHP_INT_SIZE == 4) ? PDO::PARAM_STR : PDO::PARAM_INT);
158    test_meta($db, 130, 'BIGINT UNSIGNED', '18446744073709551615', 'LONGLONG', (PHP_INT_SIZE == 4) ? PDO::PARAM_STR : PDO::PARAM_INT);
159
160    test_meta($db, 130, 'REAL', -1.01, ($real_as_float) ? 'FLOAT' : 'DOUBLE', PDO::PARAM_STR);
161    test_meta($db, 140, 'REAL UNSIGNED', 1.01, ($real_as_float) ? 'FLOAT' : 'DOUBLE', PDO::PARAM_STR);
162    test_meta($db, 150, 'REAL ZEROFILL', 1.01, ($real_as_float) ? 'FLOAT' : 'DOUBLE', PDO::PARAM_STR);
163    test_meta($db, 160, 'REAL UNSIGNED ZEROFILL', 1.01, ($real_as_float) ? 'FLOAT' : 'DOUBLE', PDO::PARAM_STR);
164
165    test_meta($db, 170, 'DOUBLE', -1.01, 'DOUBLE', PDO::PARAM_STR);
166    test_meta($db, 180, 'DOUBLE UNSIGNED', 1.01, 'DOUBLE', PDO::PARAM_STR);
167    test_meta($db, 190, 'DOUBLE ZEROFILL', 1.01, 'DOUBLE', PDO::PARAM_STR);
168    test_meta($db, 200, 'DOUBLE UNSIGNED ZEROFILL', 1.01, 'DOUBLE', PDO::PARAM_STR);
169
170    test_meta($db, 210, 'FLOAT', -1.01, 'FLOAT', PDO::PARAM_STR);
171    test_meta($db, 220, 'FLOAT UNSIGNED', 1.01, 'FLOAT', PDO::PARAM_STR);
172    test_meta($db, 230, 'FLOAT ZEROFILL', 1.01, 'FLOAT', PDO::PARAM_STR);
173    test_meta($db, 240, 'FLOAT UNSIGNED ZEROFILL', 1.01, 'FLOAT', PDO::PARAM_STR);
174
175    test_meta($db, 250, 'DECIMAL', -1.01, array('DECIMAL', 'NEWDECIMAL'), PDO::PARAM_STR);
176    test_meta($db, 260, 'DECIMAL UNSIGNED', 1.01, array('DECIMAL', 'NEWDECIMAL'), PDO::PARAM_STR);
177    test_meta($db, 270, 'DECIMAL ZEROFILL', 1.01, array('DECIMAL', 'NEWDECIMAL'), PDO::PARAM_STR);
178    test_meta($db, 280, 'DECIMAL UNSIGNED ZEROFILL', 1.01, array('DECIMAL', 'NEWDECIMAL'), PDO::PARAM_STR);
179
180    test_meta($db, 290, 'NUMERIC', -1.01, array('DECIMAL', 'NEWDECIMAL'), PDO::PARAM_STR);
181    test_meta($db, 300, 'NUMERIC UNSIGNED', 1.01, array('DECIMAL', 'NEWDECIMAL'), PDO::PARAM_STR);
182    test_meta($db, 310, 'NUMERIC ZEROFILL', 1.01, array('DECIMAL', 'NEWDECIMAL'), PDO::PARAM_STR);
183    test_meta($db, 320, 'NUMERIC UNSIGNED ZEROFILL', 1.01, array('DECIMAL', 'NEWDECIMAL'), PDO::PARAM_STR);
184
185    test_meta($db, 330, 'DATE', '2008-04-23', array('DATE', 'NEWDATE'), PDO::PARAM_STR);
186    test_meta($db, 340, 'TIME', '14:37:00', 'TIME', PDO::PARAM_STR);
187    test_meta($db, 350, 'TIMESTAMP', '2008-03-23 14:38:00', 'TIMESTAMP', PDO::PARAM_STR);
188    test_meta($db, 360, 'DATETIME', '2008-03-23 14:38:00', 'DATETIME', PDO::PARAM_STR);
189    test_meta($db, 370, 'YEAR', '2008', 'YEAR', PDO::PARAM_INT);
190
191    test_meta($db, 380, 'CHAR(1)', 'a', 'STRING', PDO::PARAM_STR);
192    test_meta($db, 390, 'CHAR(10)', '0123456789', 'STRING', PDO::PARAM_STR);
193    test_meta($db, 400, 'CHAR(255)', str_repeat('z', 255), 'STRING', PDO::PARAM_STR);
194    test_meta($db, 410, 'VARCHAR(1)', 'a', 'VAR_STRING', PDO::PARAM_STR);
195    test_meta($db, 420, 'VARCHAR(10)', '0123456789', 'VAR_STRING', PDO::PARAM_STR);
196    test_meta($db, 430, 'VARCHAR(255)', str_repeat('z', 255), 'VAR_STRING', PDO::PARAM_STR);
197
198    test_meta($db, 440, 'BINARY(1)', str_repeat('a', 1), 'STRING', PDO::PARAM_STR);
199    test_meta($db, 450, 'BINARY(255)', str_repeat('b', 255), 'STRING', PDO::PARAM_STR);
200    test_meta($db, 460, 'VARBINARY(1)', str_repeat('a', 1), 'VAR_STRING', PDO::PARAM_STR);
201    test_meta($db, 470, 'VARBINARY(255)', str_repeat('b', 255), 'VAR_STRING', PDO::PARAM_STR);
202
203    test_meta($db, 480, 'TINYBLOB', str_repeat('b', 255), 'BLOB', PDO::PARAM_STR);
204    test_meta($db, 490, 'BLOB', str_repeat('b', 256), 'BLOB', PDO::PARAM_STR);
205    test_meta($db, 500, 'MEDIUMBLOB', str_repeat('b', 256), 'BLOB', PDO::PARAM_STR);
206    test_meta($db, 510, 'LONGBLOB', str_repeat('b', 256), 'BLOB', PDO::PARAM_STR);
207
208    test_meta($db, 520, 'TINYTEXT', str_repeat('b', 255), 'BLOB', PDO::PARAM_STR);
209    test_meta($db, 530, 'TINYTEXT BINARY', str_repeat('b', 255), 'BLOB', PDO::PARAM_STR);
210
211    test_meta($db, 560, 'TEXT', str_repeat('b', 256), 'BLOB', PDO::PARAM_STR);
212    test_meta($db, 570, 'TEXT BINARY', str_repeat('b', 256), 'BLOB', PDO::PARAM_STR);
213
214    test_meta($db, 580, 'MEDIUMTEXT', str_repeat('b', 256), 'BLOB', PDO::PARAM_STR);
215    test_meta($db, 590, 'MEDIUMTEXT BINARY', str_repeat('b', 256), 'BLOB', PDO::PARAM_STR);
216
217    test_meta($db, 600, 'LONGTEXT', str_repeat('b', 256), 'BLOB', PDO::PARAM_STR);
218    test_meta($db, 610, 'LONGTEXT BINARY', str_repeat('b', 256), 'BLOB', PDO::PARAM_STR);
219
220    test_meta($db, 620, "ENUM('yes', 'no') DEFAULT 'yes'", 'no', NULL, PDO::PARAM_STR);
221    test_meta($db, 630, "SET('yes', 'no') DEFAULT 'yes'", 'no', NULL, PDO::PARAM_STR);
222
223/*
224  | spatial_type
225*/
226
227    // unique key
228    $db->exec('DROP TABLE IF EXISTS test');
229    $sql = sprintf('CREATE TABLE test(id INT, label INT UNIQUE) ENGINE = %s', MySQLPDOTest::getTableEngine());
230    if (($stmt = @$db->prepare($sql)) && @$stmt->execute()) {
231        $db->exec('INSERT INTO test(id, label) VALUES (1, 2)');
232        $stmt = $db->query('SELECT id, label FROM test');
233        $meta = $stmt->getColumnMeta(1);
234        if (!isset($meta['flags'])) {
235            printf("[1000] No flags contained in metadata %s\n", var_export($meta, true));
236        } else {
237            $flags = $meta['flags'];
238            $found = false;
239            foreach ($flags as $k => $flag) {
240                if ($flag == 'unique_key')
241                    $found = true;
242            }
243            if (!$found)
244                printf("[1001] Flags seem wrong %s\n", var_export($meta, true));
245        }
246    }
247
248    // primary key
249    $db->exec('DROP TABLE IF EXISTS test');
250    $sql = sprintf('CREATE TABLE test(id INT PRIMARY KEY NOT NULL AUTO_INCREMENT) ENGINE = %s', MySQLPDOTest::getTableEngine());
251    if (($stmt = @$db->prepare($sql)) && @$stmt->execute()) {
252        $db->exec('INSERT INTO test(id) VALUES (1)');
253        $stmt = $db->query('SELECT id FROM test');
254        $meta = $stmt->getColumnMeta(0);
255        if (!isset($meta['flags'])) {
256            printf("[1002] No flags contained in metadata %s\n", var_export($meta, true));
257        } else {
258            $flags = $meta['flags'];
259            $found = false;
260            foreach ($flags as $k => $flag) {
261                if ($flag == 'primary_key')
262                    $found = true;
263            }
264            if (!$found)
265                printf("[1003] Flags seem wrong %s\n", var_export($meta, true));
266        }
267    }
268
269    // multiple key
270    $db->exec('DROP TABLE IF EXISTS test');
271    $sql = sprintf('CREATE TABLE test(id INT, label1 INT, label2 INT, INDEX idx1(label1, label2)) ENGINE = %s', MySQLPDOTest::getTableEngine());
272    if (($stmt = @$db->prepare($sql)) && @$stmt->execute()) {
273        $db->exec('INSERT INTO test(id, label1, label2) VALUES (1, 2, 3)');
274        $stmt = $db->query('SELECT id, label1, label2 FROM test');
275        $meta = $stmt->getColumnMeta(1);
276        if (!isset($meta['flags'])) {
277            printf("[1004] No flags contained in metadata %s\n", var_export($meta, true));
278        } else {
279            $flags = $meta['flags'];
280            $found = false;
281            foreach ($flags as $k => $flag) {
282                if ($flag == 'multiple_key')
283                    $found = true;
284            }
285            if (!$found)
286                printf("[1005] Flags seem wrong %s\n", var_export($meta, true));
287        }
288    }
289
290    $stmt = $db->query('SELECT NULL AS col1');
291    $meta = $stmt->getColumnMeta(0);
292    if ('NULL' !== $meta['native_type'])
293        printf("[1006] Expecting NULL got %s\n", $meta['native_type']);
294
295} catch (PDOException $e) {
296    // we should never get here, we use warnings, but never trust a system...
297    printf("[001] %s, [%s} %s\n",
298        $e->getMessage(), $db->errorInfo(), implode(' ', $db->errorInfo()));
299}
300
301$db->exec('DROP TABLE IF EXISTS test');
302print "done!";
303?>
304--EXPECT--
305PDOStatement::getColumnMeta(): Argument #1 ($column) must be greater than or equal to 0
306Testing native PS...
307done!
308