sgbd(); $table_name = isset($params["table_name"]) ? $params["table_name"] : ""; if(!$table_name) return false; try{ $desc = $sgbd->desc_table("#--".$table_name); } catch(Exception $e){ $desc = false; } return $desc; } # ---------------------------------------------------------------------------------------- # insert # function data_insert($params = array()){ $sgbd = $this->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_values.")" : ""; 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["index_name"]) ? $params["index_name"] : ""; $index_value = isset($params["index_value"]) ? $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; } # ---------------------------------------------------------------------------------------- # 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["index_name"]) ? $params["index_name"] : false; $index_value = isset($params["index_value"]) && (strlen($params["index_value"]) > 0) ? $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.$set.$where); } catch(Exception $e) { return false; } return true; } # ---------------------------------------------------------------------------------------- # delete # function data_delete($params = array()){ $sgbd = $this->sgbd(); $table_name = isset($params["table_name"]) ? $params["table_name"] : false; $index_name = isset($params["index_name"]) ? $params["index_name"] : false; $index_value = isset($params["index_value"]) ? $params["index_value"] : false; if(!$table_name || !$index_name || !$index_value) return false; try{ $sql = "DELETE FROM `#--".$table_name."` WHERE `".$index_name."`=".$this->eq($index_value); $sgbd->query($sql); } catch(Exception $e) { debug($e->getMessage()); return false; } return true; } # ---------------------------------------------------------------------------------------- # 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["index_name"] : ""; $select = "SELECT `#--".$table_name."`.*"; $from = " FROM `#--".$table_name."`"; $where = isset($params["where"]) ? " WHERE ".$params["where"] : ""; if(isset($params["filters"])){ foreach($params["filters"] as $filter){ $and_where = ""; if(isset($filter[0]) && isset($filter[1])){ switch(strtolower($filter[1])){ case "eq": if(isset($filter[2])){ $and_where .= ($where ? " AND " : " WHERE ").$filter[0]."=".$this->eq($filter[2]); break; } case "lt": if(isset($filter[2])){ $and_where .= ($where ? " AND " : " WHERE ").$filter[0]."<".$this->eq($filter[2]); break; } case "lte": if(isset($filter[2])){ $and_where .= ($where ? " AND " : " WHERE ").$filter[0].">=".$this->eq($filter[2]); break; } case "gt": if(isset($filter[2])){ $and_where .= ($where ? " AND " : " WHERE ").$filter[0].">".$this->eq($filter[2]); break; } case "gte": if(isset($filter[2])){ $and_where .= ($where ? " AND " : " WHERE ").$filter[0].">=".$this->eq($filter[2]); break; } } } $where .= $and_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; } } ?>