mirror of
https://github.com/lkddi/Xboard.git
synced 2026-04-25 04:27:28 +08:00
fix(jobs): resolve PostgreSQL issue in StatServerJob and StatUserJob
This commit is contained in:
@@ -67,8 +67,11 @@ class StatServerJob implements ShouldQueue
|
|||||||
|
|
||||||
protected function processServerStat(int $u, int $d, int $recordAt): void
|
protected function processServerStat(int $u, int $d, int $recordAt): void
|
||||||
{
|
{
|
||||||
if (config('database.default') === 'sqlite') {
|
$driver = config('database.default');
|
||||||
|
if ($driver === 'sqlite') {
|
||||||
$this->processServerStatForSqlite($u, $d, $recordAt);
|
$this->processServerStatForSqlite($u, $d, $recordAt);
|
||||||
|
} elseif ($driver === 'pgsql') {
|
||||||
|
$this->processServerStatForPostgres($u, $d, $recordAt);
|
||||||
} else {
|
} else {
|
||||||
$this->processServerStatForOtherDatabases($u, $d, $recordAt);
|
$this->processServerStatForOtherDatabases($u, $d, $recordAt);
|
||||||
}
|
}
|
||||||
@@ -126,4 +129,33 @@ class StatServerJob implements ShouldQueue
|
|||||||
]
|
]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* PostgreSQL upsert with arithmetic increments using ON CONFLICT ... DO UPDATE
|
||||||
|
*/
|
||||||
|
protected function processServerStatForPostgres(int $u, int $d, int $recordAt): void
|
||||||
|
{
|
||||||
|
$table = (new StatServer())->getTable();
|
||||||
|
$now = time();
|
||||||
|
|
||||||
|
// Use parameter binding to avoid SQL injection and keep maintainability
|
||||||
|
$sql = "INSERT INTO {$table} (record_at, server_id, server_type, record_type, u, d, created_at, updated_at)
|
||||||
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
|
ON CONFLICT (server_id, server_type, record_at)
|
||||||
|
DO UPDATE SET
|
||||||
|
u = {$table}.u + EXCLUDED.u,
|
||||||
|
d = {$table}.d + EXCLUDED.d,
|
||||||
|
updated_at = EXCLUDED.updated_at";
|
||||||
|
|
||||||
|
DB::statement($sql, [
|
||||||
|
$recordAt,
|
||||||
|
$this->server['id'],
|
||||||
|
$this->protocol,
|
||||||
|
$this->recordType,
|
||||||
|
$u,
|
||||||
|
$d,
|
||||||
|
$now,
|
||||||
|
$now,
|
||||||
|
]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -63,8 +63,11 @@ class StatUserJob implements ShouldQueue
|
|||||||
|
|
||||||
protected function processUserStat(int $uid, array $v, int $recordAt): void
|
protected function processUserStat(int $uid, array $v, int $recordAt): void
|
||||||
{
|
{
|
||||||
if (config('database.default') === 'sqlite') {
|
$driver = config('database.default');
|
||||||
|
if ($driver === 'sqlite') {
|
||||||
$this->processUserStatForSqlite($uid, $v, $recordAt);
|
$this->processUserStatForSqlite($uid, $v, $recordAt);
|
||||||
|
} elseif ($driver === 'pgsql') {
|
||||||
|
$this->processUserStatForPostgres($uid, $v, $recordAt);
|
||||||
} else {
|
} else {
|
||||||
$this->processUserStatForOtherDatabases($uid, $v, $recordAt);
|
$this->processUserStatForOtherDatabases($uid, $v, $recordAt);
|
||||||
}
|
}
|
||||||
@@ -122,4 +125,34 @@ class StatUserJob implements ShouldQueue
|
|||||||
]
|
]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* PostgreSQL upsert with arithmetic increments using ON CONFLICT ... DO UPDATE
|
||||||
|
*/
|
||||||
|
protected function processUserStatForPostgres(int $uid, array $v, int $recordAt): void
|
||||||
|
{
|
||||||
|
$table = (new StatUser())->getTable();
|
||||||
|
$now = time();
|
||||||
|
$u = ($v[0] * $this->server['rate']);
|
||||||
|
$d = ($v[1] * $this->server['rate']);
|
||||||
|
|
||||||
|
$sql = "INSERT INTO {$table} (user_id, server_rate, record_at, record_type, u, d, created_at, updated_at)
|
||||||
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
|
ON CONFLICT (user_id, server_rate, record_at)
|
||||||
|
DO UPDATE SET
|
||||||
|
u = {$table}.u + EXCLUDED.u,
|
||||||
|
d = {$table}.d + EXCLUDED.d,
|
||||||
|
updated_at = EXCLUDED.updated_at";
|
||||||
|
|
||||||
|
DB::statement($sql, [
|
||||||
|
$uid,
|
||||||
|
$this->server['rate'],
|
||||||
|
$recordAt,
|
||||||
|
$this->recordType,
|
||||||
|
$u,
|
||||||
|
$d,
|
||||||
|
$now,
|
||||||
|
$now,
|
||||||
|
]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user