PageRenderTime 53ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/pear/PDB/tags/0.0.3/PDB.php

http://digg.googlecode.com/
PHP | 139 lines | 39 code | 10 blank | 90 comment | 2 complexity | b23a1f92659fd4fbe96394be80412e60 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. * Singleton connections
  63. *
  64. * @see PDB::singleton()
  65. * @var array $singletons
  66. */
  67. static protected $singletons = array();
  68. /**
  69. * Connect to a database
  70. *
  71. * @param string $dsn PDO DSN (e.g. mysql:host=127.0.0.1:dbname=foo)
  72. * @param string $username The DB username
  73. * @param string $password The DB password
  74. * @param array $options PDO options
  75. *
  76. * @access public
  77. * @throws {@link PDB_Exception} when unable to connect
  78. * @link http://us.php.net/manual/en/pdo.constants.php
  79. * @link http://us.php.net/manual/en/pdo.construct.php
  80. * @return object Instance of PDB driver
  81. */
  82. public static function connect($dsn,
  83. $username = null,
  84. $password = null,
  85. array $options = array())
  86. {
  87. list($type,) = explode(':', $dsn);
  88. $file = 'PDB/' . $type . '.php';
  89. include_once $file;
  90. $class = 'PDB_' . $type;
  91. if (!class_exists($class)) {
  92. throw new PDB_Exception('PDB class not found: ' . $class);
  93. }
  94. try {
  95. $instance = new $class($dsn, $username, $password, $options);
  96. } catch (PDOException $error) {
  97. throw new PDB_Exception($error);
  98. }
  99. return $instance;
  100. }
  101. /**
  102. * Create a singleton DB connection
  103. *
  104. * @param string $dsn PDO DSN (e.g. mysql:host=127.0.0.1:dbname=foo)
  105. * @param string $username The DB username
  106. * @param string $password The DB password
  107. * @param array $options PDO options
  108. *
  109. * @access public
  110. * @return object Instance of PDB driver
  111. * @throws {@link PDB_Exception} when unable to connect
  112. * @link http://us.php.net/manual/en/pdo.construct.php
  113. */
  114. static public function singleton($dsn,
  115. $username = null,
  116. $password = null,
  117. array $options = array())
  118. {
  119. $key = md5($dsn . $username . $password . serialize($options));
  120. if (!isset(self::$singletons[$key])) {
  121. self::$singletons[$key] = self::connect($dsn,
  122. $username,
  123. $password,
  124. $options);
  125. }
  126. return self::$singletons[$key];
  127. }
  128. }
  129. ?>