Browse Source

Implement handling for arrays of ranges

Multiple ranges of articles or editions were not implemented, but the
functionality is generic and could be extended if later needed.
reader
J. King 2 years ago
parent
commit
2acacd2647
  1. 4
      lib/Context/ExclusionMembers.php
  2. 88
      lib/Database.php

4
lib/Context/ExclusionMembers.php

@ -236,7 +236,7 @@ trait ExclusionMembers {
return $this->act(__FUNCTION__, func_num_args(), $spec);
}
public function modifiedRanges(array $spec) {
public function modifiedRanges(array $spec = null) {
if (isset($spec)) {
$spec = $this->cleanDateRangeArray($spec);
}
@ -253,7 +253,7 @@ trait ExclusionMembers {
return $this->act(__FUNCTION__, func_num_args(), $spec);
}
public function markedRanges(array $spec) {
public function markedRanges(array $spec = null) {
if (isset($spec)) {
$spec = $this->cleanDateRangeArray($spec);
}

88
lib/Database.php

@ -1593,10 +1593,8 @@ class Database {
"annotated" => ["annotated", "=", "bool"],
];
foreach ($options as $m => [$col, $op, $type]) {
if (!$context->$m()) {
// context is not being used
continue;
} elseif ($op === "between") {
if ($context->$m()) {
if ($op === "between") {
// option is a range
if ($context->$m[0] === null) {
// range is open at the low end
@ -1619,12 +1617,9 @@ class Database {
$q->setWhere("{$colDefs[$col]} $op ?", $type, $context->$m);
}
}
// further handle exclusionary options if specified
foreach ($options as $m => [$col, $op, $type]) {
if (!method_exists($context->not, $m) || !$context->not->$m()) {
// context option is not being used
continue;
} elseif ($op === "between") {
// handle the exclusionary version
if (method_exists($context->not, $m) && $context->not->$m()) {
if ($op === "between") {
// option is a range
if ($context->not->$m[0] === null) {
// range is open at the low end
@ -1647,7 +1642,8 @@ class Database {
$q->setWhereNot("{$colDefs[$col]} $op ?", $type, $context->not->$m);
}
}
// handle folders, labels, and tags
}
// handle folder trees, labels, and tags
$options = [
// each context array consists of a common table expression to select from, the column to match in the main join, the column to match in the CTE, the column to select in the CTE, an operator, and a type for the match in the CTE
'folder' => ["folders", "folder", "folders.id", "req", "=", "int"],
@ -1695,28 +1691,70 @@ class Database {
"annotationTerms" => ["note"],
];
foreach ($options as $m => $columns) {
if (!$context->$m()) {
continue;
} elseif (!$context->$m) {
throw new Db\ExceptionInput("tooShort", ['field' => $m, 'action' => $this->caller(), 'min' => 1]); // must have at least one array element
}
$columns = array_map(function($c) use ($colDefs) {
assert(isset($colDefs[$c]), new Exception("constantUnknown", $c));
return $colDefs[$c];
}, $columns);
$q->setWhere(...$this->generateSearch($context->$m, $columns));
if ($context->$m()) {
if (!$context->$m) {
throw new Db\ExceptionInput("tooShort", ['field' => $m, 'action' => $this->caller(), 'min' => 1]); // must have at least one array element
}
// further handle exclusionary text-matching context options
foreach ($options as $m => $columns) {
if (!$context->not->$m() || !$context->not->$m) {
continue;
$q->setWhere(...$this->generateSearch($context->$m, $columns));
}
$columns = array_map(function($c) use ($colDefs) {
assert(isset($colDefs[$c]), new Exception("constantUnknown", $c));
return $colDefs[$c];
}, $columns);
// handle the exclusionary version
if ($context->not->$m() && $context->not->$m) {
$q->setWhereNot(...$this->generateSearch($context->not->$m, $columns, true));
}
}
// handle arrays of ranges
$options = [
'modifiedRanges' => ["modified_date", "datetime"],
'markedRanges' => ["marked_date", "datetime"],
];
foreach ($options as $m => [$col, $type]) {
if ($context->$m()) {
if (!$context->$m) {
throw new Db\ExceptionInput("tooShort", ['field' => $m, 'action' => $this->caller(), 'min' => 1]); // must have at least one array element
}
$w = [];
$t = [];
$v = [];
foreach ($context->$m as $r) {
if ($r[0] === null) {
// range is open at the low end
$w[] = "{$colDefs[$col]} <= ?";
$t[] = $type;
$v[] = $r[1];
} elseif ($context->$m[1] === null) {
// range is open at the high end
$w[] = "{$colDefs[$col]} >= ?";
$t[] = $type;
$v[] = $r[0];
} else {
// range is bounded in both directions
$w[] = "{$colDefs[$col]} BETWEEN ? AND ?";
$t[] = [$type, $type];
$v[] = $r;
}
}
$q->setWhere("(".implode(" OR ", $w).")", $t, $v);
}
// handle the exclusionary version
if ($context->not->$m() && $context->not->$m) {
foreach ($context->not->$m as $r) {
if ($r[0] === null) {
// range is open at the low end
$q->setWhereNot("{$colDefs[$col]} <= ?", $type, $r[1]);
} elseif ($r[1] === null) {
// range is open at the high end
$q->setWhereNot("{$colDefs[$col]} >= ?", $type, $r[0]);
} else {
// range is bounded in both directions
$q->setWhereNot("{$colDefs[$col]} BETWEEN ? AND ?", [$type, $type], $r);
}
}
}
}
// return the query
return $q;
}

Loading…
Cancel
Save