aboutsummaryrefslogtreecommitdiffhomepage
path: root/backend/db/query.sql.go
blob: fdd40dde86451a163e28c4dcfa05279fb9ea0962 (plain)
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.26.0
// source: query.sql

package db

import (
	"context"

	"github.com/jackc/pgx/v5/pgtype"
)

const aggregateTestcaseResults = `-- name: AggregateTestcaseResults :one
SELECT
    CASE
        WHEN COUNT(CASE WHEN r.status IS NULL            THEN 1 END) > 0 THEN 'running'
        WHEN COUNT(CASE WHEN r.status = 'internal_error' THEN 1 END) > 0 THEN 'internal_error'
        WHEN COUNT(CASE WHEN r.status = 'timeout'        THEN 1 END) > 0 THEN 'timeout'
        WHEN COUNT(CASE WHEN r.status = 'runtime_error'  THEN 1 END) > 0 THEN 'runtime_error'
        WHEN COUNT(CASE WHEN r.status = 'wrong_answer'   THEN 1 END) > 0 THEN 'wrong_answer'
        ELSE 'success'
    END AS status
FROM testcases
LEFT JOIN testcase_results AS r ON testcases.testcase_id = r.testcase_id
WHERE r.submission_id = $1
`

func (q *Queries) AggregateTestcaseResults(ctx context.Context, submissionID int32) (string, error) {
	row := q.db.QueryRow(ctx, aggregateTestcaseResults, submissionID)
	var status string
	err := row.Scan(&status)
	return status, err
}

const createSubmission = `-- name: CreateSubmission :one
INSERT INTO submissions (game_id, user_id, code, code_size, code_hash)
VALUES ($1, $2, $3, $4, $5)
RETURNING submission_id
`

type CreateSubmissionParams struct {
	GameID   int32
	UserID   int32
	Code     string
	CodeSize int32
	CodeHash string
}

func (q *Queries) CreateSubmission(ctx context.Context, arg CreateSubmissionParams) (int32, error) {
	row := q.db.QueryRow(ctx, createSubmission,
		arg.GameID,
		arg.UserID,
		arg.Code,
		arg.CodeSize,
		arg.CodeHash,
	)
	var submission_id int32
	err := row.Scan(&submission_id)
	return submission_id, err
}

const createSubmissionResult = `-- name: CreateSubmissionResult :exec
INSERT INTO submission_results (submission_id, status, stdout, stderr)
VALUES ($1, $2, $3, $4)
`

type CreateSubmissionResultParams struct {
	SubmissionID int32
	Status       string
	Stdout       string
	Stderr       string
}

func (q *Queries) CreateSubmissionResult(ctx context.Context, arg CreateSubmissionResultParams) error {
	_, err := q.db.Exec(ctx, createSubmissionResult,
		arg.SubmissionID,
		arg.Status,
		arg.Stdout,
		arg.Stderr,
	)
	return err
}

const createTestcaseResult = `-- name: CreateTestcaseResult :exec
INSERT INTO testcase_results (submission_id, testcase_id, status, stdout, stderr)
VALUES ($1, $2, $3, $4, $5)
`

type CreateTestcaseResultParams struct {
	SubmissionID int32
	TestcaseID   int32
	Status       string
	Stdout       string
	Stderr       string
}

func (q *Queries) CreateTestcaseResult(ctx context.Context, arg CreateTestcaseResultParams) error {
	_, err := q.db.Exec(ctx, createTestcaseResult,
		arg.SubmissionID,
		arg.TestcaseID,
		arg.Status,
		arg.Stdout,
		arg.Stderr,
	)
	return err
}

const createUser = `-- name: CreateUser :one
INSERT INTO users (username, display_name, is_admin)
VALUES ($1, $1, false)
RETURNING user_id
`

func (q *Queries) CreateUser(ctx context.Context, username string) (int32, error) {
	row := q.db.QueryRow(ctx, createUser, username)
	var user_id int32
	err := row.Scan(&user_id)
	return user_id, err
}

