From 85680dabf2d8ba68f4197f2a64a51efa632e4b2a Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Wed, 24 Aug 2016 18:06:57 +0200 Subject: [PATCH] localFile --- lib/php/db.php | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/lib/php/db.php b/lib/php/db.php index effc423d..9df92207 100644 --- a/lib/php/db.php +++ b/lib/php/db.php @@ -302,12 +302,11 @@ class Db extends nb { 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('',$this->name,$config[1]); + $config[1] = str_replace('',($name ? $name : $this->name),$config[1]); if (!file_exists($config[0])) return array(); @@ -321,17 +320,18 @@ class Db extends nb { #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; } @@ -562,10 +562,15 @@ class Db extends nb { $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; @@ -607,6 +612,11 @@ class Db extends nb { 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); @@ -641,10 +651,10 @@ class Db extends nb { * @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); } @@ -688,7 +698,7 @@ class Db extends nb { 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" ; -- 2.47.3