Skip to content
This repository was archived by the owner on Mar 3, 2020. It is now read-only.

Commit dd9dffd

Browse files
committed
Changed count(*) to exists() and some clean up
1 parent 4241304 commit dd9dffd

File tree

7 files changed

+18
-102
lines changed

7 files changed

+18
-102
lines changed

src/models/Category.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ private static function categoryFromRow(Map<string, string> $row): Category {
9090
}
9191
self::setMCRecords('ALL_CATEGORIES', $categories);
9292
return $categories;
93-
return $categories;
9493
} else {
9594
invariant(
9695
is_array($mc_result),
@@ -105,13 +104,13 @@ private static function categoryFromRow(Map<string, string> $row): Category {
105104
$db = await self::genDb();
106105

107106
$result = await $db->queryf(
108-
'SELECT COUNT(*) FROM levels WHERE category_id = %d',
107+
'SELECT EXISTS(SELECT * FROM levels WHERE category_id = %d)',
109108
$category_id,
110109
);
111110

112111
if ($result->numRows() > 0) {
113112
invariant($result->numRows() === 1, 'Expected exactly one result');
114-
return intval($result->mapRows()[0]['COUNT(*)']) > 0;
113+
return intval($result->mapRows()[0]->firstValue()) > 0;
115114
} else {
116115
return false;
117116
}
@@ -236,13 +235,13 @@ private static function categoryFromRow(Map<string, string> $row): Category {
236235
$db = await self::genDb();
237236

238237
$result = await $db->queryf(
239-
'SELECT COUNT(*) FROM categories WHERE category = %s',
238+
'SELECT EXISTS(SELECT * FROM categories WHERE category = %s)',
240239
$category,
241240
);
242241

243242
if ($result->numRows() > 0) {
244243
invariant($result->numRows() === 1, 'Expected exactly one result');
245-
return (intval(idx($result->mapRows()[0], 'COUNT(*)')) > 0);
244+
return intval($result->mapRows()[0]->firstValue()) > 0;
246245
} else {
247246
return false;
248247
}

src/models/Configuration.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,13 @@ public function getDescription(): string {
107107
public static async function genValidField(string $field): Awaitable<bool> {
108108
$db = await self::genDb();
109109
$result = await $db->queryf(
110-
'SELECT COUNT(*) FROM configuration WHERE field = %s',
110+
'SELECT EXISTS(SELECT * FROM configuration WHERE field = %s)',
111111
$field,
112112
);
113113

114114
invariant($result->numRows() === 1, 'Expected exactly one result');
115115

116-
return intval(idx(firstx($result->mapRows()), 'COUNT(*)')) > 0;
116+
return intval($result->mapRows()[0]->firstValue()) > 0;
117117
}
118118

119119
// All the password types.

src/models/Country.php

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -316,32 +316,13 @@ private static function countryFromRow(Map<string, string> $row): Country {
316316
$db = await self::genDb();
317317

318318
$result = await $db->queryf(
319-
'SELECT COUNT(*) FROM countries WHERE iso_code = %s',
319+
'SELECT EXISTS(SELECT * FROM countries WHERE iso_code = %s)',
320320
$country,
321321
);
322322

323323
if ($result->numRows() > 0) {
324324
invariant($result->numRows() === 1, 'Expected exactly one result');
325-
return (intval(idx($result->mapRows()[0], 'COUNT(*)')) > 0);
326-
} else {
327-
return false;
328-
}
329-
}
330-
331-
// Check if a country already exists, by id
332-
public static async function genCheckExistsById(
333-
int $entity_id,
334-
): Awaitable<bool> {
335-
$db = await self::genDb();
336-
337-
$result = await $db->queryf(
338-
'SELECT COUNT(*) FROM countries WHERE id = %d',
339-
$entity_id,
340-
);
341-
342-
if ($result->numRows() > 0) {
343-
invariant($result->numRows() === 1, 'Expected exactly one result');
344-
return (intval(idx($result->mapRows()[0], 'COUNT(*)')) > 0);
325+
return intval($result->mapRows()[0]->firstValue()) > 0;
345326
} else {
346327
return false;
347328
}

src/models/Level.php

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,34 +1305,15 @@ public static function getBasesResponses(
13051305

13061306
$result =
13071307
await $db->queryf(
1308-
'SELECT COUNT(*) FROM levels WHERE type = %s AND title = %s AND entity_id IN (SELECT id FROM countries WHERE iso_code = %s)',
1308+
'SELECT EXISTS(SELECT * FROM levels WHERE type = %s AND title = %s AND entity_id IN (SELECT id from countries WHERE iso_code = %s))',
13091309
$type,
13101310
$title,
13111311
$entity_iso_code,
13121312
);
13131313

13141314
if ($result->numRows() > 0) {
13151315
invariant($result->numRows() === 1, 'Expected exactly one result');
1316-
return (intval(idx($result->mapRows()[0], 'COUNT(*)')) > 0);
1317-
} else {
1318-
return false;
1319-
}
1320-
}
1321-
1322-
// Check if a level already exists by type, title and entity.
1323-
public static async function genAlreadyExistById(
1324-
int $level_id,
1325-
): Awaitable<bool> {
1326-
$db = await self::genDb();
1327-
1328-
$result = await $db->queryf(
1329-
'SELECT COUNT(*) FROM levels WHERE id = %d',
1330-
$level_id,
1331-
);
1332-
1333-
if ($result->numRows() > 0) {
1334-
invariant($result->numRows() === 1, 'Expected exactly one result');
1335-
return (intval(idx($result->mapRows()[0], 'COUNT(*)')) > 0);
1316+
return intval($result->mapRows()[0]->firstValue()) > 0;
13361317
} else {
13371318
return false;
13381319
}
@@ -1374,15 +1355,15 @@ public static function getBasesResponses(
13741355
$db = await self::genDb();
13751356
$result =
13761357
await $db->queryf(
1377-
'SELECT COUNT(*) FROM levels WHERE type = %s AND title = %s AND description = %s AND points = %d',
1358+
'SELECT EXISTS(SELECT * FROM levels WHERE type = %s AND title = %s AND description = %s AND points = %d)',
13781359
$type,
13791360
$title,
13801361
$description,
13811362
$points,
13821363
);
13831364
if ($result->numRows() > 0) {
13841365
invariant($result->numRows() === 1, 'Expected exactly one result');
1385-
return (intval(idx($result->mapRows()[0], 'COUNT(*)')) > 0);
1366+
return intval($result->mapRows()[0]->firstValue()) > 0;
13861367
} else {
13871368
return false;
13881369
}

src/models/Session.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,11 @@ private static function sessionFromRow(Map<string, string> $row): Session {
164164
if (!$mc_result || count($mc_result) === 0 || $refresh) {
165165
$db = await self::genDb();
166166
$result = await $db->queryf(
167-
'SELECT COUNT(*) FROM sessions WHERE cookie = %s',
167+
'SELECT EXISTS(SELECT * FROM sessions WHERE cookie = %s)',
168168
$cookie,
169169
);
170170
invariant($result->numRows() === 1, 'Expected exactly one result');
171-
if (intval(idx($result->mapRows()[0], 'COUNT(*)')) > 0) {
171+
if (intval($result->mapRows()[0]->firstValue()) > 0) {
172172
await self::genCreateCacheSession($cookie);
173173
return true;
174174
}

src/models/Team.php

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -217,24 +217,6 @@ public static function regenerateHash(string $password_hash): bool {
217217
}
218218
}
219219

220-
// Check to see if the team is active.
221-
public static async function genCheckTeamStatus(
222-
int $team_id,
223-
): Awaitable<bool> {
224-
$db = await self::genDb();
225-
$result = await $db->queryf(
226-
'SELECT COUNT(*) FROM teams WHERE id = %d AND active = 1 LIMIT 1',
227-
$team_id,
228-
);
229-
230-
if ($result->numRows() > 0) {
231-
invariant($result->numRows() === 1, 'Expected exactly one result');
232-
return (intval(idx($result->mapRows()[0], 'COUNT(*)')) > 0);
233-
} else {
234-
return false;
235-
}
236-
}
237-
238220
// Create a team and return the created team id.
239221
public static async function genCreate(
240222
string $name,
@@ -542,30 +524,13 @@ public static function regenerateHash(string $password_hash): bool {
542524
$db = await self::genDb();
543525

544526
$result = await $db->queryf(
545-
'SELECT COUNT(*) FROM teams WHERE name = %s',
527+
'SELECT EXISTS(SELECT * FROM teams WHERE name = %s)',
546528
$team_name,
547529
);
548530

549531
if ($result->numRows() > 0) {
550532
invariant($result->numRows() === 1, 'Expected exactly one result');
551-
return (intval(idx($result->mapRows()[0], 'COUNT(*)')) > 0);
552-
} else {
553-
return false;
554-
}
555-
}
556-
557-
// Check if a team name is already created.
558-
public static async function genTeamExistById(
559-
int $team_id,
560-
): Awaitable<bool> {
561-
$db = await self::genDb();
562-
563-
$result =
564-
await $db->queryf('SELECT COUNT(*) FROM teams WHERE id = %d', $team_id);
565-
566-
if ($result->numRows() > 0) {
567-
invariant($result->numRows() === 1, 'Expected exactly one result');
568-
return (intval(idx($result->mapRows()[0], 'COUNT(*)')) > 0);
533+
return intval($result->mapRows()[0]->firstValue()) > 0;
569534
} else {
570535
return false;
571536
}
@@ -717,16 +682,6 @@ public static function regenerateHash(string $password_hash): bool {
717682
MultiTeam::invalidateMCRecords(); // Invalidate Memcached MultiTeam data.
718683
}
719684

720-
// Teams total number.
721-
public static async function genTeamsCount(): Awaitable<int> {
722-
$db = await self::genDb();
723-
724-
$result = await $db->queryf('SELECT COUNT(*) AS count FROM teams');
725-
726-
invariant($result->numRows() === 1, 'Expected exactly one result');
727-
return intval(idx($result->mapRows()[0], 'COUNT(*)'));
728-
}
729-
730685
public static async function genFirstCapture(
731686
int $level_id,
732687
): Awaitable<Team> {

src/models/Token.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,13 @@ private static function generate(): string {
116116

117117
$result =
118118
await $db->queryf(
119-
'SELECT COUNT(*) FROM registration_tokens WHERE used = 0 AND token = %s',
119+
'SELECT EXISTS(SELECT * FROM registration_tokens WHERE used = 0 AND token = %s)',
120120
$token,
121121
);
122122

123123
if ($result->numRows() > 0) {
124124
invariant($result->numRows() === 1, 'Expected exactly one result');
125-
return (intval($result->mapRows()[0]['COUNT(*)']) > 0);
125+
return intval($result->mapRows()[0]->firstValue()) > 0;
126126
} else {
127127
return false;
128128
}

0 commit comments

Comments
 (0)