Skip to content

Commit 0fa5903

Browse files
authored
Merge pull request #3 from MichailKon/dev/db
readme for database + one-to-one relationships
2 parents 31b97cd + c39c897 commit 0fa5903

File tree

6 files changed

+79
-20
lines changed

6 files changed

+79
-20
lines changed

db/db.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ func NewDb(config Config) (*gorm.DB, error) {
1313
slog.Error("Can't open database", "dsn", config.Dsn, "err", err)
1414
return nil, err
1515
}
16+
if err = db.AutoMigrate(&models.TestingResult{}); err != nil {
17+
slog.Error("Can't migrate TestingResult", "err", err)
18+
return nil, err
19+
}
1620
if err = db.AutoMigrate(&models.Problem{}); err != nil {
1721
slog.Error("Can't migrate Problem", "err", err)
1822
return nil, err

db/models/problem.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@ package models
22

33
import "gorm.io/gorm"
44

5+
type ProblemType int
6+
7+
const (
8+
ProblemType_ICPC ProblemType = iota + 1
9+
ProblemType_IOI
10+
)
11+
512
type Problem struct {
613
gorm.Model
7-
ProblemConfig
14+
ProblemType ProblemType
815
}

db/models/problem_config.go

Lines changed: 0 additions & 13 deletions
This file was deleted.

db/models/submission.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import "gorm.io/gorm"
44

55
type Submission struct {
66
gorm.Model
7-
TestingResult
8-
ContentId uint64 // id of submission in the storage
9-
TaskId uint64
10-
TaskVersionId uint64
7+
ProblemID uint64
8+
ProblemVersionID uint64
9+
TestingResultID int
10+
TestingResult TestingResult
1111
}

db/models/testing_result.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package models
22

3-
import "github.com/lib/pq"
3+
import (
4+
"github.com/lib/pq"
5+
"gorm.io/gorm"
6+
)
47

58
type Verdict int
69

710
const (
8-
Verdict_OK int = iota
11+
Verdict_OK Verdict = iota + 1
912
Verdict_WA
1013
Verdict_TL
1114
Verdict_ML
@@ -14,5 +17,6 @@ const (
1417
)
1518

1619
type TestingResult struct {
20+
gorm.Model
1721
Verdicts pq.Int64Array `gorm:"type:int[]"`
1822
}

db/readme.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Как пользоваться базой
2+
3+
### Подключаемся
4+
5+
```go
6+
conn, err := db.NewDb(db.Config{Dsn: "DSN_STRING"})
7+
if err != nil {...}
8+
```
9+
10+
### Создание объекта
11+
12+
```go
13+
config := &models.ProblemConfig{ProblemType: models.ProblemType_ICPC}
14+
result := conn.Create(&config)
15+
config.ID // номер нового объекта
16+
result.Error // если была ошибка
17+
result.RowsAffected // сколько строк вставилосб
18+
```
19+
20+
https://gorm.io/docs/create.html
21+
22+
### Получение объекта (обычно одного)
23+
24+
```go
25+
var problem Problem
26+
var problems []Problem
27+
err := conn.First(&problem)
28+
// теперь либо err.Error != nil, либо в problem лежит запись с минимальным id
29+
conn.Take(&problem) // рандомная запись
30+
conn.Last(&problem) // последняя запись
31+
errors.Is(result.Error, gorm.ErrRecordNotFound) // пример ошибки
32+
33+
result := map[string]interface{}{}
34+
conn.Model(&models.Problem{}).First(&result) // после этого можно обращаться как к мапе
35+
36+
conn.Find(&problem, 1) // выбрать с id == 1
37+
conn.Find(&problem, "id = ?", 1)
38+
conn.Find(&problems, []int{1, 2, 3}) // выбрать с id \in {1, 2, 3}
39+
conn.Find(&problems) // все объекты
40+
```
41+
42+
https://gorm.io/docs/query.html
43+
44+
### Условия
45+
46+
```go
47+
// все IOI задачи
48+
conn.Where(models.Problem{ProblemType: models.ProblemType_IOI}).Find(&problems)
49+
conn.Find(&problems, "problem_type = ?", models.ProblemType_IOI)
50+
conn.Find(&problems, map[string]any{"problem_type": models.ProblemType_IOI})
51+
conn.Not("problem_type = ?", models.ProblemType_ICPC).Find(&problems)
52+
// все IOI задачи или с id == 1
53+
conn.
54+
Where("problem_type = ?", models.ProblemType_IOI).
55+
Or(1).
56+
Find(&problems)
57+
```

0 commit comments

Comments
 (0)