Skip to content

Commit 8eb1e64

Browse files
authored
Merge pull request #4300 from Roardom/ansi-db
(Refactor) Use ANSI-compatible syntax for raw database queries
2 parents 18a46cb + 0e4bcb5 commit 8eb1e64

25 files changed

+112
-94
lines changed

app/Console/Commands/AutoCacheUserLeechCounts.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ class AutoCacheUserLeechCounts extends Command
4545
final public function handle(): void
4646
{
4747
$peerCounts = User::withoutGlobalScopes()
48-
->selectRaw("CONCAT('user-leeching-count:', id) as cacheKey")
49-
->selectRaw('(select COUNT(*) from peers where peers.user_id = users.id and seeder = 0 and active = 1 and visible = 1) as count')
48+
->selectRaw("'user-leeching-count:' || id as cacheKey")
49+
->selectRaw('(select COUNT(*) from peers where peers.user_id = users.id and seeder = FALSE and active = TRUE and visible = TRUE) as count')
5050
->pluck('count', 'cacheKey')
5151
->toArray();
5252

app/Console/Commands/AutoDeactivateWarning.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ final public function handle(): void
8282

8383
// Calculate User Warning Count and Enable DL Priv If Required.
8484
Warning::with('warneduser')
85-
->select(DB::raw('user_id, SUM(active = 1) as value'))
85+
->select(DB::raw('user_id, SUM(active = TRUE) as value'))
8686
->groupBy('user_id')
8787
->having('value', '<', config('hitrun.max_warnings'))
8888
->whereRelation('warneduser', 'can_download', '=', false)

app/Console/Commands/AutoHighspeedTag.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ final public function handle(): void
6464
fn ($join) => $join->on('torrents.id', '=', 'highspeed_torrents.torrent_id')
6565
)
6666
->update([
67-
'highspeed' => DB::raw('CASE WHEN highspeed_torrents.torrent_id IS NOT NULL THEN 1 ELSE 0 END'),
67+
'highspeed' => DB::raw('CASE WHEN highspeed_torrents.torrent_id IS NOT NULL THEN TRUE ELSE FALSE END'),
6868
'updated_at' => DB::raw('updated_at'),
6969
]);
7070

app/Console/Commands/AutoUpsertHistories.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ final public function handle(): void
9797
// We need to make sure seeder and active are updated after seedtime, otherwise the seedtime logic for ensuring it's not a new announce and the left was 0 in the last announce breaks.
9898
// Unfortunately, laravel sorts the keys in this array alphabetically when inserting so reordering the keys themselves in this array doesn't work.
9999
// This leaves us with this hacky fix.
100-
'seedtime' => DB::raw('IF(DATE_ADD(updated_at, INTERVAL 5400 SECOND) > VALUES(updated_at) AND seeder = 1 AND active = 1 AND VALUES(seeder) = 1, seedtime + TIMESTAMPDIFF(SECOND, updated_at, VALUES(updated_at)), seedtime), seeder = VALUES(seeder), active = VALUES(active)'),
100+
'seedtime' => DB::raw('CASE WHEN DATE_ADD(updated_at, INTERVAL 5400 SECOND) > VALUES(updated_at) AND seeder = TRUE AND active = TRUE AND VALUES(seeder) = TRUE THEN seedtime + TIMESTAMPDIFF(SECOND, updated_at, VALUES(updated_at)) ELSE seedtime END, seeder = VALUES(seeder), active = VALUES(active)'),
101101
'immune' => DB::raw('immune AND VALUES(immune)'),
102102
'completed_at' => DB::raw('COALESCE(completed_at, VALUES(completed_at))'),
103103
],

