Skip to content

Commit

Permalink
Added DefaultQueries where locate all insertXxx functions
Browse files Browse the repository at this point in the history
  • Loading branch information
matcracker committed Jun 13, 2021
1 parent 4545c8d commit 294e8b9
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 72 deletions.
25 changes: 3 additions & 22 deletions src/matcracker/BedcoreProtect/storage/PatchManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Generator;
use InvalidStateException;
use matcracker\BedcoreProtect\Main;
use matcracker\BedcoreProtect\storage\queries\DefaultQueries;
use matcracker\BedcoreProtect\storage\queries\QueriesConst;
use poggit\libasynql\DataConnector;
use SOFe\AwaitGenerator\Await;
Expand All @@ -38,17 +39,15 @@
use function version_compare;
use function yaml_parse;

final class PatchManager
final class PatchManager extends DefaultQueries
{
/** @var DataConnector */
private $connector;
/** @var Main */
private $plugin;

public function __construct(Main $plugin, DataConnector $connector)
{
parent::__construct($connector);
$this->plugin = $plugin;
$this->connector = $connector;
}

/**
Expand Down Expand Up @@ -104,12 +103,6 @@ function () use (&$patchVersion): Generator {
return $patchVersion;
}

private function executeSelect(string $query, array $args = []): Generator
{
$this->connector->executeSelect($query, $args, yield, yield Await::REJECT);
return yield Await::ONCE;
}

/**
* @param string $db_version
* @return int[][]
Expand All @@ -134,16 +127,4 @@ private function getVersionsToPatch(string $db_version): array
&& version_compare($patchVersion, $db_version) > 0;
}, ARRAY_FILTER_USE_KEY);
}

private function executeGeneric(string $query, array $args = []): Generator
{
$this->connector->executeGeneric($query, $args, yield, yield Await::REJECT);
return yield Await::ONCE;
}

private function executeChange(string $query, array $args = []): Generator
{
$this->connector->executeChange($query, $args, yield, yield Await::REJECT);
return yield Await::ONCE;
}
}
19 changes: 3 additions & 16 deletions src/matcracker/BedcoreProtect/storage/QueryManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use matcracker\BedcoreProtect\commands\CommandParser;
use matcracker\BedcoreProtect\Main;
use matcracker\BedcoreProtect\storage\queries\BlocksQueries;
use matcracker\BedcoreProtect\storage\queries\DefaultQueries;
use matcracker\BedcoreProtect\storage\queries\EntitiesQueries;
use matcracker\BedcoreProtect\storage\queries\InventoriesQueries;
use matcracker\BedcoreProtect\storage\queries\PluginQueries;
Expand All @@ -44,7 +45,7 @@
use function preg_match;
use function round;

final class QueryManager
final class QueryManager extends DefaultQueries
{
/** @var array[] */
private static $additionalReports = [];
Expand All @@ -53,8 +54,6 @@ final class QueryManager

/** @var Main */
private $plugin;
/** @var DataConnector */
private $connector;
/** @var PluginQueries */
private $pluginQueries;
/** @var BlocksQueries */
Expand All @@ -68,8 +67,8 @@ final class QueryManager

public function __construct(Main $plugin, DataConnector $connector)
{
parent::__construct($connector);
$this->plugin = $plugin;
$this->connector = $connector;

$this->pluginQueries = new PluginQueries($plugin, $connector);
$this->entitiesQueries = new EntitiesQueries($plugin, $connector);
Expand Down Expand Up @@ -108,18 +107,6 @@ function () use ($pluginVersion): Generator {
$this->connector->waitAll();
}

private function executeGeneric(string $query, array $args = []): Generator
{
$this->connector->executeGeneric($query, $args, yield, yield Await::REJECT);
return yield Await::ONCE;
}

private function executeInsert(string $query, array $args = []): Generator
{
$this->connector->executeInsert($query, $args, yield, yield Await::REJECT);
return yield Await::ONCE;
}

public function setupDefaultData(): void
{
$this->entitiesQueries->addDefaultEntities();
Expand Down
73 changes: 73 additions & 0 deletions src/matcracker/BedcoreProtect/storage/queries/DefaultQueries.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

/*
* ___ __ ___ __ __
* / _ )___ ___/ /______ _______ / _ \_______ / /____ ____/ /_
* / _ / -_) _ / __/ _ \/ __/ -_) ___/ __/ _ \/ __/ -_) __/ __/
* /____/\__/\_,_/\__/\___/_/ \__/_/ /_/ \___/\__/\__/\__/\__/
*
* Copyright (C) 2019-2021
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author matcracker
* @link https://www.github.com/matcracker/BedcoreProtect
*
*/

declare(strict_types=1);

namespace matcracker\BedcoreProtect\storage\queries;

use Generator;
use poggit\libasynql\DataConnector;
use SOFe\AwaitGenerator\Await;

abstract class DefaultQueries
{
/** @var DataConnector */
protected $connector;

public function __construct(DataConnector $connector)
{
$this->connector = $connector;
}

final protected function executeGeneric(string $query, array $args = []): Generator
{
$this->connector->executeGeneric($query, $args, yield, yield Await::REJECT);
return yield Await::ONCE;
}

final protected function executeChange(string $query, array $args = []): Generator
{
$this->connector->executeChange($query, $args, yield, yield Await::REJECT);
return yield Await::ONCE;
}

final protected function executeInsert(string $query, array $args = []): Generator
{
$this->connector->executeInsert($query, $args, yield, yield Await::REJECT);
return yield Await::ONCE;
}

final protected function executeSelect(string $query, array $args = []): Generator
{
$this->connector->executeSelect($query, $args, yield, yield Await::REJECT);
return yield Await::ONCE;
}

/**
* @param string $query
* @param array $args
* @param bool $multiParams if true, returns all parameters of callable 'onInserted(int $insertId, int $affectedRows)' instead of only $insertId.
* @return Generator
*/
final protected function executeInsertRaw(string $query, array $args = [], bool $multiParams = false): Generator
{
$this->connector->executeInsertRaw($query, $args, yield ($multiParams ? Await::RESOLVE_MULTI : Await::RESOLVE), yield Await::REJECT);
return yield Await::ONCE;
}
}
36 changes: 2 additions & 34 deletions src/matcracker/BedcoreProtect/storage/queries/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,17 @@
use SOFe\AwaitGenerator\Await;
use function mb_strtolower;

abstract class Query
abstract class Query extends DefaultQueries
{
/** @var Main */
protected $plugin;
/** @var DataConnector */
protected $connector;
/** @var ConfigParser */
protected $configParser;

public function __construct(Main $plugin, DataConnector $connector)
{
parent::__construct($connector);
$this->plugin = $plugin;
$this->connector = $connector;
$this->configParser = $plugin->getParsedConfig();
}

Expand Down Expand Up @@ -114,18 +112,6 @@ public function restore(Level $world, CommandParser $commandParser, array $logId
$this->rawRollback(false, $world, $commandParser, $logIds, $onPreComplete, $isLastRollback);
}

/**
* @param string $query
* @param array $args
* @param bool $multiParams if true, returns all parameters of callable 'onInserted(int $insertId, int $affectedRows)' instead of only $insertId.
* @return Generator
*/
final protected function executeInsertRaw(string $query, array $args = [], bool $multiParams = false): Generator
{
$this->connector->executeInsertRaw($query, $args, yield ($multiParams ? Await::RESOLVE_MULTI : Await::RESOLVE), yield Await::REJECT);
return yield Await::ONCE;
}

final protected function addRawLog(string $uuid, Vector3 $position, string $worldName, Action $action, float $time): Generator
{
return $this->executeInsert(QueriesConst::ADD_HISTORY_LOG, [
Expand All @@ -138,22 +124,4 @@ final protected function addRawLog(string $uuid, Vector3 $position, string $worl
"time" => $time
]);
}

final protected function executeInsert(string $query, array $args = []): Generator
{
$this->connector->executeInsert($query, $args, yield, yield Await::REJECT);
return yield Await::ONCE;
}

final protected function executeSelect(string $query, array $args = []): Generator
{
$this->connector->executeSelect($query, $args, yield, yield Await::REJECT);
return yield Await::ONCE;
}

final protected function executeGeneric(string $query, array $args = []): Generator
{
$this->connector->executeGeneric($query, $args, yield, yield Await::REJECT);
return yield Await::ONCE;
}
}

0 comments on commit 294e8b9

Please sign in to comment.