-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.sql
154 lines (154 loc) · 3.39 KB
/
query.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
-- name: GetTracksByArtist :many
-- Gets basic track information filtered by artist
SELECT id,
vocal_folder_path,
instrumental_folder_path,
album_id,
total_duration,
info,
instrumental,
tempo,
"key"
FROM tracks
WHERE info->>'Artist' = $1;
-- name: GetTracksByAlbumId :many
-- Gets basic track information filtered by album ID, sorted by track list
SELECT id,
vocal_folder_path,
instrumental_folder_path,
album_id,
total_duration,
info,
instrumental,
tempo,
"key"
FROM tracks
WHERE album_id = $1
ORDER BY info->>'Track';
-- name: GetTracksByGenre :many
-- Gets basic track information filtered by genre
SELECT id,
vocal_folder_path,
instrumental_folder_path,
album_id,
total_duration,
info,
instrumental,
tempo,
"key"
FROM tracks
WHERE info->>'Genre' = $1;
-- name: GetTrackCount :one
-- Gets total number of tracks
SELECT COUNT(*)
FROM tracks;
-- name: SearchTracks :many
-- Searches tracks by title, artist, or genre
SELECT id,
vocal_folder_path,
instrumental_folder_path,
album_id,
total_duration,
info,
instrumental,
tempo,
"key"
FROM tracks
WHERE info->>'Title' ILIKE '%' || $1 || '%'
OR info->>'Artist' ILIKE '%' || $1 || '%'
OR info->>'Genre' ILIKE '%' || $1 || '%'
LIMIT $2 OFFSET $3;
-- name: GetRandomUnlistenedTrack :one
-- Get a random track that hasn't been listened to by the given anonymous user
SELECT t.*
FROM tracks t
LEFT JOIN albums a ON a.id = t.album_id
LEFT JOIN listening_histories lh ON t.id = lh.track_id
AND lh.anon_id = $1
WHERE lh.track_id IS NULL
ORDER BY RANDOM()
LIMIT 1;
-- name: DeleteListeningHistoryByAnonID :exec
-- Delete all listening history for a given anonymous user
DELETE FROM listening_histories
WHERE anon_id = $1;
-- name: GetRandomTrack :one
-- Get a completely random track
SELECT t.*
FROM tracks t
LEFT JOIN albums a ON a.id = t.album_id
ORDER BY RANDOM()
LIMIT 1;
-- name: RecordListeningHistory :exec
INSERT INTO listening_histories (track_id, anon_id, listened_at)
VALUES ($1, $2, $3);
-- name: GetAlbumById :one
SELECT a.*
FROM albums a
WHERE a.id = $1;
-- name: GetAlbumByName :one
SELECT a.*
FROM albums a
WHERE a.name = $1;
-- name: GetAlbumByArtist :one
SELECT a.*
FROM albums a
WHERE a.artist = $1;
-- name: GetAlbumByNameAndArtist :one
SELECT a.*
FROM albums a
WHERE a.name = $1
AND a.artist = $2;
-- name: GetAlbumIDByName :one
SELECT a.id
FROM albums a
WHERE a.name = $1;
-- name: GetAlbumIDByNameAndArtist :one
SELECT a.id
FROM albums a
WHERE a.name = $1
AND a.artist = $2;
-- name: InsertAlbum :one
-- returns id
INSERT INTO public.albums (id, "name", cover, artist)
VALUES($1, $2, $3, $4)
RETURNING id;
-- name: GetTrackByID :one
-- Get track by ID (all columns, use GetTrackBasicByID if waveforms are not needed)
SELECT t.*
FROM tracks t
WHERE t.id = $1;
-- name: GetAlbumCoverByID :one
-- Get album cover by the album ID
SELECT a.cover
FROM albums a
WHERE a.id = $1;
-- name: InsertTrack :exec
INSERT INTO public.tracks (
id,
vocal_folder_path,
instrumental_folder_path,
album_id,
total_duration,
info,
instrumental,
tempo,
"key",
vocal_waveform,
instrumental_waveform,
album_name
)
VALUES(
$1,
$2,
$3,
$4,
$5,
$6,
$7,
$8,
$9,
$10,
$11,
$12
);