diff options
Diffstat (limited to 'h-source/Library/Db')
-rwxr-xr-x | h-source/Library/Db/Mysql.php | 85 | ||||
-rw-r--r-- | h-source/Library/Db/Mysqli.php | 87 |
2 files changed, 121 insertions, 51 deletions
diff --git a/h-source/Library/Db/Mysql.php b/h-source/Library/Db/Mysql.php index 3f8d3ef..4545ea6 100755 --- a/h-source/Library/Db/Mysql.php +++ b/h-source/Library/Db/Mysql.php @@ -95,20 +95,55 @@ class Db_Mysql { return mysql_errno($this->dblink); } - public function createSelectQuery($table,$fields='*',$where=null,$group_by=null,$order_by=null,$limit=null,$on=null,$using=null) + public function getJoinString($string) { - if (isset($where)) + if (strstr($string,':')) { - $where='WHERE '.$where; + $tArray = explode(':',$string); + switch($tArray[0]) + { + case 'i': + $jString = ' INNER JOIN ' . $tArray[1]; + break; + case 'l': + $jString = ' LEFT JOIN ' . $tArray[1]; + break; + case 'r': + $jString = ' RIGHT JOIN ' . $tArray[1]; + break; + default: + $jString = ' INNER JOIN ' . $tArray[1]; + break; + } + return $jString; } - if (isset($using)) + else + { + return ' INNER JOIN '.$string; + } + } + + public function createSelectQuery($table,$fields='*',$where=null,$group_by=null,$order_by=null,$limit=null,$on=array(),$using=array(),$join=array()) + { + $maxValue = max(count($on),count($using),count($join)); + + $joinString = null; + for ($i=0; $i < $maxValue; $i++) { - $using ='USING ('.$using.')'; - $on = null; + $joinString .= isset($join[$i]) ? $this->getJoinString($join[$i]) : null; + if (isset($using[$i])) + { + $joinString .= ' USING ('.$using[$i].')'; + } + else if (isset($on[$i])) + { + $joinString .= ' ON '.$on[$i]; + } } - if (isset($on) and !isset($using)) + + if (isset($where)) { - $on='ON '.$on; + $where='WHERE '.$where; } if (isset($order_by)) { $order_by='ORDER BY '.$order_by; @@ -120,13 +155,13 @@ class Db_Mysql { $limit='LIMIT '.$limit; } - $query="SELECT $fields FROM $table $on $using $where $group_by $order_by $limit;"; + $query="SELECT $fields FROM $table $joinString $where $group_by $order_by $limit;"; return $query; } - public function get_num_rows($table,$where=null,$group_by=null,$on=null,$using=null) { + public function get_num_rows($table,$where=null,$group_by=null,$on=array(),$using=array(),$join=array()) { - $query = $this->createSelectQuery($table,'*',$where,$group_by,null,null,$on,$using); + $query = $this->createSelectQuery($table,'*',$where,$group_by,null,null,$on,$using,$join); $this->query=$query; @@ -140,9 +175,9 @@ class Db_Mysql { } //get the maximum value of the field $field of the table $table having the $where conditions - public function getMath($func,$table,$field,$where=null,$group_by = null, $on=null,$using=null) + public function getMath($func,$table,$field,$where=null,$group_by = null, $on=array(),$using=array(),$join=array()) { - $query = $this->createSelectQuery($table,"$func($field) AS m",$where,$group_by,null,null,$on,$using); + $query = $this->createSelectQuery($table,"$func($field) AS m",$where,$group_by,null,null,$on,$using,$join); $this->query = $query; $result = mysql_query($query); @@ -158,32 +193,32 @@ class Db_Mysql { } //get the maximum value of the field $field of the table $table having the $where conditions - public function getMax($table,$field,$where=null,$group_by = null,$on=null,$using=null) + public function getMax($table,$field,$where=null,$group_by = null,$on=array(),$using=array(),$join=array()) { - return $this->getMath('MAX',$table,$field,$where,$group_by,$on,$using); + return $this->getMath('MAX',$table,$field,$where,$group_by,$on,$using,$join); } //get the minimum value of the field $field of the table $table having the $where conditions - public function getMin($table,$field,$where=null,$group_by = null,$on=null,$using=null) + public function getMin($table,$field,$where=null,$group_by = null,$on=array(),$using=array(),$join=array()) { - return $this->getMath('MIN',$table,$field,$where,$group_by,$on,$using); + return $this->getMath('MIN',$table,$field,$where,$group_by,$on,$using,$join); } //get the sum of the fields - public function getSum($table,$field,$where=null,$group_by = null,$on=null,$using=null) + public function getSum($table,$field,$where=null,$group_by = null,$on=array(),$using=array(),$join=array()) { - return $this->getMath('SUM',$table,$field,$where,$group_by,$on,$using); + return $this->getMath('SUM',$table,$field,$where,$group_by,$on,$using,$join); } //get the average of the fields - public function getAvg($table,$field,$where=null,$group_by = null,$on=null,$using=null) + public function getAvg($table,$field,$where=null,$group_by = null,$on=array(),$using=array(),$join=array()) { - return $this->getMath('AVG',$table,$field,$where,$group_by,$on,$using); + return $this->getMath('AVG',$table,$field,$where,$group_by,$on,$using,$join); } - public function select($table,$fields='*',$where=null,$group_by=null,$order_by=null,$limit=null,$on=null,$using=null) + public function select($table,$fields='*',$where=null,$group_by=null,$order_by=null,$limit=null,$on=array(),$using=array(),$join=array()) { - $query = $this->createSelectQuery($table,$fields,$where,$group_by,$order_by,$limit,$on,$using); + $query = $this->createSelectQuery($table,$fields,$where,$group_by,$order_by,$limit,$on,$using,$join); $this->query = $query; $result = mysql_query($query); @@ -364,7 +399,7 @@ class Db_Mysql { } // function to check if exist the record having the field $id_name=$id_value - public function recordExists($table,$fieldName,$fieldValue,$where = null,$groupBy=null,$on=null,$using=null) + public function recordExists($table,$fieldName,$fieldValue,$where = null,$groupBy=null,$on=array(),$using=array(),$join=array()) { if (isset($where)) { @@ -373,7 +408,7 @@ class Db_Mysql { $fieldValue = '"'.$fieldValue.'"'; - $num=$this->get_num_rows($table,$fieldName.'='.$fieldValue.$where,$groupBy,$on,$using); + $num=$this->get_num_rows($table,$fieldName.'='.$fieldValue.$where,$groupBy,$on,$using,$join); $res=($num>0) ? true : false; return $res; diff --git a/h-source/Library/Db/Mysqli.php b/h-source/Library/Db/Mysqli.php index 09bb017..e6fdb15 100644 --- a/h-source/Library/Db/Mysqli.php +++ b/h-source/Library/Db/Mysqli.php @@ -101,21 +101,56 @@ class Db_Mysqli { return $this->db->errno; } - - public function createSelectQuery($table,$fields='*',$where=null,$group_by=null,$order_by=null,$limit=null,$on=null,$using=null) + + public function getJoinString($string) { - if (isset($where)) + if (strstr($string,':')) { - $where='WHERE '.$where; + $tArray = explode(':',$string); + switch($tArray[0]) + { + case 'i': + $jString = ' INNER JOIN ' . $tArray[1]; + break; + case 'l': + $jString = ' LEFT JOIN ' . $tArray[1]; + break; + case 'r': + $jString = ' RIGHT JOIN ' . $tArray[1]; + break; + default: + $jString = ' INNER JOIN ' . $tArray[1]; + break; + } + return $jString; } - if (isset($using)) + else + { + return ' INNER JOIN '.$string; + } + } + + public function createSelectQuery($table,$fields='*',$where=null,$group_by=null,$order_by=null,$limit=null,$on=array(),$using=array(),$join=array()) + { + $maxValue = max(count($on),count($using),count($join)); + + $joinString = null; + for ($i=0; $i < $maxValue; $i++) { - $using ='USING ('.$using.')'; - $on = null; + $joinString .= isset($join[$i]) ? $this->getJoinString($join[$i]) : null; + if (isset($using[$i])) + { + $joinString .= ' USING ('.$using[$i].')'; + } + else if (isset($on[$i])) + { + $joinString .= ' ON '.$on[$i]; + } } - if (isset($on) and !isset($using)) + + if (isset($where)) { - $on='ON '.$on; + $where='WHERE '.$where; } if (isset($order_by)) { $order_by='ORDER BY '.$order_by; @@ -127,13 +162,13 @@ class Db_Mysqli $limit='LIMIT '.$limit; } - $query="SELECT $fields FROM $table $on $using $where $group_by $order_by $limit;"; + $query="SELECT $fields FROM $table $joinString $where $group_by $order_by $limit;"; return $query; } - public function get_num_rows($table,$where=null,$group_by=null,$on=null,$using=null) { + public function get_num_rows($table,$where=null,$group_by=null,$on=array(),$using=array(),$join=array()) { - $query = $this->createSelectQuery($table,'*',$where,$group_by,null,null,$on,$using); + $query = $this->createSelectQuery($table,'*',$where,$group_by,null,null,$on,$using,$join); $this->query = $query; $ris = $this->db->query($query); @@ -146,9 +181,9 @@ class Db_Mysqli } } - public function getMath($func,$table,$field,$where=null,$group_by = null, $on=null,$using=null) + public function getMath($func,$table,$field,$where=null,$group_by = null, $on=array(),$using=array(),$join=array()) { - $query = $this->createSelectQuery($table,"$func($field) AS m",$where,$group_by,null,null,$on,$using); + $query = $this->createSelectQuery($table,"$func($field) AS m",$where,$group_by,null,null,$on,$using,$join); $this->query = $query; $result = $this->db->query($query); @@ -165,32 +200,32 @@ class Db_Mysqli } //get the maximum value of the field $field of the table $table having the $where conditions - public function getMax($table,$field,$where=null,$group_by = null,$on=null,$using=null) + public function getMax($table,$field,$where=null,$group_by = null,$on=array(),$using=array(),$join=array()) { - return $this->getMath('MAX',$table,$field,$where,$group_by,$on,$using); + return $this->getMath('MAX',$table,$field,$where,$group_by,$on,$using,$join); } //get the minimum value of the field $field of the table $table having the $where conditions - public function getMin($table,$field,$where=null,$group_by = null,$on=null,$using=null) + public function getMin($table,$field,$where=null,$group_by = null,$on=array(),$using=array(),$join=array()) { - return $this->getMath('MIN',$table,$field,$where,$group_by,$on,$using); + return $this->getMath('MIN',$table,$field,$where,$group_by,$on,$using,$join); } //get the sum of the fields - public function getSum($table,$field,$where=null,$group_by = null,$on=null,$using=null) + public function getSum($table,$field,$where=null,$group_by = null,$on=array(),$using=array(),$join=array()) { - return $this->getMath('SUM',$table,$field,$where,$group_by,$on,$using); + return $this->getMath('SUM',$table,$field,$where,$group_by,$on,$using,$join); } //get the average of the fields - public function getAvg($table,$field,$where=null,$group_by = null,$on=null,$using=null) + public function getAvg($table,$field,$where=null,$group_by = null,$on=array(),$using=array(),$join=array()) { - return $this->getMath('AVG',$table,$field,$where,$group_by,$on,$using); + return $this->getMath('AVG',$table,$field,$where,$group_by,$on,$using,$join); } - public function select($table,$fields='*',$where=null,$group_by=null,$order_by=null,$limit=null,$on=null,$using=null) + public function select($table,$fields='*',$where=null,$group_by=null,$order_by=null,$limit=null,$on=array(),$using=array(),$join=array()) { - $query = $this->createSelectQuery($table,$fields,$where,$group_by,$order_by,$limit,$on,$using); + $query = $this->createSelectQuery($table,$fields,$where,$group_by,$order_by,$limit,$on,$using,$join); $this->query = $query; $result = $this->db->query($query); @@ -370,7 +405,7 @@ class Db_Mysqli //function to check if exist the record having the field $id_name=$id_value - public function recordExists($table,$fieldName,$fieldValue,$where = null,$groupBy=null,$on=null,$using=null) + public function recordExists($table,$fieldName,$fieldValue,$where = null,$groupBy=null,$on=array(),$using=array(),$join=array()) { if (isset($where)) { @@ -379,7 +414,7 @@ class Db_Mysqli $fieldValue = '"'.$fieldValue.'"'; - $num = $this->get_num_rows($table,$fieldName.'='.$fieldValue.$where,$groupBy,$on,$using); + $num = $this->get_num_rows($table,$fieldName.'='.$fieldValue.$where,$groupBy,$on,$using,$join); $res=($num>0) ? true : false; return $res; |