Menu

[r2]: / lib / hdb_query.php  Maximize  Restore  History

Download this file

232 lines (199 with data), 9.2 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?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 = "&nbsp;&nbsp;";
$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.
*/
?>
MongoDB Logo MongoDB