Skip to content

Commit 0ba2dc7

Browse files
committed
Improve QueryResult
* Add the memory usage of the query's execution in the array of results * Make FETCH_OBJECT method recursive * Can now use FETCH_OBJECT method with a foreach loop
1 parent 9115c84 commit 0ba2dc7

File tree

1 file changed

+30
-14
lines changed

1 file changed

+30
-14
lines changed

src/JSONDB/QueryResult.php

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -96,20 +96,37 @@ private function _setResults($results)
9696
*/
9797
public function valid()
9898
{
99-
if ($this->key === '#queryString') {
99+
if ($this->key === '#queryString' || $this->key === '#elapsedtime' || $this->key === '#memoryusage') {
100100
return FALSE;
101101
} else {
102102
return array_key_exists($this->key, $this->results);
103103
}
104104
}
105105

106+
public function queryString()
107+
{
108+
return $this->database->queryString();
109+
}
110+
106111
/**
107112
* Returns the current result
108-
* @return array
113+
* @return array|QueryResultObject
114+
* @throws Exception
109115
*/
110116
public function current()
111117
{
112-
return $this->results[$this->key];
118+
$return = $this->results[$this->key];
119+
120+
switch ($this->fetchMode) {
121+
case JSONDB::FETCH_ARRAY:
122+
return (array)$return;
123+
124+
case JSONDB::FETCH_OBJECT:
125+
return new QueryResultObject($return);
126+
127+
default:
128+
throw new Exception('JSONDB Query Result Error: Fetch mode not supported.');
129+
}
113130
}
114131

115132
/**
@@ -236,7 +253,7 @@ public function offsetUnset($offset)
236253
{
237254
if ($this->offsetExists($offset)) {
238255
unset($this->results[$offset]);
239-
$this->_setResults(array_values(array_slice($this->results, 2)));
256+
$this->_setResults(array_values(array_slice($this->results, 3)));
240257
$this->_parseResults();
241258
}
242259
}
@@ -257,14 +274,7 @@ public function fetch($mode = NULL)
257274
if ($this->valid()) {
258275
$return = $this->current();
259276
++$this->key;
260-
261-
switch ($this->fetchMode) {
262-
case JSONDB::FETCH_ARRAY:
263-
return (array)$return;
264-
265-
case JSONDB::FETCH_OBJECT:
266-
return new QueryResultObject($return);
267-
}
277+
return $return;
268278
}
269279
return NULL;
270280
} else {
@@ -275,10 +285,12 @@ public function fetch($mode = NULL)
275285
/**
276286
* Changes the fetch mode
277287
* @param int $mode
288+
* @return QueryResult
278289
*/
279290
public function setFetchMode($mode = JSONDB::FETCH_ARRAY)
280291
{
281292
$this->fetchMode = $mode;
293+
return $this;
282294
}
283295

284296
/**
@@ -287,8 +299,9 @@ public function setFetchMode($mode = JSONDB::FETCH_ARRAY)
287299
private function _parseResults()
288300
{
289301
$this->results = array_merge(
290-
array('#queryString' => $this->database->queryString(),
291-
'#elapsedtime' => $this->database->benchmark()->elapsed_time('jsondb_(query)_start', 'jsondb_(query)_end'))
302+
array('#queryString' => $this->queryString(),
303+
'#elapsedtime' => $this->database->benchmark()->elapsed_time('jsondb_(query)_start', 'jsondb_(query)_end'),
304+
'#memoryusage' => $this->database->benchmark()->memory_usage('jsondb_(query)_start', 'jsondb_(query)_end'))
292305
, $this->results);
293306
}
294307
}
@@ -334,6 +347,9 @@ private function _setResult($result)
334347
public function __get($name)
335348
{
336349
if (array_key_exists($name, $this->result)) {
350+
if (is_array($this->result[$name])) {
351+
return new QueryResultObject($this->result[$name]);
352+
}
337353
return $this->result[$name];
338354
} else {
339355
throw new Exception("JSONDB Query Result Error: Can't access the key \"{$name}\" in result, maybe the key doesn't exist.");

0 commit comments

Comments
 (0)