OptionsDB.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. # i think this class should go somewhere in a common PEAR-place,
  3. # but since it is not very fancy to crowd the PEAR-namespace too much i dont know where to put it yet :-(
  4. //
  5. // +----------------------------------------------------------------------+
  6. // | PHP Version 4 |
  7. // +----------------------------------------------------------------------+
  8. // | Copyright (c) 1997-2003 The PHP Group |
  9. // +----------------------------------------------------------------------+
  10. // | This source file is subject to version 2.02 of the PHP license, |
  11. // | that is bundled with this package in the file LICENSE, and is |
  12. // | available at through the world-wide-web at |
  13. // | http://www.php.net/license/2_02.txt. |
  14. // | If you did not receive a copy of the PHP license and are unable to |
  15. // | obtain it through the world-wide-web, please send a note to |
  16. // | license@php.net so we can mail you a copy immediately. |
  17. // +----------------------------------------------------------------------+
  18. // | Authors: Wolfram Kriesing <wolfram@kriesing.de> |
  19. // +----------------------------------------------------------------------+
  20. //
  21. // $Id: OptionsDB.php,v 1.4 2003/01/04 11:56:27 mj Exp $
  22. require_once('Tree/Options.php');
  23. /**
  24. * this class additionally retreives a DB connection and saves it
  25. * in the property "dbh"
  26. *
  27. * @package Tree
  28. * @access public
  29. * @author Wolfram Kriesing <wolfram@kriesing.de>
  30. *
  31. */
  32. class Tree_OptionsDB extends Tree_Options
  33. {
  34. /**
  35. * @var object
  36. */
  37. var $dbh;
  38. /**
  39. * this constructor sets the options, since i normally need this and
  40. * in case the constructor doesnt need to do anymore i already have it done :-)
  41. *
  42. * @version 02/01/08
  43. * @access public
  44. * @author Wolfram Kriesing <wolfram@kriesing.de>
  45. * @param boolean true if loggedIn
  46. */
  47. function Tree_OptionsDB( $dsn , $options=array() )
  48. {
  49. $res = $this->_connectDB( $dsn );
  50. if( !PEAR::isError($res) )
  51. {
  52. $this->dbh->setFetchmode(DB_FETCHMODE_ASSOC);
  53. }
  54. else
  55. {
  56. return $res;
  57. }
  58. $this->Tree_Options( $options ); // do options afterwards since it overrules
  59. }
  60. /**
  61. * Connect to database by using the given DSN string
  62. *
  63. * @author copied from PEAR::Auth, Martin Jansen, slightly modified
  64. * @access private
  65. * @param string DSN string
  66. * @return mixed Object on error, otherwise bool
  67. */
  68. function _connectDB( $dsn )
  69. {
  70. // only include the db if one really wants to connect
  71. require_once('DB.php');
  72. if (is_string($dsn) || is_array($dsn) )
  73. {
  74. // put the dsn parameters in an array
  75. // DB would be confused with an additional URL-queries, like ?table=...
  76. // so we do it before connecting to the DB
  77. if( is_string($dsn) )
  78. $dsn = DB::parseDSN( $dsn );
  79. $this->dbh = DB::Connect($dsn);
  80. }
  81. else
  82. {
  83. if(get_parent_class($dsn) == "db_common")
  84. {
  85. $this->dbh = $dsn;
  86. }
  87. else
  88. {
  89. if (is_object($dsn) && DB::isError($dsn))
  90. {
  91. return new DB_Error($dsn->code, PEAR_ERROR_DIE);
  92. }
  93. else
  94. {
  95. return new PEAR_Error("The given dsn was not valid in file " . __FILE__ . " at line " . __LINE__,
  96. 41,
  97. PEAR_ERROR_RETURN,
  98. null,
  99. null
  100. );
  101. }
  102. }
  103. }
  104. if (DB::isError($this->dbh))
  105. return new DB_Error($this->dbh->code, PEAR_ERROR_DIE);
  106. return true;
  107. }
  108. } // end of class
  109. ?>