]> git.nbdom.net Git - nb.git/commitdiff
pdo
authorNico Boisselier <nico@dhcp-192-168-4-150.semantico.net>
Wed, 26 Oct 2016 13:52:13 +0000 (14:52 +0100)
committerNico Boisselier <nico@dhcp-192-168-4-150.semantico.net>
Wed, 26 Oct 2016 13:52:13 +0000 (14:52 +0100)
lib/php/db.php

index f8c7e16454307c94ec2bb75c99619c9a0497f029..4d42b7c76ba6b781eac983db844a2d314afa385c 100644 (file)
@@ -154,15 +154,19 @@ class Db extends nb {
     } else {
 
       # Extract args from pdo
-      if (preg_match_all("/[:;](\w+)=([^;]+)/",$this->pdo,$m,PREG_SET_ORDER)) {
-        foreach ($m as $param) {
-          $k = $param[1];
-          if ($k == 'dbname') $k = 'name';
-          if (!empty($this->$k)) continue;
-          $this->$k = $param[2];
-        }
+      foreach ($this->pdo2h($this->pdo) as $k=>$v) {
+        $this->$k = $v;
       }
 
+# NB 26.10.16       if (preg_match_all("/[:;](\w+)=([^;]+)/",$this->pdo,$m,PREG_SET_ORDER)) {
+# NB 26.10.16         foreach ($m as $param) {
+# NB 26.10.16           $k = $param[1];
+# NB 26.10.16           if ($k == 'dbname') $k = 'name';
+# NB 26.10.16           if (!empty($this->$k)) continue;
+# NB 26.10.16           $this->$k = $param[2];
+# NB 26.10.16         }
+# NB 26.10.16       }
+
       if ($p = $this->p('db.host')) $this->host = $p;
 
       # Add args to pdo
@@ -180,6 +184,7 @@ class Db extends nb {
         $this->pdo = preg_replace("/\b$v=[^;]*(;|$)/","",$this->pdo);
         $this->pdo .= (preg_match('/[:;]$/',$this->pdo) ? '' : ';') . "$v=".$this->$k;
       }
+#bye($this->pdo);
 
     }
 
@@ -192,14 +197,14 @@ class Db extends nb {
     }
   }
 
-       function disconnect() {
+       public function disconnect() {
     if (empty($this->conn)) return null;
     #$this->conn->exec('COMMIT');
     $this->method('disconnect');
 # NB 09.09.16     return $this->conn->close();
   }
 
-       function connect() {
+       public function connect() {
 
     if (!empty($this->conn)) return false;
     # Pdo
@@ -210,6 +215,10 @@ class Db extends nb {
     $this->conf_type_load();
 
     # Connect
+    #debug([$this->pdo,$this->user,$this->password,$this->options]);
+    #$this->conn = new PDO($this->pdo,$this->user,$this->password);
+    #bye('zaza');
+  #if (false) {
     try {
       $this->conn = new PDO($this->pdo,$this->user,$this->password,$this->options);
       #if (isset($this->pdo_error)) $this->conn->setAttribute($this->pdo_error[0], $this->pdo_error[1]);
@@ -222,10 +231,13 @@ class Db extends nb {
       $msg .= "\n".$e->getMessage();
       throw new Exception($msg, (int)$e->getCode());
       self::bye($msg);
-
     }
+  #}
 
-    if (empty($this->conn)) return false;
+    if (empty($this->conn)) {
+      $this->bye("Can't connect to: ".$this->pdo_info($this->pdo));
+      return false;
+    }
 
     # Create functions
     foreach ((array)$this->conf_type('sqliteCreateFunction') as $name => $fct) {
@@ -645,15 +657,20 @@ class Db extends nb {
 
     # Check db=pdo
     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) {
-        if (strpos($o,'=') === false) continue;
-        list($k,$v) = explode('=',$o);
-        $conf[$id][$k] = $v;
-      }
+      if (empty($conf[$id])) $conf[$id] = [];
+      $conf[$id] = $conf[$id] + self::pdo2h($id);
+# NB 26.10.16       $conf[$id] = array(
+# NB 26.10.16         'pdo' => $id,
+# NB 26.10.16         'type' => $m[1],
+# NB 26.10.16       );
+# NB 26.10.16       debug(self::pdo2h($id));
+# NB 26.10.16 
+# NB 26.10.16       foreach (explode(' ',self::pdo_info($m[2])) as $o) {
+# NB 26.10.16         if (strpos($o,'=') === false) continue;
+# NB 26.10.16         list($k,$v) = explode('=',$o);
+# NB 26.10.16         $conf[$id][$k] = $v;
+# NB 26.10.16       }
+      #debug(self::pdo_info($m[2])));
     }
 
     if (!$conf) return false;
@@ -702,7 +719,7 @@ class Db extends nb {
     # Connection
     $Db->__construct($db);
 # NB 21.09.16     if (empty($Db->_no_connect)) {
-      $Db->connect();
+    $Db->connect();
 # NB 21.09.16     }
 
     /*
@@ -733,10 +750,27 @@ class Db extends nb {
   * @copyright NB 12.08.16
   * Transform pdo string into assoc array
   */
+  public static function pdo2h($pdo) {
+    $hash = [];
+    if (preg_match('/^(\w+):(.*)/',$pdo,$m)) {
+      $hash['type'] = $m[1];
+      $pdo = $m[2];
+    }
+
+    while (preg_match('/^(\w+)=([^;]*)(?:;?)(.*?)$/',$pdo,$m)) {
+      if ($m[1] == 'dbname') $m[1] = 'name';
+      $hash[$m[1]] = $m[2];
+      $pdo = $m[3];
+    }
+
+    return $hash;
+  }
+
   public static function pdo_info($pdo) {
-    return preg_replace_callback('/(\w+)=([^;]*)(;?)/',function($m){
-      return preg_match('/^(host|dbname|name)/',$m[1]) ? $m[0] : '';
-    },$pdo);
+    $infos = self::pdo2h($pdo);
+    foreach ($infos as $k=>$v) {
+      if (!preg_match('/^(host|dbname|name)/')) delete($infos[$k]);
+    }
   }
 
   public function dump2csv() {