PDOTask.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. /*
  3. * $Id: PDOTask.php 905 2010-10-05 16:28:03Z mrook $
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information please see
  19. * <http://phing.info>.
  20. */
  21. require_once 'phing/Task.php';
  22. include_once 'phing/types/Reference.php';
  23. /**
  24. * Handles PDO configuration needed by SQL type tasks.
  25. *
  26. * @author Hans Lellelid <hans@xmpl.org> (Phing)
  27. * @author Nick Chalko <nick@chalko.com> (Ant)
  28. * @author Jeff Martin <jeff@custommonkey.org> (Ant)
  29. * @author Michael McCallum <gholam@xtra.co.nz> (Ant)
  30. * @author Tim Stephenson <tim.stephenson@sybase.com> (Ant)
  31. * @version $Revision: 905 $
  32. * @package phing.tasks.system
  33. */
  34. abstract class PDOTask extends Task {
  35. private $caching = true;
  36. /**
  37. * Autocommit flag. Default value is false
  38. */
  39. private $autocommit = false;
  40. /**
  41. * DB url.
  42. */
  43. private $url;
  44. /**
  45. * User name.
  46. */
  47. private $userId;
  48. /**
  49. * Password
  50. */
  51. private $password;
  52. /**
  53. * RDBMS Product needed for this SQL.
  54. **/
  55. private $rdbms;
  56. /**
  57. * Initialize CreoleTask.
  58. * This method includes any necessary Creole libraries and triggers
  59. * appropriate error if they cannot be found. This is not done in header
  60. * because we may want this class to be loaded w/o triggering an error.
  61. */
  62. function init() {
  63. if (!class_exists('PDO')) {
  64. throw new Exception("PDOTask depends on PDO feature being included in PHP.");
  65. }
  66. }
  67. /**
  68. * Caching loaders / driver. This is to avoid
  69. * getting an OutOfMemoryError when calling this task
  70. * multiple times in a row; default: true
  71. * @param $enable
  72. */
  73. public function setCaching($enable) {
  74. $this->caching = $enable;
  75. }
  76. /**
  77. * Sets the database connection URL; required.
  78. * @param url The url to set
  79. */
  80. public function setUrl($url) {
  81. $this->url = $url;
  82. }
  83. /**
  84. * Sets the password; required.
  85. * @param password The password to set
  86. */
  87. public function setPassword($password) {
  88. $this->password = $password;
  89. }
  90. /**
  91. * Auto commit flag for database connection;
  92. * optional, default false.
  93. * @param autocommit The autocommit to set
  94. */
  95. public function setAutocommit($autocommit) {
  96. $this->autocommit = $autocommit;
  97. }
  98. /**
  99. * Sets the version string, execute task only if
  100. * rdbms version match; optional.
  101. * @param version The version to set
  102. */
  103. public function setVersion($version) {
  104. $this->version = $version;
  105. }
  106. protected function getLoaderMap() {
  107. return self::$loaderMap;
  108. }
  109. /**
  110. * Creates a new Connection as using the driver, url, userid and password specified.
  111. * The calling method is responsible for closing the connection.
  112. * @return Connection the newly created connection.
  113. * @throws BuildException if the UserId/Password/Url is not set or there is no suitable driver or the driver fails to load.
  114. */
  115. protected function getConnection() {
  116. if ($this->url === null) {
  117. throw new BuildException("Url attribute must be set!", $this->location);
  118. }
  119. try {
  120. $this->log("Connecting to " . $this->getUrl(), Project::MSG_VERBOSE);
  121. $user = null;
  122. $pass = null;
  123. if ($this->userId) {
  124. $user = $this->getUserId();
  125. }
  126. if ($this->password) {
  127. $pass = $this->getPassword();
  128. }
  129. $conn = new PDO($this->getUrl(), $user, $pass);
  130. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  131. if ($this->autocommit) {
  132. try {
  133. $conn->setAttribute(PDO::ATTR_AUTOCOMMIT, $this->autocommit);
  134. } catch (PDOException $pe) {
  135. $this->log("Unable to enable auto-commit for this database: " . $pe->getMessage(), Project::MSG_WARN);
  136. }
  137. }
  138. return $conn;
  139. } catch (SQLException $e) {
  140. throw new BuildException($e->getMessage(), $this->location);
  141. }
  142. }
  143. public function isCaching($value) {
  144. $this->caching = $value;
  145. }
  146. /**
  147. * Gets the autocommit.
  148. * @return Returns a boolean
  149. */
  150. public function isAutocommit() {
  151. return $this->autocommit;
  152. }
  153. /**
  154. * Gets the url.
  155. * @return Returns a String
  156. */
  157. public function getUrl() {
  158. return $this->url;
  159. }
  160. /**
  161. * Gets the userId.
  162. * @return Returns a String
  163. */
  164. public function getUserId() {
  165. return $this->userId;
  166. }
  167. /**
  168. * Set the user name for the connection; required.
  169. * @param userId The userId to set
  170. */
  171. public function setUserid($userId) {
  172. $this->userId = $userId;
  173. }
  174. /**
  175. * Gets the password.
  176. * @return Returns a String
  177. */
  178. public function getPassword() {
  179. return $this->password;
  180. }
  181. }