sfPDODatabase.class.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. * sfPDODatabase provides connectivity for the PDO database abstraction layer.
  12. *
  13. * @package symfony
  14. * @subpackage database
  15. * @author Daniel Swarbrick (daniel@pressure.net.nz)
  16. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  17. * @author Sean Kerr <sean@code-box.org>
  18. * @author Dustin Whittle <dustin.whittle@symfony-project.com>
  19. * @version SVN: $Id: sfPDODatabase.class.php 9372 2008-05-29 18:35:54Z dwhittle $
  20. */
  21. class sfPDODatabase extends sfDatabase
  22. {
  23. /**
  24. * Connects to the database.
  25. *
  26. * @throws <b>sfDatabaseException</b> If a connection could not be created
  27. */
  28. public function connect()
  29. {
  30. // determine how to get our parameters
  31. $method = $this->getParameter('method', 'dsn');
  32. // get parameters
  33. switch ($method)
  34. {
  35. case 'dsn':
  36. $dsn = $this->getParameter('dsn');
  37. if ($dsn == null)
  38. {
  39. // missing required dsn parameter
  40. throw new sfDatabaseException('Database configuration specifies method "dsn", but is missing dsn parameter.');
  41. }
  42. break;
  43. }
  44. try
  45. {
  46. $pdo_class = $this->getParameter('class', 'PDO');
  47. $username = $this->getParameter('username');
  48. $password = $this->getParameter('password');
  49. $persistent = $this->getParameter('persistent');
  50. $options = ($persistent) ? array(PDO::ATTR_PERSISTENT => true) : array();
  51. $this->connection = new $pdo_class($dsn, $username, $password, $options);
  52. }
  53. catch (PDOException $e)
  54. {
  55. throw new sfDatabaseException($e->getMessage());
  56. }
  57. // lets generate exceptions instead of silent failures
  58. if(sfConfig::get('sf_debug'))
  59. {
  60. $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  61. }
  62. else
  63. {
  64. $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
  65. }
  66. // compatability
  67. $compatability = $this->getParameter('compat');
  68. if($compatability)
  69. {
  70. $this->connection->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL);
  71. }
  72. // nulls
  73. $nulls = $this->getParameter('nulls');
  74. if($nulls)
  75. {
  76. $this->connection->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_EMPTY_STRING);
  77. }
  78. // auto commit
  79. $autocommit = $this->getParameter('autocommit');
  80. if($autocommit)
  81. {
  82. $this->connection->setAttribute(PDO::ATTR_AUTOCOMMIT, true);
  83. }
  84. $this->resource = $this->connection;
  85. }
  86. /**
  87. * Execute the shutdown procedure.
  88. *
  89. * @return void
  90. */
  91. public function shutdown ()
  92. {
  93. if ($this->connection !== null)
  94. {
  95. @$this->connection = null;
  96. }
  97. }
  98. /**
  99. * Magic method for calling PDO directly via sfPDODatabase
  100. *
  101. * @param string $method
  102. * @param array $arguments
  103. * @return mixed
  104. */
  105. public function __call($method, $arguments)
  106. {
  107. return $this->getConnection()->$method($arguments);
  108. }
  109. }