-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery.sql
More file actions
235 lines (192 loc) · 5.33 KB
/
Copy pathquery.sql
File metadata and controls
235 lines (192 loc) · 5.33 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
-- name: CreateUser :one
INSERT INTO users (email, password, username)
VALUES ($1, $2, $3)
RETURNING *;
-- name: GetUser :one
SELECT * FROM users
WHERE id = $1 LIMIT 1;
-- name: GetUserByEmail :one
SELECT * FROM users
WHERE email = $1 LIMIT 1;
-- name: GetUserByUsername :one
SELECT * FROM users
WHERE username = $1 LIMIT 1;
-- name: ListUsers :many
SELECT * FROM users
ORDER BY username;
-- name: UpdateUser :one
UPDATE users
SET email = $2, password = $3, username = $4, updated_at = CURRENT_TIMESTAMP
WHERE id = $1
RETURNING *;
-- name: DeleteUser :exec
DELETE FROM users
WHERE id = $1;
-- name: CreateTeam :one
INSERT INTO teams (name)
VALUES ($1)
RETURNING *;
-- name: GetTeam :one
SELECT * FROM teams
WHERE id = $1 LIMIT 1;
-- name: GetTeamByName :one
SELECT * FROM teams
WHERE name = $1 LIMIT 1;
-- name: ListTeams :many
SELECT * FROM teams
ORDER BY name;
-- name: UpdateTeam :one
UPDATE teams
SET name = $2, updated_at = CURRENT_TIMESTAMP
WHERE id = $1
RETURNING *;
-- name: DeleteTeam :exec
DELETE FROM teams
WHERE id = $1;
-- name: CreateDevice :one
INSERT INTO devices (cert_fingerprint, device_uuid, name)
VALUES ($1, $2, $3)
RETURNING *;
-- name: GetDevice :one
SELECT * FROM devices
WHERE id = $1 LIMIT 1;
-- name: GetDeviceByCertFingerprint :one
SELECT * FROM devices
WHERE cert_fingerprint = $1 LIMIT 1;
-- name: GetDeviceByUuid :one
SELECT * FROM devices
WHERE device_uuid = $1 LIMIT 1;
-- name: ListDevices :many
SELECT * FROM devices
ORDER BY name;
-- name: UpdateDevice :one
UPDATE devices
SET cert_fingerprint = $2, device_uuid = $3, name = $4, updated_at = CURRENT_TIMESTAMP
WHERE id = $1
RETURNING *;
-- name: DeleteDevice :exec
DELETE FROM devices
WHERE id = $1;
-- name: CreatePermission :one
INSERT INTO permissions (view_locations, view_route_history, manage_devices, manage_users, manage_team)
VALUES ($1, $2, $3, $4, $5)
RETURNING *;
-- name: GetPermission :one
SELECT * FROM permissions
WHERE id = $1 LIMIT 1;
-- name: ListPermissions :many
SELECT * FROM permissions
ORDER BY id;
-- name: UpdatePermission :one
UPDATE permissions
SET view_locations = $2, view_route_history = $3, manage_devices = $4, manage_users = $5, manage_team = $6, updated_at = CURRENT_TIMESTAMP
WHERE id = $1
RETURNING *;
-- name: DeletePermission :exec
DELETE FROM permissions
WHERE id = $1;
-- name: AddUserToTeam :exec
INSERT INTO user_teams (user_id, team_id)
VALUES ($1, $2);
-- name: RemoveUserFromTeam :exec
DELETE FROM user_teams
WHERE user_id = $1 AND team_id = $2;
-- name: GetUserTeams :many
SELECT t.* FROM teams t
JOIN user_teams ut ON t.id = ut.team_id
WHERE ut.user_id = $1
ORDER BY t.name;
-- name: GetTeamUsers :many
SELECT u.* FROM users u
JOIN user_teams ut ON u.id = ut.user_id
WHERE ut.team_id = $1
ORDER BY u.username;
-- name: IsUserInTeam :one
SELECT EXISTS(
SELECT 1 FROM user_teams
WHERE user_id = $1 AND team_id = $2
);
-- name: AssignUserTeamPermission :exec
INSERT INTO user_team_permissions (user_id, team_id, permission_id)
VALUES ($1, $2, $3);
-- name: RevokeUserTeamPermission :exec
DELETE FROM user_team_permissions
WHERE user_id = $1 AND team_id = $2 AND permission_id = $3;
-- name: GetUserTeamPermissions :many
SELECT p.* FROM permissions p
JOIN user_team_permissions utp ON p.id = utp.permission_id
WHERE utp.user_id = $1 AND utp.team_id = $2
ORDER BY p.id;
-- name: GetUserPermissionsInTeam :one
SELECT
p.view_locations,
p.view_route_history,
p.manage_devices,
p.manage_users,
p.manage_team
FROM permissions p
JOIN user_team_permissions utp ON p.id = utp.permission_id
WHERE utp.user_id = $1 AND utp.team_id = $2
LIMIT 1;
-- name: ListUserTeamPermissions :many
SELECT
u.id as user_id,
u.username,
t.id as team_id,
t.name as team_name,
p.id as permission_id,
p.view_locations,
p.view_route_history,
p.manage_devices,
p.manage_users,
p.manage_team
FROM user_team_permissions utp
JOIN users u ON utp.user_id = u.id
JOIN teams t ON utp.team_id = t.id
JOIN permissions p ON utp.permission_id = p.id
ORDER BY u.username, t.name;
-- name: AddDeviceToTeam :exec
INSERT INTO team_devices (team_id, device_id)
VALUES ($1, $2);
-- name: RemoveDeviceFromTeam :exec
DELETE FROM team_devices
WHERE team_id = $1 AND device_id = $2;
-- name: GetTeamDevices :many
SELECT d.* FROM devices d
JOIN team_devices td ON d.id = td.device_id
WHERE td.team_id = $1
ORDER BY d.name;
-- name: GetDeviceTeams :many
SELECT t.* FROM teams t
JOIN team_devices td ON t.id = td.team_id
WHERE td.device_id = $1
ORDER BY t.name;
-- name: IsDeviceInTeam :one
SELECT EXISTS(
SELECT 1 FROM team_devices
WHERE team_id = $1 AND device_id = $2
);
-- name: GetUserDevicesInTeam :many
SELECT DISTINCT d.* FROM devices d
JOIN team_devices td ON d.id = td.device_id
JOIN user_teams ut ON td.team_id = ut.team_id
WHERE ut.user_id = $1 AND ut.team_id = $2
ORDER BY d.name;
-- name: GetUserAllDevices :many
SELECT DISTINCT d.* FROM devices d
JOIN team_devices td ON d.id = td.device_id
JOIN user_teams ut ON td.team_id = ut.team_id
WHERE ut.user_id = $1
ORDER BY d.name;
-- name: CountUsersByTeam :many
SELECT t.id, t.name, COUNT(ut.user_id) as user_count
FROM teams t
LEFT JOIN user_teams ut ON t.id = ut.team_id
GROUP BY t.id, t.name
ORDER BY t.name;
-- name: CountDevicesByTeam :many
SELECT t.id, t.name, COUNT(td.device_id) as device_count
FROM teams t
LEFT JOIN team_devices td ON t.id = td.team_id
GROUP BY t.id, t.name
ORDER BY t.name;