CreoleTask.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. /*
  3. * $Id: CreoleTask.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 Creole 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 CreoleTask extends Task {
  35. /**
  36. * Used for caching loaders / driver. This is to avoid
  37. * getting an OutOfMemoryError when calling this task
  38. * multiple times in a row.
  39. *
  40. * NOT IMPLEMENTED YET
  41. */
  42. private static $loaderMap = array();
  43. private $caching = true;
  44. /**
  45. * Autocommit flag. Default value is false
  46. */
  47. private $autocommit = false;
  48. /**
  49. * [optional] Classpath to Creole driver to use.
  50. * @param string
  51. */
  52. private $driver;
  53. /**
  54. * DB url.
  55. */
  56. private $url;
  57. /**
  58. * User name.
  59. */
  60. private $userId;
  61. /**
  62. * Password
  63. */
  64. private $password;
  65. /**
  66. * RDBMS Product needed for this SQL.
  67. **/
  68. private $rdbms;
  69. /**
  70. * Initialize CreoleTask.
  71. * This method includes any necessary Creole libraries and triggers
  72. * appropriate error if they cannot be found. This is not done in header
  73. * because we may want this class to be loaded w/o triggering an error.
  74. */
  75. function init() {
  76. include_once 'creole/Creole.php';
  77. if (!class_exists('Creole')) {
  78. throw new Exception("Creole task depends on Creole classes being on include_path. (i.e. include of 'creole/Creole.php' failed.)");
  79. }
  80. }
  81. /**
  82. * Caching loaders / driver. This is to avoid
  83. * getting an OutOfMemoryError when calling this task
  84. * multiple times in a row; default: true
  85. * @param $enable
  86. */
  87. public function setCaching($enable) {
  88. $this->caching = $enable;
  89. }
  90. /**
  91. * Sets the database connection URL; required.
  92. * @param url The url to set
  93. */
  94. public function setUrl($url) {
  95. $this->url = $url;
  96. }
  97. /**
  98. * Set the Creole driver to be used.
  99. *
  100. * @param string $driver driver class name
  101. */
  102. public function setDriver($driver)
  103. {
  104. $this->driver = $driver;
  105. }
  106. /**
  107. * Sets the password; required.
  108. * @param password The password to set
  109. */
  110. public function setPassword($password) {
  111. $this->password = $password;
  112. }
  113. /**
  114. * Auto commit flag for database connection;
  115. * optional, default false.
  116. * @param autocommit The autocommit to set
  117. */
  118. public function setAutocommit($autocommit) {
  119. $this->autocommit = $autocommit;
  120. }
  121. /**
  122. * Sets the version string, execute task only if
  123. * rdbms version match; optional.
  124. * @param version The version to set
  125. */
  126. public function setVersion($version) {
  127. $this->version = $version;
  128. }
  129. protected function getLoaderMap() {
  130. return self::$loaderMap;
  131. }
  132. /**
  133. * Creates a new Connection as using the driver, url, userid and password specified.
  134. * The calling method is responsible for closing the connection.
  135. * @return Connection the newly created connection.
  136. * @throws BuildException if the UserId/Password/Url is not set or there is no suitable driver or the driver fails to load.
  137. */
  138. protected function getConnection() {
  139. if ($this->url === null) {
  140. throw new BuildException("Url attribute must be set!", $this->location);
  141. }
  142. try {
  143. $this->log("Connecting to " . $this->getUrl(), Project::MSG_VERBOSE);
  144. $info = new Properties();
  145. $dsn = Creole::parseDSN($this->url);
  146. if (!isset($dsn["username"]) && $this->userId === null) {
  147. throw new BuildException("Username must be in URL or userid attribute must be set.", $this->location);
  148. }
  149. if ($this->userId) {
  150. $dsn["username"] = $this->getUserId();
  151. }
  152. if ($this->password) {
  153. $dsn["password"] = $this->getPassword();
  154. }
  155. if ($this->driver) {
  156. Creole::registerDriver($dsn['phptype'], $this->driver);
  157. }
  158. $conn = Creole::getConnection($dsn);
  159. $conn->setAutoCommit($this->autocommit);
  160. return $conn;
  161. } catch (SQLException $e) {
  162. throw new BuildException($e->getMessage(), $this->location);
  163. }
  164. }
  165. public function isCaching($value) {
  166. $this->caching = $value;
  167. }
  168. /**
  169. * Gets the autocommit.
  170. * @return Returns a boolean
  171. */
  172. public function isAutocommit() {
  173. return $this->autocommit;
  174. }
  175. /**
  176. * Gets the url.
  177. * @return Returns a String
  178. */
  179. public function getUrl() {
  180. return $this->url;
  181. }
  182. /**
  183. * Gets the userId.
  184. * @return Returns a String
  185. */
  186. public function getUserId() {
  187. return $this->userId;
  188. }
  189. /**
  190. * Set the user name for the connection; required.
  191. * @param userId The userId to set
  192. */
  193. public function setUserid($userId) {
  194. $this->userId = $userId;
  195. }
  196. /**
  197. * Gets the password.
  198. * @return Returns a String
  199. */
  200. public function getPassword() {
  201. return $this->password;
  202. }
  203. }