3 class mw_data_sql_crud extends mw_data{
5 # ----------------------------------------------------------------------------------------
9 function data_desc($params = array()){
10 $sgbd = $this->sgbd();
11 $table_name = isset($params["table_name"]) ? $params["table_name"] : "";
12 if(!$table_name) return false;
14 $desc = $sgbd->desc_table("#--".$table_name);
16 catch(Exception $e){ $desc = false; }
20 # ----------------------------------------------------------------------------------------
24 function data_insert($params = array()){
25 $sgbd = $this->sgbd();
26 $table_name = isset($params["table_name"]) ? $params["table_name"] : "";
27 if(!$table_name) return false;
28 $values = (isset($params["values"]) && is_array($params["values"])) ? $params["values"] : false;
29 if(!$values) return false;
30 $return_insert_id = isset($params["return_insert_id"]) ? $params["return_insert_id"] : false;
31 $attributs_names = "";
32 $attributs_values = "";
33 foreach($values as $attribut_name => $attribut_value){
34 $attributs_names .= ($attributs_names ? ", " : "")."`".$attribut_name."`";
35 $attributs_values .= ($attributs_values ? ", " : "").$this->eq($attribut_value);
37 $attributs_names = $attributs_names ? "(".$attributs_names.")" : "";
38 $attributs_values = $attributs_values ? " VALUES (".$attributs_values.")" : "";
40 $sgbd->query("INSERT INTO `#--".$table_name."`".$attributs_names.$attributs_values);
41 $res = $return_insert_id ? $sgbd->insert_id() : true;
43 catch(Exception $e) { $res = false; }
47 # ----------------------------------------------------------------------------------------
51 function data_read($params = array()){
52 $sgbd = $this->sgbd();
53 $table_name = isset($params["table_name"]) ? $params["table_name"] : "";
54 $index_name = isset($params["index_name"]) ? $params["index_name"] : "";
55 $index_value = isset($params["index_value"]) ? $params["index_value"] : "";
56 if(!$table_name || !$index_name || !$index_value) return false;
59 $sql = "SELECT * from `#--".$table_name."` WHERE `".$index_name."`=".$this->eq($index_value)." LIMIT 1";
60 $rst = $sgbd->query($sql);
61 if($v_rst = $sgbd->fetch_assoc($rst)) $item = $v_rst;
62 $sgbd->free_result($rst);
64 catch(Exception $e) { $item = false; }
68 # ----------------------------------------------------------------------------------------
72 function data_update($params = array()){
73 $sgbd = $this->sgbd();
74 $table_name = isset($params["table_name"]) ? $params["table_name"] : false;
75 $values = (isset($params["values"]) && is_array($params["values"])) ? $params["values"] : false;
76 $index_name = isset($params["index_name"]) ? $params["index_name"] : false;
77 $index_value = isset($params["index_value"]) && (strlen($params["index_value"]) > 0) ? $params["index_value"] : false;
78 if(!$table_name || !$index_name || !$index_value || !$values) return false;
80 $sql = "UPDATE `#--".$table_name."`";
82 foreach($values as $attribut_name => $attribut_value){
83 $set .= ($set ? ", " : "")."`".$attribut_name."`=".$this->eq($attribut_value);
85 $set = $set ? " SET ".$set : "";
86 $where = " WHERE `".$index_name."`=".$this->eq($index_value);
87 $sgbd->query($sql.$set.$where);
89 catch(Exception $e) { return false; }
93 # ----------------------------------------------------------------------------------------
97 function data_delete($params = array()){
98 $sgbd = $this->sgbd();
99 $table_name = isset($params["table_name"]) ? $params["table_name"] : false;
100 $index_name = isset($params["index_name"]) ? $params["index_name"] : false;
101 $index_value = isset($params["index_value"]) ? $params["index_value"] : false;
102 if(!$table_name || !$index_name || !$index_value) return false;
104 $sql = "DELETE FROM `#--".$table_name."` WHERE `".$index_name."`=".$this->eq($index_value);
107 catch(Exception $e) { debug($e->getMessage()); return false; }
111 # ----------------------------------------------------------------------------------------
115 function data_list($params = array()){
116 $sgbd = $this->sgbd();
117 $table_name = isset($params["table_name"]) ? $params["table_name"] : "";
118 if(!$table_name) return false;
119 $index_name = isset($params["index_name"]) ? $params["index_name"] : "";
120 $select = "SELECT `#--".$table_name."`.*";
121 $from = " FROM `#--".$table_name."`";
122 $where = isset($params["where"]) ? " WHERE ".$params["where"] : "";
123 if(isset($params["filters"])){
124 foreach($params["filters"] as $filter){
126 if(isset($filter[0]) && isset($filter[1])){
127 switch(strtolower($filter[1])){
129 if(isset($filter[2])){
130 $and_where .= ($where ? " AND " : " WHERE ").$filter[0]."=".$this->eq($filter[2]);
134 if(isset($filter[2])){
135 $and_where .= ($where ? " AND " : " WHERE ").$filter[0]."<".$this->eq($filter[2]);
139 if(isset($filter[2])){
140 $and_where .= ($where ? " AND " : " WHERE ").$filter[0].">=".$this->eq($filter[2]);
144 if(isset($filter[2])){
145 $and_where .= ($where ? " AND " : " WHERE ").$filter[0].">".$this->eq($filter[2]);
149 if(isset($filter[2])){
150 $and_where .= ($where ? " AND " : " WHERE ").$filter[0].">=".$this->eq($filter[2]);
155 $where .= $and_where;
158 $order = isset($params["order_by"]) ?
159 " ORDER BY ".$params["order_by"].(isset($params["order"]) ? " ".$params["order"] : "")
161 $limit = isset($params["limit"]) ?
162 " LIMIT ".$params["limit"].(isset($params["offset"]) ? " OFFSET ".$params["offset"] : "")
164 $list = array("list" => array(), "total" => 0);
166 $sql = "SELECT count(*) as n FROM(".$select.$from.$where.") res";
167 $rst = $sgbd->query($sql);
168 if($v_rst = $sgbd->fetch_assoc($rst)) $list["total"] = $v_rst["n"];
169 $sgbd->free_result($rst);
170 if($list["total"] > 0){
171 $sql = "SELECT * FROM(".$select.$from.$where.$order.$limit.") res";
172 $rst = $sgbd->query($sql);
173 while($v_rst = $sgbd->fetch_assoc($rst)){
174 if($index_name) $list["list"][$v_rst[$index_name]] = $v_rst;
175 else $list["list"][] = $v_rst;
177 $sgbd->free_result($rst);
180 catch(Exception $e) { $list = false; }