Skip to content

Commit

Permalink
fix: polymorphic relationships use wrong id's
Browse files Browse the repository at this point in the history
  • Loading branch information
dosco committed Mar 9, 2021
1 parent d2f8f20 commit 30a8834
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 28 deletions.
2 changes: 1 addition & 1 deletion core/internal/psql/columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (c *compilerContext) renderUnionColumn(sel, csel *qcode.Select) {
usel := &c.qc.Selects[cid]

c.w.WriteString(`WHEN `)
colWithTableID(c.w, sel.Table, sel.ID, csel.Rel.Right.Col.Name)
colWithTableID(c.w, sel.Table, sel.ID, csel.Rel.Left.Col.FKeyCol)
c.w.WriteString(` = `)
c.squoted(usel.Table)
c.w.WriteString(` THEN `)
Expand Down
4 changes: 2 additions & 2 deletions core/internal/psql/rels.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ func (c *compilerContext) renderRel(
colWithTableID(c.w, rel.Right.Col.Table, pid, rel.Right.Col.Name)

case sdata.RelPolymorphic:
colWithTable(c.w, ti.Name, rel.Left.Col.Name)
colWithTable(c.w, ti.Name, rel.Right.Col.Name)
c.w.WriteString(`) = (`)
colWithTableID(c.w, rel.Left.Col.Table, pid, rel.Left.Col.Name)
c.w.WriteString(`) AND (`)
colWithTableID(c.w, rel.Left.Col.Table, pid, rel.Right.Col.Name)
colWithTableID(c.w, rel.Left.Col.Table, pid, rel.Left.Col.FKeyCol)
c.w.WriteString(`) = (`)
c.squoted(ti.Name)

Expand Down
5 changes: 4 additions & 1 deletion core/internal/qcode/columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,11 @@ func (co *Compiler) addRelColumns(qc *QCode, sel *Select, rel sdata.DBRel) error
sel.SkipRender = SkipTypeRemote

case sdata.RelPolymorphic:
typeCol := rel.Left.Col
typeCol.Name = rel.Left.Col.FKeyCol

psel.addCol(Column{Col: rel.Left.Col}, true)
psel.addCol(Column{Col: rel.Right.Col}, true)
psel.addCol(Column{Col: typeCol}, true)

case sdata.RelRecursive:
sel.addCol(Column{Col: rel.Left.Col}, true)
Expand Down
2 changes: 1 addition & 1 deletion core/internal/qcode/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func (co *Compiler) AddRole(role, schema, table string, trc TRConfig) error {
func (co *Compiler) getRole(role, schema, table, field string) trval {
var k string

if co.s.IsAlias(schema, field) {
if co.s.IsAlias(field) {
k = (role + ":" + schema + ":" + field)
} else {
k = (role + ":" + schema + ":" + table)
Expand Down
9 changes: 4 additions & 5 deletions core/internal/sdata/dwg.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ func (s *DBSchema) addNode(t DBTable) int32 {

func (s *DBSchema) addAliases(t DBTable, nodeID int32, aliases []string) {
for _, al := range aliases {
k := (t.Schema + ":" + al)
s.tindex[k] = nodeInfo{nodeID}
s.ai[k] = nodeInfo{nodeID}
s.tindex[(t.Schema + ":" + al)] = nodeInfo{nodeID}
s.ai[al] = nodeInfo{nodeID}
}
}

Expand All @@ -48,8 +47,8 @@ func (s *DBSchema) GetAliases() map[string]DBTable {
return ts
}

func (s *DBSchema) IsAlias(schema, name string) bool {
_, ok := s.ai[(schema + ":" + name)]
func (s *DBSchema) IsAlias(name string) bool {
_, ok := s.ai[name]
return ok
}

Expand Down
30 changes: 24 additions & 6 deletions core/internal/sdata/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,12 @@ func (s *DBSchema) addPolymorphicRel(t DBTable) error {
return err
}

pc, err := pt.GetColumn(t.PrimaryCol.FKeyCol)
// pc, err := pt.GetColumn(t.PrimaryCol.FKeyCol)
// if err != nil {
// return err
// }

pc, err := pt.GetColumn(t.SecondaryCol.Name)
if err != nil {
return err
}
Expand Down Expand Up @@ -245,18 +250,31 @@ func (s *DBSchema) addVirtual(vt VirtualTable) error {
ID: -1,
Schema: t.Schema,
Table: t.Name,
Name: vt.FKeyColumn,
Name: idCol.Name,
Type: idCol.Type,
FKeySchema: typeCol.Schema,
FKeyTable: typeCol.Table,
FKeyCol: typeCol.Name,
}

fIDCol, ok := t.getColumn(vt.FKeyColumn)
if !ok {
continue
}

col2 := DBColumn{
ID: -1,
Schema: t.Schema,
Table: t.Name,
Name: fIDCol.Name,
}

pt := DBTable{
Name: vt.Name,
Schema: t.Schema,
Type: "virtual",
PrimaryCol: col1,
Name: vt.Name,
Schema: t.Schema,
Type: "virtual",
PrimaryCol: col1,
SecondaryCol: col2,
}
s.addNode(pt)
}
Expand Down
2 changes: 1 addition & 1 deletion core/internal/sdata/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ func (ti *DBTable) String() string {
func (col DBColumn) String() string {
if col.FKeyCol != "" {
return fmt.Sprintf("%s.%s.%s -FK-> %s.%s.%s",
col.Schema, col.Table, col.Name, col.FKeySchema, col.FKeyTable, col.Name)
col.Schema, col.Table, col.Name, col.FKeySchema, col.FKeyTable, col.FKeyCol)
} else {
return fmt.Sprintf("%s.%s.%s", col.Schema, col.Table, col.Name)
}
Expand Down
15 changes: 4 additions & 11 deletions core/prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ func (gj *GraphJin) prepareRoleStmt() error {
io.WriteString(w, `) THEN `)

io.WriteString(w, `(SELECT (CASE`)
for _, role := range gj.conf.Roles {
for roleName, role := range gj.roles {
if role.Match == "" {
continue
}
io.WriteString(w, ` WHEN `)
io.WriteString(w, role.Match)
io.WriteString(w, ` THEN '`)
io.WriteString(w, role.Name)
io.WriteString(w, roleName)
io.WriteString(w, `'`)
}

Expand Down Expand Up @@ -100,15 +100,8 @@ func (gj *GraphJin) initAllowList() error {
vars: []byte(v.Vars),
}

switch q.op {
case qcode.QTQuery, qcode.QTSubscription:
gj.queries[(v.Name + "user")] = &cquery{q: q}
gj.queries[(v.Name + "anon")] = &cquery{q: q}

case qcode.QTMutation:
for _, role := range gj.conf.Roles {
gj.queries[(v.Name + role.Name)] = &cquery{q: q}
}
for roleName := range gj.roles {
gj.queries[(v.Name + roleName)] = &cquery{q: q}
}
}

Expand Down

0 comments on commit 30a8834

Please sign in to comment.