app/Console/Commands/SyncPeers.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ final public function handle(): void
5353
->leftJoinSub(
5454
Peer::query()
5555
->select('torrent_id')
56-
->addSelect(DB::raw('SUM(peers.left = 0 AND peers.active = 1 AND peers.visible = 1) AS updated_seeders'))
57-
->addSelect(DB::raw('SUM(peers.left != 0 AND peers.active = 1 AND peers.visible = 1) AS updated_leechers'))
56+
->addSelect(DB::raw('SUM(peers.left = 0 AND peers.active = TRUE AND peers.visible = TRUE) AS updated_seeders'))
57+
->addSelect(DB::raw('SUM(peers.left != 0 AND peers.active = TRUE AND peers.visible = TRUE) AS updated_leechers'))
5858
->groupBy('torrent_id'),
5959
'seeders_leechers',
6060
fn ($join) => $join->on('torrents.id', '=', 'seeders_leechers.torrent_id')

app/Http/Controllers/HomeController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function index(Request $request): \Illuminate\Contracts\View\Factory|\Ill
6464
},
6565
])
6666
->where('last_action', '>', now()->subMinutes(60))
67-
->orderByRaw('(select position from `groups` where `groups`.id = users.group_id), group_id, username')
67+
->orderByRaw('(select position from "groups" where "groups".id = users.group_id), group_id, username')
6868
->get()
6969
->sortBy(fn ($user) => $user->privacy?->hidden || ! $user->isVisible($user, 'other', 'show_online')),
7070
),

app/Http/Controllers/PlaylistController.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,15 @@ public function show(Request $request, Playlist $playlist): \Illuminate\Contract
100100

