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

Debug toolbar