const createUserAuth = `-- name: CreateUserAuth :exec
INSERT INTO user_auths (user_id, auth_type)
VALUES ($1, $2)
`

type CreateUserAuthParams struct {
	UserID   int32
	AuthType string
}

func (q *Queries) CreateUserAuth(ctx context.Context, arg CreateUserAuthParams) error {
	_, err := q.db.Exec(ctx, createUserAuth, arg.UserID, arg.AuthType)
	return err
}

const getGameByID = `-- name: GetGameByID :one
SELECT game_id, game_type, state, display_name, duration_seconds, created_at, started_at, games.problem_id, problems.problem_id, title, description FROM games
JOIN problems ON games.problem_id = problems.problem_id
WHERE games.game_id = $1
LIMIT 1
`

type GetGameByIDRow struct {
	GameID          int32
	GameType        string
	State           string
	DisplayName     string
	DurationSeconds int32
	CreatedAt       pgtype.Timestamp
	StartedAt       pgtype.Timestamp
	ProblemID       int32
	ProblemID_2     int32
	Title           string
	Description     string
}

func (q *Queries) GetGameByID(ctx context.Context, gameID int32) (GetGameByIDRow, error) {
	row := q.db.QueryRow(ctx, getGameByID, gameID)
	var i GetGameByIDRow
	err := row.Scan(
		&i.GameID,
		&i.GameType,
		&i.State,
		&i.DisplayName,
		&i.DurationSeconds,
		&i.CreatedAt,
		&i.StartedAt,
		&i.ProblemID,
		&i.ProblemID_2,
		&i.Title,
		&i.Description,
	)
	return i, err
}

const getSubmissionCodeSizeByID = `-- name: GetSubmissionCodeSizeByID :one
SELECT code_size FROM submissions
WHERE submission_id = $1
LIMIT 1
`

func (q *Queries) GetSubmissionCodeSizeByID(ctx context.Context, submissionID int32) (int32, error) {
	row := q.db.QueryRow(ctx, getSubmissionCodeSizeByID, submissionID)
	var code_size int32
	err := row.Scan(&code_size)
	return code_size, err
}

const getUserAuthByUsername = `-- name: GetUserAuthByUsername :one
SELECT users.user_id, username, display_name, icon_path, is_admin, created_at, user_auth_id, user_auths.user_id, auth_type, password_hash FROM users
JOIN user_auths ON users.user_id = user_auths.user_id
WHERE users.username = $1
LIMIT 1
`

type GetUserAuthByUsernameRow struct {
	UserID       int32
	Username     string
	DisplayName  string
	IconPath     *string
	IsAdmin      bool
	CreatedAt    pgtype.Timestamp
	UserAuthID   int32
	UserID_2     int32
	AuthType     string
	PasswordHash *string
}

func (q *Queries) GetUserAuthByUsername(ctx context.Context, username string) (GetUserAuthByUsernameRow, error) {
	row := q.db.QueryRow(ctx, getUserAuthByUsername, username)
	var i GetUserAuthByUsernameRow
	err := row.Scan(
		&i.UserID,
		&i.Username,
		&i.DisplayName,
		&i.IconPath,
		&i.IsAdmin,
		&i.CreatedAt,
		&i.UserAuthID,
		&i.UserID_2,
		&i.AuthType,
		&i.PasswordHash,
	)
	return i, err
}

const getUserByID = `-- name: GetUserByID :one
SELECT user_id, username, display_name, icon_path, is_admin, created_at FROM users
WHERE users.user_id = $1
LIMIT 1
`

func (q *Queries) GetUserByID(ctx context.Context, userID int32) (User, error) {
	row := q.db.QueryRow(ctx, getUserByID, userID)
	var i User
	err := row.Scan(
		&i.UserID,
		&i.Username,
		&i.DisplayName,
		&i.IconPath,
		&i.IsAdmin,
		&i.CreatedAt,
	)
	return i, err
}

const getUserIDByUsername = `-- name: GetUserIDByUsername :one
SELECT user_id FROM users
WHERE users.username = $1
LIMIT 1
`

