Source for file DataBaseManager.php

Documentation is available at DataBaseManager.php

  1. <?php
  2. /*
  3. * This file is part of Sylar.
  4. *
  5. * Sylar is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * Sylar is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public License
  16. * along with Sylar. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * @copyright Copyright Sylar Development Team
  19. * @license http://www.gnu.org/licenses/ GNU Public License V2.0
  20. * @see https://launchpad.net/sylar/
  21. * @see http://www.giano-solutions.com
  22. */
  23.  
  24. import('sylar.common.db.DataBaseConfiguration');
  25.  
  26.  
  27. /**
  28. * DataBases manager
  29. * This class manage all databases supported by Sylar
  30. *
  31. * @package Sylar
  32. * @version 1.0
  33. * @since 18/mar/08
  34. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  35. * @copyright Sylar Development Team
  36. */
  37. class Sylar_DataBaseManager{
  38. /** List of available DB Driver */
  39.  
  40. private $aSupportedDbList;
  41.  
  42. function __construct(){
  43. $this->fillDbList();
  44. }
  45. function __destruct() {
  46. # nothing to do
  47. }
  48. /**
  49. * Check for installed driver
  50. * It controls if the request driver is avaiable in Sylar Framework
  51. *
  52. * @param string $sDriverName the driver name
  53. * @return boolean
  54. */
  55. public function driverIsInstalled($sDriverName){
  56. if(!is_array($this->aSupportedDbList) || !array_key_exists($sDriverName, $this->aSupportedDbList)){
  57. throw new ExceptionInSylar("Request DataBase Driver ".$sDriverName." did not exist in Sylar.", 1 );
  58. return false;
  59. }
  60. return true;
  61. }
  62. /**
  63. * Return the Db Object
  64. * It return the DataBase Driver Interfaces Type with the right implementation.
  65. * The implementation dipends on Driver defined in the Configuration
  66. *
  67. * @return Sylar_DataBaseDriver
  68. * @param Sylar_DataBaseConfiguration $oDbConfig
  69. */
  70. public function driverDispatcher(Sylar_DataBaseConfiguration $oDbConfig){
  71. try{
  72. if($oDbConfig->checkConfiguration()){
  73. // DataBase Driver Dinamic Loading
  74. //
  75. switch ($oDbConfig->getDriver()){
  76. case "mysql":
  77. import("sylar.common.db.mysql.MysqlDriver");
  78. return new Sylar_MysqlDriver();
  79. break;
  80. case "oracle":
  81. import("sylar.common.db.mysql.OracleDriver");
  82. // TODO Oracle and other Db Driver
  83. break;
  84. }
  85. }
  86. }catch (ExceptionInSylar $ex){
  87. throw $ex;
  88. return null;
  89. }
  90. }
  91. /**
  92. * Get Specified Db Class Path
  93. * It return the specified DataBase Driver classpath path for use it in the
  94. * import function. It return something like:
  95. * sylar.common.db.mysql
  96. * sylar.common.db.oracle
  97. * It depend on Driver defined in the configuration
  98. *
  99. * @return string
  100. * @param Sylar_DataBaseConfiguration $oDbConfig
  101. */
  102. public function getDriverClassPath(Sylar_DataBaseConfiguration $oDbConfig){
  103. try{
  104. if($oDbConfig->checkConfiguration()){
  105. return "sylar.common.db.".$oDbConfig->getDriver();
  106. }
  107. }catch (ExceptionInSylar $ex){
  108. throw $ex;
  109. return null;
  110. }
  111. }
  112. /**
  113. * Default Db Config
  114. * It return the object with the default Database configuration, if doesn't exists return null
  115. *
  116. * @return Sylar_DataBaseConfiguration
  117. */
  118. public static function getDefaultDbConfiguration(){
  119. global $SYLAR_DEFAULT_DB_CONFIG;
  120. if($SYLAR_DEFAULT_DB_CONFIG){
  121. return $SYLAR_DEFAULT_DB_CONFIG;
  122. }else{
  123. throw new ExceptionInSylar("No default Db configuration defined for Sylar.", 1 );
  124. return null;
  125. }
  126. }
  127.  
  128. /**
  129. * Fill the Database List
  130. * Fill the list of Sylar supported database.
  131. * In the future this method will be able to load the list from a configuration file.
  132. *
  133. * @return void
  134. */
  135. private function fillDbList(){
  136. $this->aSupportedDbList['mysql'] = new Sylar_DataBaseManagerItem("MySQL", "mysql", "5", "MySQL Generic Driver for 5.* version");
  137. $this->aSupportedDbList['oracle'] = new Sylar_DataBaseManagerItem("Oracle 9i", "oracle", "9i", "Oracle Generic Driver for 9.i version");
  138. }
  139. }
  140.  
  141.  
  142. /**
  143. * Tit File
  144. *
  145. * Desc File
  146. *
  147. * @package Sylar
  148. * @version 1.0
  149. * @since 18/mar/08
  150. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  151. * @copyright Sylar Development Team
  152. */
  153. class Sylar_DataBaseManagerItem{
  154. private $sName;
  155. private $sDriver;
  156. private $sVersion;
  157. private $sDescription;
  158. function __construct($sName, $sDriver, $sVersion, $sDescription=null){
  159. $this->setName($sName);
  160. $this->setDriver($sDriver);
  161. $this->setVersion($sVersion);
  162. $this->setDescription($sDescription);
  163. }
  164. function __destruct() {
  165. # nothing to do
  166. }
  167. // Setter and Getter
  168. public function setName($sName){
  169. $this->sName = $sName;
  170. }
  171. public function setDriver($sDrive){
  172. $this->sDriver = $sDrive;
  173. }
  174. public function setVersion($sVersion){
  175. $this->sVersion = $sVersion;
  176. }
  177. public function setDescription($sDescription){
  178. $this->sDescription = $sDescription;
  179. }
  180. public function getName(){
  181. return $this->sName;
  182. }
  183. public function getDriver(){
  184. return $this->sDriver;
  185. }
  186. public function getVersion(){
  187. return $this->sVersion;
  188. }
  189. public function getDescription(){
  190. return $this->sDescription;
  191. }
  192. }
  193.  
  194. ?>

Documentation generated on Thu, 24 Apr 2008 16:14:14 +0200 by phpDocumentor 1.3.0RC3