From: dj3c1t Date: Sun, 24 Mar 2013 17:48:31 +0000 (+0100) Subject: ajout de fuctions CRUD generiques X-Git-Tag: mtweb.0.7.2~1 X-Git-Url: http://git.dj3c1t.com/index.cgi?a=commitdiff_plain;h=7ad1ac4f506df343ef4127d7c56a846edd307374;p=mtweb ajout de fuctions CRUD generiques --- diff --git a/mw/app/data/modules/sql/mw_data_crud.php b/mw/app/data/modules/sql/mw_data_crud.php new file mode 100644 index 0000000..9ac527e --- /dev/null +++ b/mw/app/data/modules/sql/mw_data_crud.php @@ -0,0 +1,132 @@ +sgbd(); + $table_name = isset($params["table_name"]) ? $params["table_name"] : ""; + if(!$table_name) return false; + $values = (isset($params["values"]) && is_array($params["values"])) ? $params["values"] : false; + if(!$values) return false; + $return_insert_id = isset($params["return_insert_id"]) ? $params["return_insert_id"] : false; + $attributs_names = ""; + $attributs_values = ""; + foreach($values as $attribut_name => $attribut_value){ + $attributs_names .= ($attributs_names ? ", " : "")."`".$attribut_name."`"; + $attributs_values .= ($attributs_values ? ", " : "").$this->eq($attribut_value); + } + $attributs_names = $attributs_names ? "(".$attributs_names.")" : ""; + $attributs_values = $attributs_values ? " VALUES (".$attributs_names.")" : ""; + try{ + $sgbd->query("INSERT INTO `#--".$table_name."`".$attributs_names.$attributs_values); + $res = $return_insert_id ? $sgbd->insert_id() : true; + } + catch(Exception $e) { $res = false; } + return $res; + } + + # ---------------------------------------------------------------------------------------- + # read + # + + function data_read($params = array()){ + $sgbd = $this->sgbd(); + $table_name = isset($params["table_name"]) ? $params["table_name"] : ""; + $index_name = isset($params[""]) ? $params["index_name"] : ""; + $index_value = isset($params[""]) ? $params["index_value"] : ""; + if(!$table_name || !$index_name || !$index_value) return false; + $item = array(); + try{ + $sql = "SELECT * from `#--".$table_name."` WHERE `".$index_name."`=".$this->eq($index_value)." LIMIT 1"; + $rst = $sgbd->query($sql); + if($v_rst = $sgbd->fetch_assoc($rst)) $item = $v_rst; + $sgbd->free_result($rst); + } + catch(Exception $e) { $item = false; } + return $item; + } + + # ---------------------------------------------------------------------------------------- + # list + # + + function data_list($params = array()){ + $sgbd = $this->sgbd(); + $table_name = isset($params["table_name"]) ? $params["table_name"] : ""; + if(!$table_name) return false; + $index_name = isset($params["index_name"]) ? $params["table_index"] : ""; + $select = "SELECT `#--".$table_name."`.*"; + $from = " FROM `#--".$table_name."`"; + $where = isset($params["where"]) ? " WHERE ".$params["where"] : ""; + $order = isset($params["order_by"]) ? + " ORDER BY ".$params["order_by"].(isset($params["order"]) ? " ".$params["order"] : "") + : ""; + $limit = isset($params["limit"]) ? + " LIMIT ".$params["limit"].(isset($params["offset"]) ? " OFFSET ".$params["offset"] : "") + : ""; + $list = array("list" => array(), "total" => 0); + try{ + $sql = "SELECT count(*) as n FROM(".$select.$from.$where.") res"; + $rst = $sgbd->query($sql); + if($v_rst = $sgbd->fetch_assoc($rst)) $list["total"] = $v_rst["n"]; + $sgbd->free_result($rst); + if($list["total"] > 0){ + $sql = "SELECT * FROM(".$select.$from.$where.$order.$limit.") res"; + $rst = $sgbd->query($sql); + while($v_rst = $sgbd->fetch_assoc($rst)){ + if($index_name) $list["list"][$v_rst[$index_name]] = $v_rst; + else $list["list"][] = $v_rst; + } + $sgbd->free_result($rst); + } + } + catch(Exception $e) { $list = false; } + return $list; + } + + # ---------------------------------------------------------------------------------------- + # update + # + + function data_update($params = array()){ + $sgbd = $this->sgbd(); + $table_name = isset($params["table_name"]) ? $params["table_name"] : false; + $values = (isset($params["values"]) && is_array($params["values"])) ? $params["values"] : false; + $index_name = isset($params[""]) ? $params["index_name"] : false; + $index_value = isset($params[""]) ? $params["index_value"] : false; + if(!$table_name || !$index_name || !$index_value || !$values) return false; + try{ + $sql = "UPDATE `#--".$table_name."`"; + $set = ""; + foreach($values as $attribut_name => $attribut_value){ + $set .= ($set ? ", " : "")."`".$attribut_name."`=".$this->eq($attribut_value); + } + $set = $set ? " SET ".$set : ""; + $where = " WHERE `".$index_name."`=".$this->eq($index_value); + $sgbd->query($sql); + } + catch(Exception $e) { return false; } + return true; + } + + # ---------------------------------------------------------------------------------------- + # delete + # + + function data_delete($params = array()){ + $sgbd = $this->sgbd(); + try{ + $sql = "DELETE FROM `#--".$table_name."` WHERE `".$index_name."`=".$this->eq($index_value); + $sgbd->query($sql); + } + catch(Exception $e) { return false; } + return true; + } + + } + +?> \ No newline at end of file