PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/PDB.php

http://github.com/digg/pdb
PHP | 154 lines | 41 code | 12 blank | 101 comment | 2 complexity | 966e71491a0deab71b6b5b8bb9f221f2 MD5 | raw file
  1. <?php
  2. /**
  3. * A simplistic wrapper for PDO
  4. *
  5. * PDB is a simplistic wrapper that adds helper functions to PDO. It was
  6. * creatd in the vain of DB and MDB2, but a pure PHP5/PDO implementation.
  7. *
  8. * PHP version 5
  9. *
  10. * Copyright (c) 2007, Digg, Inc.
  11. *
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright notice,
  18. * this list of conditions and the following disclaimer.
  19. * - Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * - Neither the name of the Digg, Inc. nor the names of its contributors
  23. * may be used to endorse or promote products derived from this software
  24. * without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  30. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. * @category DB
  39. * @package PDB
  40. * @author Joe Stump <joe@joestump.net>
  41. * @copyright 2007-2008 (c) Digg.com
  42. * @license http://tinyurl.com/42zef New BSD License
  43. * @version CVS: $Id:$
  44. * @link http://www.php.net/pdo
  45. * @link http://pear.php.net/package/PDB
  46. * @filesource
  47. */
  48. /**
  49. * Base PDB class
  50. *
  51. * @category DB
  52. * @package PDB
  53. * @author Joe Stump <joe@joestump.net>
  54. * @copyright 2007-2008 (c) Digg.com
  55. * @license http://tinyurl.com/42zef New BSD License
  56. * @version Release: @package_version@
  57. * @link http://pear.php.net/package/PDB
  58. */
  59. abstract class PDB
  60. {
  61. /**
  62. * All PDB-specific attributes have this bit set
  63. *
  64. * @var int
  65. */
  66. const PDB_ATTRS = 0xf0000;
  67. /**
  68. * Whether PDB should attempt to reconnect when we get a "2006
  69. * MySQL server has gone away" exception.
  70. *
  71. * @var int
  72. */
  73. const RECONNECT = 0xf0001;
  74. /**
  75. * Singleton connections
  76. *
  77. * @see PDB::singleton()
  78. * @var array $singletons
  79. */
  80. static protected $singletons = array();
  81. /**
  82. * Connect to a database
  83. *
  84. * @param string $dsn PDO DSN (e.g. mysql:host=127.0.0.1:dbname=foo)
  85. * @param string $username The DB username
  86. * @param string $password The DB password
  87. * @param array $options PDO options
  88. *
  89. * @access public
  90. * @throws {@link PDB_Exception} when unable to connect
  91. * @link http://us.php.net/manual/en/pdo.constants.php
  92. * @link http://us.php.net/manual/en/pdo.construct.php
  93. * @return object Instance of PDB driver
  94. */
  95. public static function connect($dsn,
  96. $username = null,
  97. $password = null,
  98. array $options = array())
  99. {
  100. list($type,) = explode(':', $dsn);
  101. $file = 'PDB/' . $type . '.php';
  102. include_once $file;
  103. $class = 'PDB_' . $type;
  104. if (!class_exists($class)) {
  105. throw new PDB_Exception('PDB class not found: ' . $class);
  106. }
  107. try {
  108. $instance = new $class($dsn, $username, $password, $options);
  109. } catch (PDOException $error) {
  110. throw new PDB_Exception($error);
  111. }
  112. return $instance;
  113. }
  114. /**
  115. * Create a singleton DB connection
  116. *
  117. * @param string $dsn PDO DSN (e.g. mysql:host=127.0.0.1:dbname=foo)
  118. * @param string $username The DB username
  119. * @param string $password The DB password
  120. * @param array $options PDO options
  121. *
  122. * @access public
  123. * @return object Instance of PDB driver
  124. * @throws {@link PDB_Exception} when unable to connect
  125. * @link http://us.php.net/manual/en/pdo.construct.php
  126. */
  127. static public function singleton($dsn,
  128. $username = null,
  129. $password = null,
  130. array $options = array())
  131. {
  132. $key = md5($dsn . $username . $password . serialize($options));
  133. if (!isset(self::$singletons[$key])) {
  134. self::$singletons[$key] = self::connect($dsn,
  135. $username,
  136. $password,
  137. $options);
  138. }
  139. return self::$singletons[$key];
  140. }
  141. }
  142. ?>