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