00001 <?php
00002 class MQuery extends MDataSet
00003 {
00004 var $conn;
00005 var $objSql;
00006 var $sql;
00007 var $error;
00008 var $statement;
00009 var $fetched;
00010 var $maxrows;
00011 var $offset;
00012 var $order;
00013 var $filtered;
00014 var $pagelength;
00015 var $queryfilter;
00016 var $_miolo;
00017
00018 function __construct()
00019 {
00020 parent::__construct();
00021 $this->fetched = false;
00022 $this->row = -1;
00023 $this->error = NULL;
00024 $this->filtered = false;
00025 $this->pagelength = 0;
00026 $this->queryfilter = NULL;
00027 $this->_miolo = MIOLO::GetInstance();
00028 }
00029
00030 public function _query()
00031 {
00032 }
00033
00034 public function _error()
00035 {
00036 }
00037
00038 public function _close()
00039 {
00040 }
00041
00042 public function _setmetadata()
00043 {
00044 }
00045
00046 function GetError()
00047 {
00048 return $this->error;
00049 }
00050
00051 function Open($maxrows = null, $offset = null, $stmt = NULL)
00052 {
00053 $this->_miolo->LogSQL($this->sql, false, $this->conn->db->conf);
00054
00055 $this->maxrows = $maxrows;
00056 $this->offset = $offset;
00057
00058 if ($stmt != NULL)
00059 {
00060 $this->_querystmt($stmt);
00061 }
00062 else
00063 {
00064 $this->_query();
00065 }
00066
00067 $this->_setmetadata();
00068
00069 if ($this->rowCount)
00070 {
00071 $this->row = 0;
00072 $this->eof = $this->bof = false;
00073 $this->fetched = true;
00074 }
00075 else
00076 {
00077 $this->result = NULL;
00078 $this->row = -1;
00079 $this->eof = $this->bof = true;
00080 $this->fetched = false;
00081 }
00082
00083 $this->error = $this->_error();
00084
00085 if ($this->error)
00086 throw new EDatabaseQueryException($this->error);
00087
00088 return ($this->result != NULL);
00089 }
00090
00091 function Close()
00092 {
00093 if ($this->fetched)
00094 {
00095 $this->_close();
00096 }
00097 }
00098
00099 function SetConnection(&$conn)
00100 {
00101 $this->conn = $conn;
00102 }
00103
00104 function SetSQL(&$sql)
00105 {
00106 $this->sql = $sql->Select();
00107 $this->objSql = &$sql;
00108 }
00109
00110 function SetSQLCommand($sqlCommand)
00111 {
00112 $this->sql = $sqlCommand;
00113 }
00114
00115 function SetOrder($order)
00116 {
00117 $order = explode(',', $order);
00118 $this->order = $order;
00119
00120 foreach ($this->order as $o)
00121 $p[] = $this->GetColumnNumber($o);
00122
00123 $n = count($this->result[0]);
00124
00125 foreach ($this->result as $key => $row)
00126 {
00127 for ($i = 0; $i < $n; $i++)
00128 $arr[$i][$key] = $row[$i];
00129 }
00130
00131 foreach ($p as $i => $o)
00132 $sortcols .= ($i > 0 ? ",\$arr[$o]" : "\$arr[$o]");
00133
00134 for ($i = 0; $i < $n; $i++)
00135 if (!in_array($i, $p))
00136 $sortcols .= ",\$arr[$i]";
00137
00138 eval ("array_multisort({$sortcols}, SORT_ASC);");
00139 $this->result = array
00140 (
00141 );
00142
00143 for ($i = 0; $i < $n; $i++)
00144 {
00145 foreach ($arr[$i] as $key => $row)
00146 $this->result[$key][$i] = $row;
00147 }
00148 }
00149
00150 function isFiltered()
00151 {
00152 return $this->filtered;
00153 }
00154
00155 function AddFilter($field, $oper, $value, $conector = 'AND')
00156 {
00157 if (!$this->queryfilter)
00158 $this->queryfilter = new QueryFilter($this);
00159 $this->queryfilter->AddFilter($field, $oper, $value, $conector);
00160 }
00161
00162 function ApplyFilter()
00163 {
00164 if (!$this->queryfilter)
00165 return;
00166
00167 $this->result = $this->queryfilter->ApplyFilter($this->result);
00168 $this->filtered = true;
00169 $this->rowCount = count($this->result);
00170
00171 if ($this->rowCount)
00172 {
00173 $this->row = 0;
00174 $this->eof = $this->bof = false;
00175 $this->fetched = true;
00176 }
00177 else
00178 {
00179 $this->result = NULL;
00180 $this->row = -1;
00181 $this->eof = $this->bof = true;
00182 $this->fetched = false;
00183 }
00184 }
00185
00186 function SetPageLength($pagelength)
00187 {
00188 $this->pagelength = $pagelength;
00189 }
00190
00191 function GetPageCount()
00192 {
00193 return (int)(($this->rowCount - 1 + $this->pagelength) / $this->pagelength);
00194 }
00195
00196 function GetPage($pageno)
00197 {
00198 if ($this->result)
00199 {
00200 if ($this->pagelength)
00201 {
00202 return array_slice($this->result, $this->pagelength * ($pageno - 1), $this->pagelength);
00203 }
00204 else
00205 return $this->result;
00206 }
00207 }
00208
00209 function GetCSV($filename = '')
00210 {
00211 global $MIOLO;
00212 $MIOLO->Uses('csv.class');
00213 $csvdump = new csvdump();
00214
00215 if ($this->result)
00216 {
00217 $csvdump->dump($this->result, $filename);
00218 }
00219 exit;
00220 }
00221 }
00222
00223
00224 class QueryFilter
00225 {
00226 var $filters;
00227 var $count = 0;
00228 var $query;
00229
00230 function __construct($query)
00231 {
00232 $this->query = $query;
00233 }
00234
00235 function AddFilter($field, $oper, $value, $conector = 'AND')
00236 {
00237 $oper = strtolower($oper);
00238
00239 if ($oper == 'like')
00240 {
00241 $value = str_replace("?", ".", $value);
00242 $value = str_replace("_", ".", $value);
00243 $value = str_replace("%", "(.*?)", $value);
00244 $value = "^" . $value . "(.*?)";
00245 $oper = 'regex';
00246 }
00247
00248 $this->filters[$this->count]['field'] = $field;
00249 $this->filters[$this->count]['fieldpos'] = $this->query->GetColumnNumber($field);
00250 $this->filters[$this->count]['oper'] = $oper;
00251 $this->filters[$this->count]['value'] = trim($value);
00252 $this->filters[$this->count]['sizevalue'] = strlen(trim($value));
00253 $this->filters[$this->count]['conector'] = strtoupper($conector);
00254 $this->count++;
00255 }
00256
00257 function ApplyFilter($data)
00258 {
00259 foreach ($this->filters as $f)
00260 {
00261 $value[$this->query->GetColumnNumber($f['field'])] = $f['value'];
00262 }
00263
00264 foreach ($data as $row)
00265 {
00266 $this->filtered = array
00267 (
00268 );
00269
00270 foreach ($this->filters as $f)
00271 {
00272 $p = $f['fieldpos'];
00273 $n = $f['sizevalue'];
00274 $v = $f['value'];
00275
00276 switch ($f['oper'])
00277 {
00278 case "=":
00279 $this->filtered[] = (!strncasecmp($row[$p], $v, $n));
00280
00281 break;
00282
00283 case "!=":
00284 $this->filtered[] = (strncasecmp($row[$p], $v, $n));
00285
00286 break;
00287
00288 case "like":
00289 $this->filtered[] = (!strncasecmp($row[$p], $v, $n));
00290
00291 break;
00292
00293 case "regex":
00294 $this->filtered[] = preg_match("/$v/i", $row[$p]);
00295
00296 break;
00297
00298 default: $this->filtered[] = (!strncasecmp($row[$p], $v, $n));
00299 }
00300 }
00301
00302 $filtered = $this->filtered[0];
00303
00304 for ($i = 1; $i < count($this->filtered); $i++)
00305 {
00306 switch ($this->filters[$i]['conector'])
00307 {
00308 case "AND":
00309 $filtered = $filtered && $this->filtered[$i];
00310
00311 break;
00312
00313 case "OR":
00314 $filtered = $filtered || $this->filtered[$i];
00315
00316 break;
00317 }
00318 }
00319
00320 if ($filtered)
00321 {
00322 $result[] = $row;
00323 }
00324 }
00325
00326 return $result;
00327 }
00328 }
00329 ?>