Skip to content

Commit

Permalink
Fix: foreign key column not found: X.Y #312
Browse files Browse the repository at this point in the history
  • Loading branch information
dosco committed Oct 8, 2022
1 parent 01d54ca commit 603989a
Show file tree
Hide file tree
Showing 13 changed files with 42 additions and 43 deletions.
8 changes: 4 additions & 4 deletions core/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (gj *graphjin) initConfig() error {
tm := make(map[string]struct{})

for _, t := range c.Tables {
k := strings.ToLower(t.Schema + t.Name)
k := t.Schema + t.Name
if _, ok := tm[k]; ok {
return fmt.Errorf("duplicate table found: %s", t.Name)
}
Expand All @@ -65,8 +65,8 @@ func (gj *graphjin) initConfig() error {
gj.roles = make(map[string]*Role)

for i, role := range c.Roles {
k := strings.ToLower(role.Name)
if _, ok := gj.roles[k]; ok {
k := role.Name
if _, ok := gj.roles[(role.Name)]; ok {
return fmt.Errorf("duplicate role found: %s", role.Name)
}

Expand Down Expand Up @@ -450,7 +450,7 @@ func (c *Column) getFK(defaultSchema string) ([3]string, bool) {
}

func sanitize(value string) string {
return strings.ToLower(strings.TrimSpace(value))
return strings.TrimSpace(value)
}

func isASCII(s string) (int, bool) {
Expand Down
8 changes: 4 additions & 4 deletions core/internal/psql/columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ func (c *compilerContext) renderColumns(sel *qcode.Select) {
if i != 0 {
c.w.WriteString(", ")
}
colWithTableID(c.w, sel.Table, sel.ID, col.Col.Name)
c.colWithTableID(sel.Table, sel.ID, col.Col.Name)
c.alias(col.FieldName)
i++
}
for _, fn := range sel.Funcs {
if i != 0 {
c.w.WriteString(", ")
}
colWithTableID(c.w, sel.Table, sel.ID, fn.FieldName)
c.colWithTableID(sel.Table, sel.ID, fn.FieldName)
if fn.Alias != "" {
c.alias(fn.Alias)
} else {
Expand Down Expand Up @@ -92,12 +92,12 @@ 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.Left.Col.FKeyCol)
c.colWithTableID(sel.Table, sel.ID, csel.Rel.Left.Col.FKeyCol)
c.w.WriteString(` = `)
c.squoted(usel.Table)
c.w.WriteString(` THEN `)

if usel.SkipRender == qcode.SkipTypeUserNeeded ||
if usel.SkipRender == qcode.SkipTypeUserNeeded ||
usel.SkipRender == qcode.SkipTypeBlocked {
c.w.WriteString(`NULL `)
} else {
Expand Down
8 changes: 4 additions & 4 deletions core/internal/psql/exp.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (c *expContext) renderOp(ex *qcode.Exp) {
if ex.Left.ID == -1 {
c.colWithTable(table, ex.Left.Col.Name)
} else {
colWithTableID(c.w, table, ex.Left.ID, ex.Left.Col.Name)
c.colWithTableID(table, ex.Left.ID, ex.Left.Col.Name)
}
c.w.WriteString(`) `)
}
Expand Down Expand Up @@ -349,7 +349,7 @@ func (c *expContext) renderVal(ex *qcode.Exp) {
if pid == -1 {
c.colWithTable(table, ex.Right.Col.Name)
} else {
colWithTableID(c.w, table, pid, ex.Right.Col.Name)
c.colWithTableID(table, pid, ex.Right.Col.Name)
}
}
c.w.WriteString(`)`)
Expand Down Expand Up @@ -464,7 +464,7 @@ func (c *compilerContext) renderValArrayColumn(ex *qcode.Exp, table string, pid
if pid == -1 {
c.colWithTable(table, col.Name)
} else {
colWithTableID(c.w, table, pid, col.Name)
c.colWithTableID(table, pid, col.Name)
}
c.w.WriteString(` AS JSON) as ids) j, `)
c.w.WriteString(`JSON_TABLE(j.ids, "$[*]" COLUMNS(`)
Expand All @@ -477,7 +477,7 @@ func (c *compilerContext) renderValArrayColumn(ex *qcode.Exp, table string, pid
if pid == -1 {
c.colWithTable(table, col.Name)
} else {
colWithTableID(c.w, table, pid, col.Name)
c.colWithTableID(table, pid, col.Name)
}
}
}
12 changes: 6 additions & 6 deletions core/internal/psql/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ func (c *compilerContext) renderSelect(sel *qcode.Select) {
if sel.Paging.Cursor {
for i, ob := range sel.OrderBy {
c.w.WriteString(`, LAST_VALUE(`)
colWithTableID(c.w, sel.Table, sel.ID, ob.Col.Name)
c.colWithTableID(sel.Table, sel.ID, ob.Col.Name)
c.w.WriteString(`) OVER() AS __cur_`)
int32String(c.w, int32(i))
}
Expand All @@ -300,16 +300,16 @@ func (c *compilerContext) renderSelect(sel *qcode.Select) {
c.renderBaseSelect(sel)
}
c.w.WriteString(`)`)
aliasWithID(c.w, sel.Table, sel.ID)
c.aliasWithID(sel.Table, sel.ID)
}

func (c *compilerContext) renderSelectClose(sel *qcode.Select) {
c.w.WriteString(`)`)
aliasWithID(c.w, "__sr", sel.ID)
c.aliasWithID("__sr", sel.ID)

if !sel.Singular {
c.w.WriteString(`)`)
aliasWithID(c.w, "__sj", sel.ID)
c.aliasWithID("__sj", sel.ID)
}
}

Expand All @@ -319,7 +319,7 @@ func (c *compilerContext) renderLateralJoin() {

func (c *compilerContext) renderLateralJoinClose(sel *qcode.Select) {
c.w.WriteString(`)`)
aliasWithID(c.w, `__sj`, sel.ID)
c.aliasWithID(`__sj`, sel.ID)
c.w.WriteString(` ON true`)
}

Expand Down Expand Up @@ -420,7 +420,7 @@ func (c *compilerContext) renderRecursiveSelect(sel *qcode.Select) {
c.w.WriteString(` WHERE (`)
c.colWithTable(sel.Table, sel.Ti.PrimaryCol.Name)
c.w.WriteString(`) = (`)
colWithTableID(c.w, psel.Table, psel.ID, sel.Ti.PrimaryCol.Name)
c.colWithTableID(psel.Table, psel.ID, sel.Ti.PrimaryCol.Name)
c.w.WriteString(`) LIMIT 1) UNION ALL `)

c.w.WriteString(`SELECT `)
Expand Down
20 changes: 9 additions & 11 deletions core/internal/psql/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,19 @@ func (c *compilerContext) alias(alias string) {
c.quoted(alias)
}

func aliasWithID(w *bytes.Buffer, alias string, id int32) {
w.WriteString(` AS `)
w.WriteString(alias)
w.WriteString(`_`)
int32String(w, id)
func (c *compilerContext) aliasWithID(alias string, id int32) {
c.w.WriteString(` AS `)
c.quoted(alias + "_" + strconv.Itoa(int(id)))
}

func colWithTableID(w *bytes.Buffer, table string, id int32, col string) {
w.WriteString(table)
func (c *compilerContext) colWithTableID(table string, id int32, col string) {
if id >= 0 {
w.WriteString(`_`)
int32String(w, id)
c.quoted(table + "_" + strconv.Itoa(int(id)))
} else {
c.quoted(table)
}
w.WriteString(`.`)
w.WriteString(col)
c.w.WriteString(`.`)
c.quoted(col)
}

func (c *compilerContext) table(schema, table string, alias bool) {
Expand Down
4 changes: 1 addition & 3 deletions core/internal/qcode/config.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package qcode

import (
"strings"

"github.com/gobuffalo/flect"
)

Expand Down Expand Up @@ -300,7 +298,7 @@ func makeSet(list []string) map[string]struct{} {
m := make(map[string]struct{}, len(list))

for i := range list {
m[strings.ToLower(list[i])] = struct{}{}
m[list[i]] = struct{}{}
}
return m
}
2 changes: 2 additions & 0 deletions core/internal/qcode/exp.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ func (ast *aexpst) parseNode(av aexp, node *graph.Node) (*Exp, error) {
return ex, nil
}

// TODO: Make this function work with schemas
if _, err := ast.processColumn(av, ex, node); err != nil {
return nil, err
}
Expand Down Expand Up @@ -479,6 +480,7 @@ func (ast *aexpst) processNestedTable(av aexp, ex *Exp, node *graph.Node) (bool,
}

var path []sdata.TPath
// TODO: Make this function work with schemas
if path, err = s.FindPath(curr, prev, ""); err != nil {
break
}
Expand Down
4 changes: 2 additions & 2 deletions core/internal/qcode/qcode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestCompile1(t *testing.T) {
qc, _ := qcode.NewCompiler(dbs, qcode.Config{})
err := qc.AddRole("user", "public", "products", qcode.TRConfig{
Query: qcode.QueryConfig{
Columns: []string{"id", "Name"},
Columns: []string{"id", "name"},
},
})
if err != nil {
Expand All @@ -39,7 +39,7 @@ func TestCompile1(t *testing.T) {
} }`), nil, "user", "")

if err != nil {
t.Fatal(errors.New("this should not be an error id can be a number"))
t.Fatal(err)
}
}

Expand Down
2 changes: 1 addition & 1 deletion core/internal/sdata/dwg.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func (s *DBSchema) addEdge(name string, edge TEdge, inSchema bool) error {
ei1 := edgeInfo{nodeID: edge.From, edgeIDs: []int32{edgeID}}
// ei2 := edgeInfo{nodeID: edge.To, edgeIDs: []int32{edgeID}}

k1 := strings.ToLower(name)
k1 := name
// k2 := strings.ToLower(edge.RT.Name)

s.addEdgeInfo(k1, ei1)
Expand Down
4 changes: 2 additions & 2 deletions core/internal/sdata/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func NewDBSchema(

for k, f := range info.Functions {
if len(f.Params) == 1 {
schema.fm[strings.ToLower(f.Name)] = info.Functions[k]
schema.fm[f.Name] = info.Functions[k]
}
}

Expand Down Expand Up @@ -360,7 +360,7 @@ func (s *DBSchema) GetFunctions() map[string]DBFunction {
}

func getRelName(colName string) string {
cn := strings.ToLower(colName)
cn := colName

if strings.HasSuffix(cn, "_id") {
return colName[:len(colName)-3]
Expand Down
2 changes: 1 addition & 1 deletion core/internal/sdata/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func NewDBTable(schema, name, _type string, cols []DBColumn) DBTable {
ti.PrimaryCol = c

}
ti.colMap[strings.ToLower(c.Name)] = i
ti.colMap[c.Name] = i
}
return ti
}
Expand Down
4 changes: 2 additions & 2 deletions core/introspec.go
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ func getGQLType(col sdata.DBColumn, id bool) (schema.Type, string) {
var typeName string
var ok bool

k := strings.ToLower(col.Type)
k := col.Type
if i := strings.IndexAny(k, "(["); i != -1 {
k = k[:i]
}
Expand All @@ -732,7 +732,7 @@ func getGQLTypeFunc(col sdata.DBFuncParam) (schema.Type, string) {
var typeName string
var ok bool

k := strings.ToLower(col.Type)
k := col.Type
if i := strings.IndexAny(k, "(["); i != -1 {
k = k[:i]
}
Expand Down
7 changes: 4 additions & 3 deletions core/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ func TestMutiSchema(t *testing.T) {
column9 NUMERIC,
column10 JSONB,
column11 TEXT,
column12 TEXT,
column13 %s
"colUMN_12" TEXT,
"colUMN_13" %s
);`

_, err := db.Exec(fmt.Sprintf(createTableSQL, st, refCol))
Expand All @@ -62,6 +62,7 @@ func TestMutiSchema(t *testing.T) {
column1
column2
column3
colUMN_13
}
}`, sn, tn, sn)

Expand All @@ -71,7 +72,7 @@ func TestMutiSchema(t *testing.T) {
conf := newConfig(&core.Config{DBType: dbType, DisableAllowList: true})

err := conf.AddRoleTable("user", tname, core.Query{
Filters: []string{`{ column1: { is_null: true } }`},
Filters: []string{`{ colUMN_12: { is_null: true } }`},
Limit: 1,
})
assert.NoError(t, err)
Expand Down

0 comments on commit 603989a

Please sign in to comment.