/
relation.go
51 lines (39 loc) · 1.04 KB
/
relation.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
package model
import (
"strings"
"github.com/ant31/bungen/util"
)
// Relation stores relation
type Relation struct {
PKFields []string
FKFields []string
GoName string
TargetPGName string
TargetPGSchema string
TargetPGFullName string
TargetEntity *Entity
GoType string
}
// NewRelation creates relation from Postgres info
func NewRelation(sourceColumns []string, targetSchema, targetTable string, targetColumns []string) Relation {
names := make([]string, len(sourceColumns))
for i, name := range sourceColumns {
names[i] = util.ReplaceSuffix(util.ColumnName(name), util.ID, "")
}
typ := util.EntityName(targetTable)
if targetSchema != util.PublicSchema {
typ = util.CamelCased(targetSchema) + typ
}
return Relation{
PKFields: targetColumns,
FKFields: sourceColumns,
GoName: strings.Join(names, ""),
TargetPGName: targetTable,
TargetPGSchema: targetSchema,
TargetPGFullName: util.JoinF(targetSchema, targetTable),
GoType: typ,
}
}
func (r *Relation) AddEntity(entity *Entity) {
r.TargetEntity = entity
}