1--TEST--
2MySQL PDO->exec(), native types wo ZEROFILL
3--EXTENSIONS--
4pdo_mysql
5--SKIPIF--
6<?php
7require_once __DIR__ . '/inc/mysql_pdo_test.inc';
8MySQLPDOTest::skip();
9?>
10--FILE--
11<?php
12    require_once __DIR__ . '/inc/mysql_pdo_test.inc';
13
14    function test_type(&$db, $offset, $sql_type, $value, $ret_value = NULL, $pattern = NULL, $alternative_type = NULL) {
15
16        $db->exec('DROP TABLE IF EXISTS test_mysql_types');
17        $sql = sprintf('CREATE TABLE test_mysql_types(id INT, label %s) ENGINE=%s', $sql_type, MySQLPDOTest::getTableEngine());
18        $db->exec($sql);
19        if ($db->errorCode() != 0) {
20            // not all MySQL Server versions and/or engines might support the type
21            return true;
22        }
23
24        $stmt = $db->prepare('INSERT INTO test_mysql_types(id, label) VALUES (?, ?)');
25        $stmt->bindValue(1, $offset);
26        $stmt->bindValue(2, $value);
27        if (!$stmt->execute()) {
28            printf("[%03d + 1] INSERT failed, %s\n", $offset, var_export($stmt->errorInfo(), true));
29            return false;
30        }
31        $db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
32        $stmt = $db->query('SELECT  id, label FROM test_mysql_types');
33        $row = $stmt->fetch(PDO::FETCH_ASSOC);
34        $stmt->closeCursor();
35
36        if (!isset($row['id']) || !isset($row['label'])) {
37            printf("[%03d + 2] Fetched result seems wrong, dumping result: %s\n", $offset, var_export($row, true));
38            return false;
39        }
40
41        if ($row['id'] != $offset) {
42            printf("[%03d + 3] Expecting %s got %s\n", $offset, $row['id']);
43            return false;
44        }
45
46        if (!is_null($pattern)) {
47            if (!preg_match($pattern, $row['label'])) {
48                printf("[%03d + 5] Value seems wrong, accepting pattern %s got %s, check manually\n",
49                    $offset, $pattern, var_export($row['label'], true));
50                return false;
51            }
52
53        } else {
54
55            $exp = $value;
56            if (!is_null($ret_value)) {
57                // we expect a different return value than our input value
58                // typically the difference is only the type
59                $exp = $ret_value;
60            }
61            if ($row['label'] !== $exp && !is_null($alternative_type) && gettype($row['label']) != $alternative_type) {
62                printf("[%03d + 4] %s - input = %s/%s, output = %s/%s (alternative type: %s)\n", $offset,
63                    $sql_type, var_export($exp, true), gettype($exp),
64                    var_export($row['label'], true), gettype($row['label']),
65                    $alternative_type);
66                return false;
67            }
68
69        }
70
71        $db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, true);
72        $stmt = $db->query('SELECT id, label FROM test_mysql_types');
73        $row_string = $stmt->fetch(PDO::FETCH_ASSOC);
74        $stmt->closeCursor();
75        if (is_null($pattern) && ($row['label'] != $row_string['label'])) {
76            printf("%s - STRINGIGY = %s, NATIVE = %s\n", $sql_type, var_export($row_string['label'], true), var_export($row['label'], true));
77            return false;
78        } else if (!is_null($pattern) && !preg_match($pattern, $row_string['label'])) {
79            printf("%s - STRINGIGY = %s, NATIVE = %s, pattern '%s'\n", $sql_type, var_export($row_string['label'], true), var_export($row['label'], true), $pattern);
80            return false;
81        }
82
83
84        return true;
85    }
86
87    $db = MySQLPDOTest::factory();
88    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
89    $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
90    $db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
91
92/*
93    test_type($db, 20, 'BIT(8)', 1);
94*/
95    $is_mysqlnd = MySQLPDOTest::isPDOMySQLnd();
96
97    test_type($db, 30, 'TINYINT', -127, ($is_mysqlnd) ? -127: '-127');
98    test_type($db, 40, 'TINYINT UNSIGNED', 255, ($is_mysqlnd) ? 255 : '255');
99    test_type($db, 50, 'BOOLEAN', 1, ($is_mysqlnd) ? 1 : '1');
100
101    test_type($db, 60, 'SMALLINT', -32768, ($is_mysqlnd) ? -32768 : '-32768');
102    test_type($db, 70, 'SMALLINT UNSIGNED', 65535, ($is_mysqlnd) ? 65535 : '65535');
103
104    test_type($db, 80, 'MEDIUMINT', -8388608, ($is_mysqlnd) ? -8388608 : '-8388608');
105    test_type($db, 90, 'MEDIUMINT UNSIGNED', 16777215, ($is_mysqlnd) ? 16777215 : '16777215');
106
107    test_type($db, 100, 'INT', -2147483648,
108        ($is_mysqlnd) ? ((PHP_INT_SIZE > 4) ? (int)-2147483648 : (double)-2147483648) : '-2147483648',
109        NULL, ($is_mysqlnd) ? 'integer' : NULL);
110
111    test_type($db, 110, 'INT UNSIGNED', 4294967295, ($is_mysqlnd) ? ((PHP_INT_SIZE > 4) ? 4294967295 : '4294967295') : '4294967295');
112
113    // no chance to return int with the current PDO version - we are forced to return strings
114    test_type($db, 120, 'BIGINT', 1, ($is_mysqlnd) ? 1 : '1');
115    // to avoid trouble with  numeric ranges, lets pass the numbers as a string
116    test_type($db, 130, 'BIGINT', '-9223372036854775808', NULL, '/^\-9[\.]*22/');
117    test_type($db, 140, 'BIGINT UNSIGNED', '18446744073709551615', NULL, '/^1[\.]*844/');
118
119    test_type($db, 150, 'REAL', -1.01, ($is_mysqlnd) ? -1.01 : '-1.01');
120    test_type($db, 160, 'REAL UNSIGNED', 1.01, ($is_mysqlnd) ? 1.01 : '1.01');
121
122    test_type($db, 170, 'DOUBLE', -1.01, ($is_mysqlnd) ? -1.01 : '-1.01');
123    test_type($db, 180, 'DOUBLE UNSIGNED', 1.01, ($is_mysqlnd) ? 1.01 : '1.01');
124
125    test_type($db, 210, 'FLOAT', -1.01, NULL, '/^\-1.0\d+/');
126    test_type($db, 220, 'FLOAT UNSIGNED', 1.01, NULL, '/^1.0\d+/');
127
128    test_type($db, 250, 'DECIMAL', -1.01, '-1');
129    test_type($db, 260, 'DECIMAL UNSIGNED', 1.01, '1');
130
131
132    test_type($db, 290, 'NUMERIC', -1.01, '-1');
133    test_type($db, 300, 'NUMERIC UNSIGNED', 1.01, '1');
134
135    test_type($db, 330, 'DATE', '2008-04-23');
136    test_type($db, 340, 'TIME', '14:37:00');
137    test_type($db, 350, 'TIMESTAMP', '2008-05-06 21:09:00');
138    test_type($db, 360, 'DATETIME', '2008-03-23 14:38:00');
139    test_type($db, 370, 'YEAR', 2008, ($is_mysqlnd) ? 2008 : '2008');
140
141    test_type($db, 380, 'CHAR(1)', 'a');
142    test_type($db, 390, 'CHAR(10)', '0123456789');
143    test_type($db, 400, 'CHAR(255)', str_repeat('z', 255));
144    test_type($db, 410, 'VARCHAR(1)', 'a');
145    test_type($db, 420, 'VARCHAR(10)', '0123456789');
146    test_type($db, 430, 'VARCHAR(255)', str_repeat('z', 255));
147
148    test_type($db, 440, 'BINARY(1)', str_repeat('a', 1));
149    test_type($db, 450, 'BINARY(255)', str_repeat('b', 255));
150    test_type($db, 460, 'VARBINARY(1)', str_repeat('a', 1));
151    test_type($db, 470, 'VARBINARY(255)', str_repeat('b', 255));
152
153    test_type($db, 480, 'TINYBLOB', str_repeat('b', 255));
154    test_type($db, 490, 'BLOB', str_repeat('b', 256));
155    test_type($db, 500, 'MEDIUMBLOB', str_repeat('b', 256));
156    test_type($db, 510, 'LONGBLOB', str_repeat('b', 256));
157
158    test_type($db, 520, 'TINYTEXT', str_repeat('b', 255));
159    test_type($db, 530, 'TINYTEXT BINARY', str_repeat('b', 255));
160
161    test_type($db, 560, 'TEXT', str_repeat('b', 256));
162    test_type($db, 570, 'TEXT BINARY', str_repeat('b', 256));
163
164    test_type($db, 580, 'MEDIUMTEXT', str_repeat('b', 256));
165    test_type($db, 590, 'MEDIUMTEXT BINARY', str_repeat('b', 256));
166
167    test_type($db, 600, 'LONGTEXT', str_repeat('b', 256));
168    test_type($db, 610, 'LONGTEXT BINARY', str_repeat('b', 256));
169
170    test_type($db, 620, "ENUM('yes', 'no') DEFAULT 'yes'", 'no');
171    test_type($db, 630, "SET('yes', 'no') DEFAULT 'yes'", 'no');
172
173    test_type($db, 640, 'DECIMAL(3,2)', -1.01, '-1.01');
174
175
176    echo "done!\n";
177?>
178--CLEAN--
179<?php
180require_once __DIR__ . '/inc/mysql_pdo_test.inc';
181$db = MySQLPDOTest::factory();
182$db->exec('DROP TABLE IF EXISTS test_mysql_types');
183?>
184--EXPECT--
185done!
186