]> git.nbdom.net Git - nb.git/commitdiff
localFile
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Wed, 24 Aug 2016 16:06:57 +0000 (18:06 +0200)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Wed, 24 Aug 2016 16:06:57 +0000 (18:06 +0200)
lib/php/db.php

index effc423d8bcc49ae468be38bd4e1754fa24e99c0..9df92207e49772c6281abb16aa80cb2a78654884 100644 (file)
@@ -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('<NAME>',$this->name,$config[1]);
+    $config[1] = str_replace('<NAME>',($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"
     ;