101101
$torrents = $playlist->torrents()
102102
->select('*')
103-
->selectRaw("
103+
->selectRaw(<<<'SQL'
104104
CASE
105-
WHEN category_id IN (SELECT `id` from `categories` where `movie_meta` = 1) THEN 'movie'
106-
WHEN category_id IN (SELECT `id` from `categories` where `tv_meta` = 1) THEN 'tv'
107-
WHEN category_id IN (SELECT `id` from `categories` where `game_meta` = 1) THEN 'game'
108-
WHEN category_id IN (SELECT `id` from `categories` where `music_meta` = 1) THEN 'music'
109-
WHEN category_id IN (SELECT `id` from `categories` where `no_meta` = 1) THEN 'no'
105+
WHEN category_id IN (SELECT id from categories where movie_meta = TRUE) THEN 'movie'
106+
WHEN category_id IN (SELECT id from categories where tv_meta = TRUE) THEN 'tv'
107+
WHEN category_id IN (SELECT id from categories where game_meta = TRUE) THEN 'game'
108+
WHEN category_id IN (SELECT id from categories where music_meta = TRUE) THEN 'music'
109+
WHEN category_id IN (SELECT id from categories where no_meta = TRUE) THEN 'no'
110110
END as meta
111-
")
111+
SQL)
112112
->with(['category', 'resolution', 'type', 'user.group'])
113113
->orderBy('name')
114114
->paginate(26);

app/Http/Controllers/Staff/DonationController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function index(Request $request): \Illuminate\Contracts\View\View|\Illumi
4545
->orderBy('date')
4646
->get();
4747

48-
$monthlyDonations = Donation::selectRaw('YEAR(donations.created_at) as year, MONTH(donations.created_at) as month, SUM(donation_packages.cost) as total')
48+
$monthlyDonations = Donation::selectRaw('EXTRACT(YEAR FROM donations.created_at) as year, EXTRACT(MONTH FROM donations.created_at) as month, SUM(donation_packages.cost) as total')
4949
->join('donation_packages', 'donations.package_id', '=', 'donation_packages.id')
5050
->where('donations.status', '=', Donation::APPROVED)
5151
->groupBy('year', 'month')

app/Http/Controllers/Staff/HomeController.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,23 +53,23 @@ public function index(Request $request): \Illuminate\Contracts\View\Factory|\Ill
5353

5454
return view('Staff.dashboard.index', [
5555
'users' => cache()->remember('dashboard_users', 300, fn () => DB::table('users')
56-
->selectRaw('count(*) as total')
57-
->selectRaw(\sprintf('count(case when group_id = %s then 1 end) as banned', $bannedGroup[0]))
58-
->selectRaw(\sprintf('count(case when group_id = %s then 1 end) as validating', $validatingGroup[0]))
56+
->selectRaw('COUNT(*) AS total')
57+
->selectRaw('SUM(group_id = ?) AS banned', [$bannedGroup[0]])
58+
->selectRaw('SUM(group_id = ?) AS validating', [$validatingGroup[0]])
5959
->first()),
6060
'torrents' => cache()->remember('dashboard_torrents', 300, fn () => DB::table('torrents')
61-
->selectRaw('count(*) as total')
62-
->selectRaw('count(case when status = 0 then 1 end) as pending')
63-
->selectRaw('count(case when status = 1 then 1 end) as approved')
64-
->selectRaw('count(case when status = 2 then 1 end) as rejected')
65-
->selectRaw('count(case when status = 3 then 1 end) as postponed')
61+
->selectRaw('COUNT(*) AS total')
62+
->selectRaw('SUM(status = 0) AS pending')
63+
->selectRaw('SUM(status = 1) AS approved')
64+
->selectRaw('SUM(status = 2) AS rejected')
65+
->selectRaw('SUM(status = 3) AS postponed')
6666
->first()),
6767
'peers' => cache()->remember('dashboard_peers', 300, fn () => DB::table('peers')
68-
->selectRaw('count(*) as total')
69-
->selectRaw('sum(active = 1) as active')
70-
->selectRaw('sum(active = 0) as inactive')
71-
->selectRaw('sum(seeder = 0 AND active = 1) as leechers')
72-
->selectRaw('sum(seeder = 1 AND active = 1) as seeders')
68+
->selectRaw('COUNT(*) AS total')
69+
->selectRaw('SUM(active = TRUE) AS active')
70+
->selectRaw('SUM(active = FALSE) AS inactive')
71+
->selectRaw('SUM(seeder = FALSE AND active = TRUE) AS leechers')
72+
->selectRaw('SUM(seeder = TRUE AND active = TRUE) AS seeders')
7373
->first()),
7474
'unsolvedReportsCount' => DB::table('reports')->whereNull('snoozed_until')->where('solved', '=', false)->count(),
7575
'pendingApplicationsCount' => DB::table('applications')->where('status', '=', 0)->count(),

app/Http/Controllers/User/UserController.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function show(Request $request, User $user): \Illuminate\Contracts\View\F
4444
'application',
4545
'privacy',
4646
'userban' => ['banneduser', 'staffuser'],
47-
'tickets' => fn ($query) => $query->orderByRaw('ISNULL(closed_at) desc')->orderByDesc('id'),
47+
'tickets' => fn ($query) => $query->orderByRaw('CASE WHEN closed_at IS NULL THEN 1 ELSE 0 END DESC')->orderByDesc('id'),
4848
])
4949
->loadCount([
5050
'torrents as anon_uploads_count' => fn ($query) => $query->where('anon', '=', true),
@@ -104,9 +104,9 @@ public function show(Request $request, User $user): \Illuminate\Contracts\View\F
104104
->whereNotNull('unlocked_at')
105105
->get(),
106106
'peers' => Peer::query()
107-
->selectRaw('SUM(seeder = 0 AND active = 1) as leeching')
108-
->selectRaw('SUM(seeder = 1 AND active = 1) as seeding')
109-
->selectRaw('SUM(active = 0) as inactive')
107+
->selectRaw('SUM(seeder = FALSE AND active = TRUE) as leeching')
108+
->selectRaw('SUM(seeder = TRUE AND active = TRUE) as seeding')
109+
->selectRaw('SUM(active = FALSE) as inactive')
110110
->where('user_id', '=', $user->id)
111111
->first(),
112112
'watch' => $user->watchlist,

app/Http/Livewire/HistorySearch.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ final public function histories(): \Illuminate\Pagination\LengthAwarePaginator
118118
DB::raw('SUM(active AND seeder) AS seeding_count'),
119119
DB::raw('SUM(active AND NOT seeder) AS leeching_count'),
120120
DB::raw('SUM(prewarned_at IS NOT NULL) AS prewarn_count'),
121-
DB::raw('SUM(hitrun = 1) AS hitrun_count'),
122-
DB::raw('SUM(immune = 1) AS immune_count'),
121+
DB::raw('SUM(hitrun = TRUE) AS hitrun_count'),
122+
DB::raw('SUM(immune = TRUE) AS immune_count'),
123123
])
124124
->withCasts([
125125
'created_at_min' => 'datetime',

app/Http/Livewire/PeerSearch.php

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,15 @@ final public function peers(): \Illuminate\Contracts\Pagination\LengthAwarePagin
132132
->selectRaw('INET6_NTOA(peers.ip) as ip')
133133
->selectRaw('SUM(peers.uploaded) as uploaded')
134134
->selectRaw('SUM(peers.downloaded) as downloaded')
135-
->selectRaw('SUM(peers.`left`) as `left`')
135+
->selectRaw('SUM(peers."left") as "left"')
136136
->selectRaw('MIN(peers.created_at) as created_at')
137137
->selectRaw('MAX(peers.updated_at) as updated_at')
138138
->selectRaw('COUNT(DISTINCT peers.torrent_id, peers.user_id, peers.peer_id) as peer_count')
139-
->selectRaw('SUM(peers.connectable = 1) as connectable_count')
140-
->selectRaw('SUM(peers.connectable = 0) as unconnectable_count')
141-
->selectRaw('SUM(peers.active = 1) as active_count')
142-
->selectRaw('SUM(peers.active = 0) as inactive_count')
143-
->selectRaw('ROUND(COALESCE(SUM(peers.active = 0) / SUM(peers.active = 1), 0), 2) as inactive_ratio')
139+
->selectRaw('SUM(peers.connectable = TRUE) as connectable_count')
140+
->selectRaw('SUM(peers.connectable = FALSE) as unconnectable_count')
141+
->selectRaw('SUM(peers.active = TRUE) as active_count')
142+
->selectRaw('SUM(peers.active = FALSE) as inactive_count')
143+
->selectRaw('ROUND(COALESCE(SUM(peers.active = FALSE) / SUM(peers.active = TRUE), 0), 2) as inactive_ratio')
144144
->groupBy(['peers.user_id', 'peers.agent', 'peers.ip', 'peers.port'])
145145
->with(['user', 'user.group'])
146146
)
@@ -154,15 +154,15 @@ final public function peers(): \Illuminate\Contracts\Pagination\LengthAwarePagin
154154
->selectRaw('COUNT(DISTINCT peers.port) as port')
155155
->selectRaw('SUM(peers.uploaded) as uploaded')
156156
->selectRaw('SUM(peers.downloaded) as downloaded')
157-
->selectRaw('SUM(`left`) as `left`')
157+
->selectRaw('SUM("left") as "left"')
158158
->selectRaw('MIN(peers.created_at) as created_at')
159159
->selectRaw('MAX(peers.updated_at) as updated_at')
160160
->selectRaw('COUNT(*) as peer_count')
161-
->selectRaw('SUM(peers.connectable = 1) as connectable_count')
162-
->selectRaw('SUM(peers.connectable = 0) as unconnectable_count')
163-
->selectRaw('SUM(peers.active = 1) as active_count')
164-
->selectRaw('SUM(peers.active = 0) as inactive_count')
165-
->selectRaw('ROUND(COALESCE(SUM(peers.active = 0) / SUM(peers.active = 1), 0), 2) as inactive_ratio')
161+
->selectRaw('SUM(peers.connectable = TRUE) as connectable_count')
162+
->selectRaw('SUM(peers.connectable = FALSE) as unconnectable_count')
163+
->selectRaw('SUM(peers.active = TRUE) as active_count')
164+
->selectRaw('SUM(peers.active = FALSE) as inactive_count')
165+
->selectRaw('ROUND(COALESCE(SUM(peers.active = FALSE) / SUM(peers.active = TRUE), 0), 2) as inactive_ratio')
166166
->groupBy(['peers.user_id', 'peers.ip'])
167167
->with(['user', 'user.group'])
168168
)
@@ -176,15 +176,15 @@ final public function peers(): \Illuminate\Contracts\Pagination\LengthAwarePagin
176176
->selectRaw('COUNT(DISTINCT peers.port) as port')
177177
->selectRaw('SUM(peers.uploaded) as uploaded')
178178
->selectRaw('SUM(peers.downloaded) as downloaded')
179-
->selectRaw('SUM(`left`) as `left`')
179+
->selectRaw('SUM("left") as "left"')
180180
->selectRaw('MIN(peers.created_at) as created_at')
181181
->selectRaw('MAX(peers.updated_at) as updated_at')
182182
->selectRaw('COUNT(*) as peer_count')
183-
->selectRaw('SUM(peers.connectable = 1) as connectable_count')
184-
->selectRaw('SUM(peers.connectable = 0) as unconnectable_count')
185-
->selectRaw('SUM(peers.active = 1) as active_count')
186-
->selectRaw('SUM(peers.active = 0) as inactive_count')
187-
->selectRaw('ROUND(COALESCE(SUM(peers.active = 0) / SUM(peers.active = 1), 0), 2) as inactive_ratio')
183+
->selectRaw('SUM(peers.connectable = TRUE) as connectable_count')
184+
->selectRaw('SUM(peers.connectable = FALSE) as unconnectable_count')
185+
->selectRaw('SUM(peers.active = TRUE) as active_count')
186+
->selectRaw('SUM(peers.active = FALSE) as inactive_count')
187+
->selectRaw('ROUND(COALESCE(SUM(peers.active = FALSE) / SUM(peers.active = TRUE), 0), 2) as inactive_ratio')
188188
->groupBy(['peers.user_id'])
189189
->with(['user', 'user.group'])
190190
)
@@ -220,12 +220,12 @@ final public function peers(): \Illuminate\Contracts\Pagination\LengthAwarePagin
220220
$this->groupBy === 'none',
221221
fn ($query) => $query
222222
->selectRaw('torrents.size as size')
223-
->selectRaw('IF(peers.connectable = 1, torrents.size, 0) as connectable_size')
224-
->selectRaw('IF(peers.connectable = 0, torrents.size, 0) as unconnectable_size'),
223+
->selectRaw('CASE WHEN peers.connectable = TRUE THEN torrents.size ELSE 0 END as connectable_size')
224+
->selectRaw('CASE WHEN peers.connectable = FALSE THEN torrents.size ELSE 0 END as unconnectable_size'),
225225
fn ($query) => $query
226226
->selectRaw('SUM(torrents.size) as size')
227-
->selectRaw('SUM(IF(peers.connectable = 1, torrents.size, 0)) as connectable_size')
228-
->selectRaw('SUM(IF(peers.connectable = 0, torrents.size, 0)) as unconnectable_size')
227+
->selectRaw('SUM(CASE WHEN peers.connectable = TRUE THEN torrents.size ELSE 0 END) as connectable_size')
228+
->selectRaw('SUM(CASE WHEN peers.connectable = FALSE THEN torrents.size ELSE 0 END) as unconnectable_size')
229229
)
230230
)
231231
->when($this->ip !== '', fn ($query) => $query->where(DB::raw('INET6_NTOA(ip)'), 'LIKE', $this->ip.'%'))

