-
Notifications
You must be signed in to change notification settings - Fork 0
/
speaker.go
63 lines (52 loc) · 1.09 KB
/
speaker.go
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
package treksum
import (
"fmt"
"github.com/jackc/pgx"
)
const (
CREATE_SPEAKER_TABLE = `
create table "speaker" (
"id" serial primary key,
"series_id" int,
"name" citext,
foreign key ("series_id") references "series" ("id"),
unique ("series_id", "name")
)
`
INSERT_SPEAKER = `
insert into "speaker"
("series_id", "name")
select $1, lower($2)
where not exists (
select "id"
from "speaker"
where "series_id" = $1
and "name" = lower($2)
)
`
SELECT_SPEAKER = `
SELECT "id"
FROM "speaker"
WHERE "series_id" = $1
AND "name" = $2
`
)
type Speaker struct {
ID int64 `json:"id"`
Series *Series `json:"series"`
Name string `json:"name"`
}
func NewSpeaker(series *Series, name string) (s *Speaker) {
s = &Speaker{
Series: series,
Name: CleanUnicode(name),
}
return s
}
func (this *Speaker) String() string {
return fmt.Sprintf("%s", this.Name)
}
func (this *Speaker) Save(tx *pgx.Tx) (err error) {
tx.Exec(INSERT_SPEAKER, this.Series.ID, this.Name)
return tx.QueryRow(SELECT_SPEAKER, this.Series.ID, this.Name).Scan(&this.ID)
}