nyroFwk  0.2
db.class.php
Go to the documentation of this file.
1 <?php
10 final class db {
11 
12  const FETCH_ASSOC = PDO::FETCH_ASSOC;
13  const FETCH_BOTH = PDO::FETCH_BOTH;
14  const FETCH_NUM = PDO::FETCH_NUM;
15  const FETCH_ROWSET = PDO::FETCH_CLASS;
16  const FETCH_COLUMN = PDO::FETCH_COLUMN;
17 
23  private static $instances = array();
24 
30  private static $cfg = null;
31 
37  private static $tables = array();
38 
44  private static $log = array();
45 
49  private function __construct() {}
50 
58  public static function getInstance($cfg=null) {
59  if (is_array($cfg)) {
60  $cfg['getInstanceCfg'] = $cfg;
61  return factory::get('db_'.$cfg['use'], $cfg);
62  }
63 
64  self::init();
65 
66  if (is_null($cfg))
67  $cfg = self::$cfg->get('defCfg');
68 
69  if (!array_key_exists($cfg, self::$instances)) {
70  $tmp = self::$cfg->get($cfg);
71  $tmp['getInstanceCfg'] = $cfg;
72  $use = $tmp['use'];
73  unset($tmp['use']);
74  self::$instances[$cfg] = factory::get('db_'.$use, $tmp);
75  }
76  return self::$instances[$cfg];
77  }
78 
87  public static function get($type, $table, array $prm = array()) {
88  if ($type == 'table' && array_key_exists($table, self::$tables))
89  return self::$tables[$table];
90 
91  self::init();
92 
93  if ($table instanceof db_table) {
94  $tableName = $table->getName();
95  if (!array_key_exists('table', $prm))
96  $prm['table'] = $table;
97  } else {
98  $tableName = $table;
99  if ($type == 'table' && !array_key_exists('name', $prm))
100  $prm['name'] = $table;
101  else if ($type == 'row' && !array_key_exists('table', $prm)) {
102  $db = array_key_exists('db', $prm)? $prm['db'] : self::getInstance();
103  $prm['table'] = self::get('table', $tableName, array('db'=>$db));
104  }
105  }
106 
107  if (!($className = self::$cfg->getInArray($type, $tableName)) &&
108  (!factory::isCreable($className = 'db_'.$type.'_'.$tableName)))
109  $className = self::$cfg->get($type.'Class');
110 
111  if ($type == 'table') {
112  self::$tables[$table] = factory::get($className, $prm);
113  return self::$tables[$table];
114  }
115 
116  return factory::get($className, $prm);
117  }
118 
125  public static function getCfg($key) {
126  return self::$cfg->get($key);
127  }
128 
135  public static function isI18nName($field) {
136  return substr($field, 0, strlen(self::getCfg('i18n'))) == self::getCfg('i18n');
137  }
138 
145  public static function unI18nName($field) {
146  return self::isI18nName($field)? substr($field, strlen(self::getCfg('i18n'))) : $field;
147  }
148 
156  public static function log($sql=null, $bind=null) {
157  if (is_null($sql))
158  return self::$log;
159  else if (empty($bind))
160  self::$log[] = $sql;
161  else
162  self::$log[] = array($sql, $bind);
163  }
164 
168  protected static function init() {
169  if (self::$cfg == null)
170  self::$cfg = new config(factory::loadCfg(__CLASS__));
171  }
172 
173 }
__construct()
Definition: db.class.php:49
static $log
Definition: db.class.php:44
const FETCH_BOTH
Definition: db.class.php:13
static init()
Definition: db.class.php:168
Definition: db.class.php:10
static $tables
Definition: db.class.php:37
static $cfg
Definition: db.class.php:30
static $instances
Definition: db.class.php:23
const FETCH_ASSOC
Definition: db.class.php:12
static getCfg($key)
Definition: db.class.php:125
const FETCH_NUM
Definition: db.class.php:14
static unI18nName($field)
Definition: db.class.php:145
static log($sql=null, $bind=null)
Definition: db.class.php:156
static isI18nName($field)
Definition: db.class.php:135
static loadCfg($className, $searchParent=true)
const FETCH_COLUMN
Definition: db.class.php:16
const FETCH_ROWSET
Definition: db.class.php:15
static isCreable($className)
static getInstance($cfg=null)
Definition: db.class.php:58
static get($className, array $cfg=array())
Generated on Sun Oct 15 2017 22:25:20 for nyroFwk by doxygen 1.8.13