mysql : gestion des erreurs en exceptions
[mtweb] / web / app / data / modules / sql / mw_data_config.php
index 0d6f4d2..ca45faa 100644 (file)
@@ -5,57 +5,64 @@
     function config($key = null){
       $sgbd = $this->sgbd();
       $value = false;
-      if(isset($key)){
-        $sql =
-         "SELECT `value` FROM #--config"
-        ." WHERE `key`=".$this->eq($key);
-        $rst = $sgbd->query($sql);
-        if(!isset($rst)) return false;
-        if($v_rst = $sgbd->fetch_assoc($rst)) $value = $v_rst["value"];
-        else $value = "";
-        $sgbd->free_result($rst);
-      }
-      else{
-        $value = array();
-        $sql =
-         "SELECT * FROM #--config";
-        $rst = $sgbd->query($sql);
-        if(!isset($rst)) return false;
-        while($v_rst = $sgbd->fetch_assoc($rst)) $value[$v_rst["key"]] = $v_rst["value"];
-        $sgbd->free_result($rst);
+      try{
+        if(isset($key)){
+          $value = "";
+          $sql = "SELECT `value` FROM #--config WHERE `key`=".$this->eq($key);
+          $rst = $sgbd->query($sql);
+          if($v_rst = $sgbd->fetch_assoc($rst)) $value = $v_rst["value"];
+          $sgbd->free_result($rst);
+        }
+        else{
+          $value = array();
+          $sql = "SELECT * FROM #--config";
+          $rst = $sgbd->query($sql);
+          while($v_rst = $sgbd->fetch_assoc($rst)) $value[$v_rst["key"]] = $v_rst["value"];
+          $sgbd->free_result($rst);
+        }
       }
+      catch(Exception $e) { $value = false; }
       return $value;
     }
 
     function config_exists($key){
       $sgbd = $this->sgbd();
       $exists = false;
-      $sql = "SELECT count(*) as n FROM #--config"
-      ." WHERE `key`=".$this->eq($key);
-      $rst = $sgbd->query($sql);
-      if(!isset($rst)) return false;
-      if($v_rst = $sgbd->fetch_assoc($rst)) $exists = $v_rst["n"];
-      $sgbd->free_result($rst);
+      try{
+        $sql = "SELECT count(*) as n FROM #--config WHERE `key`=".$this->eq($key);
+        $rst = $sgbd->query($sql);
+        if($v_rst = $sgbd->fetch_assoc($rst)) $exists = $v_rst["n"];
+        $sgbd->free_result($rst);
+      }
+      catch(Exception $e) { $exists = false; }
       return $exists;
     }
 
     function set_config($key, $value){
       $sgbd = $this->sgbd();
-      if($this->config_exists($key)) $sql =
-       "UPDATE #--config"
-      ." SET `value`=".$this->eq($value)
-      ." WHERE `key`=".$this->eq($key);
-      else $sql =
-       "INSERT INTO #--config"
-      ." VALUES(NULL, ".$this->eq($key).", ".$this->eq($value).")";
-      $rst = $sgbd->query($sql);
-      if(!isset($rst)) return false;
+      if(($exists = $this->config_exists($key)) === false) return false;
+      try{
+        if($exists) $sql =
+         "UPDATE #--config"
+        ." SET `value`=".$this->eq($value)
+        ." WHERE `key`=".$this->eq($key);
+        else $sql =
+         "INSERT INTO #--config"
+        ." VALUES(NULL, ".$this->eq($key).", ".$this->eq($value).")";
+        $sgbd->query($sql);
+      }
+      catch(Exception $e) { return false; }
       return true;
     }
 
     function del_config($key){
       $sgbd = $this->sgbd();
-      return $sgbd->query("DELETE FROM #--config WHERE `key`=".$this->eq($key)) ? true : false;
+      try{
+        $sql = "DELETE FROM #--config WHERE `key`=".$this->eq($key);
+        $sgbd->query($sql);
+      }
+      catch(Exception $e) { return false; }
+      return true;
     }
 
   }