Skip to content

Commit 04c3c31

Browse files
committed
Slight refactoring of search result
1 parent ea625c4 commit 04c3c31

File tree

2 files changed

+53
-56
lines changed

2 files changed

+53
-56
lines changed

meta/Search.php

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,6 @@ public function setSelectLatest($selectLatest): void
390390
public function getResult()
391391
{
392392
if (is_null($this->result)) {
393-
$this->result = new SearchResult();
394393
$this->run();
395394
}
396395
return $this->result;
@@ -477,39 +476,8 @@ protected function run()
477476
static fn($pageidAndRevOnly, Column $col) => $pageidAndRevOnly && ($col->getTid() == 0),
478477
true
479478
);
480-
while ($row = $res->fetch(\PDO::FETCH_ASSOC)) {
481-
$this->result->increaseCount();
482-
if ($this->result->getCount() < $this->range_begin) continue;
483-
if ($this->range_end && $this->result->getCount() >= $this->range_end) continue;
484-
485-
$C = 0;
486-
$resrow = [];
487-
$isempty = true;
488-
foreach ($this->columns as $col) {
489-
$val = $row["C$C"];
490-
if ($col->isMulti()) {
491-
$val = explode(self::CONCAT_SEPARATOR, $val);
492-
}
493-
$value = new Value($col, $val);
494-
$isempty &= $this->isEmptyValue($value);
495-
$resrow[] = $value;
496-
$C++;
497-
}
498-
499-
// skip empty rows
500-
if ($isempty && !$pageidAndRevOnly) {
501-
$this->result->decreaseCount();
502-
continue;
503-
}
504-
505-
$this->result->addPid($row['PID']);
506-
$this->result->addRid($row['rid']);
507-
$this->result->addRev($row['rev']);
508-
$this->result->addRow($resrow);
509-
}
510479

511-
$res->closeCursor();
512-
$this->result->increaseCount();
480+
$this->result = new SearchResult($res, $this->range_begin, $this->range_end, $this->columns, $pageidAndRevOnly);
513481
}
514482

515483
/**
@@ -678,17 +646,4 @@ public function findColumn($colname, $strict = false)
678646

679647
return $col;
680648
}
681-
682-
/**
683-
* Check if the given row is empty or references our own row
684-
*
685-
* @param Value $value
686-
* @return bool
687-
*/
688-
protected function isEmptyValue(Value $value)
689-
{
690-
if ($value->isEmpty()) return true;
691-
if ($value->getColumn()->getTid() == 0) return true;
692-
return false;
693-
}
694649
}

meta/SearchResult.php

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,50 @@ class SearchResult
1919
/** @var int */
2020
protected $count = -1;
2121

22-
/** @var SearchResult */
23-
protected static $instance;
24-
2522
/**
26-
* Get the singleton instance of SearchResult
23+
* Construct SearchResult
2724
*
28-
* @return SearchResult
25+
* @param \PDOStatemen $res
26+
* @param int $rangeBegin
27+
* @param int $rangeEnd
28+
* @param array $columns
29+
* @param bool $pageidAndRevOnly
2930
*/
30-
public static function getInstance()
31+
public function __construct($res, $rangeBegin, $rangeEnd, $columns, $pageidAndRevOnly)
3132
{
32-
if (is_null(self::$instance)) {
33-
$class = static::class;
34-
self::$instance = new $class();
33+
while ($row = $res->fetch(\PDO::FETCH_ASSOC)) {
34+
$this->increaseCount();
35+
if ($this->getCount() < $rangeBegin) continue;
36+
if ($rangeEnd && $this->getCount() >= $rangeEnd) continue;
37+
38+
$C = 0;
39+
$resrow = [];
40+
$isempty = true;
41+
foreach ($columns as $col) {
42+
$val = $row["C$C"];
43+
if ($col->isMulti()) {
44+
$val = explode(Search::CONCAT_SEPARATOR, $val);
45+
}
46+
$value = new Value($col, $val);
47+
$isempty &= $this->isEmptyValue($value);
48+
$resrow[] = $value;
49+
$C++;
50+
}
51+
52+
// skip empty rows
53+
if ($isempty && !$pageidAndRevOnly) {
54+
$this->decreaseCount();
55+
continue;
56+
}
57+
58+
$this->addPid($row['PID']);
59+
$this->addRid($row['rid']);
60+
$this->addRev($row['rev']);
61+
$this->addRow($resrow);
3562
}
36-
return self::$instance;
63+
64+
$res->closeCursor();
65+
$this->increaseCount();
3766
}
3867

3968
/**
@@ -126,4 +155,17 @@ public function decreaseCount()
126155
{
127156
$this->count--;
128157
}
158+
159+
/**
160+
* Check if the given row is empty or references our own row
161+
*
162+
* @param Value $value
163+
* @return bool
164+
*/
165+
protected function isEmptyValue(Value $value)
166+
{
167+
if ($value->isEmpty()) return true;
168+
if ($value->getColumn()->getTid() == 0) return true;
169+
return false;
170+
}
129171
}

0 commit comments

Comments
 (0)