MIOLO20
Carregando...
Procurando...
Nenhuma entrada encontrada
mquery.class
Ir para a documentação deste ficheiro.
1<?php
2class MQuery extends MDataSet
3{
4 var $conn; // the connection object
5 var $objSql; // the SQL object
6 var $sql; // the SQL command string
7 var $error; // the query's error message from the query execution
8 var $statement; // a parsed sql command - used by some drivers
9 var $fetched; // true for a valid result
10 var $maxrows; // a max number of rows to be fetched
11 var $offset; // a start point to fetch
12 var $order; // fields names used to sort resultset
13 var $filtered; // is filtered?
14 var $pagelength; // pagelength for paging
15 var $queryfilter; // object used to filter
16 var $_miolo; // MIOLO object
17
18 function __construct()
19 {
20 parent::__construct();
21 $this->fetched = false;
22 $this->row = -1;
23 $this->error = NULL;
24 $this->filtered = false;
25 $this->pagelength = 0;
26 $this->queryfilter = NULL;
27 $this->_miolo = MIOLO::GetInstance();
28 }
29
30 public function _query()
31 {
32 }
33
34 public function _error()
35 {
36 }
37
38 public function _close()
39 {
40 }
41
42 public function _setmetadata()
43 {
44 }
45
46 function GetError()
47 {
48 return $this->error;
49 }
50
51 function Open($maxrows = null, $offset = null, $stmt = NULL)
52 {
53 $this->_miolo->LogSQL($this->sql, false, $this->conn->db->conf);
54
55 $this->maxrows = $maxrows;
56 $this->offset = $offset;
57
58 if ($stmt != NULL)
59 {
60 $this->_querystmt($stmt);
61 }
62 else
63 {
64 $this->_query();
65 }
66
67 $this->_setmetadata();
68
69 if ($this->rowCount)
70 {
71 $this->row = 0;
72 $this->eof = $this->bof = false;
73 $this->fetched = true;
74 }
75 else
76 {
77 $this->result = NULL;
78 $this->row = -1;
79 $this->eof = $this->bof = true;
80 $this->fetched = false;
81 }
82
83 $this->error = $this->_error();
84
85 if ($this->error)
86 throw new EDatabaseQueryException($this->error);
87
88 return ($this->result != NULL);
89 }
90
91 function Close()
92 {
93 if ($this->fetched)
94 {
95 $this->_close();
96 }
97 }
98
99 function SetConnection(&$conn)
100 {
101 $this->conn = $conn;
102 }
103
104 function SetSQL(&$sql)
105 {
106 $this->sql = $sql->Select();
107 $this->objSql = &$sql;
108 }
109
111 {
112 $this->sql = $sqlCommand;
113 }
114
115 function SetOrder($order)
116 {
117 $order = explode(',', $order);
118 $this->order = $order;
119
120 foreach ($this->order as $o)
121 $p[] = $this->GetColumnNumber($o);
122
123 $n = count($this->result[0]);
124
125 foreach ($this->result as $key => $row)
126 {
127 for ($i = 0; $i < $n; $i++)
128 $arr[$i][$key] = $row[$i];
129 }
130
131 foreach ($p as $i => $o)
132 $sortcols .= ($i > 0 ? ",\$arr[$o]" : "\$arr[$o]");
133
134 for ($i = 0; $i < $n; $i++)
135 if (!in_array($i, $p))
136 $sortcols .= ",\$arr[$i]";
137
138 eval ("array_multisort({$sortcols}, SORT_ASC);");
139 $this->result = array
140 (
141 );
142
143 for ($i = 0; $i < $n; $i++)
144 {
145 foreach ($arr[$i] as $key => $row)
146 $this->result[$key][$i] = $row;
147 }
148 }
149
150 function isFiltered()
151 {
152 return $this->filtered;
153 }
154
155 function AddFilter($field, $oper, $value, $conector = 'AND')
156 {
157 if (!$this->queryfilter)
158 $this->queryfilter = new QueryFilter($this);
159 $this->queryfilter->AddFilter($field, $oper, $value, $conector);
160 }
161
162 function ApplyFilter()
163 {
164 if (!$this->queryfilter)
165 return;
166
167 $this->result = $this->queryfilter->ApplyFilter($this->result);
168 $this->filtered = true;
169 $this->rowCount = count($this->result);
170
171 if ($this->rowCount)
172 {
173 $this->row = 0;
174 $this->eof = $this->bof = false;
175 $this->fetched = true;
176 }
177 else
178 {
179 $this->result = NULL;
180 $this->row = -1;
181 $this->eof = $this->bof = true;
182 $this->fetched = false;
183 }
184 }
185
186 function SetPageLength($pagelength)
187 {
188 $this->pagelength = $pagelength;
189 }
190
191 function GetPageCount()
192 {
193 return (int)(($this->rowCount - 1 + $this->pagelength) / $this->pagelength);
194 }
195
196 function GetPage($pageno)
197 {
198 if ($this->result)
199 {
200 if ($this->pagelength)
201 {
202 return array_slice($this->result, $this->pagelength * ($pageno - 1), $this->pagelength);
203 }
204 else
205 return $this->result;
206 }
207 }
208
209 function GetCSV($filename = '')
210 {
212 $MIOLO->Uses('csv.class');
213 $csvdump = new csvdump();
214
215 if ($this->result)
216 {
217 $csvdump->dump($this->result, $filename);
218 }
219 exit;
220 }
221}
222
223
225{
226 var $filters; //array with filters
227 var $count = 0;
229
231 {
232 $this->query = $query;
233 }
234
235 function AddFilter($field, $oper, $value, $conector = 'AND')
236 {
237 $oper = strtolower($oper);
238
239 if ($oper == 'like')
240 {
241 $value = str_replace("?", ".", $value);
242 $value = str_replace("_", ".", $value);
243 $value = str_replace("%", "(.*?)", $value);
244 $value = "^" . $value . "(.*?)";
245 $oper = 'regex';
246 }
247
248 $this->filters[$this->count]['field'] = $field;
249 $this->filters[$this->count]['fieldpos'] = $this->query->GetColumnNumber($field);
250 $this->filters[$this->count]['oper'] = $oper;
251 $this->filters[$this->count]['value'] = trim($value);
252 $this->filters[$this->count]['sizevalue'] = strlen(trim($value));
253 $this->filters[$this->count]['conector'] = strtoupper($conector);
254 $this->count++;
255 }
256
257 function ApplyFilter($data) // a multidimensional array
258 {
259 foreach ($this->filters as $f)
260 {
261 $value[$this->query->GetColumnNumber($f['field'])] = $f['value'];
262 }
263
264 foreach ($data as $row)
265 {
266 $this->filtered = array
267 (
268 );
269
270 foreach ($this->filters as $f)
271 {
272 $p = $f['fieldpos'];
273 $n = $f['sizevalue'];
274 $v = $f['value'];
275
276 switch ($f['oper'])
277 {
278 case "=":
279 $this->filtered[] = (!strncasecmp($row[$p], $v, $n));
280
281 break;
282
283 case "!=":
284 $this->filtered[] = (strncasecmp($row[$p], $v, $n));
285
286 break;
287
288 case "like":
289 $this->filtered[] = (!strncasecmp($row[$p], $v, $n));
290
291 break;
292
293 case "regex":
294 $this->filtered[] = preg_match("/$v/i", $row[$p]);
295
296 break;
297
298 default: $this->filtered[] = (!strncasecmp($row[$p], $v, $n));
299 }
300 }
301
302 $filtered = $this->filtered[0];
303
304 for ($i = 1; $i < count($this->filtered); $i++)
305 {
306 switch ($this->filters[$i]['conector'])
307 {
308 case "AND":
309 $filtered = $filtered && $this->filtered[$i];
310
311 break;
312
313 case "OR":
314 $filtered = $filtered || $this->filtered[$i];
315
316 break;
317 }
318 }
319
320 if ($filtered)
321 {
322 $result[] = $row;
323 }
324 }
325
326 return $result;
327 }
328}
329?>
GetColumnNumber($colName)
Definição mdataset.class:202
SetConnection(&$conn)
Definição mquery.class:99
__construct()
Definição mquery.class:18
SetOrder($order)
Definição mquery.class:115
GetPage($pageno)
Definição mquery.class:196
Close()
Definição mquery.class:91
AddFilter($field, $oper, $value, $conector='AND')
Definição mquery.class:155
$fetched
Definição mquery.class:9
_close()
Definição mquery.class:38
isFiltered()
Definição mquery.class:150
SetPageLength($pagelength)
Definição mquery.class:186
$queryfilter
Definição mquery.class:15
GetCSV($filename='')
Definição mquery.class:209
$pagelength
Definição mquery.class:14
$statement
Definição mquery.class:8
_setmetadata()
Definição mquery.class:42
_error()
Definição mquery.class:34
_query()
Definição mquery.class:30
Open($maxrows=null, $offset=null, $stmt=NULL)
Definição mquery.class:51
SetSQLCommand($sqlCommand)
Definição mquery.class:110
$filtered
Definição mquery.class:13
ApplyFilter()
Definição mquery.class:162
GetPageCount()
Definição mquery.class:191
$objSql
Definição mquery.class:5
GetError()
Definição mquery.class:46
$maxrows
Definição mquery.class:10
$error
Definição mquery.class:7
SetSQL(&$sql)
Definição mquery.class:104
ApplyFilter($data)
Definição mquery.class:257
AddFilter($field, $oper, $value, $conector='AND')
Definição mquery.class:235
__construct($query)
Definição mquery.class:230