PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/hphp/test/zend/bad/ext/mysqli/tests/mysqli_last_insert_id.php

http://github.com/facebook/hiphop-php
PHP | 172 lines | 131 code | 30 blank | 11 comment | 46 complexity | f58324022bc9261662e94f5e7b354ecf MD5 | raw file
Possible License(s): LGPL-2.1, BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, MIT, LGPL-2.0, Apache-2.0
  1. <?php
  2. /*
  3. CAUTION: the insert_id() API call is not supposed to return
  4. the same value as a call to the LAST_INSERT_ID() SQL function.
  5. It is not necessarily a bug if API and SQL function return different
  6. values. Check the MySQL C API reference manual for details.
  7. */
  8. require_once("connect.inc");
  9. function get_sql_id($link) {
  10. if (!($res = $link->query("SELECT LAST_INSERT_ID() AS _id"))) {
  11. printf("[003] [%d] %s\n", $link->errno, $link->error);
  12. return NULL;
  13. }
  14. $row = $res->fetch_assoc();
  15. $res->close();
  16. return $row['_id'];
  17. }
  18. if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
  19. printf("[001] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
  20. $host, $user, $db, $port, $socket);
  21. if (!$link->query("DROP TABLE IF EXISTS test_mysqli_last_insert_id_table_1") ||
  22. !$link->query("CREATE TABLE test_mysqli_last_insert_id_table_1 (id INT auto_increment, label varchar(10) not null, PRIMARY KEY (id)) ENGINE=MyISAM") ||
  23. !$link->query("INSERT INTO test_mysqli_last_insert_id_table_1 (id, label) VALUES (null, 'a')")) {
  24. printf("[002] [%d] %s\n", $link->errno, $link->error);
  25. }
  26. $api_id = $link->insert_id;
  27. $sql_id = get_sql_id($link);
  28. printf("API: %d, SQL: %d\n", $api_id, $sql_id);
  29. if ($api_id < 1)
  30. printf("[004] Expecting id > 0 got %d, [%d] %s\n", $api_id, $link->errno, $link->error) ;
  31. if ($api_id != $sql_id)
  32. printf("[005] SQL id %d should be equal to API id %d\n", $sql_id, $api_id);
  33. // Not an INSERT, API value must become 0
  34. if (!($res = $link->query("SELECT 1 FROM test_mysqli_last_insert_id_table_2")))
  35. printf("[006] [%d] %s\n", $link->errno, $link->error);
  36. else
  37. $res->close();
  38. $api_id = $link->insert_id;
  39. $new_sql_id = get_sql_id($link);
  40. if (0 !== $api_id) {
  41. printf("[007] API id should have been reset to 0 because previous query was SELECT, got API %d, SQL %d\n",
  42. $api_id, $new_sql_id);
  43. }
  44. if ($new_sql_id != $sql_id) {
  45. printf("[008] The servers LAST_INSERT_ID() changed unexpectedly from %d to %d\n", $sql_id, $new_sql_id);
  46. }
  47. // Insert fails, LAST_INSERT_ID shall not change, API shall return 0
  48. if ($link->query("INSERT INTO test_mysqli_last_insert_id_table_1 (id, label) VALUES (null, null)")) {
  49. printf("[009] The INSERT did not fail as planned, [%d] %s\n", $link->errno, $link->error);
  50. }
  51. $api_id = $link->insert_id;
  52. $new_sql_id = get_sql_id($link);
  53. if (0 !== $api_id) {
  54. printf("[010] API id should have been reset to 0 because previous query was SELECT, got API %d, SQL %d\n",
  55. $api_id, $new_sql_id);
  56. }
  57. if ($new_sql_id != $sql_id) {
  58. printf("[011] The servers LAST_INSERT_ID() changed unexpectedly from %d to %d\n", $sql_id, $new_sql_id);
  59. }
  60. // Sequence counter pattern...
  61. if (!$link->query("UPDATE test_mysqli_last_insert_id_table_1 SET id=LAST_INSERT_ID(id+1)"))
  62. printf("[012] [%d] %s\n", $link->errno, $link->error);
  63. $api_id = $link->insert_id;
  64. $new_sql_id = get_sql_id($link);
  65. if ($api_id < 1)
  66. printf("[013] Expecting id > 0 got %d, [%d] %s\n", $api_id, $link->errno, $link->error) ;
  67. if ($api_id != $new_sql_id)
  68. printf("[014] SQL id %d should be equal to API id %d\n", $new_sql_id, $api_id);
  69. if ($sql_id == $new_sql_id)
  70. printf("[015] SQL id %d should have had changed, got %d\n", $sql_id, $new_sql_id);
  71. $sql_id = $new_sql_id;
  72. // Not an INSERT (after UPDATE), API value must become 0
  73. if (!$link->query("SET @myvar=1"))
  74. printf("[016] [%d] %s\n", $link->errno, $link->error);
  75. $api_id = $link->insert_id;
  76. $new_sql_id = get_sql_id($link);
  77. if (0 !== $api_id) {
  78. printf("[017] API id should have been reset to 0 because previous query was SET, got API %d, SQL %d\n",
  79. $api_id, $new_sql_id);
  80. }
  81. if ($new_sql_id != $sql_id) {
  82. printf("[018] The servers LAST_INSERT_ID() changed unexpectedly from %d to %d\n", $sql_id, $new_sql_id);
  83. }
  84. if (!$link->query("INSERT INTO test_mysqli_last_insert_id_table_1(id, label) VALUES (LAST_INSERT_ID(id + 1), 'b')"))
  85. printf("[019] [%d] %s\n", $link->errno, $link->error);
  86. $api_id = $link->insert_id;
  87. $sql_id = get_sql_id($link);
  88. if ($api_id != $sql_id)
  89. printf("[020] SQL id %d should be equal to API id %d\n", $sql_id, $api_id);
  90. if (!$link->query("INSERT INTO test_mysqli_last_insert_id_table_1(label) VALUES ('c')"))
  91. printf("[021] [%d] %s\n", $link->errno, $link->error);
  92. $api_id = $link->insert_id;
  93. $sql_id = get_sql_id($link);
  94. if ($api_id != $sql_id)
  95. printf("[022] SQL id %d should be equal to API id %d\n", $sql_id, $api_id);
  96. if (!($res = $link->query("SELECT id, label FROM test_mysqli_last_insert_id_table_1 ORDER BY id ASC")))
  97. printf("[023] [%d] %s\n", $link->errno, $link->error);
  98. printf("Dumping table contents before INSERT...SELECT experiments...\n");
  99. while ($row = $res->fetch_assoc()) {
  100. printf("id = %d, label = '%s'\n", $row['id'], $row['label']);
  101. }
  102. $res->close();
  103. if (!$link->query("INSERT INTO test_mysqli_last_insert_id_table_1(label) SELECT CONCAT(label, id) FROM test_mysqli_last_insert_id_table_1 ORDER BY id ASC"))
  104. printf("[024] [%d] %s\n", $link->errno, $link->error);
  105. $api_id = $link->insert_id;
  106. $sql_id = get_sql_id($link);
  107. if ($api_id != $sql_id)
  108. printf("[025] SQL id %d should be equal to API id %d\n", $sql_id, $api_id);
  109. if ($link->query("INSERT INTO test_mysqli_last_insert_id_table_1(id, label) SELECT id, CONCAT(label, id) FROM test_mysqli_last_insert_id_table_1 ORDER BY id ASC"))
  110. printf("[026] INSERT should have failed because of duplicate PK value, [%d] %s\n", $link->errno, $link->error);
  111. $api_id = $link->insert_id;
  112. $new_sql_id = get_sql_id($link);
  113. if (0 !== $api_id) {
  114. printf("[027] API id should have been reset to 0 because previous query failed, got API %d, SQL %d\n",
  115. $api_id, $new_sql_id);
  116. }
  117. if ($new_sql_id != $sql_id) {
  118. printf("[028] The servers LAST_INSERT_ID() changed unexpectedly from %d to %d\n", $sql_id, $new_sql_id);
  119. }
  120. /* API insert id will be 101 because of UPDATE, SQL unchanged */
  121. if (!$link->query(sprintf("INSERT INTO test_mysqli_last_insert_id_table_1(id, label) VALUES (%d, 'z') ON DUPLICATE KEY UPDATE id = 101", $sql_id) ))
  122. printf("[029] [%d] %s\n", $link->errno, $link->error);
  123. $api_id = $link->insert_id;
  124. $new_sql_id = get_sql_id($link);
  125. if ($api_id != 101)
  126. printf("[030] API id should be %d got %d\n", $sql_id, $api_id);
  127. if ($new_sql_id != $sql_id) {
  128. printf("[031] The servers LAST_INSERT_ID() changed unexpectedly from %d to %d\n", $sql_id, $new_sql_id);
  129. }
  130. if (!($res = $link->query("SELECT id, label FROM test_mysqli_last_insert_id_table_1 ORDER BY id ASC")))
  131. printf("[032] [%d] %s\n", $link->errno, $link->error);
  132. printf("Dumping table contents after INSERT...SELECT...\n");
  133. while ($row = $res->fetch_assoc()) {
  134. printf("id = %d, label = '%s'\n", $row['id'], $row['label']);
  135. }
  136. $res->close();
  137. print "done!";
  138. ?>
  139. <?php error_reporting(0); ?>
  140. <?php
  141. $test_table_name = 'test_mysqli_last_insert_id_table_1'; require_once("clean_table.inc");
  142. ?>