From 603989a229d7b028ce7d4ebb962e1c8730f6e2d5 Mon Sep 17 00:00:00 2001 From: dosco <832235+dosco@users.noreply.github.com> Date: Sat, 8 Oct 2022 15:05:16 -0700 Subject: [PATCH] Fix: foreign key column not found: X.Y #312 --- core/init.go | 8 ++++---- core/internal/psql/columns.go | 8 ++++---- core/internal/psql/exp.go | 8 ++++---- core/internal/psql/query.go | 12 ++++++------ core/internal/psql/util.go | 20 +++++++++----------- core/internal/qcode/config.go | 4 +--- core/internal/qcode/exp.go | 2 ++ core/internal/qcode/qcode_test.go | 4 ++-- core/internal/sdata/dwg.go | 2 +- core/internal/sdata/schema.go | 4 ++-- core/internal/sdata/tables.go | 2 +- core/introspec.go | 4 ++-- core/postgres_test.go | 7 ++++--- 13 files changed, 42 insertions(+), 43 deletions(-) diff --git a/core/init.go b/core/init.go index 779cc100..c0991ad5 100644 --- a/core/init.go +++ b/core/init.go @@ -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) } @@ -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) } @@ -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) { diff --git a/core/internal/psql/columns.go b/core/internal/psql/columns.go index d4d13bf6..454803b4 100644 --- a/core/internal/psql/columns.go +++ b/core/internal/psql/columns.go @@ -11,7 +11,7 @@ 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++ } @@ -19,7 +19,7 @@ func (c *compilerContext) renderColumns(sel *qcode.Select) { 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 { @@ -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 { diff --git a/core/internal/psql/exp.go b/core/internal/psql/exp.go index da509ea7..9bc0532e 100644 --- a/core/internal/psql/exp.go +++ b/core/internal/psql/exp.go @@ -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(`) `) } @@ -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(`)`) @@ -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(`) @@ -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) } } } diff --git a/core/internal/psql/query.go b/core/internal/psql/query.go index e5711653..adec9478 100644 --- a/core/internal/psql/query.go +++ b/core/internal/psql/query.go @@ -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)) } @@ -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) } } @@ -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`) } @@ -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 `) diff --git a/core/internal/psql/util.go b/core/internal/psql/util.go index d9b7fbc9..1694a6f0 100644 --- a/core/internal/psql/util.go +++ b/core/internal/psql/util.go @@ -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) { diff --git a/core/internal/qcode/config.go b/core/internal/qcode/config.go index 7e323201..a9404949 100644 --- a/core/internal/qcode/config.go +++ b/core/internal/qcode/config.go @@ -1,8 +1,6 @@ package qcode import ( - "strings" - "github.com/gobuffalo/flect" ) @@ -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 } diff --git a/core/internal/qcode/exp.go b/core/internal/qcode/exp.go index 4af78d50..56e7d193 100644 --- a/core/internal/qcode/exp.go +++ b/core/internal/qcode/exp.go @@ -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 } @@ -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 } diff --git a/core/internal/qcode/qcode_test.go b/core/internal/qcode/qcode_test.go index c25bc87a..de70793f 100644 --- a/core/internal/qcode/qcode_test.go +++ b/core/internal/qcode/qcode_test.go @@ -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 { @@ -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) } } diff --git a/core/internal/sdata/dwg.go b/core/internal/sdata/dwg.go index c1ba9314..6334e4a8 100644 --- a/core/internal/sdata/dwg.go +++ b/core/internal/sdata/dwg.go @@ -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) diff --git a/core/internal/sdata/schema.go b/core/internal/sdata/schema.go index 9e102898..a76ad820 100644 --- a/core/internal/sdata/schema.go +++ b/core/internal/sdata/schema.go @@ -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] } } @@ -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] diff --git a/core/internal/sdata/tables.go b/core/internal/sdata/tables.go index 43d07002..c8a19890 100644 --- a/core/internal/sdata/tables.go +++ b/core/internal/sdata/tables.go @@ -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 } diff --git a/core/introspec.go b/core/introspec.go index b232763d..f15714ee 100644 --- a/core/introspec.go +++ b/core/introspec.go @@ -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] } @@ -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] } diff --git a/core/postgres_test.go b/core/postgres_test.go index d77bf262..c2300044 100644 --- a/core/postgres_test.go +++ b/core/postgres_test.go @@ -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)) @@ -62,6 +62,7 @@ func TestMutiSchema(t *testing.T) { column1 column2 column3 + colUMN_13 } }`, sn, tn, sn) @@ -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)