DBPool.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2006-2007 by Konstantin V. Arkhipov                     *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 /* $Id: DBPool.class.php 4687 2007-12-09 18:57:18Z voxus $ */
00012 
00018     final class DBPool extends Singleton implements Instantiatable
00019     {
00020         private $default = null;
00021         
00022         private $pool = array();
00023         
00027         public static function me()
00028         {
00029             return Singleton::getInstance(__CLASS__);
00030         }
00031         
00035         public static function getByDao(GenericDAO $dao)
00036         {
00037             return self::me()->getLink($dao->getLinkName());
00038         }
00039         
00043         public function setDefault(DB $db)
00044         {
00045             $this->default = $db;
00046             
00047             return $this;
00048         }
00049         
00053         public function dropDefault()
00054         {
00055             $this->default = null;
00056             
00057             return $this;
00058         }
00059         
00064         public function addLink($name, DB $db)
00065         {
00066             if (isset($this->pool[$name]))
00067                 throw new WrongArgumentException(
00068                     "already have '{$name}' link"
00069                 );
00070             
00071             $this->pool[$name] = $db;
00072             
00073             return $this;
00074         }
00075         
00080         public function dropLink($name)
00081         {
00082             if (!isset($this->pool[$name]))
00083                 throw new MissingElementException(
00084                     "link '{$name}' not found"
00085                 );
00086             
00087             unset($this->pool[$name]);
00088             
00089             return $this;
00090         }
00091         
00096         public function getLink($name = null)
00097         {
00098             $link = null;
00099             
00100             // single-DB project
00101             if (!$name) {
00102                 if (!$this->default)
00103                     throw new MissingElementException(
00104                         'i have no default link and requested link name is null'
00105                     );
00106                 
00107                 $link = $this->default;
00108             } elseif (isset($this->pool[$name]))
00109                 $link = $this->pool[$name];
00110             
00111             if ($link) {
00112                 if (!$link->isConnected())
00113                     $link->connect();
00114                 
00115                 return $link;
00116             }
00117             
00118             throw new MissingElementException(
00119                 "can't find link with '{$name}' name"
00120             );
00121         }
00122         
00126         public function shutdown()
00127         {
00128             $this->disconnect();
00129             
00130             $this->default = null;
00131             $this->pool = array();
00132             
00133             return $this;
00134         }
00135         
00139         public function disconnect()
00140         {
00141             if ($this->default)
00142                 $this->default->disconnect();
00143             
00144             foreach ($this->pool as $db)
00145                 $db->disconnect();
00146             
00147             return $this;
00148         }
00149     }
00150 ?>

Generated on Sun Dec 9 21:56:23 2007 for onPHP by  doxygen 1.5.4