Advertisement
Guest User

Untitled

a guest
Mar 11th, 2017
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. /**
  5. *
  6. * @param string $connection_id
  7. *
  8. * @return PDO
  9. */
  10. function connection($connection_id) {
  11. $host = '';
  12. $database = '';
  13. $username = '';
  14. $password = '';
  15.  
  16. static $connections = [];
  17. if (!isset($connections[$connection_id])) {
  18. $pdo_options = [];
  19. $pdo_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
  20. $pdo_options[PDO::SQLSRV_ATTR_ENCODING] = PDO::SQLSRV_ENCODING_UTF8;
  21. $cnn = new \PDO("sqlsrv:server=$host;Database=$database", $username, $password, $pdo_options);
  22. $cnn->setAttribute(PDO::SQLSRV_ATTR_ENCODING, PDO::SQLSRV_ENCODING_UTF8);
  23. $connections[$connection_id] = $cnn;
  24.  
  25. }
  26. return $connections[$connection_id];
  27. }
  28.  
  29. /**
  30. * Summary of prepare
  31. *
  32. * @param mixed $connection
  33. * @param mixed $query
  34. * @return PDOStatement
  35. */
  36. function prepare($connection, $query, $buffered, $emulate_prepares) {
  37. $pdo_options = array();
  38. if ($emulate_prepares) {
  39. $pdo_options[PDO::ATTR_EMULATE_PREPARES] = TRUE;
  40. }
  41. $pdo_options[PDO::SQLSRV_ATTR_DIRECT_QUERY] = TRUE;
  42. $pdo_options[PDO::SQLSRV_ATTR_ENCODING] = PDO::SQLSRV_ENCODING_UTF8;
  43. if ($buffered) {
  44. $pdo_options[PDO::ATTR_CURSOR] = PDO::CURSOR_SCROLL;
  45. $pdo_options[PDO::SQLSRV_ATTR_CURSOR_SCROLL_TYPE] = PDO::SQLSRV_CURSOR_BUFFERED;
  46. }
  47. return $connection->prepare($query, $pdo_options);
  48. }
  49.  
  50. /**
  51. * Summary of execute
  52. *
  53. * @param PDO $connection
  54. * @param string $query
  55. *
  56. * @param PDOStatement;
  57. */
  58. function execute($connection,
  59. $query,
  60. array $args = [],
  61. $buffered = TRUE,
  62. $emulate_prepares = TRUE) {
  63. $st = prepare($connection, $query, $buffered, $emulate_prepares);
  64. $st->execute($args);
  65. return $st;
  66. }
  67.  
  68.  
  69. //*******************************************************
  70. // TEST BEGIN
  71. //*******************************************************
  72.  
  73. $connection = connection('default');
  74.  
  75. // Drop
  76. try {
  77. execute($connection, 'DROP TABLE TEST');
  78. }
  79. catch(\Exception $e) {}
  80.  
  81.  
  82. // Recreate
  83. execute($connection, "CREATE TABLE TEST([id] [int] IDENTITY(1,1) NOT NULL, [name] nvarchar(max))");
  84.  
  85. // No transaction yet, so this should be inmediately commited. OK.
  86. execute($connection, "INSERT INTO TEST(name) VALUES(:p0)", ['p0' => 'JUAN'], TRUE, FALSE);
  87.  
  88. // Start a transaction
  89. $connection->beginTransaction();
  90.  
  91. // Update the name within a transaction
  92. execute($connection, "UPDATE TEST SET NAME=:p0 WHERE NAME=:p1", ['p0' => 'DAVID','p1' => 'JUAN'], TRUE, FALSE);
  93.  
  94. $currentName = execute($connection, "SELECT TOP(1) NAME FROM TEST")->fetchColumn();
  95. echo "currentName is $currentName and should be DAVID<br/>";
  96.  
  97. // Let's throw a PDO exception that DOES NOT DOOM THE TRANSACTION
  98. try {
  99. execute($connection, "SELECT COUNT(*) FROM THIS_TABLE_DOES_NOT_EXIST");
  100. }
  101. catch (\Exception $e) {}
  102.  
  103. $currentName = execute($connection, "SELECT TOP(1) NAME FROM TEST")->fetchColumn();
  104. echo "currentName is $currentName and should be DAVID<br/>";
  105.  
  106. // Let's throw a PDO exception that DOES DOOM THE TRANSACTION
  107. try {
  108. execute($connection, "UPDATE TEST SET NAME=:p0 WHERE id=:p1", ['p0' => 'DAVID','p1' => 'SORRY FOR THE GRAMMAR, THIS AIN\'T NO NUMBER'], TRUE, FALSE);
  109. }
  110. catch (\Exception $e) {}
  111.  
  112. // THIS IS WHERE THE MAGIC HAPPENS. ¿¿¿WHAT HAPPENED TO THE TRANSACTION??
  113. // LOOK LIKE IT WAS ROLLED BACK INTERNALLY :( BUT THE FIRST EXCEPTION DIDN'T???
  114. $currentName = execute($connection, "SELECT TOP(1) NAME FROM TEST")->fetchColumn();
  115. echo "currentName is $currentName and should be DAVID<br/>";
  116.  
  117. // THE MOST ANNOYING PART IS NOT THAT IT WAS ROLLED BACK, BUT THE FACT THAT YOU
  118. // CAN STILL DO STUFF "AS IF" YOU STILL WERE INSIDE THE TRANSACTION
  119. execute($connection, "UPDATE TEST SET NAME=:p0 WHERE NAME=:p1", ['p0' => 'DAVID','p1' => 'JUAN'], TRUE, FALSE);
  120.  
  121. $currentName = execute($connection, "SELECT TOP(1) NAME FROM TEST")->fetchColumn();
  122. echo "currentName is $currentName and should be DAVID<br/>";
  123.  
  124. // THIS ROLLBACK DOES NOTHING. LOOKS LIKE THE PDO WENT BACK TO AMBIENT
  125. // TRANSACTION YET HAD INTERNALL TRACK OF THE PREVIOUS TRANSACTION.
  126. $connection->rollBack();
  127.  
  128. // NOT SURE WHAT TO EXPECT FROM THE PREVIOUS ROLLBACK
  129. $currentName = execute($connection, "SELECT TOP(1) NAME FROM TEST")->fetchColumn();
  130. echo "currentName is $currentName and should be JUAN???<br/>";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement