mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Optimize queries for statistics.
This commit is contained in:
@@ -28,7 +28,6 @@ use FireflyIII\User;
|
||||
use Illuminate\Contracts\Auth\Authenticatable;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
use function Safe\json_encode;
|
||||
|
||||
/**
|
||||
@@ -37,16 +36,16 @@ use function Safe\json_encode;
|
||||
class AccountSearch implements GenericSearchInterface
|
||||
{
|
||||
/** @var string */
|
||||
public const string SEARCH_ALL = 'all';
|
||||
public const string SEARCH_ALL = 'all';
|
||||
|
||||
/** @var string */
|
||||
public const string SEARCH_IBAN = 'iban';
|
||||
public const string SEARCH_IBAN = 'iban';
|
||||
|
||||
/** @var string */
|
||||
public const string SEARCH_ID = 'id';
|
||||
public const string SEARCH_ID = 'id';
|
||||
|
||||
/** @var string */
|
||||
public const string SEARCH_NAME = 'name';
|
||||
public const string SEARCH_NAME = 'name';
|
||||
|
||||
/** @var string */
|
||||
public const string SEARCH_NUMBER = 'number';
|
||||
@@ -63,10 +62,9 @@ class AccountSearch implements GenericSearchInterface
|
||||
public function search(): Collection
|
||||
{
|
||||
$searchQuery = $this->user->accounts()
|
||||
->leftJoin('account_types', 'accounts.account_type_id', '=', 'account_types.id')
|
||||
->leftJoin('account_meta', 'accounts.id', '=', 'account_meta.account_id')
|
||||
->whereIn('account_types.type', $this->types)
|
||||
;
|
||||
->leftJoin('account_types', 'accounts.account_type_id', '=', 'account_types.id')
|
||||
->leftJoin('account_meta', 'accounts.id', '=', 'account_meta.account_id')
|
||||
->whereIn('account_types.type', $this->types);
|
||||
$like = sprintf('%%%s%%', $this->query);
|
||||
$originalQuery = $this->query;
|
||||
|
||||
@@ -92,7 +90,7 @@ class AccountSearch implements GenericSearchInterface
|
||||
break;
|
||||
|
||||
case self::SEARCH_ID:
|
||||
$searchQuery->where('accounts.id', '=', (int) $originalQuery);
|
||||
$searchQuery->where('accounts.id', '=', (int)$originalQuery);
|
||||
|
||||
break;
|
||||
|
||||
@@ -137,7 +135,7 @@ class AccountSearch implements GenericSearchInterface
|
||||
$this->types = $types;
|
||||
}
|
||||
|
||||
public function setUser(Authenticatable|User|null $user): void
|
||||
public function setUser(Authenticatable | User | null $user): void
|
||||
{
|
||||
if ($user instanceof User) {
|
||||
$this->user = $user;
|
||||
|
File diff suppressed because it is too large
Load Diff
@@ -32,7 +32,6 @@ use Gdbots\QueryParser\QueryParser as BaseQueryParser;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use LogicException;
|
||||
use TypeError;
|
||||
|
||||
use function Safe\fwrite;
|
||||
|
||||
class GdbotsQueryParser implements QueryParserInterface
|
||||
@@ -52,12 +51,12 @@ class GdbotsQueryParser implements QueryParserInterface
|
||||
try {
|
||||
$result = $this->parser->parse($query);
|
||||
$nodes = array_map(
|
||||
fn (GdbotsNode\Node $node) => $this->convertNode($node),
|
||||
fn(GdbotsNode\Node $node) => $this->convertNode($node),
|
||||
$result->getNodes()
|
||||
);
|
||||
|
||||
return new NodeGroup($nodes);
|
||||
} catch (LogicException|TypeError $e) {
|
||||
} catch (LogicException | TypeError $e) {
|
||||
fwrite(STDERR, "Setting up GdbotsQueryParserTest\n");
|
||||
app('log')->error($e->getMessage());
|
||||
app('log')->error(sprintf('Could not parse search: "%s".', $query));
|
||||
@@ -76,7 +75,7 @@ class GdbotsQueryParser implements QueryParserInterface
|
||||
case $node instanceof GdbotsNode\Field:
|
||||
return new FieldNode(
|
||||
$node->getValue(),
|
||||
(string) $node->getNode()->getValue(),
|
||||
(string)$node->getNode()->getValue(),
|
||||
BoolOperator::PROHIBITED === $node->getBoolOperator()
|
||||
);
|
||||
|
||||
@@ -85,7 +84,7 @@ class GdbotsQueryParser implements QueryParserInterface
|
||||
|
||||
return new NodeGroup(
|
||||
array_map(
|
||||
fn (GdbotsNode\Node $subNode) => $this->convertNode($subNode),
|
||||
fn(GdbotsNode\Node $subNode) => $this->convertNode($subNode),
|
||||
$node->getNodes()
|
||||
)
|
||||
);
|
||||
@@ -98,7 +97,7 @@ class GdbotsQueryParser implements QueryParserInterface
|
||||
case $node instanceof GdbotsNode\Mention:
|
||||
case $node instanceof GdbotsNode\Emoticon:
|
||||
case $node instanceof GdbotsNode\Emoji:
|
||||
return new StringNode((string) $node->getValue(), BoolOperator::PROHIBITED === $node->getBoolOperator());
|
||||
return new StringNode((string)$node->getValue(), BoolOperator::PROHIBITED === $node->getBoolOperator());
|
||||
|
||||
default:
|
||||
throw new FireflyException(
|
||||
|
@@ -46,22 +46,6 @@ class QueryParser implements QueryParserInterface
|
||||
return $this->buildNodeGroup(false);
|
||||
}
|
||||
|
||||
private function buildNodeGroup(bool $isSubquery, bool $prohibited = false): NodeGroup
|
||||
{
|
||||
$nodes = [];
|
||||
$nodeResult = $this->buildNextNode($isSubquery);
|
||||
|
||||
while ($nodeResult->node instanceof Node) {
|
||||
$nodes[] = $nodeResult->node;
|
||||
if ($nodeResult->isSubqueryEnd) {
|
||||
break;
|
||||
}
|
||||
$nodeResult = $this->buildNextNode($isSubquery);
|
||||
}
|
||||
|
||||
return new NodeGroup($nodes, $prohibited);
|
||||
}
|
||||
|
||||
private function buildNextNode(bool $isSubquery): NodeResult
|
||||
{
|
||||
$tokenUnderConstruction = '';
|
||||
@@ -155,7 +139,7 @@ class QueryParser implements QueryParserInterface
|
||||
if ('' === $tokenUnderConstruction) {
|
||||
// In any other location, it's just a normal character
|
||||
$tokenUnderConstruction .= $char;
|
||||
$skipNext = true;
|
||||
$skipNext = true;
|
||||
}
|
||||
if ('' !== $tokenUnderConstruction && !$skipNext) { // @phpstan-ignore-line
|
||||
Log::debug(sprintf('Turns out that "%s" is a field name. Reset the token.', $tokenUnderConstruction));
|
||||
@@ -187,13 +171,29 @@ class QueryParser implements QueryParserInterface
|
||||
++$this->position;
|
||||
}
|
||||
|
||||
$finalNode = '' !== $tokenUnderConstruction || '' !== $fieldName
|
||||
$finalNode = '' !== $tokenUnderConstruction || '' !== $fieldName
|
||||
? $this->createNode($tokenUnderConstruction, $fieldName, $prohibited)
|
||||
: null;
|
||||
|
||||
return new NodeResult($finalNode, true);
|
||||
}
|
||||
|
||||
private function buildNodeGroup(bool $isSubquery, bool $prohibited = false): NodeGroup
|
||||
{
|
||||
$nodes = [];
|
||||
$nodeResult = $this->buildNextNode($isSubquery);
|
||||
|
||||
while ($nodeResult->node instanceof Node) {
|
||||
$nodes[] = $nodeResult->node;
|
||||
if ($nodeResult->isSubqueryEnd) {
|
||||
break;
|
||||
}
|
||||
$nodeResult = $this->buildNextNode($isSubquery);
|
||||
}
|
||||
|
||||
return new NodeGroup($nodes, $prohibited);
|
||||
}
|
||||
|
||||
private function createNode(string $token, string $fieldName, bool $prohibited): Node
|
||||
{
|
||||
if ('' !== $fieldName) {
|
||||
|
Reference in New Issue
Block a user