/src/storage/connection/base/AphrontDatabaseConnection.php

https://github.com/groupspaces/phabricator · PHP · 143 lines · 64 code · 26 blank · 53 comment · 9 complexity · 32fd27534ef58f7bed5abdb775c59292 MD5 · raw file

  1. <?php
  2. /*
  3. * Copyright 2012 Facebook, Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /**
  18. * @task xaction Transaction Management
  19. * @group storage
  20. */
  21. abstract class AphrontDatabaseConnection {
  22. private static $transactionStates = array();
  23. abstract public function getInsertID();
  24. abstract public function getAffectedRows();
  25. abstract public function selectAllResults();
  26. abstract public function executeRawQuery($raw_query);
  27. abstract protected function getTransactionKey();
  28. abstract public function escapeString($string);
  29. abstract public function escapeColumnName($string);
  30. abstract public function escapeMultilineComment($string);
  31. abstract public function escapeStringForLikeClause($string);
  32. public function queryData($pattern/*, $arg, $arg, ... */) {
  33. $args = func_get_args();
  34. array_unshift($args, $this);
  35. return call_user_func_array('queryfx_all', $args);
  36. }
  37. public function query($pattern/*, $arg, $arg, ... */) {
  38. $args = func_get_args();
  39. array_unshift($args, $this);
  40. return call_user_func_array('queryfx', $args);
  41. }
  42. /* -( Transaction Management )--------------------------------------------- */
  43. /**
  44. * Begin a transaction, or set a savepoint if the connection is already
  45. * transactional.
  46. *
  47. * @return this
  48. * @task xaction
  49. */
  50. public function openTransaction() {
  51. $state = $this->getTransactionState();
  52. $point = $state->getSavepointName();
  53. $depth = $state->increaseDepth();
  54. $new_transaction = ($depth == 1);
  55. if ($new_transaction) {
  56. $this->query('START TRANSACTION');
  57. } else {
  58. $this->query('SAVEPOINT '.$point);
  59. }
  60. return $this;
  61. }
  62. /**
  63. * Commit a transaction, or stage a savepoint for commit once the entire
  64. * transaction completes if inside a transaction stack.
  65. *
  66. * @return this
  67. * @task xaction
  68. */
  69. public function saveTransaction() {
  70. $state = $this->getTransactionState();
  71. $depth = $state->decreaseDepth();
  72. if ($depth == 0) {
  73. $this->query('COMMIT');
  74. }
  75. return $this;
  76. }
  77. /**
  78. * Rollback a transaction, or unstage the last savepoint if inside a
  79. * transaction stack.
  80. *
  81. * @return this
  82. */
  83. public function killTransaction() {
  84. $state = $this->getTransactionState();
  85. $depth = $state->decreaseDepth();
  86. if ($depth == 0) {
  87. $this->query('ROLLBACK');
  88. } else {
  89. $this->query('ROLLBACK TO SAVEPOINT '.$state->getSavepointName());
  90. }
  91. return $this;
  92. }
  93. /**
  94. * Returns true if the connection is transactional.
  95. *
  96. * @return bool True if the connection is currently transactional.
  97. * @task xaction
  98. */
  99. public function isInsideTransaction() {
  100. $state = $this->getTransactionState();
  101. return ($state->getDepth() > 0);
  102. }
  103. /**
  104. * Get the current @{class:AphrontDatabaseTransactionState} object, or create
  105. * one if none exists.
  106. *
  107. * @return AphrontDatabaseTransactionState Current transaction state.
  108. * @task xaction
  109. */
  110. protected function getTransactionState() {
  111. $key = $this->getTransactionKey();
  112. if (empty(self::$transactionStates[$key])) {
  113. self::$transactionStates[$key] = new AphrontDatabaseTransactionState();
  114. }
  115. return self::$transactionStates[$key];
  116. }
  117. }