[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/dbal/src/toolkit/ -> lmbDbTools.class.php (source)

   1  <?php
   2  /*

   3   * Limb PHP Framework

   4   *

   5   * @link http://limb-project.com

   6   * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)

   7   * @license    LGPL http://www.gnu.org/copyleft/lesser.html

   8   */
   9  lmb_require('limb/toolkit/src/lmbAbstractTools.class.php');
  10  lmb_require('limb/dbal/src/lmbDBAL.class.php');
  11  lmb_require('limb/dbal/src/lmbDbDSN.class.php');
  12  lmb_require('limb/dbal/src/drivers/lmbDbCachedInfo.class.php');
  13  
  14  /**

  15   * class lmbDbTools.

  16   *

  17   * @package dbal

  18   * @version $Id: lmbDbTools.class.php 5996 2007-06-18 12:20:00Z pachanga $

  19   */
  20  class lmbDbTools extends lmbAbstractTools
  21  {
  22    protected $default_connection;
  23    protected $default_db_config;
  24    protected $cache_db_info = true;
  25    protected $db_info = array();
  26    protected $db_tables = array();
  27    protected $db_env = 'devel';
  28  
  29    function setDbEnvironment($env)
  30    {
  31      $this->db_env = $env;
  32      $this->default_db_config = null;
  33      $this->default_connection = null;
  34    }
  35  
  36    function getDbEnvironment()
  37    {
  38      return $this->db_env;
  39    }
  40  
  41    function setDefaultDbDSN($conf)
  42    {
  43      if(is_object($conf))
  44        $this->default_db_config = $conf;
  45      else
  46        $this->default_db_config = new lmbDbDSN($conf);
  47    }
  48  
  49    function getDefaultDbDSN()
  50    {
  51      if(is_object($this->default_db_config))
  52        return $this->default_db_config;
  53  
  54      $conf = $this->toolkit->getConf('db');
  55  
  56      //for BC 'dsn' overrides other db environments

  57      if($dsn = $conf->get('dsn'))
  58      {
  59        $this->default_db_config = new lmbDbDSN($dsn);
  60      }
  61      else
  62      {
  63        $env = $conf->get($this->db_env);
  64        if(!is_array($env) || !isset($env['dsn']))
  65          throw new lmbException("Could not find database connection settings for environment '{$this->db_env}'");
  66  
  67        $this->default_db_config = new lmbDbDSN($env['dsn']);
  68      }
  69  
  70      return $this->default_db_config;
  71    }
  72  
  73    function getDbDSN($env)
  74    {
  75      $conf = $this->toolkit->getConf('db');
  76      $array = $conf->get($env);
  77  
  78      if(!is_array($array) || !isset($array['dsn']))
  79        throw new lmbException("Could not find database connection settings for environment '{$env}'");
  80  
  81      return new lmbDbDSN($array['dsn']);
  82    }
  83  
  84    function getDefaultDbConnection()
  85    {
  86      if(is_object($this->default_connection))
  87        return $this->default_connection;
  88  
  89      if(!is_object($dsn = $this->toolkit->getDefaultDbDSN()))
  90        throw new lmbException('Default database DSN is not valid');
  91  
  92      $this->default_connection = $this->toolkit->createDbConnection($dsn);
  93      return $this->default_connection;
  94    }
  95  
  96    function createDbConnection($dsn)
  97    {
  98      $driver = $dsn->getDriver();
  99      $class = 'lmb' . ucfirst($driver) . 'Connection';
 100      $file = dirname(__FILE__) . '/../drivers/' . $driver . '/' . $class . '.class.php';
 101      if(!file_exists($file))
 102        throw new lmbException("Driver '$driver' file not found for DSN '" . $dsn->toString() . "'!");
 103  
 104      lmb_require($file);
 105      return new $class($dsn);
 106    }
 107  
 108    function cacheDbInfo($flag = true)
 109    {
 110      $this->cache_db_info = $flag;
 111    }
 112  
 113    function getDbInfo($conn)
 114    {
 115      $id = $conn->getHash();
 116  
 117      if(isset($this->db_info[$id]))
 118        return $this->db_info[$id];
 119  
 120      if($this->cache_db_info && defined('LIMB_VAR_DIR'))
 121        $db_info = new lmbDbCachedInfo($conn, LIMB_VAR_DIR);
 122      else
 123        $db_info = $conn->getDatabaseInfo();
 124  
 125      $this->db_info[$id] = $db_info;
 126      return $this->db_info[$id];
 127    }
 128  
 129    function setDefaultDbConnection($conn)
 130    {
 131      $this->default_connection = $conn;
 132    }
 133  
 134    function createTableGateway($table_name, $conn = null)
 135    {
 136      if(!$conn)
 137        $cache_key = $table_name;
 138      else
 139        $cache_key = $table_name . $conn->getHash();
 140  
 141      if(isset($this->db_tables[$cache_key]))
 142        return $this->db_tables[$cache_key];
 143  
 144      $db_table = new lmbTableGateway($table_name, $conn);
 145      $this->db_tables[$cache_key] = $db_table;
 146      return $db_table;
 147    }
 148  }
 149  ?>


Generated: Sat Sep 6 04:46:52 2008 Cross-referenced by PHPXref 0.7