/*
* @function 解析where条件数组
* @param $where where条件数组
* @return array
* @notice 只支持简单的条件语句
* $where的数据结构如下
$where=array(
array("id",">","3"),
array("id","<","10"),
);
*/
public function analyzeWhere($where){
$arr=array();
if(!is_array($where)||empty($where)){
$arr["str"]="";
$arr["value_arr"]=array();
}
else{
$where_arr=array();
$where_value_arr=array();
foreach($where as $key=>$value){
if(count($value)==3){
if($value[1]=="in"){
if(empty($value[2])){
continue;
}
else{
$where_arr[]= "`".$value[0]."` ".$val[1]." ('".implode("','",$value[2])."') ";
}
}
else if($value[1]=="like"){
$where_arr[]="`".$value[0]."` ".$value[1]." '%".$value[2]."%' ";
}
else{
$where_arr[]="`".$value[0]."` ".$value[1]." ? ";
if(is_numeric($value[2])){
$where_value_arr[]=$value[2];
}
else{
$where_value_arr[]='\''.$value[2]."\'";
}
}
}
}
$arr["str"]=!empty($where_arr) ? " WHERE ".implode(" and ",$where_arr) : "";
$arr["value_arr"]=$where_value_arr;
}
return $arr;
}
/*
* @function 解析group数组
* @param $group group数组
* @return array
* @notice group by 后跟字段,所以不需要占位符
$group数据格式如下
$group=array(
"id","ct",
);
*/
public function analyzeGroup($group){
$arr=array();
if(!is_array($group)||empty($group)){
$arr["str"]="";
}
else{
$groupArr=array();
foreach($group as $key=>$value){
$groupArr[]=$value;
}
$str_group=implode(",",$groupArr);
$arr["str"]=" GROUP BY ".$str_group;
}
$arr["value_arr"]=array();
return $arr;
}
/*
* @function 解析having数组
* @param $having having数组
* @return array
* @notice
$having数据格式如下
$having=array(
"id","=","6",
);
*/
public function analyzeHaving($having){
$arr=array();
if(!is_array($having)||empty($having)){
$arr["str"]="";
}
else{
$str_having="";
foreach($having as $key=>$value){
$str_having.=$value." ";
}
$arr["str"]=" HAVING ".$str_having;
}
$arr["value_arr"]=array();
return $arr;
}
/*
* @function 解析order数组
* @param $order order数组
* @return array
* @notice order by 后跟字段所以不需要占位符
$order数据格式如下
$order=array(
"id","ct",
);
*/
public function analyzeOrder($order){
$arr=array();
if(!is_array($order)||empty($order)){
$arr["str"]="";
}
else{
$orderArr=array();
foreach($order as $key=>$value){
$orderArr[]=$value;
}
$str_order=implode(",",$orderArr);
$arr["str"]=" ORDER BY ".$str_order;
}
$arr["value_arr"]=array();
return $arr;
}
/*
* @function 解析limit数组
* @param $limit limit数组
* @return array
* @notice $limit数据格式如下
$limit=array(
"id","ct",
);
*/
public function analyzeLimit($limit){
$arr=array();
if(!is_array($limit)||empty($limit)){
$arr["str"]="";
$arr["value_arr"]=array();
}
else{
$limitArr=array();
$limitValueArr=array();
foreach($limit as $key=>$value){
$limitArr[]="?";
$limitValueArr[]=$value;
}
$str_limit=implode(",",$limitArr);
$arr["str"]=" LIMIT ".$str_limit;
$arr["value_arr"]=$limitValueArr;
}
return $arr;
}
/*
* @function 释放结果集
*/
public function free(){
if($this->stmt){
$this->stmt=null;
}
}
/*
* @function 断开连接
*/
public function close(){
if($this->conn){
$this->conn=null;
}
}
}
$dbhelper=new pdo_dbhelper();
$result=$dbhelper->readAll("category",array("name"),array("where"=>array(array("id","=","3"))));
$dbhelper->free();
$dbhelper->close();
print_r($result);
?>