Skip to content

Commit 7d5a9cd

Browse files
committed
Register unregistered fields in the correlated subquery.
1 parent 2c0c1c6 commit 7d5a9cd

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

soql/parser/postprocess/normalize3.go

+36-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package postprocess
22

33
import (
4+
"errors"
5+
"strings"
6+
7+
"github.com/shellyln/go-nameutil/nameutil"
48
. "github.com/shellyln/go-open-soql-parser/soql/parser/types"
59
)
610

7-
func (ctx *normalizeQueryContext) applyColIndexToFields(q *SoqlQuery, fields []SoqlFieldInfo) {
11+
func (ctx *normalizeQueryContext) applyColIndexToFields(q *SoqlQuery, fields []SoqlFieldInfo) error {
812
for i := range fields {
913
switch fields[i].Type {
1014
case SoqlFieldInfo_Field:
@@ -13,16 +17,44 @@ func (ctx *normalizeQueryContext) applyColIndexToFields(q *SoqlQuery, fields []S
1317
} else {
1418
fields[i].ColIndex = -1
1519
}
20+
1621
if len(fields[i].Name) <= len(q.From[0].Name) {
1722
q.IsCorelated = true
18-
// if fields[i].ColIndex == -1 {
19-
// // TODO:
20-
// }
23+
24+
if fields[i].ColIndex == -1 {
25+
isSet := false
26+
27+
if q.Parent != nil {
28+
ns := nameutil.GetNamespaceFromName(fields[i].Name)
29+
nsKey := nameutil.MakeDottedKeyIgnoreCase(ns, len(ns))
30+
31+
for j := range q.Parent.From {
32+
if q.Parent.From[j].Key == nsKey {
33+
idx := len(q.Parent.From[j].PerObjectQuery.Fields)
34+
35+
ctx.colIndexMap[fields[i].Key] = idx
36+
fields[i].ColIndex = idx
37+
38+
q.Parent.Fields = append(q.Parent.Fields, fields[i])
39+
q.Parent.From[j].PerObjectQuery.Fields = append(q.Parent.From[j].PerObjectQuery.Fields, fields[i])
40+
41+
isSet = true
42+
break
43+
}
44+
}
45+
}
46+
if !isSet {
47+
return errors.New(
48+
"An incorrect ancestor field of object referred to in the correlated subquery: " +
49+
strings.Join(fields[i].Name, "."))
50+
}
51+
}
2152
}
2253
case SoqlFieldInfo_Function:
2354
ctx.applyColIndexToFields(q, fields[i].Parameters)
2455
}
2556
}
57+
return nil
2658
}
2759

2860
func (ctx *normalizeQueryContext) applyColIndexToConditions(q *SoqlQuery, conditions []SoqlCondition) {

0 commit comments

Comments
 (0)