adaptations pour pludieurs instances d'applications
[mtweb] / mw / app / data / modules / sql / mw_data_sql_crud.php
1 <?php
2
3   class mw_data_sql_crud extends mw_data{
4
5     # ----------------------------------------------------------------------------------------
6     #                                                                              description
7     #
8
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;
13       try{
14         $desc = $sgbd->desc_table("#--".$table_name);
15       }
16       catch(Exception $e){ $desc = false; }
17       return $desc;
18     }
19
20     # ----------------------------------------------------------------------------------------
21     #                                                                                   insert
22     #
23
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);
36       }
37       $attributs_names = $attributs_names ? "(".$attributs_names.")" : "";
38       $attributs_values = $attributs_values ? " VALUES (".$attributs_values.")" : "";
39       try{
40         $sgbd->query("INSERT INTO `#--".$table_name."`".$attributs_names.$attributs_values);
41         $res = $return_insert_id ? $sgbd->insert_id() : true;
42       }
43       catch(Exception $e) { $res = false; }
44       return $res;
45     }
46
47     # ----------------------------------------------------------------------------------------
48     #                                                                                     read
49     #
50
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;
57       $item = array();
58       try{
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);
63       }
64       catch(Exception $e) { $item = false; }
65       return $item;
66     }
67
68     # ----------------------------------------------------------------------------------------
69     #                                                                                   update
70     #
71
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;
79       try{
80         $sql = "UPDATE `#--".$table_name."`";
81         $set = "";
82         foreach($values as $attribut_name => $attribut_value){
83           $set .= ($set ? ", " : "")."`".$attribut_name."`=".$this->eq($attribut_value);
84         }
85         $set = $set ? " SET ".$set : "";
86         $where = " WHERE `".$index_name."`=".$this->eq($index_value);
87         $sgbd->query($sql.$set.$where);
88       }
89       catch(Exception $e) { return false; }
90       return true;
91     }
92
93     # ----------------------------------------------------------------------------------------
94     #                                                                                   delete
95     #
96
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;
103       try{
104         $sql = "DELETE FROM `#--".$table_name."` WHERE `".$index_name."`=".$this->eq($index_value);
105         $sgbd->query($sql);
106       }
107       catch(Exception $e) { debug($e->getMessage()); return false; }
108       return true;
109     }
110
111     # ----------------------------------------------------------------------------------------
112     #                                                                                     list
113     #
114
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){
125           $and_where = "";
126           if(isset($filter[0]) && isset($filter[1])){
127             switch(strtolower($filter[1])){
128               case "eq":
129                 if(isset($filter[2])){
130                   $and_where .= ($where ? " AND " : " WHERE ").$filter[0]."=".$this->eq($filter[2]);
131                   break;
132                 }
133               case "lt":
134                 if(isset($filter[2])){
135                   $and_where .= ($where ? " AND " : " WHERE ").$filter[0]."<".$this->eq($filter[2]);
136                   break;
137                 }
138               case "lte":
139                 if(isset($filter[2])){
140                   $and_where .= ($where ? " AND " : " WHERE ").$filter[0].">=".$this->eq($filter[2]);
141                   break;
142                 }
143               case "gt":
144                 if(isset($filter[2])){
145                   $and_where .= ($where ? " AND " : " WHERE ").$filter[0].">".$this->eq($filter[2]);
146                   break;
147                 }
148               case "gte":
149                 if(isset($filter[2])){
150                   $and_where .= ($where ? " AND " : " WHERE ").$filter[0].">=".$this->eq($filter[2]);
151                   break;
152                 }
153             }
154           }
155           $where .= $and_where;
156         }
157       }
158       $order = isset($params["order_by"]) ?
159         " ORDER BY ".$params["order_by"].(isset($params["order"]) ? " ".$params["order"] : "")
160       : "";
161       $limit = isset($params["limit"]) ?
162         " LIMIT ".$params["limit"].(isset($params["offset"]) ? " OFFSET ".$params["offset"] : "")
163       : "";
164       $list = array("list" => array(), "total" => 0);
165       try{
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;
176           }
177           $sgbd->free_result($rst);
178         }
179       }
180       catch(Exception $e) { $list = false; }
181       return $list;
182     }
183
184   }
185
186 ?>