]> git.nbdom.net Git - nb.git/commitdiff
pdo errors catch
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Thu, 7 Apr 2016 10:14:05 +0000 (12:14 +0200)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Thu, 7 Apr 2016 10:14:05 +0000 (12:14 +0200)
etc/dbs.yaml
lib/php/db.php

index da678f433525e92b8c1e246cc3449f66fdef411a..fee137b044507d2ac93e4739879f95ec74ba2a0c 100644 (file)
@@ -1,7 +1,5 @@
 _mysql:
   type: mysql
-  options:
-    - "PDO::MYSQL_ATTR_INIT_COMMAND: SET NAMES utf8"
 
 crypt:
   pdo: 'sqlite:/dev/shm/crypt.db'
index ec39dfc6689e7465934e97499519f5026a4b1ab6..58a44594e24e16fbf2b9ef424d390d97c8865393 100644 (file)
@@ -17,8 +17,9 @@ class Db extends nb {
   # PDO Connection
   public $conn;
   public $pdo;
-  public $pdo_error = PDO::ERRMODE_EXCEPTION; # See: http://php.net/manual/en/pdo.error-handling.php
-  public $options;
+# NB 07.04.16   public $pdo_error = array( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); # See: http://php.net/manual/en/pdo.error-handling.php
+  public $options = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION); # See: http://php.net/manual/en/pdo.error-handling.php
+  #public $options;
   public $host = null;
   public $port = null;
   public $user = null;
@@ -139,9 +140,16 @@ class Db extends nb {
       $this->bye("Can't read database file `".$this->name."` type=`$this->type`",false);
 
     }
-    $this->conn = new PDO($this->pdo,$this->user,$this->password,$this->options);
+    try {
+      $this->conn = new PDO($this->pdo,$this->user,$this->password,$this->options);
+      #$this->conn = new PDO($this->pdo,$this->user,$this->password,$this->pdo_error);
+      #if (isset($this->pdo_error)) $this->conn->setAttribute($this->pdo_error[0], $this->pdo_error[1]);
+
+    } catch (PDOException $e) {
+      throw new CustomException('Connection failed: ' . $e->getMessage(), (int)$e->getCode( ));
+      self::bye('Connection failed: ' . $e->getMessage());
 
-    if (isset($this->pdo_error)) $this->conn->setAttribute(PDO::ATTR_ERRMODE, $this->pdo_error);
+    }
 
     if (empty($this->conn)) return false;
 
@@ -672,21 +680,50 @@ EOF;
   }
 
   public function status() {
-    if ($status=$this->type('status')) {
-      $new = array();
-      foreach ($status as $sql => $fct) {
-        foreach ($rows = $this->rows($sql) as $i=>$r) {
-        #bye($rows);
-          #$fct($rows[$i]);
-          #if (!$rows[$i] = $fct($r)) unset($rows[$i]);
-          if ($v = $fct($r)) $new += $v;
-        }
-        $status = $new;
-        #debug($rows);
+
+    if (!($status=$this->type('status'))) return array();
+
+    $new = array();
+    foreach ($status as $sql => $fct) {
+
+      try {
+        $sth = $this->conn->prepare($sql);
+        $sth->execute();
+      } catch(PDOException $e){ 
+        continue;
       }
-    } else {
-      $status = array();
+
+      $rows = $sth->fetchAll(PDO::FETCH_BOTH);
+      foreach ($rows as $i=>$r) {
+        if ($v = $fct($r)) $new += $v;
+      }
+      #$status = $new;
+      #debug($rows);
     }
+
+    $new = array(
+      'host' => $this->host,
+      'port' => $this->port,
+      'name' => $this->name,
+      'user' => $this->user,
+      #'tables' => count($this->tables()),
+      #'dbs' => count($this->dbs),
+      'tables' => count($this->tables()),
+    )+$new;
+
+    $status = array();
+    #debug($new);
+    foreach ($new as $k=>$v) {
+      $status[] = array(
+        'name' => $k,
+        'value' => $v,
+      );
+    }
+
+    return $status;
+    /*
+    */
+
     return array(
       'host' => $this->host,
       'port' => $this->port,
@@ -698,6 +735,7 @@ EOF;
       #'tables' => join(' ',array_keys($this->tables())),
       #'dbs' => join(' ',array_values($this->dbs)),
     )+$status;
+
   }
 
   public function fields($st) {