<?php
class HDB_Query{
function __construct($queryType='content'){
$this->topOperator = 'AND';
$this->queryType = $queryType;
return $this;
}
function filter(){
$args = func_get_args();
if (count($args) == 3){
list($field, $operator, $value) = func_get_args();
}elseif((count($args) -1)%3 == 0){
for ($i = 1; $i < count($args); $i+=3){
$operator[] = array ('field' => $args[$i],
'operator' =>$args[$i+1],
'value' =>$args[$i+2]
);
}
}else{
throw new Exception('HDB_Query: Unreasonable number of arguments'
. ' submitted to HDB_Query->filter()');
}
$this->filter[] = array ( 'field' =>$field,
'operator' => $operator,
'value' => $value );
return $this;
}
function setTopOperator($operator){
$OPERATORS = array('OR','AND');
if (in_array($operator, $OPERATORS)){
$this->topOperator = $operator;
}
}
function execute(){
global $link, $shapes;
mysql_select_db('adjoottshirtuser', $link) ;
$sql = $this->getSQL($this->filter, 'AND');
$userQuery = mysql_query($sql)
or die('SELECT FAILED: '.mysql_error());
if(mysql_num_rows($userQuery) > 0){
while ($row = mysql_fetch_array( $userQuery ))
$results[] = $row;
//print_r($row);
//return $row;
}
//cast result into shape
if (!empty($results) && ($this->queryType=='content')){
foreach ($results as $result){
if (!empty($shapes[$result['type']])){
$newObject = $shapes[$result['type']]->castObject($result);
$newResults[] = $newObject;
unset($newResult);
}
}
}
elseif(!empty($results) && ($this->queryType=='count')){
$newResults = $results[0][0];
}
return $newResults;
}
function getSQL($filters=FALSE, $joinOperator=FALSE, $sub=FALSE){
global $shapes, $contentTable,
$allowedStrAttributes, $allowedNumAttributes,
$allowedStrOperators, $allowedNumOperators;
if (empty($joinOperator)){
$joinOperator = $this->topOperator;
}
$OPERATORS = array( 'AND' => 'OR',
'OR' => 'AND',
);
//support empty filters argument
if (empty($filters)){
$filters = $this->filter;
}
if ($this->queryType == 'content'){
$sql = 'SELECT * from '.$contentTable.' ';
}
elseif($this->queryType == 'count'){
$sql = 'SELECT COUNT(*) from '.$contentTable.' ';
}
if (!empty($filters)){
foreach ($filters as $filter){
if (is_array($filter['operator'])){
$sqlWhereArray[] = $this->getSQL($filter['operator'], $OPERATORS[$joinOperator], TRUE);
}elseif (substr($filter['field'], 0, 2) == 'my'){
//handle my attributes
$sqlAttributes = $this->_getSQLFromDev($filter['field']);
$sqlMyArray = array();
if (!empty($sqlAttributes)){
foreach ($sqlAttributes as $attArray){
$dataType = $attArray['dataType'];
$sqlField = $attArray['sql'];
if ($dataType == 'num'){
$sqlMyArray[] = $sqlField .' '.$filter['operator'].' '.$filter['value'];
}
else{
$sqlMyArray[] = $sqlField .' '.$filter['operator'].' "'.$filter['value'].'"';
}
}
$sqlWhereArray[] = '('.implode(' OR ', $sqlMyArray).')';
}
//handle standard attributes
}elseif($allowedStrAttributes[$filter['field']]){
if ($allowedStrOperators[$filter['operator']]){
if ($allowedStrOperators[$filter['operator']]!='function'){
$sqlWhereArray[] = $filter['field'].' '.$allowedStrOperators[$filter['operator']].' "'.$filter['value'].'"';
}
}else{
echo "illegal str operator<br>";
}
}elseif($allowedNumAttributes[$filter['field']]){
if ($allowedNumOperators[$filter['operator']]){
if ($allowedNumOperators[$filter['operator']]!='function'){
$sqlWhereArray[] = $filter['field'].' '.$allowedNumOperators[$filter['operator']].' '.$filter['value'];
}
}
}else{
echo "<br>invalid filter: ".$filter['field']."<br>";
}
}
}
if (!empty($sqlWhereArray)){
$sqlWhere = implode(' '.$joinOperator.' ', $sqlWhereArray);
$sql .= 'WHERE '.$sqlWhere;
}
$sql .= ' limit 100';
if ($sub){
return '('.$sqlWhere.')';
}else{
return $sql;
}
}
function debugHTML(){
$tab = " ";
$tab2 = $tab.$tab;
$output = "<pre>Query:<br>";
if (!empty($this->filter)){
foreach ($this->filter as $filter){
$output .= $tab . "Next Filter<br>";
$output .= $tab2 . "Field: ".$filter['field']."<br>";
$output .= $tab2 . "Operator: ".$filter['operator']."<br>";
$output .= $tab2 . "Value: ".$filter['value']."<br>";
}
}
$output .= "</pre>";
return $output;
}
function _getSQLFromDev($myAttribute){
global $shapes;
//remove my. or my-> if necessary
if (substr($myAttribute, 0, 3) == 'my.'){
$myAttribute = (substr($myAttribute, 3));
}
elseif (substr($myAttribute, 0, 4) == 'my->'){
$myAttribute = (substr($myAttribute, 4));
}
//only numbers and letters allowed from this point on
$cleanAtt = preg_replace('/[^a-zA-Z0-9]/', '', $myAttribute);
if ($myAttribute == $cleanAtt){
//iterate over shapes, returning all possible matches
if (!empty($shapes)){
foreach ($shapes as $shape){
$SQL = $shape->reverse->$myAttribute;
if (!empty($SQL)){
$dataType = getMyDataType($SQL);
if (!empty($dataType)){
$sqlAttributes[] = array('sql' => $SQL,
'dataType' => $dataType);
}
}
}
}
return $sqlAttributes;
}
}
}
/*
Copyright 2011 Oversee.net. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are
permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of
conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list
of conditions and the following disclaimer in the documentation and/or other materials
provided with the distribution.
THIS SOFTWARE IS PROVIDED BY OVERSEE.NET ``AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL OVERSEE.NET OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those of the
authors and should not be interpreted as representing official policies, either expressed
or implied, of Oversee.net.
*/
?>