app/Http/Livewire/TopTorrents.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,15 @@ final public function torrents(): \Illuminate\Support\Collection
6464
->orWhereNotNull('completed_at')
6565
),
6666
])
67-
->selectRaw("
67+
->selectRaw(<<<SQL
6868
CASE
69-
WHEN category_id IN (SELECT `id` from `categories` where `movie_meta` = 1) THEN 'movie'
70-
WHEN category_id IN (SELECT `id` from `categories` where `tv_meta` = 1) THEN 'tv'
71-
WHEN category_id IN (SELECT `id` from `categories` where `game_meta` = 1) THEN 'game'
72-
WHEN category_id IN (SELECT `id` from `categories` where `music_meta` = 1) THEN 'music'
73-
WHEN category_id IN (SELECT `id` from `categories` where `no_meta` = 1) THEN 'no'
74-
END as meta
75-
")
69+
WHEN category_id IN (SELECT id FROM categories WHERE movie_meta = 1) THEN 'movie'
70+
WHEN category_id IN (SELECT id FROM categories WHERE tv_meta = 1) THEN 'tv'
71+
WHEN category_id IN (SELECT id FROM categories WHERE game_meta = 1) THEN 'game'
72+
WHEN category_id IN (SELECT id FROM categories WHERE music_meta = 1) THEN 'music'
73+
WHEN category_id IN (SELECT id FROM categories WHERE no_meta = 1) THEN 'no'
74+
END AS meta
75+
SQL)
7676
->withCount(['thanks', 'comments'])
7777
->when($this->tab === 'newest', fn ($query) => $query->orderByDesc('id'))
7878
->when($this->tab === 'seeded', fn ($query) => $query->orderByDesc('seeders'))

0 commit comments

Comments
 (0)