mirror of
https://codeberg.org/demostf/api.git
synced 2026-06-03 18:04:08 +02:00
remove lessql
This commit is contained in:
parent
3e53b254d5
commit
5dc07c31d8
6 changed files with 1738 additions and 723 deletions
|
|
@ -1,7 +1,6 @@
|
||||||
{
|
{
|
||||||
"require": {
|
"require": {
|
||||||
"mikecao/flight": "1.3.8",
|
"mikecao/flight": "1.3.8",
|
||||||
"morris/lessql": "^0.3.0",
|
|
||||||
"vlucas/phpdotenv": "^1.1",
|
"vlucas/phpdotenv": "^1.1",
|
||||||
"ircmaxell/random-lib": "^1.1",
|
"ircmaxell/random-lib": "^1.1",
|
||||||
"ehesp/steam-login": "^1.2",
|
"ehesp/steam-login": "^1.2",
|
||||||
|
|
|
||||||
2174
composer.lock
generated
2174
composer.lock
generated
File diff suppressed because it is too large
Load diff
|
|
@ -5,10 +5,7 @@ declare(strict_types=1);
|
||||||
namespace Demostf\API\Providers;
|
namespace Demostf\API\Providers;
|
||||||
|
|
||||||
use Doctrine\DBAL\Connection;
|
use Doctrine\DBAL\Connection;
|
||||||
use Doctrine\DBAL\Platforms\MySqlPlatform;
|
|
||||||
use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
|
|
||||||
use Doctrine\DBAL\Query\QueryBuilder;
|
use Doctrine\DBAL\Query\QueryBuilder;
|
||||||
use LessQL\Database;
|
|
||||||
|
|
||||||
class BaseProvider {
|
class BaseProvider {
|
||||||
/**
|
/**
|
||||||
|
|
@ -16,11 +13,6 @@ class BaseProvider {
|
||||||
*/
|
*/
|
||||||
protected $connection;
|
protected $connection;
|
||||||
|
|
||||||
/**
|
|
||||||
* @var Database
|
|
||||||
*/
|
|
||||||
protected $db;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* BaseProvider constructor.
|
* BaseProvider constructor.
|
||||||
*
|
*
|
||||||
|
|
@ -33,41 +25,9 @@ class BaseProvider {
|
||||||
*/
|
*/
|
||||||
public function __construct(Connection $connection) {
|
public function __construct(Connection $connection) {
|
||||||
$this->connection = $connection;
|
$this->connection = $connection;
|
||||||
$this->db = new Database($connection->getWrappedConnection());
|
|
||||||
$this->dbConfig();
|
|
||||||
}
|
|
||||||
|
|
||||||
private function dbConfig() {
|
|
||||||
$platform = $this->connection->getDatabasePlatform();
|
|
||||||
if ($platform instanceof MySqlPlatform) {
|
|
||||||
$this->db->setIdentifierDelimiter('`');
|
|
||||||
} else {
|
|
||||||
$this->db->setIdentifierDelimiter('"');
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->db->setRewrite(function ($table) {
|
|
||||||
$rawNames = ['chat'];
|
|
||||||
$aliases = [
|
|
||||||
];
|
|
||||||
if (isset($aliases[$table])) {
|
|
||||||
return $aliases[$table];
|
|
||||||
} elseif (false === array_search($table, $rawNames, true)) {
|
|
||||||
return $table . 's';
|
|
||||||
} else {
|
|
||||||
return $table;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function query(string $sql, array $params = []) {
|
protected function query(string $sql, array $params = []) {
|
||||||
$delimiter = $this->db->getIdentifierDelimiter();
|
|
||||||
$platform = $this->connection->getDatabasePlatform();
|
|
||||||
$sql = str_replace('`', $delimiter, $sql);
|
|
||||||
|
|
||||||
if ($platform instanceof PostgreSqlPlatform) {
|
|
||||||
$sql = str_replace('FROM_UNIXTIME(', 'to_timestamp(', $sql);
|
|
||||||
}
|
|
||||||
|
|
||||||
$query = $this->connection->prepare($sql);
|
$query = $this->connection->prepare($sql);
|
||||||
$query->execute($params);
|
$query->execute($params);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -13,19 +13,29 @@ use PDO;
|
||||||
|
|
||||||
class DemoListProvider extends BaseProvider {
|
class DemoListProvider extends BaseProvider {
|
||||||
public function listUploads(string $steamId, int $page, array $where = []) {
|
public function listUploads(string $steamId, int $page, array $where = []) {
|
||||||
$user = $this->db->user()->where('steamid', $steamId);
|
$query = $this->getQueryBuilder();
|
||||||
$where['uploader'] = $user->fetch()->id;
|
$query->select('id')
|
||||||
|
->from('users')
|
||||||
|
->where($query->expr()->eq('steamid', $query->createNamedParameter($steamId, PDO::PARAM_STR)));
|
||||||
|
|
||||||
|
$result = $query->execute();
|
||||||
|
$userId = $result->fetch(PDO::FETCH_COLUMN);
|
||||||
|
$result->closeCursor();
|
||||||
|
|
||||||
|
$where['uploader'] = $userId;
|
||||||
|
|
||||||
return $this->listDemos($page, $where);
|
return $this->listDemos($page, $where);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function listProfile(int $page, array $where = []) {
|
public function listProfile(int $page, array $where = []) {
|
||||||
$users = $this->db->user()->where('steamid', $where['players']);
|
$query = $this->getQueryBuilder();
|
||||||
|
$query->select('id')
|
||||||
|
->from('users')
|
||||||
|
->where($query->expr()->in('steamid', $query->createNamedParameter($where['players'], Connection::PARAM_STR_ARRAY)));
|
||||||
unset($where['players']);
|
unset($where['players']);
|
||||||
$userIds = [];
|
$result = $query->execute();
|
||||||
foreach ($users as $user) {
|
$userIds = $result->fetchAll(PDO::FETCH_COLUMN);
|
||||||
$userIds[] = $user['id'];
|
$result->closeCursor();
|
||||||
}
|
|
||||||
|
|
||||||
$query = $this->getQueryBuilder();
|
$query = $this->getQueryBuilder();
|
||||||
$query->select('p.demo_id')
|
$query->select('p.demo_id')
|
||||||
|
|
@ -51,11 +61,15 @@ class DemoListProvider extends BaseProvider {
|
||||||
|
|
||||||
$result = $query->execute();
|
$result = $query->execute();
|
||||||
$demoIds = $result->fetchAll(PDO::FETCH_COLUMN);
|
$demoIds = $result->fetchAll(PDO::FETCH_COLUMN);
|
||||||
|
$result->closeCursor();
|
||||||
|
|
||||||
$demos = $this->db->demo()->where('id', $demoIds)
|
$query = $this->getQueryBuilder();
|
||||||
|
$query->select('*')
|
||||||
|
->from('demos')
|
||||||
|
->where($query->expr()->in('id', $query->createNamedParameter($demoIds, Connection::PARAM_INT_ARRAY)))
|
||||||
->orderBy('id', 'DESC');
|
->orderBy('id', 'DESC');
|
||||||
|
|
||||||
return $this->formatList($demos->fetchAll());
|
return $this->formatList($query->execute()->fetchAll());
|
||||||
}
|
}
|
||||||
|
|
||||||
private function addWhere(QueryBuilder $query, array $where = []) {
|
private function addWhere(QueryBuilder $query, array $where = []) {
|
||||||
|
|
|
||||||
|
|
@ -5,10 +5,8 @@ declare(strict_types=1);
|
||||||
namespace Demostf\API\Providers;
|
namespace Demostf\API\Providers;
|
||||||
|
|
||||||
use Doctrine\DBAL\Connection;
|
use Doctrine\DBAL\Connection;
|
||||||
use LessQL\Database;
|
|
||||||
use const DATE_ATOM;
|
use const DATE_ATOM;
|
||||||
use Demostf\API\Data\DemoPlayer;
|
use Demostf\API\Data\DemoPlayer;
|
||||||
use Demostf\API\Data\User;
|
|
||||||
use Demostf\API\Demo\Demo;
|
use Demostf\API\Demo\Demo;
|
||||||
use PDO;
|
use PDO;
|
||||||
|
|
||||||
|
|
@ -23,9 +21,17 @@ class DemoProvider extends BaseProvider {
|
||||||
$this->userProvider = $userProvider;
|
$this->userProvider = $userProvider;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function get(int $id, bool $fetchDetails = true): ?Demo {
|
private function fetchDemo(int $id): ?Demo {
|
||||||
$demo = $this->db->demo()->where('id', $id);
|
$query = $this->getQueryBuilder();
|
||||||
|
$query->select('*')
|
||||||
|
->from('demos')
|
||||||
|
->where($query->expr()->eq('id', $query->createNamedParameter($id, PDO::PARAM_INT)));
|
||||||
|
$row = $query->execute()->fetch();
|
||||||
|
|
||||||
|
return $row ? Demo::fromRow($row) : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get(int $id, bool $fetchDetails = true): ?Demo {
|
||||||
// sql magic
|
// sql magic
|
||||||
$sql = 'WITH demokills AS (SELECT attacker_id, assister_id, victim_id FROM kills WHERE demo_id = ?)
|
$sql = 'WITH demokills AS (SELECT attacker_id, assister_id, victim_id FROM kills WHERE demo_id = ?)
|
||||||
SELECT players.id, user_id, players.name, team, class, users.steamid, users.avatar,
|
SELECT players.id, user_id, players.name, team, class, users.steamid, users.avatar,
|
||||||
|
|
@ -36,18 +42,17 @@ class DemoProvider extends BaseProvider {
|
||||||
INNER JOIN users ON players.user_id = users.id
|
INNER JOIN users ON players.user_id = users.id
|
||||||
WHERE demo_id = ?';
|
WHERE demo_id = ?';
|
||||||
|
|
||||||
$demoData = $demo->fetch();
|
$demo = $this->fetchDemo($id);
|
||||||
if (!$demoData) {
|
if ($demo === null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
$formattedDemo = Demo::fromRow($demoData);
|
|
||||||
|
|
||||||
if ($fetchDetails) {
|
if ($fetchDetails) {
|
||||||
$uploader = $this->userProvider->getById($demoData['uploader']);
|
$uploader = $this->userProvider->getById($demo->getUploader());
|
||||||
$playerQuery = $this->query($sql, [$formattedDemo->getId(), $formattedDemo->getId()]);
|
$playerQuery = $this->query($sql, [$demo->getId(), $demo->getId()]);
|
||||||
$players = $playerQuery->fetchAll(PDO::FETCH_ASSOC);
|
$players = $playerQuery->fetchAll(PDO::FETCH_ASSOC);
|
||||||
|
|
||||||
$formattedDemo->setUploaderUser($uploader);
|
$demo->setUploaderUser($uploader);
|
||||||
$uniquePlayers = [];
|
$uniquePlayers = [];
|
||||||
foreach ($players as $player) {
|
foreach ($players as $player) {
|
||||||
$key = $player['steamid'] . $player['team'];
|
$key = $player['steamid'] . $player['team'];
|
||||||
|
|
@ -55,12 +60,12 @@ class DemoProvider extends BaseProvider {
|
||||||
$uniquePlayers[$key] = $player;
|
$uniquePlayers[$key] = $player;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$formattedDemo->setPlayers(array_map(function ($player) {
|
$demo->setPlayers(array_map(function ($player) {
|
||||||
return DemoPlayer::fromRow($player);
|
return DemoPlayer::fromRow($player);
|
||||||
}, array_values($uniquePlayers)));
|
}, array_values($uniquePlayers)));
|
||||||
}
|
}
|
||||||
|
|
||||||
return $formattedDemo;
|
return $demo;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function demoIdByHash(string $hash): int {
|
public function demoIdByHash(string $hash): int {
|
||||||
|
|
|
||||||
|
|
@ -8,15 +8,24 @@ use PDO;
|
||||||
|
|
||||||
class InfoProvider extends BaseProvider {
|
class InfoProvider extends BaseProvider {
|
||||||
public function listMaps() {
|
public function listMaps() {
|
||||||
$sql = 'SELECT map, count FROM map_list';
|
$query = $this->getQueryBuilder();
|
||||||
$result = $this->query($sql);
|
$query->select('map', 'count')
|
||||||
|
->from('map_list');
|
||||||
|
$result = $query->execute();
|
||||||
|
|
||||||
return $result->fetchAll(PDO::FETCH_COLUMN);
|
return $result->fetchAll(PDO::FETCH_COLUMN);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function count(string $table): int {
|
||||||
|
$query = $this->getQueryBuilder();
|
||||||
|
$query->select('count(*)')
|
||||||
|
->from($table);
|
||||||
|
return $query->execute()->fetch(PDO::FETCH_COLUMN);
|
||||||
|
}
|
||||||
|
|
||||||
public function getStats() {
|
public function getStats() {
|
||||||
$demoCount = $this->db->demo()->count();
|
$demoCount = $this->count('demos');
|
||||||
$playerCount = $this->db->user()->count();
|
$playerCount = $this->count('users');
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'demos' => $demoCount,
|
'demos' => $demoCount,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue