Skip to content

Commit

Permalink
Slight refactoring of search result
Browse files Browse the repository at this point in the history
  • Loading branch information
annda committed Feb 12, 2024
1 parent ea625c4 commit 04c3c31
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 56 deletions.
47 changes: 1 addition & 46 deletions meta/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,6 @@ public function setSelectLatest($selectLatest): void
public function getResult()
{
if (is_null($this->result)) {
$this->result = new SearchResult();
$this->run();
}
return $this->result;
Expand Down Expand Up @@ -477,39 +476,8 @@ protected function run()
static fn($pageidAndRevOnly, Column $col) => $pageidAndRevOnly && ($col->getTid() == 0),
true
);
while ($row = $res->fetch(\PDO::FETCH_ASSOC)) {
$this->result->increaseCount();
if ($this->result->getCount() < $this->range_begin) continue;
if ($this->range_end && $this->result->getCount() >= $this->range_end) continue;

$C = 0;
$resrow = [];
$isempty = true;
foreach ($this->columns as $col) {
$val = $row["C$C"];
if ($col->isMulti()) {
$val = explode(self::CONCAT_SEPARATOR, $val);
}
$value = new Value($col, $val);
$isempty &= $this->isEmptyValue($value);
$resrow[] = $value;
$C++;
}

// skip empty rows
if ($isempty && !$pageidAndRevOnly) {
$this->result->decreaseCount();
continue;
}

$this->result->addPid($row['PID']);
$this->result->addRid($row['rid']);
$this->result->addRev($row['rev']);
$this->result->addRow($resrow);
}

$res->closeCursor();
$this->result->increaseCount();
$this->result = new SearchResult($res, $this->range_begin, $this->range_end, $this->columns, $pageidAndRevOnly);
}

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

return $col;
}

/**
* Check if the given row is empty or references our own row
*
* @param Value $value
* @return bool
*/
protected function isEmptyValue(Value $value)
{
if ($value->isEmpty()) return true;
if ($value->getColumn()->getTid() == 0) return true;
return false;
}
}
62 changes: 52 additions & 10 deletions meta/SearchResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,50 @@ class SearchResult
/** @var int */
protected $count = -1;

/** @var SearchResult */
protected static $instance;

/**
* Get the singleton instance of SearchResult
* Construct SearchResult
*
* @return SearchResult
* @param \PDOStatemen $res
* @param int $rangeBegin
* @param int $rangeEnd
* @param array $columns
* @param bool $pageidAndRevOnly
*/
public static function getInstance()
public function __construct($res, $rangeBegin, $rangeEnd, $columns, $pageidAndRevOnly)
{
if (is_null(self::$instance)) {
$class = static::class;
self::$instance = new $class();
while ($row = $res->fetch(\PDO::FETCH_ASSOC)) {
$this->increaseCount();
if ($this->getCount() < $rangeBegin) continue;
if ($rangeEnd && $this->getCount() >= $rangeEnd) continue;

$C = 0;
$resrow = [];
$isempty = true;
foreach ($columns as $col) {
$val = $row["C$C"];
if ($col->isMulti()) {
$val = explode(Search::CONCAT_SEPARATOR, $val);
}
$value = new Value($col, $val);
$isempty &= $this->isEmptyValue($value);
$resrow[] = $value;
$C++;
}

// skip empty rows
if ($isempty && !$pageidAndRevOnly) {
$this->decreaseCount();
continue;
}

$this->addPid($row['PID']);
$this->addRid($row['rid']);
$this->addRev($row['rev']);
$this->addRow($resrow);
}
return self::$instance;

$res->closeCursor();
$this->increaseCount();
}

/**
Expand Down Expand Up @@ -126,4 +155,17 @@ public function decreaseCount()
{
$this->count--;
}

/**
* Check if the given row is empty or references our own row
*
* @param Value $value
* @return bool
*/
protected function isEmptyValue(Value $value)
{
if ($value->isEmpty()) return true;
if ($value->getColumn()->getTid() == 0) return true;
return false;
}
}

0 comments on commit 04c3c31

Please sign in to comment.