sfDatabase.class.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. * (c) 2004-2006 Sean Kerr <sean@code-box.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * sfDatabase is a base abstraction class that allows you to setup any type of
  12. * database connection via a configuration file.
  13. *
  14. * @package symfony
  15. * @subpackage database
  16. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  17. * @author Sean Kerr <sean@code-box.org>
  18. * @version SVN: $Id: sfDatabase.class.php 9086 2008-05-20 01:56:29Z Carl.Vondrick $
  19. */
  20. abstract class sfDatabase
  21. {
  22. protected
  23. $parameterHolder = null,
  24. $connection = null,
  25. $resource = null;
  26. /**
  27. * Class constructor.
  28. *
  29. * @see initialize()
  30. */
  31. public function __construct($parameters = array())
  32. {
  33. $this->initialize($parameters);
  34. }
  35. /**
  36. * Initializes this sfDatabase object.
  37. *
  38. * @param array $parameters An associative array of initialization parameters
  39. *
  40. * @return bool true, if initialization completes successfully, otherwise false
  41. *
  42. * @throws <b>sfInitializationException</b> If an error occurs while initializing this sfDatabase object
  43. */
  44. public function initialize($parameters = array())
  45. {
  46. $this->parameterHolder = new sfParameterHolder();
  47. $this->parameterHolder->add($parameters);
  48. }
  49. /**
  50. * Connects to the database.
  51. *
  52. * @throws <b>sfDatabaseException</b> If a connection could not be created
  53. */
  54. abstract function connect();
  55. /**
  56. * Retrieves the database connection associated with this sfDatabase implementation.
  57. *
  58. * When this is executed on a Database implementation that isn't an
  59. * abstraction layer, a copy of the resource will be returned.
  60. *
  61. * @return mixed A database connection
  62. *
  63. * @throws <b>sfDatabaseException</b> If a connection could not be retrieved
  64. */
  65. public function getConnection()
  66. {
  67. if (is_null($this->connection))
  68. {
  69. $this->connect();
  70. }
  71. return $this->connection;
  72. }
  73. /**
  74. * Retrieves a raw database resource associated with this sfDatabase implementation.
  75. *
  76. * @return mixed A database resource
  77. *
  78. * @throws <b>sfDatabaseException</b> If a resource could not be retrieved
  79. */
  80. public function getResource()
  81. {
  82. if (is_null($this->resource))
  83. {
  84. $this->connect();
  85. }
  86. return $this->resource;
  87. }
  88. /**
  89. * Gets the parameter holder for this object.
  90. *
  91. * @return sfParameterHolder A sfParameterHolder instance
  92. */
  93. public function getParameterHolder()
  94. {
  95. return $this->parameterHolder;
  96. }
  97. /**
  98. * Gets the parameter associated with the given key.
  99. *
  100. * This is a shortcut for:
  101. *
  102. * <code>$this->getParameterHolder()->get()</code>
  103. *
  104. * @param string $name The key name
  105. * @param string $default The default value
  106. *
  107. * @return string The value associated with the key
  108. *
  109. * @see sfParameterHolder
  110. */
  111. public function getParameter($name, $default = null)
  112. {
  113. return $this->parameterHolder->get($name, $default);
  114. }
  115. /**
  116. * Returns true if the given key exists in the parameter holder.
  117. *
  118. * This is a shortcut for:
  119. *
  120. * <code>$this->getParameterHolder()->has()</code>
  121. *
  122. * @param string $name The key name
  123. *
  124. * @return boolean true if the given key exists, false otherwise
  125. *
  126. * @see sfParameterHolder
  127. */
  128. public function hasParameter($name)
  129. {
  130. return $this->parameterHolder->has($name);
  131. }
  132. /**
  133. * Sets the value for the given key.
  134. *
  135. * This is a shortcut for:
  136. *
  137. * <code>$this->getParameterHolder()->set()</code>
  138. *
  139. * @param string $name The key name
  140. * @param string $value The value
  141. *
  142. * @see sfParameterHolder
  143. */
  144. public function setParameter($name, $value)
  145. {
  146. $this->parameterHolder->set($name, $value);
  147. }
  148. /**
  149. * Executes the shutdown procedure.
  150. *
  151. * @return void
  152. *
  153. * @throws <b>sfDatabaseException</b> If an error occurs while shutting down this database
  154. */
  155. abstract function shutdown();
  156. }