From 07f425cff1732c22a8ce1d8cc38f0f04b495983a Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Thu, 7 Apr 2016 12:14:05 +0200 Subject: [PATCH] pdo errors catch --- etc/dbs.yaml | 2 -- lib/php/db.php | 72 ++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 55 insertions(+), 19 deletions(-) diff --git a/etc/dbs.yaml b/etc/dbs.yaml index da678f43..fee137b0 100644 --- a/etc/dbs.yaml +++ b/etc/dbs.yaml @@ -1,7 +1,5 @@ _mysql: type: mysql - options: - - "PDO::MYSQL_ATTR_INIT_COMMAND: SET NAMES utf8" crypt: pdo: 'sqlite:/dev/shm/crypt.db' diff --git a/lib/php/db.php b/lib/php/db.php index ec39dfc6..58a44594 100644 --- a/lib/php/db.php +++ b/lib/php/db.php @@ -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) { -- 2.47.3