3 class mw_data_sql_crud extends mw_data{
5 # ----------------------------------------------------------------------------------------
9 public function set_sgbd_param($key, $value){
11 if(($env->bdd("sgbd") == "pdo_mysql") || ($env->bdd("sgbd") == "mysql")){
12 return $this->set_sgbd_mysql_param($key, $value);
17 public function set_sgbd_mysql_param($key, $value){
18 $sgbd = $this->sgbd();
19 $sql = "SET ".$key."=\"".$value."\"";
29 # ----------------------------------------------------------------------------------------
33 public function data_create_table($params){
35 if($env->bdd("sgbd") == "pdo_sqlite"){
36 return $this->data_create_sqlite_table($params);
38 return $this->data_create_mysql_table($params);
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;
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;
55 switch($field["type"]){
57 $value = isset($field["value"]) ? $field["value"] : "11";
58 if(!preg_match("/^[0-9]+$/", $value)) return false;
59 $sql_field .= "`".$field_name."` int(".$value.")";
62 $value = isset($field["value"]) ? $field["value"] : "255";
63 if(!preg_match("/^[0-9]+$/", $value)) return false;
64 $sql_field .= "`".$field_name."` varchar(".$value.")";
67 $sql_field .= "`".$field_name."` text";
70 $sql_field .= "`".$field_name."` date";
73 $sql_field .= "`".$field_name."` datetime";
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"]);
82 if(isset($field["autoincrement"]) && $field["autoincrement"]){
83 $sql_field .= " AUTO_INCREMENT";
85 $sql_fields .= ($sql_fields ? ", " : "").$sql_field;
89 foreach($keys as $key){
92 foreach($key["fields"] as $key_field){
93 $sql_key_fields .= ($sql_key_fields ? "," : "")."`".$key_field."`";
95 $sql_key = "KEY `#--".$table_name."_".$key_index."`(".$sql_key_fields.")";
96 if(isset($key["primary"]) && $key["primary"]){
97 $sql_key = "PRIMARY ".$sql_key;
99 $sql_keys .= ($sql_keys ? ", " : "").$sql_key;
101 foreach($options as $option_name => $option_value){
102 switch($option_name){
103 case "default_charset":
104 $sql_options .= " DEFAULT CHARSET=".$option_value;
108 $sql = "CREATE TABLE `#--".$table_name."`(".$sql_fields.", ".$sql_keys.")".$sql_options;
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;
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;
132 switch($field["type"]){
134 $sql_field .= "`".$field_name."` INTEGER";
140 $sql_field .= "`".$field_name."` TEXT";
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"]);
149 $sql_fields .= ($sql_fields ? ", " : "").$sql_field;
153 foreach($keys as $key){
154 if(isset($key["primary"]) && $key["primary"]){
156 $sql_key_fields = "";
157 foreach($key["fields"] as $key_field){
158 $sql_key_fields .= ($sql_key_fields ? "," : "")."`".$key_field."`";
160 $sql_key = "PRIMARY KEY (".$sql_key_fields.")";
161 $sql_keys .= ($sql_keys ? ", " : "").$sql_key;
164 $sql = "CREATE TABLE `#--".$table_name."`(".$sql_fields.", ".$sql_keys.")".$sql_options;
174 # ----------------------------------------------------------------------------------------
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;
183 $desc = $sgbd->desc_table("#--".$table_name);
185 catch(Exception $e){ $desc = false; }
189 # ----------------------------------------------------------------------------------------
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);
206 $attributs_names = $attributs_names ? "(".$attributs_names.")" : "";
207 $attributs_values = $attributs_values ? " VALUES (".$attributs_values.")" : "";
209 $sgbd->query("INSERT INTO `#--".$table_name."`".$attributs_names.$attributs_values);
210 $res = $return_insert_id ? $sgbd->insert_id() : true;
212 catch(Exception $e) { $res = false; }
216 # ----------------------------------------------------------------------------------------
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;
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);
233 catch(Exception $e) { $item = false; }
237 # ----------------------------------------------------------------------------------------
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;
249 $sql = "UPDATE `#--".$table_name."`";
251 foreach($values as $attribut_name => $attribut_value){
252 $set .= ($set ? ", " : "")."`".$attribut_name."`=".$this->eq($attribut_value);
254 $set = $set ? " SET ".$set : "";
255 $where = " WHERE `".$index_name."`=".$this->eq($index_value);
256 $sgbd->query($sql.$set.$where);
258 catch(Exception $e) { return false; }
262 # ----------------------------------------------------------------------------------------
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;
273 $sql = "DELETE FROM `#--".$table_name."` WHERE `".$index_name."`=".$this->eq($index_value);
276 catch(Exception $e) { return false; }
280 # ----------------------------------------------------------------------------------------
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){
295 if(isset($filter[0]) && isset($filter[1])){
296 switch(strtolower($filter[1])){
298 if(isset($filter[2])){
299 $and_where .= ($where ? " AND " : " WHERE ").$filter[0]."=".$this->eq($filter[2]);
303 if(isset($filter[2])){
304 $and_where .= ($where ? " AND " : " WHERE ").$filter[0]."<".$this->eq($filter[2]);
308 if(isset($filter[2])){
309 $and_where .= ($where ? " AND " : " WHERE ").$filter[0].">=".$this->eq($filter[2]);
313 if(isset($filter[2])){
314 $and_where .= ($where ? " AND " : " WHERE ").$filter[0].">".$this->eq($filter[2]);
318 if(isset($filter[2])){
319 $and_where .= ($where ? " AND " : " WHERE ").$filter[0].">=".$this->eq($filter[2]);
324 $where .= $and_where;
327 $order = isset($params["order_by"]) ?
328 " ORDER BY ".$params["order_by"].(isset($params["order"]) ? " ".$params["order"] : "")
330 $limit = isset($params["limit"]) ?
331 " LIMIT ".$params["limit"].(isset($params["offset"]) ? " OFFSET ".$params["offset"] : "")
333 $list = array("list" => array(), "total" => 0);
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;
346 $sgbd->free_result($rst);
349 catch(Exception $e) { $list = false; }