return new Table($name,$opt+['db'=>$this]);
}
- public function localFile() {
+ public static function localFile($name='') {
- #$config = $this->type('localFile',true);
- if (!($config = $this->type('localFile'))) return [];
+ if (!($config = self::type('localFile'))) return [];
- $config[1] = str_replace('<NAME>',$this->name,$config[1]);
+ $config[1] = str_replace('<NAME>',($name ? $name : $this->name),$config[1]);
if (!file_exists($config[0])) return array();
#debug(self::ar_map('$a == null ? "" : $a',$return));
#debug($return);
+ if (empty($return['user']) and !empty($_SERVER['USER'])) $return['user'] = $_SERVER['USER'];
return $return;
}
- public function type($key=null,$die=false,$type=null) {
+ public static function type($key=null,$die=false,$type=null) {
global $DB_TYPES;
if (!$type) $type = self::$type;
# Load php file
static $require = array();
if (empty($require[$type])) {
- if (empty($type)) self::bye("Type is required");
+ if (empty($type)) self::bye('Db::$type is required');
require_once(dirname(__FILE__).'/db/types/'.$type.'.php');
$require[$type] = 1;
}
$conf = self::is_hash($conf) ? $conf : self::conf_dbs($conf);
# Check db=pdo
- if (preg_match('/^\w+:/',$id)) {
+ if ($id and preg_match('/^(\w+):(.*)/',$id,$m)) {
$conf[$id] = array(
'pdo' => $id,
+ 'type' => $m[1],
);
+ foreach (explode(' ',self::pdo_info($m[2])) as $o) {
+ list($k,$v) = explode('=',$o);
+ $conf[$id][$k] = $v;
+ }
}
if (!$conf) return false;
if (empty($Db)) return false;
$db = self::conf_search_db($conf);
+ if (self::p('localFile')) {
+ foreach (self::localFile($db['name']) as $k=>$v) {
+ if (empty($db[$k])) $db[$k] = $v;
+ }
+ }
# Connection
$Db->__construct($db);
* @copyright NB 12.08.16
* Transform pdo string into assoc array
*/
- public function pdo_info() {
+ public static function pdo_info($pdo) {
return preg_replace_callback('/(\w+)=([^;]*)(;?)/',function($m){
return preg_match('/^(host|dbname|name)/',$m[1]) ? $m[0] : '';
- },$this->pdo);
+ },$pdo);
}
public function dump() { return $this->sql(true); }
echo ''
#."-- Database : ".$this->name."\n"
."-- Date : ".strftime('%F %T')."\n"
- ."-- Pdo : ".$this->pdo_info()."\n"
+ ."-- Pdo : ".$this->pdo_info($this->pdo)."\n"
#."-- Host : ".$this->host."\n"
;