Go to the documentation of this file.00001 <?php
00010 final class db {
00011
00012 const FETCH_ASSOC = PDO::FETCH_ASSOC;
00013 const FETCH_BOTH = PDO::FETCH_BOTH;
00014 const FETCH_NUM = PDO::FETCH_NUM;
00015 const FETCH_ROWSET = PDO::FETCH_CLASS;
00016 const FETCH_COLUMN = PDO::FETCH_COLUMN;
00017
00023 private static $instances = array();
00024
00030 private static $cfg = null;
00031
00037 private static $tables = array();
00038
00044 private static $log = array();
00045
00049 private function __construct() {}
00050
00058 public static function getInstance($cfg=null) {
00059 if (is_array($cfg)) {
00060 $cfg['getInstanceCfg'] = $cfg;
00061 return factory::get('db_'.$cfg['use'], $cfg);
00062 }
00063
00064 self::init();
00065
00066 if (is_null($cfg))
00067 $cfg = self::$cfg->get('defCfg');
00068
00069 if (!array_key_exists($cfg, self::$instances)) {
00070 $tmp = self::$cfg->get($cfg);
00071 $tmp['getInstanceCfg'] = $cfg;
00072 $use = $tmp['use'];
00073 unset($tmp['use']);
00074 self::$instances[$cfg] = factory::get('db_'.$use, $tmp);
00075 }
00076 return self::$instances[$cfg];
00077 }
00078
00087 public static function get($type, $table, array $prm = array()) {
00088 if ($type == 'table' && array_key_exists($table, self::$tables))
00089 return self::$tables[$table];
00090
00091 self::init();
00092
00093 if ($table instanceof db_table) {
00094 $tableName = $table->getName();
00095 if (!array_key_exists('table', $prm))
00096 $prm['table'] = $table;
00097 } else {
00098 $tableName = $table;
00099 if ($type == 'table' && !array_key_exists('name', $prm))
00100 $prm['name'] = $table;
00101 else if ($type == 'row' && !array_key_exists('table', $prm)) {
00102 $db = array_key_exists('db', $prm)? $prm['db'] : self::getInstance();
00103 $prm['table'] = self::get('table', $tableName, array('db'=>$db));
00104 }
00105 }
00106
00107 if (!($className = self::$cfg->getInArray($type, $tableName)) &&
00108 (!factory::isCreable($className = 'db_'.$type.'_'.$tableName)))
00109 $className = self::$cfg->get($type.'Class');
00110
00111 if ($type == 'table') {
00112 self::$tables[$table] = factory::get($className, $prm);
00113 return self::$tables[$table];
00114 }
00115
00116 return factory::get($className, $prm);
00117 }
00118
00125 public static function getCfg($key) {
00126 self::$cfg;
00127 return self::$cfg->get($key);
00128 }
00129
00136 public static function isI18nName($field) {
00137 return substr($field, 0, strlen(self::getCfg('i18n'))) == self::getCfg('i18n');
00138 }
00139
00146 public static function unI18nName($field) {
00147 return self::isI18nName($field)? substr($field, strlen(self::getCfg('i18n'))) : $field;
00148 }
00149
00157 public static function log($sql=null, $bind=null) {
00158 if (is_null($sql))
00159 return self::$log;
00160 else if (empty($bind))
00161 self::$log[] = $sql;
00162 else
00163 self::$log[] = array($sql, $bind);
00164 }
00165
00169 protected static function init() {
00170 if (self::$cfg == null)
00171 self::$cfg = new config(factory::loadCfg(__CLASS__));
00172 }
00173
00174 }