public attr / function, constructeurs __construct
[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     #                                                                       parametres du sgbd
7     #
8
9     public function set_sgbd_param($key, $value){
10       $env = $this->env();
11       if(($env->bdd("sgbd") == "pdo_mysql") || ($env->bdd("sgbd") == "mysql")){
12         return $this->set_sgbd_mysql_param($key, $value);
13       }
14       return false;
15     }
16
17     public function set_sgbd_mysql_param($key, $value){
18       $sgbd = $this->sgbd();
19       $sql = "SET ".$key."=\"".$value."\"";
20       try{
21         $sgbd->query($sql);
22       }
23       catch(Exception $e){
24         return false;
25       }
26       return true;
27     }
28
29     # ----------------------------------------------------------------------------------------
30     #                                                                             create table
31     #
32
33     public function data_create_table($params){
34       $env = $this->env();
35       if($env->bdd("sgbd") == "pdo_sqlite"){
36         return $this->data_create_sqlite_table($params);
37       }
38       return $this->data_create_mysql_table($params);
39     }
40
41     public function data_create_mysql_table($params){
42       $sgbd = $this->sgbd();
43       $table_name = isset($params["table_name"]) ? $params["table_name"] : "";
44       $fields = isset($params["fields"]) ? $params["fields"] : array();
45       $keys = isset($params["keys"]) ? $params["keys"] : array();
46       $options = isset($params["options"]) ? $params["options"] : array();
47       if(!$table_name || !$fields) return false;
48       $sql_fields = "";
49       $sql_keys = "";
50       $sql_options = "";\r
51       foreach($fields as $field_name => $field){
52         if(!preg_match("/^[a-z]+[a-z_0-9]*$/", $field_name)) return false;
53         if(!isset($field["type"])) return false;
54         $sql_field = "";
55         switch($field["type"]){
56           case "int":
57             $value = isset($field["value"]) ? $field["value"] : "11";
58             if(!preg_match("/^[0-9]+$/", $value)) return false;
59             $sql_field .= "`".$field_name."` int(".$value.")";
60             break;
61           case "varchar":
62             $value = isset($field["value"]) ? $field["value"] : "255";
63             if(!preg_match("/^[0-9]+$/", $value)) return false;
64             $sql_field .= "`".$field_name."` varchar(".$value.")";
65             break;
66           case "text":
67             $sql_field .= "`".$field_name."` text";
68             break;
69           case "date":
70             $sql_field .= "`".$field_name."` date";
71             break;
72           case "datetime":
73             $sql_field .= "`".$field_name."` datetime";
74             break;
75         }
76         if($sql_field){
77           $null = isset($field["null"]) ? $field["null"] : false;
78           $sql_field .= ($null ? " DEFAULT" : " NOT")." NULL";
79           if($null && isset($field["default"])){
80             $sql_field .= " DEFAULT ".$this->eq($field["default"]);
81           }
82           if(isset($field["autoincrement"]) && $field["autoincrement"]){
83             $sql_field .= " AUTO_INCREMENT";
84           }
85           $sql_fields .= ($sql_fields ? ", " : "").$sql_field;
86         }
87       }
88       $key_index = 0;
89       foreach($keys as $key){
90         $key_index++;
91         $sql_key_fields = "";
92         foreach($key["fields"] as $key_field){
93           $sql_key_fields .= ($sql_key_fields ? "," : "")."`".$key_field."`";
94         }
95         $sql_key = "KEY `#--".$table_name."_".$key_index."`(".$sql_key_fields.")";
96         if(isset($key["primary"]) && $key["primary"]){
97           $sql_key = "PRIMARY ".$sql_key;
98         }
99         $sql_keys .= ($sql_keys ? ", " : "").$sql_key;
100       }
101       foreach($options as $option_name => $option_value){
102         switch($option_name){
103           case "default_charset":
104             $sql_options .= " DEFAULT CHARSET=".$option_value;
105             break;
106         }
107       }
108       $sql = "CREATE TABLE `#--".$table_name."`(".$sql_fields.", ".$sql_keys.")".$sql_options;
109       try{
110         $sgbd->query($sql);
111       }
112       catch(Exception $e){
113         return false;
114       }
115       return true;
116     }
117
118     public function data_create_sqlite_table($params){
119       $sgbd = $this->sgbd();
120       $table_name = isset($params["table_name"]) ? $params["table_name"] : "";
121       $fields = isset($params["fields"]) ? $params["fields"] : array();
122       $keys = isset($params["keys"]) ? $params["keys"] : array();
123       $options = isset($params["options"]) ? $params["options"] : array();
124       if(!$table_name || !$fields) return false;
125       $sql_fields = "";
126       $sql_keys = "";
127       $sql_options = "";\r
128       foreach($fields as $field_name => $field){
129         if(!preg_match("/^[a-z]+[a-z_0-9]*$/", $field_name)) return false;
130         if(!isset($field["type"])) return false;
131         $sql_field = "";
132         switch($field["type"]){
133           case "int":
134             $sql_field .= "`".$field_name."` INTEGER";
135             break;
136           case "varchar":
137           case "text":
138           case "date":
139           case "datetime":
140             $sql_field .= "`".$field_name."` TEXT";
141             break;
142         }
143         if($sql_field){
144           $null = isset($field["null"]) ? $field["null"] : false;
145           $sql_field .= ($null ? " DEFAULT" : " NOT")." NULL";
146           if($null && isset($field["default"])){
147             $sql_field .= " DEFAULT ".$this->eq($field["default"]);
148           }
149           $sql_fields .= ($sql_fields ? ", " : "").$sql_field;
150         }
151       }
152       $key_index = 0;
153       foreach($keys as $key){
154         if(isset($key["primary"]) && $key["primary"]){
155           $key_index++;
156           $sql_key_fields = "";
157           foreach($key["fields"] as $key_field){
158             $sql_key_fields .= ($sql_key_fields ? "," : "")."`".$key_field."`";
159           }
160           $sql_key = "PRIMARY KEY (".$sql_key_fields.")";
161           $sql_keys .= ($sql_keys ? ", " : "").$sql_key;
162         }
163       }
164       $sql = "CREATE TABLE `#--".$table_name."`(".$sql_fields.", ".$sql_keys.")".$sql_options;
165       try{
166         $sgbd->query($sql);
167       }
168       catch(Exception $e){
169         return false;
170       }
171       return true;
172     }
173
174     # ----------------------------------------------------------------------------------------
175     #                                                                              description
176     #
177
178     public function data_desc($params = array()){
179       $sgbd = $this->sgbd();
180       $table_name = isset($params["table_name"]) ? $params["table_name"] : "";
181       if(!$table_name) return false;
182       try{
183         $desc = $sgbd->desc_table("#--".$table_name);
184       }
185       catch(Exception $e){ $desc = false; }
186       return $desc;
187     }
188
189     # ----------------------------------------------------------------------------------------
190     #                                                                                   insert
191     #
192
193     public function data_insert($params = array()){
194       $sgbd = $this->sgbd();
195       $table_name = isset($params["table_name"]) ? $params["table_name"] : "";
196       if(!$table_name) return false;
197       $values = (isset($params["values"]) && is_array($params["values"])) ? $params["values"] : false;
198       if(!$values) return false;
199       $return_insert_id = isset($params["return_insert_id"]) ? $params["return_insert_id"] : false;
200       $attributs_names = "";
201       $attributs_values = "";
202       foreach($values as $attribut_name => $attribut_value){
203         $attributs_names .= ($attributs_names ? ", " : "")."`".$attribut_name."`";
204         $attributs_values .= ($attributs_values ? ", " : "").$this->eq($attribut_value);
205       }
206       $attributs_names = $attributs_names ? "(".$attributs_names.")" : "";
207       $attributs_values = $attributs_values ? " VALUES (".$attributs_values.")" : "";
208       try{
209         $sgbd->query("INSERT INTO `#--".$table_name."`".$attributs_names.$attributs_values);
210         $res = $return_insert_id ? $sgbd->insert_id() : true;
211       }
212       catch(Exception $e) { $res = false; }
213       return $res;
214     }
215
216     # ----------------------------------------------------------------------------------------
217     #                                                                                     read
218     #
219
220     public function data_read($params = array()){
221       $sgbd = $this->sgbd();
222       $table_name = isset($params["table_name"]) ? $params["table_name"] : "";
223       $index_name = isset($params["index_name"]) ? $params["index_name"] : "";
224       $index_value = isset($params["index_value"]) ? $params["index_value"] : "";
225       if(!$table_name || !$index_name || !$index_value) return false;
226       $item = array();
227       try{
228         $sql = "SELECT * from `#--".$table_name."` WHERE `".$index_name."`=".$this->eq($index_value)." LIMIT 1";
229         $rst = $sgbd->query($sql);
230         if($v_rst = $sgbd->fetch_assoc($rst)) $item = $v_rst;
231         $sgbd->free_result($rst);
232       }
233       catch(Exception $e) { $item = false; }
234       return $item;
235     }
236
237     # ----------------------------------------------------------------------------------------
238     #                                                                                   update
239     #
240
241     public function data_update($params = array()){
242       $sgbd = $this->sgbd();
243       $table_name = isset($params["table_name"]) ? $params["table_name"] : false;
244       $values = (isset($params["values"]) && is_array($params["values"])) ? $params["values"] : false;
245       $index_name = isset($params["index_name"]) ? $params["index_name"] : false;
246       $index_value = isset($params["index_value"]) && (strlen($params["index_value"]) > 0) ? $params["index_value"] : false;
247       if(!$table_name || !$index_name || !$index_value || !$values) return false;
248       try{
249         $sql = "UPDATE `#--".$table_name."`";
250         $set = "";
251         foreach($values as $attribut_name => $attribut_value){
252           $set .= ($set ? ", " : "")."`".$attribut_name."`=".$this->eq($attribut_value);
253         }
254         $set = $set ? " SET ".$set : "";
255         $where = " WHERE `".$index_name."`=".$this->eq($index_value);
256         $sgbd->query($sql.$set.$where);
257       }
258       catch(Exception $e) { return false; }
259       return true;
260     }
261
262     # ----------------------------------------------------------------------------------------
263     #                                                                                   delete
264     #
265
266     public function data_delete($params = array()){
267       $sgbd = $this->sgbd();
268       $table_name = isset($params["table_name"]) ? $params["table_name"] : false;
269       $index_name = isset($params["index_name"]) ? $params["index_name"] : false;
270       $index_value = isset($params["index_value"]) ? $params["index_value"] : false;
271       if(!$table_name || !$index_name || !$index_value) return false;
272       try{
273         $sql = "DELETE FROM `#--".$table_name."` WHERE `".$index_name."`=".$this->eq($index_value);
274         $sgbd->query($sql);
275       }
276       catch(Exception $e) { debug($e->getMessage()); return false; }
277       return true;
278     }
279
280     # ----------------------------------------------------------------------------------------
281     #                                                                                     list
282     #
283
284     public function data_list($params = array()){
285       $sgbd = $this->sgbd();
286       $table_name = isset($params["table_name"]) ? $params["table_name"] : "";
287       if(!$table_name) return false;
288       $index_name = isset($params["index_name"]) ? $params["index_name"] : "";
289       $select = "SELECT `#--".$table_name."`.*";
290       $from = " FROM `#--".$table_name."`";
291       $where = isset($params["where"]) ? " WHERE ".$params["where"] : "";
292       if(isset($params["filters"])){
293         foreach($params["filters"] as $filter){
294           $and_where = "";
295           if(isset($filter[0]) && isset($filter[1])){
296             switch(strtolower($filter[1])){
297               case "eq":
298                 if(isset($filter[2])){
299                   $and_where .= ($where ? " AND " : " WHERE ").$filter[0]."=".$this->eq($filter[2]);
300                   break;
301                 }
302               case "lt":
303                 if(isset($filter[2])){
304                   $and_where .= ($where ? " AND " : " WHERE ").$filter[0]."<".$this->eq($filter[2]);
305                   break;
306                 }
307               case "lte":
308                 if(isset($filter[2])){
309                   $and_where .= ($where ? " AND " : " WHERE ").$filter[0].">=".$this->eq($filter[2]);
310                   break;
311                 }
312               case "gt":
313                 if(isset($filter[2])){
314                   $and_where .= ($where ? " AND " : " WHERE ").$filter[0].">".$this->eq($filter[2]);
315                   break;
316                 }
317               case "gte":
318                 if(isset($filter[2])){
319                   $and_where .= ($where ? " AND " : " WHERE ").$filter[0].">=".$this->eq($filter[2]);
320                   break;
321                 }
322             }
323           }
324           $where .= $and_where;
325         }
326       }
327       $order = isset($params["order_by"]) ?
328         " ORDER BY ".$params["order_by"].(isset($params["order"]) ? " ".$params["order"] : "")
329       : "";
330       $limit = isset($params["limit"]) ?
331         " LIMIT ".$params["limit"].(isset($params["offset"]) ? " OFFSET ".$params["offset"] : "")
332       : "";
333       $list = array("list" => array(), "total" => 0);
334       try{
335         $sql = "SELECT count(*) as n FROM(".$select.$from.$where.") res";
336         $rst = $sgbd->query($sql);
337         if($v_rst = $sgbd->fetch_assoc($rst)) $list["total"] = $v_rst["n"];
338         $sgbd->free_result($rst);
339         if($list["total"] > 0){
340           $sql = "SELECT * FROM(".$select.$from.$where.$order.$limit.") res";
341           $rst = $sgbd->query($sql);
342           while($v_rst = $sgbd->fetch_assoc($rst)){
343             if($index_name) $list["list"][$v_rst[$index_name]] = $v_rst;
344             else $list["list"][] = $v_rst;
345           }
346           $sgbd->free_result($rst);
347         }
348       }
349       catch(Exception $e) { $list = false; }
350       return $list;
351     }
352
353   }