mysql : gestion des erreurs en exceptions
[mtweb] / web / app / data / modules / sql / mw_data_users.php
index cfa4fd9..3377c15 100644 (file)
       $sgbd = $this->sgbd();
       $env = $this->env();
       $users = array("list" => array(), "total" => 0);
-      $SELECT = "SELECT *";
-      $FROM = " FROM #--users";
-      $WHERE = "";
-      $WHERE .= (isset($alpha) ? ($WHERE ? " AND" : " WHERE")." LEFT(login, 1)=".$this->eq($alpha) : "");
-      $WHERE .= (isset($status) ? ($WHERE ? " AND" : " WHERE")." status=".$this->eq($status) : "");
-      $LIMIT = ($env->config("max_list") ? " LIMIT ".$env->config("max_list")." OFFSET ".$start : "");
-      $sql = "SELECT count(*) as n FROM(".$SELECT.$FROM.$WHERE.") res";
-      $rst = $sgbd->query($sql);
-      if(!isset($rst)) return false;
-      if($v_rst = $sgbd->fetch_assoc($rst)) $users["total"] = $v_rst["n"];
-      $sgbd->free_result($rst);
-      if($users["total"] > 0){
-        $sql = "SELECT * FROM(".$SELECT.$FROM.$WHERE.$LIMIT.") res";
+      try{
+        $SELECT = "SELECT *";
+        $FROM = " FROM #--users";
+        $WHERE = "";
+        $WHERE .= (isset($alpha) ? ($WHERE ? " AND" : " WHERE")." LEFT(login, 1)=".$this->eq($alpha) : "");
+        $WHERE .= (isset($status) ? ($WHERE ? " AND" : " WHERE")." status=".$this->eq($status) : "");
+        $LIMIT = ($env->config("max_list") ? " LIMIT ".$env->config("max_list")." OFFSET ".$start : "");
+        $sql = "SELECT count(*) as n FROM(".$SELECT.$FROM.$WHERE.") res";
         $rst = $sgbd->query($sql);
-        if(!isset($rst)) return false;
-        while($v_rst = $sgbd->fetch_assoc($rst)) $users["list"][$v_rst["id"]] = $v_rst;
+        if($v_rst = $sgbd->fetch_assoc($rst)) $users["total"] = $v_rst["n"];
         $sgbd->free_result($rst);
+        if($users["total"] > 0){
+          $sql = "SELECT * FROM(".$SELECT.$FROM.$WHERE.$LIMIT.") res";
+          $rst = $sgbd->query($sql);
+          while($v_rst = $sgbd->fetch_assoc($rst)) $users["list"][$v_rst["id"]] = $v_rst;
+          $sgbd->free_result($rst);
+        }
       }
+      catch(Exception $e) { $users = false; }
       return $users;
     }
 
     function user_by_id($id){
       $sgbd = $this->sgbd();
       $user = array();
-      $sql = "SELECT * from #--users WHERE id=".$this->eq($id);
-      $rst = $sgbd->query($sql);
-      if(!isset($rst)) return false;
-      if($v_rst = $sgbd->fetch_assoc($rst)) $user = $v_rst;
-      $sgbd->free_result($rst);
+      try{
+        $sql = "SELECT * from #--users WHERE id=".$this->eq($id);
+        $rst = $sgbd->query($sql);
+        if($v_rst = $sgbd->fetch_assoc($rst)) $user = $v_rst;
+        $sgbd->free_result($rst);
+      }
+      catch(Exception $e) { $user = false; }
       return $user;
     }
 
     function user($login){
       $sgbd = $this->sgbd();
       $user = array();
-      $sql = "SELECT * from #--users WHERE login=".$this->eq($login);
-      $rst = $sgbd->query($sql);
-      if(!isset($rst)) return false;
-      if($v_rst = $sgbd->fetch_assoc($rst)) $user = $v_rst;
-      $sgbd->free_result($rst);
+      try{
+        $sql = "SELECT * from #--users WHERE login=".$this->eq($login);
+        $rst = $sgbd->query($sql);
+        if($v_rst = $sgbd->fetch_assoc($rst)) $user = $v_rst;
+        $sgbd->free_result($rst);
+      }
+      catch(Exception $e) { $user = false; }
       return $user;
     }
 
     function user_exists($login){
       $sgbd = $this->sgbd();
       $EXISTS = 0;
-      $sql = "SELECT count(*) as n from #--users WHERE login=".$this->eq($login);
-      $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 #--users WHERE login=".$this->eq($login);
+        $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 add_user($login, $password, $email, $status){
       $sgbd = $this->sgbd();
-      $sql =
-       "INSERT INTO #--users(login, password, email, status) VALUES"
-      ."( ".$this->eq($login)
-      .", ".$this->eq($password)
-      .", ".$this->eq($email)
-      .", ".$status
-      .")";
-      return $sgbd->query($sql);
+      $user_id = false;
+      try{
+        $sql =
+         "INSERT INTO #--users(login, password, email, status) VALUES"
+        ."( ".$this->eq($login)
+        .", ".$this->eq($password)
+        .", ".$this->eq($email)
+        .", ".$status
+        .")";
+        $sgbd->query($sql);
+        $user_id = $sgbd->insert_id();
+      }
+      catch(Exception $e) { $user_id = false; }
+      return $user_id;
     }
 
     function set_user($id, $login, $password, $email, $status){
       $sgbd = $this->sgbd();
-      $sql =
-       "UPDATE #--users SET"
-      ."  login=".$this->eq($login)
-      .", password=".$this->eq($password)
-      .", email=".$this->eq($email)
-      .", status=".$status
-      ." WHERE id=".$id;
-      return $sgbd->query($sql);
+      try{
+        $sql =
+         "UPDATE #--users SET"
+        ."  login=".$this->eq($login)
+        .", password=".$this->eq($password)
+        .", email=".$this->eq($email)
+        .", status=".$status
+        ." WHERE id=".$id;
+        $sgbd->query($sql);
+      }
+      catch(Exception $e) { return false; }
+      return true;
     }
 
     function del_user($login){
       $sgbd = $this->sgbd();
-      $sql = "DELETE FROM #--users WHERE login=".$this->eq($login);
-      return $sgbd->query($sql);
+      try{
+        $sql = "DELETE FROM #--users WHERE login=".$this->eq($login);
+        $sgbd->query($sql);
+      }
+      catch(Exception $e) { return false; }
+      return true;
     }
 
     # ----------------------------------------------------------------------------------------
     function init_user_status($status = array()){
       $sgbd = $this->sgbd();
       $this->user_status = array();
-      $sql = "SELECT * FROM #--user_status";
-      $rst = $sgbd->query($sql);
-      if(!isset($rst)) return false;
-      while($v_rst = $sgbd->fetch_assoc($rst)) $this->user_status[$v_rst["id"]] = $v_rst;
-      $sgbd->free_result($rst);
+      try{
+        $sql = "SELECT * FROM #--user_status";
+        $rst = $sgbd->query($sql);
+        while($v_rst = $sgbd->fetch_assoc($rst)) $this->user_status[$v_rst["id"]] = $v_rst;
+        $sgbd->free_result($rst);
+      }
+      catch(Exception $e) { $this->user_status = false; }
       return $this->user_status;
     }
 
       if(!isset($this->user_status)) return false;
       $sgbd = $this->sgbd();
       $this->action_status = array();
-      $sql = "SELECT * FROM #--action_status";
-      $rst = $sgbd->query($sql);
-      if(!isset($rst)) return false;
-      while($v_rst = $sgbd->fetch_assoc($rst)) $this->action_status[$v_rst["id"]] = $v_rst;
-      $sgbd->free_result($rst);
+      try{
+        $sql = "SELECT * FROM #--action_status";
+        $rst = $sgbd->query($sql);
+        while($v_rst = $sgbd->fetch_assoc($rst)) $this->action_status[$v_rst["id"]] = $v_rst;
+        $sgbd->free_result($rst);
+      }
+      catch(Exception $e) { $this->action_status = false; }
       return $this->action_status;
     }
 
     function get_action_status($mod, $controller = "index", $action = "index", $set_status = array()){
       $sgbd = $this->sgbd();
       $status = array();
-      $sql =
-       "SELECT action, id_status"
-      ." FROM #--action_status"
-      ." WHERE action=".$this->eq($mod)
-      ." OR action=".$this->eq($mod."/".$controller)
-      ." OR action=".$this->eq($mod."/".$controller."/".$action);
-      $rst = $sgbd->query($sql);
-      if(!isset($rst)) return false;
-      while($v_rst = $sgbd->fetch_assoc($rst)){
-        if(!isset($status[$v_rst["action"]])) $status[$v_rst["action"]] = array();
-        $status[$v_rst["action"]][$v_rst["id_status"]] = true;
+      try{
+        $sql =
+         "SELECT action, id_status"
+        ." FROM #--action_status"
+        ." WHERE action=".$this->eq($mod)
+        ." OR action=".$this->eq($mod."/".$controller)
+        ." OR action=".$this->eq($mod."/".$controller."/".$action);
+        $rst = $sgbd->query($sql);
+        while($v_rst = $sgbd->fetch_assoc($rst)){
+          if(!isset($status[$v_rst["action"]])) $status[$v_rst["action"]] = array();
+          $status[$v_rst["action"]][$v_rst["id_status"]] = true;
+        }
+        $sgbd->free_result($rst);
       }
-      $sgbd->free_result($rst);
+      catch(Exception $e) { $status = false; }
       return $status;
     }
 
     function creation_default_status(){
       $sgbd = $this->sgbd();
       $default_status = 0;
-      $sql = "SELECT id FROM #--user_status WHERE creation_default=1 LIMIT 0,1";
-      $rst = $sgbd->query($sql);
-      if(!isset($rst)) return false;
-      if($v_rst = $sgbd->fetch_assoc($rst)) $default_status = $v_rst["id"];
-      $sgbd->free_result($rst);
+      try{
+        $sql = "SELECT id FROM #--user_status WHERE creation_default=1 LIMIT 0,1";
+        $rst = $sgbd->query($sql);
+        if($v_rst = $sgbd->fetch_assoc($rst)) $default_status = $v_rst["id"];
+        $sgbd->free_result($rst);
+      }
+      catch(Exception $e) { $default_status = false; }
       return $default_status;
     }