mirror of
https://github.com/php/php-src.git
synced 2024-10-21 08:22:42 +00:00
be0793d2e7
pecl#12794, pecl#12401 # Running the tests: # (Note: Doesn't work currnetly on HEAD, see: # http://news.php.net/php.qa/64378) # # PDO_MYSQL_TEST_DSN - DSN # For example: mysql:dbname=test;host=localhost;port=3306 # # PDO_MYSQL_TEST_HOST - database host # PDO_MYSQL_TEST_DB - database (schema) name # PDO_MYSQL_TEST_SOCKET - database server socket # PDO_MYSQL_TEST_ENGINE - storage engine to use # PDO_MYSQL_TEST_USER - database user # PDO_MYSQL_TEST_PASS - database user password # PDO_MYSQL_TEST_CHARSET - database charset # # NOTE: if any of PDO_MYSQL_TEST_[HOST|DB|SOCKET|ENGINE|CHARSET] is # part of PDO_MYSQL_TEST_DSN, the values must match. That is, for example, # for PDO_MYSQL_TEST_DSN = mysql:dbname=test you MUST set PDO_MYSQL_TEST_DB=test.
115 lines
4.2 KiB
PHP
115 lines
4.2 KiB
PHP
--TEST--
|
|
MySQL PDO->lastInsertId()
|
|
--SKIPIF--
|
|
<?php
|
|
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'skipif.inc');
|
|
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
|
|
MySQLPDOTest::skip();
|
|
$db = MySQLPDOTest::factory();
|
|
?>
|
|
--FILE--
|
|
<?php
|
|
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
|
|
$db = MySQLPDOTest::factory();
|
|
|
|
try {
|
|
if ('0' !== ($tmp = $db->lastInsertId()))
|
|
printf("[001] No query has been run, lastInsertId() should return '0'/string got '%s'/%s\n",
|
|
var_export($tmp, true), gettype($tmp));
|
|
|
|
if ('0' !== ($tmp = $db->lastInsertId('sequence_name')))
|
|
printf("[002] MySQL does not support sequences, expecting '0'/string got '%s'/%s\n",
|
|
var_export($tmp, true), gettype($tmp));
|
|
|
|
$db->exec('DROP TABLE IF EXISTS test');
|
|
if ('0' !== ($tmp = $db->lastInsertId()))
|
|
printf("[003] Expecting '0'/string got '%s'/%s", var_export($tmp, true), gettype($tmp));
|
|
|
|
$db->exec(sprintf('CREATE TABLE test(id INT, col1 CHAR(10)) ENGINE=%s', PDO_MYSQL_TEST_ENGINE));
|
|
if ('0' !== ($tmp = $db->lastInsertId()))
|
|
printf("[004] Expecting '0'/string got '%s'/%s", var_export($tmp, true), gettype($tmp));
|
|
|
|
$stmt = $db->query('SELECT id FROM test LIMIT 1');
|
|
if ('0' !== ($tmp = $db->lastInsertId()))
|
|
printf("[005] Expecting '0'/string got '%s'/%s", var_export($tmp, true), gettype($tmp));
|
|
|
|
// no auto increment column
|
|
$db->exec('INSERT INTO test(id, col1) VALUES (100, "a")');
|
|
if ('0' !== ($tmp = $db->lastInsertId()))
|
|
printf("[006] Expecting '0'/string got '%s'/%s", var_export($tmp, true), gettype($tmp));
|
|
|
|
$db->exec('ALTER TABLE test MODIFY id INT AUTO_INCREMENT PRIMARY KEY');
|
|
if ('0' !== ($tmp = $db->lastInsertId()))
|
|
printf("[006] Expecting '0'/string got '%s'/%s", var_export($tmp, true), gettype($tmp));
|
|
|
|
// duplicate key
|
|
@$db->exec('INSERT INTO test(id, col1) VALUES (100, "a")');
|
|
if ('0' !== ($tmp = $db->lastInsertId()))
|
|
printf("[007] Expecting '0'/string got '%s'/%s", var_export($tmp, true), gettype($tmp));
|
|
|
|
$db->exec('INSERT INTO test(id, col1) VALUES (101, "b")');
|
|
if ('101' !== ($tmp = $db->lastInsertId()))
|
|
printf("[008] Expecting '0'/string got '%s'/%s", var_export($tmp, true), gettype($tmp));
|
|
|
|
$db->exec('ALTER TABLE test MODIFY col1 CHAR(10) UNIQUE');
|
|
// replace = delete + insert -> new auto increment value
|
|
$db->exec('REPLACE INTO test(col1) VALUES ("b")');
|
|
$next_id = (int)$db->lastInsertId();
|
|
|
|
if ($next_id <= 101)
|
|
printf("[009] Expecting at least 102, got %d\n",$next_id);
|
|
|
|
$stmt = $db->query('SELECT LAST_INSERT_ID() as _last_id');
|
|
$row = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
$last_id = $row['_last_id'];
|
|
if ($next_id != $last_id) {
|
|
printf("[010] LAST_INSERT_ID() = %d and lastInserId() = %d differ\n",
|
|
$last_id, $next_id);
|
|
}
|
|
|
|
$db->exec('INSERT INTO test(col1) VALUES ("c"), ("d"), ("e")');
|
|
$next_id = (int)$db->lastInsertId();
|
|
if ($next_id <= $last_id)
|
|
printf("[011] Expecting at least %d, got %d\n", $last_id + 1, $next_id);
|
|
|
|
// warnings are unhandy, lets go for exceptions for a second
|
|
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
try {
|
|
$ignore_exception = true;
|
|
$db->exec('LOCK TABLE test WRITE');
|
|
$ignore_exception = false;
|
|
|
|
if (MySQLPDOTest::getServerVersion($db) >= 50000) {
|
|
$stmt = $db->query('SELECT @@auto_increment_increment AS inc');
|
|
$row = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
$inc = $row['inc'];
|
|
} else {
|
|
$inc = 1;
|
|
}
|
|
|
|
$stmt = $db->query('SELECT LAST_INSERT_ID() as _last_id');
|
|
$row = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
$last_id = $row['_last_id'];
|
|
|
|
$db->exec('INSERT INTO test(col1) VALUES ("z")');
|
|
$next_id = (int)$db->lastInsertId();
|
|
if ($next_id < ($last_id + $inc))
|
|
printf("[012] Expecting at least %d, got %d\n", $last_id + $inc, $next_id);
|
|
|
|
} catch (PDOException $e) {
|
|
if (!$ignore_exception)
|
|
printf("[014] %s, [%s} %s\n", $e->getMessage(), $db->errorCode(), implode(' ', $db->errorInfo()));
|
|
}
|
|
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
|
|
@$db->exec('UNLOCK TABLE test');
|
|
|
|
} catch (PDOException $e) {
|
|
printf("[001] %s [%s] %s\n",
|
|
$e->getMessage(), $db->errorCode(), implode(' ', $db->errorInfo()));
|
|
}
|
|
|
|
$db->exec(sprintf('DROP TABLE IF EXISTS test'));
|
|
print "done!";
|
|
--EXPECTF--
|
|
done!
|