From 30a8834d94438dfb5373220b0b2d09644482c235 Mon Sep 17 00:00:00 2001 From: Vikram Rangnekar Date: Tue, 9 Mar 2021 10:21:25 -0500 Subject: [PATCH] fix: polymorphic relationships use wrong id's --- core/internal/psql/columns.go | 2 +- core/internal/psql/rels.go | 4 ++-- core/internal/qcode/columns.go | 5 ++++- core/internal/qcode/config.go | 2 +- core/internal/sdata/dwg.go | 9 ++++----- core/internal/sdata/schema.go | 30 ++++++++++++++++++++++++------ core/internal/sdata/strings.go | 2 +- core/prepare.go | 15 ++++----------- 8 files changed, 41 insertions(+), 28 deletions(-) diff --git a/core/internal/psql/columns.go b/core/internal/psql/columns.go index e38d77aa..35ed76b2 100644 --- a/core/internal/psql/columns.go +++ b/core/internal/psql/columns.go @@ -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 `) diff --git a/core/internal/psql/rels.go b/core/internal/psql/rels.go index b877b491..ab553f52 100644 --- a/core/internal/psql/rels.go +++ b/core/internal/psql/rels.go @@ -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) diff --git a/core/internal/qcode/columns.go b/core/internal/qcode/columns.go index 32bab9cf..a79a7e0c 100644 --- a/core/internal/qcode/columns.go +++ b/core/internal/qcode/columns.go @@ -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) diff --git a/core/internal/qcode/config.go b/core/internal/qcode/config.go index 54b1cfd1..aed12c81 100644 --- a/core/internal/qcode/config.go +++ b/core/internal/qcode/config.go @@ -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) diff --git a/core/internal/sdata/dwg.go b/core/internal/sdata/dwg.go index 0ad737af..ffea8a0a 100644 --- a/core/internal/sdata/dwg.go +++ b/core/internal/sdata/dwg.go @@ -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} } } @@ -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 } diff --git a/core/internal/sdata/schema.go b/core/internal/sdata/schema.go index 3b9a508c..e92b25cc 100644 --- a/core/internal/sdata/schema.go +++ b/core/internal/sdata/schema.go @@ -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 } @@ -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) } diff --git a/core/internal/sdata/strings.go b/core/internal/sdata/strings.go index 1732197f..06797627 100644 --- a/core/internal/sdata/strings.go +++ b/core/internal/sdata/strings.go @@ -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) } diff --git a/core/prepare.go b/core/prepare.go index 9ccb5469..c1acb849 100644 --- a/core/prepare.go +++ b/core/prepare.go @@ -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, `'`) } @@ -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} } }