/
entity.go
288 lines (254 loc) · 7.63 KB
/
entity.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
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
package imdb
import (
"github.com/BurntSushi/csql"
)
// Atom corresponds to a unique identifier for an entity. If any two entities
// have different atoms, then they are considered logically distinct.
type Atom int32
func (a Atom) String() string {
return sf("%d", a)
}
// EntityKind represents all possible types of entities supported by this
// package.
type EntityKind int
// All possible entities.
const (
EntityMovie EntityKind = iota
EntityTvshow
EntityEpisode
EntityActor
)
// Entities is a map from a string representation of an entity type to a Goim
// entity type.
var Entities = map[string]EntityKind{
"movie": EntityMovie,
"tvshow": EntityTvshow,
"episode": EntityEpisode,
"actor": EntityActor,
}
func entityKindFromString(e string) EntityKind {
ent, ok := Entities[e]
if !ok {
panic(sf("BUG: unrecognized entity %s", e))
}
return ent
}
func (e EntityKind) String() string {
switch e {
case EntityMovie:
return "movie"
case EntityTvshow:
return "tvshow"
case EntityEpisode:
return "episode"
case EntityActor:
return "actor"
}
panic(sf("unrecognized entity %d", e))
}
// Entity is an interface that all types claiming to be an entity must satisfy.
type Entity interface {
// Returns a unique atom identifier for this entity.
Ident() Atom
// The type of this entity.
Type() EntityKind
// A name representing this entity. It need not be unique among all
// entities.
Name() string
// Returns the year associated with this entity. If no such year exists
// or is not relevant, it may be 0.
EntityYear() int
// Attrs uses double dispatch to load all attribute values for the given
// Attributer for this entity.
Attrs(csql.Queryer, Attributer) error
// Scan loads an entity from a row in the database.
Scan(rs csql.RowScanner) error
}
// FromAtom returns an entity given its type and its unique identifier.
func FromAtom(db csql.Queryer, ent EntityKind, id Atom) (Entity, error) {
switch ent {
case EntityMovie:
return atomToMovie(db, id)
case EntityTvshow:
return atomToTvshow(db, id)
case EntityEpisode:
return atomToEpisode(db, id)
case EntityActor:
return atomToActor(db, id)
}
return nil, ef("Unrecognized entity type: %s", ent)
}
// fromAtomGuess is just like FromAtom, except it doesn't use an entity type
// as a hint for which table to select from. Therefore, it tries all entity
// types until it gets a hit. If no entities could be found matching the
// identifier given, an error is returned.
func fromAtomGuess(db csql.Queryer, id Atom) (e Entity, err error) {
e, err = atomToMovie(db, id)
if err == nil {
return e, nil
}
e, err = atomToTvshow(db, id)
if err == nil {
return e, nil
}
e, err = atomToEpisode(db, id)
if err == nil {
return e, nil
}
e, err = atomToActor(db, id)
if err == nil {
return e, nil
}
return nil, ef("Could not find any entity corresponding to atom %d", id)
}
// Movie represents a single movie in IMDb. This includes "made for tv" and
// "made for video" movies.
type Movie struct {
Id Atom
Title string
Year int // Year released.
Sequence string // Non-data. Used by IMDb for unique entity strings.
Tv bool
Video bool
}
// Tvshow represents a single TV show in IMDb. Typically TV shows lack
// attribute data in lieu of individual episodes containing the data, and are
// instead a way of connecting episodes together.
type Tvshow struct {
Id Atom
Title string
Year int // Year started.
Sequence string // Non-data. Used by IMDb for unique entity strings.
YearStart int
YearEnd int // Year ended or 0 if still on air.
}
// Episode represents a single episode for a single TV show in IMDb.
type Episode struct {
Id Atom
TvshowId Atom
Title string
Year int
Season, EpisodeNum int // May be 0!
}
// Actor represents a single cast member that has appeared in the credits of
// at least one movie, TV show or episode in IMDb.
type Actor struct {
Id Atom
FullName string
Sequence string // Non-data. Used by IMDb for unique entity strings.
}
func entityString(title string, year int) string {
var s string
if len(title) > 0 {
s = title
} else {
s = "N/A"
}
if year > 0 {
s += sf(" (%d)", year)
}
return s
}
func (e *Movie) Ident() Atom { return e.Id }
func (e *Movie) Type() EntityKind { return EntityMovie }
func (e *Movie) Name() string { return e.Title }
func (e *Movie) EntityYear() int { return e.Year }
func (e *Movie) String() string { return entityString(e.Title, e.Year) }
func (e *Movie) Attrs(db csql.Queryer, attrs Attributer) error {
return attrs.ForEntity(db, e)
}
func (e *Tvshow) Ident() Atom { return e.Id }
func (e *Tvshow) Type() EntityKind { return EntityTvshow }
func (e *Tvshow) Name() string { return e.Title }
func (e *Tvshow) EntityYear() int { return e.Year }
func (e *Tvshow) String() string { return entityString(e.Title, e.Year) }
func (e *Tvshow) Attrs(db csql.Queryer, attrs Attributer) error {
return attrs.ForEntity(db, e)
}
func (e *Episode) Ident() Atom { return e.Id }
func (e *Episode) Type() EntityKind { return EntityEpisode }
func (e *Episode) Name() string { return e.Title }
func (e *Episode) EntityYear() int { return e.Year }
func (e *Episode) String() string { return entityString(e.Title, e.Year) }
func (e *Episode) Attrs(db csql.Queryer, attrs Attributer) error {
return attrs.ForEntity(db, e)
}
func (e *Actor) Ident() Atom { return e.Id }
func (e *Actor) Type() EntityKind { return EntityActor }
func (e *Actor) Name() string { return e.FullName }
func (e *Actor) EntityYear() int { return 0 }
func (e *Actor) String() string { return e.FullName }
func (e *Actor) Attrs(db csql.Queryer, attrs Attributer) error {
return attrs.ForEntity(db, e)
}
func (e *Movie) Scan(rs csql.RowScanner) error {
if e == nil {
e = new(Movie)
}
return rs.Scan(&e.Id, &e.Title, &e.Year, &e.Sequence, &e.Tv, &e.Video)
}
func (e *Tvshow) Scan(rs csql.RowScanner) error {
if e == nil {
e = new(Tvshow)
}
return rs.Scan(&e.Id, &e.Title, &e.Year, &e.Sequence,
&e.YearStart, &e.YearEnd)
}
func (e *Episode) Scan(rs csql.RowScanner) error {
if e == nil {
e = new(Episode)
}
return rs.Scan(&e.Id, &e.TvshowId, &e.Title,
&e.Year, &e.Season, &e.EpisodeNum)
}
func (e *Actor) Scan(rs csql.RowScanner) error {
if e == nil {
e = new(Actor)
}
return rs.Scan(&e.Id, &e.FullName, &e.Sequence)
}
func atomToMovie(db csql.Queryer, id Atom) (*Movie, error) {
e := new(Movie)
err := e.Scan(db.QueryRow(`
SELECT m.atom_id, n.name, m.year, m.sequence, m.tv, m.video
FROM movie AS m
LEFT JOIN name AS n ON n.atom_id = m.atom_id
WHERE m.atom_id = $1
`, id))
return e, err
}
func atomToTvshow(db csql.Queryer, id Atom) (*Tvshow, error) {
e := new(Tvshow)
err := e.Scan(db.QueryRow(`
SELECT t.atom_id, n.name, t.year, t.sequence, t.year_start, t.year_end
FROM tvshow AS t
LEFT JOIN name AS n ON n.atom_id = t.atom_id
WHERE t.atom_id = $1
`, id))
return e, err
}
func atomToEpisode(db csql.Queryer, id Atom) (*Episode, error) {
e := new(Episode)
err := e.Scan(db.QueryRow(`
SELECT e.atom_id, e.tvshow_atom_id, n.name,
e.year, e.season, e.episode_num
FROM episode AS e
LEFT JOIN name AS n ON n.atom_id = e.atom_id
WHERE e.atom_id = $1
`, id))
return e, err
}
func atomToActor(db csql.Queryer, id Atom) (*Actor, error) {
e := new(Actor)
err := e.Scan(db.QueryRow(`
SELECT a.atom_id, n.name, a.sequence
FROM actor AS a
LEFT JOIN name AS n ON n.atom_id = a.atom_id
WHERE a.atom_id = $1
`, id))
return e, err
}
// Tvshow returns a TV show entity that corresponds to this episode.
func (e *Episode) Tvshow(db csql.Queryer) (*Tvshow, error) {
return atomToTvshow(db, e.TvshowId)
}