1--TEST-- 2MySQL PDO->lastInsertId() 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 15 try { 16 if ('0' !== ($tmp = $db->lastInsertId())) 17 printf("[001] No query has been run, lastInsertId() should return '0'/string got '%s'/%s\n", 18 var_export($tmp, true), gettype($tmp)); 19 20 if ('0' !== ($tmp = $db->lastInsertId('sequence_name'))) 21 printf("[002] MySQL does not support sequences, expecting '0'/string got '%s'/%s\n", 22 var_export($tmp, true), gettype($tmp)); 23 24 $db->exec('DROP TABLE IF EXISTS test'); 25 if ('0' !== ($tmp = $db->lastInsertId())) 26 printf("[003] Expecting '0'/string got '%s'/%s", var_export($tmp, true), gettype($tmp)); 27 28 $db->exec(sprintf('CREATE TABLE test(id INT, col1 CHAR(10)) ENGINE=%s', PDO_MYSQL_TEST_ENGINE)); 29 if ('0' !== ($tmp = $db->lastInsertId())) 30 printf("[004] Expecting '0'/string got '%s'/%s", var_export($tmp, true), gettype($tmp)); 31 32 $stmt = $db->query('SELECT id FROM test LIMIT 1'); 33 if ('0' !== ($tmp = $db->lastInsertId())) 34 printf("[005] Expecting '0'/string got '%s'/%s", var_export($tmp, true), gettype($tmp)); 35 36 // no auto increment column 37 $db->exec("INSERT INTO test(id, col1) VALUES (100, 'a')"); 38 if ('0' !== ($tmp = $db->lastInsertId())) 39 printf("[006] Expecting '0'/string got '%s'/%s", var_export($tmp, true), gettype($tmp)); 40 41 $db->exec('ALTER TABLE test MODIFY id INT AUTO_INCREMENT PRIMARY KEY'); 42 if ('0' !== ($tmp = $db->lastInsertId())) 43 printf("[006] Expecting '0'/string got '%s'/%s", var_export($tmp, true), gettype($tmp)); 44 45 // duplicate key 46 @$db->exec("INSERT INTO test(id, col1) VALUES (100, 'a')"); 47 if ('0' !== ($tmp = $db->lastInsertId())) 48 printf("[007] Expecting '0'/string got '%s'/%s", var_export($tmp, true), gettype($tmp)); 49 50 $db->exec("INSERT INTO test(id, col1) VALUES (101, 'b')"); 51 if ('101' !== ($tmp = $db->lastInsertId())) 52 printf("[008] Expecting '0'/string got '%s'/%s", var_export($tmp, true), gettype($tmp)); 53 54 $db->exec('ALTER TABLE test MODIFY col1 CHAR(10) UNIQUE'); 55 // replace = delete + insert -> new auto increment value 56 $db->exec("REPLACE INTO test(col1) VALUES ('b')"); 57 $next_id = (int)$db->lastInsertId(); 58 59 if ($next_id <= 101) 60 printf("[009] Expecting at least 102, got %d\n",$next_id); 61 62 $stmt = $db->query('SELECT LAST_INSERT_ID() as _last_id'); 63 $row = $stmt->fetch(PDO::FETCH_ASSOC); 64 $last_id = $row['_last_id']; 65 if ($next_id != $last_id) { 66 printf("[010] LAST_INSERT_ID() = %d and lastInserId() = %d differ\n", 67 $last_id, $next_id); 68 } 69 70 $db->exec("INSERT INTO test(col1) VALUES ('c'), ('d'), ('e')"); 71 $next_id = (int)$db->lastInsertId(); 72 if ($next_id <= $last_id) 73 printf("[011] Expecting at least %d, got %d\n", $last_id + 1, $next_id); 74 75 // warnings are unhandy, lets go for exceptions for a second 76 $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 77 try { 78 $ignore_exception = true; 79 $db->exec('LOCK TABLE test WRITE'); 80 $ignore_exception = false; 81 82 if (MySQLPDOTest::getServerVersion($db) >= 50000) { 83 $stmt = $db->query('SELECT @@auto_increment_increment AS inc'); 84 $row = $stmt->fetch(PDO::FETCH_ASSOC); 85 $inc = $row['inc']; 86 } else { 87 $inc = 1; 88 } 89 90 $stmt = $db->query('SELECT LAST_INSERT_ID() as _last_id'); 91 $row = $stmt->fetch(PDO::FETCH_ASSOC); 92 $last_id = $row['_last_id']; 93 94 $db->exec("INSERT INTO test(col1) VALUES ('z')"); 95 $next_id = (int)$db->lastInsertId(); 96 if ($next_id < ($last_id + $inc)) 97 printf("[012] Expecting at least %d, got %d\n", $last_id + $inc, $next_id); 98 99 } catch (PDOException $e) { 100 if (!$ignore_exception) 101 printf("[014] %s, [%s} %s\n", $e->getMessage(), $db->errorCode(), implode(' ', $db->errorInfo())); 102 } 103 $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); 104 @$db->exec('UNLOCK TABLE test'); 105 106 } catch (PDOException $e) { 107 printf("[001] %s [%s] %s\n", 108 $e->getMessage(), $db->errorCode(), implode(' ', $db->errorInfo())); 109 } 110 111 print "done!"; 112?> 113--CLEAN-- 114<?php 115require dirname(__FILE__) . '/mysql_pdo_test.inc'; 116MySQLPDOTest::dropTestTable(); 117?> 118--EXPECTF-- 119done! 120