func (q *Queries) GetUserIDByUsername(ctx context.Context, username string) (int32, error) {
	row := q.db.QueryRow(ctx, getUserIDByUsername, username)
	var user_id int32
	err := row.Scan(&user_id)
	return user_id, err
}

const listGamePlayers = `-- name: ListGamePlayers :many
SELECT game_id, game_players.user_id, users.user_id, username, display_name, icon_path, is_admin, created_at FROM game_players
JOIN users ON game_players.user_id = users.user_id
WHERE game_players.game_id = $1
ORDER BY game_players.user_id
`

type ListGamePlayersRow struct {
	GameID      int32
	UserID      int32
	UserID_2    int32
	Username    string
	DisplayName string
	IconPath    *string
	IsAdmin     bool
	CreatedAt   pgtype.Timestamp
}

func (q *Queries) ListGamePlayers(ctx context.Context, gameID int32) ([]ListGamePlayersRow, error) {
	rows, err := q.db.Query(ctx, listGamePlayers, gameID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []ListGamePlayersRow
	for rows.Next() {
		var i ListGamePlayersRow
		if err := rows.Scan(
			&i.GameID,
			&i.UserID,
			&i.UserID_2,
			&i.Username,
			&i.DisplayName,
			&i.IconPath,
			&i.IsAdmin,
			&i.CreatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listGames = `-- name: ListGames :many
SELECT game_id, game_type, state, display_name, duration_seconds, created_at, started_at, games.problem_id, problems.problem_id, title, description FROM games
JOIN problems ON games.problem_id = problems.problem_id
ORDER BY games.game_id
`

type ListGamesRow struct {
	GameID          int32
	GameType        string
	State           string
	DisplayName     string
	DurationSeconds int32
	CreatedAt       pgtype.Timestamp
	StartedAt       pgtype.Timestamp
	ProblemID       int32
	ProblemID_2     int32
	Title           string
	Description     string
}

func (q *Queries) ListGames(ctx context.Context) ([]ListGamesRow, error) {
	rows, err := q.db.Query(ctx, listGames)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []ListGamesRow
	for rows.Next() {
		var i ListGamesRow
		if err := rows.Scan(
			&i.GameID,
			&i.GameType,
			&i.State,
			&i.DisplayName,
			&i.DurationSeconds,
			&i.CreatedAt,
			&i.StartedAt,
			&i.ProblemID,
			&i.ProblemID_2,
			&i.Title,
			&i.Description,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listGamesForPlayer = `-- name: ListGamesForPlayer :many
SELECT games.game_id, game_type, state, display_name, duration_seconds, created_at, started_at, games.problem_id, problems.problem_id, title, description, game_players.game_id, user_id FROM games
JOIN problems ON games.problem_id = problems.problem_id
JOIN game_players ON games.game_id = game_players.game_id
WHERE game_players.user_id = $1
ORDER BY games.game_id
`

type ListGamesForPlayerRow struct {
	GameID          int32
	GameType        string
	State           string
	DisplayName     string
	DurationSeconds int32
	CreatedAt       pgtype.Timestamp
	StartedAt       pgtype.Timestamp
	ProblemID       int32
	ProblemID_2     int32
	Title           string
	Description     string
	GameID_2        int32
	UserID          int32
}

func (q *Queries) ListGamesForPlayer(ctx context.Context, userID int32) ([]ListGamesForPlayerRow, error) {
	rows, err := q.db.Query(ctx, listGamesForPlayer, userID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []ListGamesForPlayerRow
	for rows.Next() {
		var i ListGamesForPlayerRow
		if err := rows.Scan(
			&i.GameID,
			&i.GameType,
			&i.State,
			&i.DisplayName,
			&i.DurationSeconds,
			&i.CreatedAt,
			&i.StartedAt,
			&i.ProblemID,
			&i.ProblemID_2,
			&i.Title,
			&i.Description,
			&i.GameID_2,
			&i.UserID,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listTestcaseIDsByGameID = `-- name: ListTestcaseIDsByGameID :many
SELECT testcases.testcase_id FROM testcases
WHERE testcases.problem_id = (SELECT problem_id FROM games WHERE game_id = $1)
ORDER BY testcases.testcase_id
`

func (q *Queries) ListTestcaseIDsByGameID(ctx context.Context, gameID int32) ([]int32, error) {
	rows, err := q.db.Query(ctx, listTestcaseIDsByGameID, gameID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []int32
	for rows.Next() {
		var testcase_id int32
		if err := rows.Scan(&testcase_id); err != nil {
			return nil, err
		}
		items = append(items, testcase_id)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listTestcasesByGameID = `-- name: ListTestcasesByGameID :many
SELECT testcase_id, problem_id, stdin, stdout FROM testcases
WHERE testcases.problem_id = (SELECT problem_id FROM games WHERE game_id = $1)
ORDER BY testcases.testcase_id
`

func (q *Queries) ListTestcasesByGameID(ctx context.Context, gameID int32) ([]Testcase, error) {
	rows, err := q.db.Query(ctx, listTestcasesByGameID, gameID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Testcase
	for rows.Next() {
		var i Testcase
		if err := rows.Scan(
			&i.TestcaseID,
			&i.ProblemID,
			&i.Stdin,
			&i.Stdout,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listUsers = `-- name: ListUsers :many
SELECT user_id, username, display_name, icon_path, is_admin, created_at FROM users
ORDER BY users.user_id
`

func (q *Queries) ListUsers(ctx context.Context) ([]User, error) {
	rows, err := q.db.Query(ctx, listUsers)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []User
	for rows.Next() {
		var i User
		if err := rows.Scan(
			&i.UserID,
			&i.Username,
			&i.DisplayName,
			&i.IconPath,
			&i.IsAdmin,
			&i.CreatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const updateGame = `-- name: UpdateGame :exec
UPDATE games
SET
    game_type = $2,
    state = $3,
    display_name = $4,
    duration_seconds = $5,
    started_at = $6,
    problem_id = $7
WHERE game_id = $1
`

type UpdateGameParams struct {
	GameID          int32
	GameType        string
	State           string
	DisplayName     string
	DurationSeconds int32
	StartedAt       pgtype.Timestamp
	ProblemID       int32
}

func (q *Queries) UpdateGame(ctx context.Context, arg UpdateGameParams) error {
	_, err := q.db.Exec(ctx, updateGame,
		arg.GameID,
		arg.GameType,
		arg.State,
		arg.DisplayName,
		arg.DurationSeconds,
		arg.StartedAt,
		arg.ProblemID,
	)
	return err
}

const updateGameStartedAt = `-- name: UpdateGameStartedAt :exec
UPDATE games
SET started_at = $2
WHERE game_id = $1
`

type UpdateGameStartedAtParams struct {
	GameID    int32
	StartedAt pgtype.Timestamp
}

func (q *Queries) UpdateGameStartedAt(ctx context.Context, arg UpdateGameStartedAtParams) error {
	_, err := q.db.Exec(ctx, updateGameStartedAt, arg.GameID, arg.StartedAt)
	return err
}

const updateGameState = `-- name: UpdateGameState :exec
UPDATE games
SET state = $2
WHERE game_id = $1
`

type UpdateGameStateParams struct {
	GameID int32
	State  string
}

func (q *Queries) UpdateGameState(ctx context.Context, arg UpdateGameStateParams) error {
	_, err := q.db.Exec(ctx, updateGameState, arg.GameID, arg.State)
	return err
}

const updateUserIconPath = `-- name: UpdateUserIconPath :exec
UPDATE users
SET icon_path = $2
WHERE user_id = $1
`

type UpdateUserIconPathParams struct {
	UserID   int32
	IconPath *string
}

func (q *Queries) UpdateUserIconPath(ctx context.Context, arg UpdateUserIconPathParams) error {
	_, err := q.db.Exec(ctx, updateUserIconPath, arg.UserID, arg.IconPath)
	return err
}