1
0
Fork 0
mirror of https://codeberg.org/demostf/api.git synced 2026-06-03 18:04:08 +02:00

remove lessql

This commit is contained in:
Robin Appelman 2020-11-28 15:32:16 +01:00
commit 5dc07c31d8
6 changed files with 1738 additions and 723 deletions

View file

@ -1,7 +1,6 @@
{
"require": {
"mikecao/flight": "1.3.8",
"morris/lessql": "^0.3.0",
"vlucas/phpdotenv": "^1.1",
"ircmaxell/random-lib": "^1.1",
"ehesp/steam-login": "^1.2",

2174
composer.lock generated

File diff suppressed because it is too large Load diff

View file

@ -5,10 +5,7 @@ declare(strict_types=1);
namespace Demostf\API\Providers;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
use Doctrine\DBAL\Query\QueryBuilder;
use LessQL\Database;
class BaseProvider {
/**
@ -16,11 +13,6 @@ class BaseProvider {
*/
protected $connection;
/**
* @var Database
*/
protected $db;
/**
* BaseProvider constructor.
*
@ -33,41 +25,9 @@ class BaseProvider {
*/
public function __construct(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 = []) {
$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->execute($params);

View file

@ -13,19 +13,29 @@ use PDO;
class DemoListProvider extends BaseProvider {
public function listUploads(string $steamId, int $page, array $where = []) {
$user = $this->db->user()->where('steamid', $steamId);
$where['uploader'] = $user->fetch()->id;
$query = $this->getQueryBuilder();
$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);
}
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']);
$userIds = [];
foreach ($users as $user) {
$userIds[] = $user['id'];
}
$result = $query->execute();
$userIds = $result->fetchAll(PDO::FETCH_COLUMN);
$result->closeCursor();
$query = $this->getQueryBuilder();
$query->select('p.demo_id')
@ -51,11 +61,15 @@ class DemoListProvider extends BaseProvider {
$result = $query->execute();
$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');
return $this->formatList($demos->fetchAll());
return $this->formatList($query->execute()->fetchAll());
}
private function addWhere(QueryBuilder $query, array $where = []) {

View file

@ -5,10 +5,8 @@ declare(strict_types=1);
namespace Demostf\API\Providers;
use Doctrine\DBAL\Connection;
use LessQL\Database;
use const DATE_ATOM;
use Demostf\API\Data\DemoPlayer;
use Demostf\API\Data\User;
use Demostf\API\Demo\Demo;
use PDO;
@ -23,9 +21,17 @@ class DemoProvider extends BaseProvider {
$this->userProvider = $userProvider;
}
public function get(int $id, bool $fetchDetails = true): ?Demo {
$demo = $this->db->demo()->where('id', $id);
private function fetchDemo(int $id): ?Demo {
$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 = '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,
@ -36,18 +42,17 @@ class DemoProvider extends BaseProvider {
INNER JOIN users ON players.user_id = users.id
WHERE demo_id = ?';
$demoData = $demo->fetch();
if (!$demoData) {
$demo = $this->fetchDemo($id);
if ($demo === null) {
return null;
}
$formattedDemo = Demo::fromRow($demoData);
if ($fetchDetails) {
$uploader = $this->userProvider->getById($demoData['uploader']);
$playerQuery = $this->query($sql, [$formattedDemo->getId(), $formattedDemo->getId()]);
$uploader = $this->userProvider->getById($demo->getUploader());
$playerQuery = $this->query($sql, [$demo->getId(), $demo->getId()]);
$players = $playerQuery->fetchAll(PDO::FETCH_ASSOC);
$formattedDemo->setUploaderUser($uploader);
$demo->setUploaderUser($uploader);
$uniquePlayers = [];
foreach ($players as $player) {
$key = $player['steamid'] . $player['team'];
@ -55,12 +60,12 @@ class DemoProvider extends BaseProvider {
$uniquePlayers[$key] = $player;
}
}
$formattedDemo->setPlayers(array_map(function ($player) {
$demo->setPlayers(array_map(function ($player) {
return DemoPlayer::fromRow($player);
}, array_values($uniquePlayers)));
}
return $formattedDemo;
return $demo;
}
public function demoIdByHash(string $hash): int {

View file

@ -8,15 +8,24 @@ use PDO;
class InfoProvider extends BaseProvider {
public function listMaps() {
$sql = 'SELECT map, count FROM map_list';
$result = $this->query($sql);
$query = $this->getQueryBuilder();
$query->select('map', 'count')
->from('map_list');
$result = $query->execute();
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() {
$demoCount = $this->db->demo()->count();
$playerCount = $this->db->user()->count();
$demoCount = $this->count('demos');
$playerCount = $this->count('users');
return [
'demos' => $demoCount,