Skip to content

Commit

Permalink
fix(db): Manually track if where() is called when not empty to avoi…
Browse files Browse the repository at this point in the history
…d recursion

Signed-off-by: Joas Schilling <coding@schilljs.com>
  • Loading branch information
nickvergessen committed Jul 18, 2024
1 parent 796d641 commit 7d34c84
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions lib/private/DB/QueryBuilder/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class QueryBuilder implements IQueryBuilder {

/** @var bool */
private $automaticTablePrefix = true;
private bool $nonEmptyWhere = false;

/** @var string */
protected $lastInsertedTable;
Expand Down Expand Up @@ -189,24 +190,24 @@ private function prepareForExecute() {
}
}

if (!empty($this->getQueryPart('select'))) {
$select = $this->getQueryPart('select');
$hasSelectAll = array_filter($select, static function ($s) {
return $s === '*';
});
$hasSelectSpecific = array_filter($select, static function ($s) {
return $s !== '*';
});

if (empty($hasSelectAll) === empty($hasSelectSpecific)) {
$exception = new QueryException('Query is selecting * and specific values in the same query. This is not supported in Oracle.');
$this->logger->error($exception->getMessage(), [
'query' => $this->getSQL(),
'app' => 'core',
'exception' => $exception,
]);
}
}
// if (!empty($this->getQueryPart('select'))) {
// $select = $this->getQueryPart('select');
// $hasSelectAll = array_filter($select, static function ($s) {
// return $s === '*';
// });
// $hasSelectSpecific = array_filter($select, static function ($s) {
// return $s !== '*';
// });

// if (empty($hasSelectAll) === empty($hasSelectSpecific)) {
// $exception = new QueryException('Query is selecting * and specific values in the same query. This is not supported in Oracle.');
// $this->logger->error($exception->getMessage(), [
// 'query' => $this->getSQL(),
// 'app' => 'core',
// 'exception' => $exception,
// ]);
// }
// }

$numberOfParameters = 0;
$hasTooLargeArrayParameter = false;
Expand Down Expand Up @@ -834,12 +835,14 @@ public function set($key, $value) {
* @return $this This QueryBuilder instance.
*/
public function where(...$predicates) {
if ($this->getQueryPart('where') !== null && $this->systemConfig->getValue('debug', false)) {
if ($this->nonEmptyWhere && $this->systemConfig->getValue('debug', false)) {
// Only logging a warning, not throwing for now.
$e = new QueryException('Using where() on non-empty WHERE part, please verify it is intentional to not call andWhere() or orWhere() instead. Otherwise consider creating a new query builder object or call resetQueryPart(\'where\') first.');
$this->logger->warning($e->getMessage(), ['exception' => $e]);
}

$this->nonEmptyWhere = true;

call_user_func_array(
[$this->queryBuilder, 'where'],
$predicates
Expand Down Expand Up @@ -867,6 +870,7 @@ public function where(...$predicates) {
* @see where()
*/
public function andWhere(...$where) {
$this->nonEmptyWhere = true;
call_user_func_array(
[$this->queryBuilder, 'andWhere'],
$where
Expand Down Expand Up @@ -894,6 +898,7 @@ public function andWhere(...$where) {
* @see where()
*/
public function orWhere(...$where) {
$this->nonEmptyWhere = true;
call_user_func_array(
[$this->queryBuilder, 'orWhere'],
$where
Expand Down

0 comments on commit 7d34c84

Please sign in to comment.