1--TEST-- 2MySQL PDO->exec(), native types - 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) { 15 16 $db->exec('DROP TABLE IF EXISTS test_mysql_types_zerofill'); 17 $sql = sprintf('CREATE TABLE test_mysql_types_zerofill(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_zerofill(id, label) VALUES (?, ?)'); 25 $stmt->bindValue(1, $offset); 26 $stmt->bindValue(2, $value); 27 try { 28 if (!$stmt->execute()) { 29 printf("[%03d + 1] INSERT failed, %s\n", $offset, var_export($stmt->errorInfo(), true)); 30 return false; 31 } 32 } catch (PDOException $e) { 33 // This might be a SQL warning on signed values inserted in unsigned columns 34 // Zerofill implies unsigned but the test plays with signed = negative values as well! 35 return true; 36 } 37 38 $db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false); 39 $stmt = $db->query('SELECT id, label FROM test_mysql_types_zerofill'); 40 $row = $stmt->fetch(PDO::FETCH_ASSOC); 41 $stmt->closeCursor(); 42 if (!isset($row['id']) || !isset($row['label'])) { 43 printf("[%03d + 2] Fetched result seems wrong, dumping result: %s\n", $offset, var_export($row, true)); 44 return false; 45 } 46 47 if ($row['id'] != $offset) { 48 printf("[%03d + 3] Expecting %s got %s\n", $offset, $row['id']); 49 return false; 50 } 51 52 if (!is_null($pattern)) { 53 54 if (!preg_match($pattern, $row['label'])) { 55 printf("[%03d + 5] Value seems wrong, accepting pattern %s got %s, check manually\n", 56 $offset, $pattern, var_export($row['label'], true)); 57 return false; 58 } 59 60 } else { 61 62 $exp = $value; 63 if (!is_null($ret_value)) { 64 // we expect a different return value than our input value 65 // typically the difference is only the type 66 $exp = $ret_value; 67 } 68 69 if ($row['label'] !== $exp) { 70 printf("[%03d + 4] %s - input = %s/%s, output = %s/%s\n", $offset, 71 $sql_type, var_export($exp, true), gettype($exp), 72 var_export($row['label'], true), gettype($row['label'])); 73 return false; 74 } 75 76 } 77 78 $db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, true); 79 $stmt = $db->query('SELECT id, label FROM test_mysql_types_zerofill'); 80 $row_string = $stmt->fetch(PDO::FETCH_ASSOC); 81 $stmt->closeCursor(); 82 if ($row['label'] != $row_string['label']) { 83 printf("%s - STRINGIGY = %s, NATIVE = %s\n", $sql_type, var_export($row_string['label'], true), var_export($row['label'], true)); 84 return false; 85 } 86 87 return true; 88 } 89 90 $db = MySQLPDOTest::factory(); 91 $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 92 $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); 93 $db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false); 94 95 $stmt = $db->prepare('SELECT @@sql_mode AS _mode'); 96 $stmt->execute(); 97 $row = $stmt->fetch(PDO::FETCH_ASSOC); 98 $real_as_float = (false === stristr($row['_mode'], "REAL_AS_FLOAT")) ? false : true; 99 100 test_type($db, 100, 'REAL ZEROFILL', -1.01, NULL, '/^[0]*0$/'); 101 test_type($db, 110, 'REAL ZEROFILL', 1.01, NULL, ($real_as_float) ? '/^[0]*1\.0.*$/' : '/^[0]*1\.01$/'); 102 test_type($db, 120, 'REAL UNSIGNED ZEROFILL', 1.01, NULL, ($real_as_float) ? '/^[0]*1\..*$/' : '/^[0]*1\.01$/'); 103 104 test_type($db, 130, 'DOUBLE ZEROFILL', -1.01, NULL, '/^[0]*0$/'); 105 test_type($db, 140, 'DOUBLE ZEROFILL', 1.01, NULL, '/^[0]*1\.01$/'); 106 test_type($db, 150, 'DOUBLE UNSIGNED ZEROFILL', 1.01, NULL, '/^[0]*1\.01$/'); 107 108 test_type($db, 160, 'FLOAT ZEROFILL', -1.01, NULL, '/^[0]*0$/'); 109 test_type($db, 170, 'FLOAT ZEROFILL', 1, NULL, '/^[0]*1$/'); 110 test_type($db, 180, 'FLOAT UNSIGNED ZEROFILL', -1, NULL, '/^[0]*0$/'); 111 112 test_type($db, 190, 'DECIMAL ZEROFILL', -1.01, NULL, '/^[0]*0$/'); 113 test_type($db, 200, 'DECIMAL ZEROFILL', 1.01, NULL, '/^[0]*1$/'); 114 test_type($db, 210, 'DECIMAL UNSIGNED ZEROFILL', 1.01, NULL, '/^[0]*1$/'); 115 116 test_type($db, 220, 'NUMERIC ZEROFILL', -1, NULL, '/^[0]*0$/'); 117 test_type($db, 230, 'NUMERIC ZEROFILL', 1, NULL, '/^[0]*1$/'); 118 test_type($db, 240, 'NUMERIC UNSIGNED ZEROFILL', 1.01, NULL, '/^[0]*1$/'); 119 120 echo "done!\n"; 121?> 122--CLEAN-- 123<?php 124require_once __DIR__ . '/inc/mysql_pdo_test.inc'; 125$db = MySQLPDOTest::factory(); 126$db->exec('DROP TABLE IF EXISTS test_mysql_types_zerofill'); 127?> 128--EXPECT-- 129done! 130