Skip to content

Commit 161b2c7

Browse files
committed
Add object max depth to query meta info
1 parent d7237e5 commit 161b2c7

File tree

3 files changed

+37
-16
lines changed

3 files changed

+37
-16
lines changed

soql/parser/parser_test.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,15 @@ func TestParse(t *testing.T) {
8181
want: nil,
8282
wantErr: false,
8383
}, {
84-
name: "fieldset 2",
85-
args: args{s: `SELECT fields(con.acc.all) FROM Contact con, con.Account acc`},
84+
name: "fieldset 2",
85+
args: args{s: `SELECT fields(con.acc.all) FROM Contact con, con.Account acc`},
86+
want: nil,
87+
wantErr: true,
88+
}, {
89+
name: "cond subquery",
90+
args: args{s: `SELECT Id FROM Contact WHERE Name in (select asd.x from qwe)`},
8691
want: nil,
87-
wantErr: true,
92+
wantErr: false,
8893
dbgBreak: true,
8994
}}
9095
for _, tt := range tests {
@@ -172,6 +177,7 @@ func TestParse2(t *testing.T) {
172177
, CONCAT(TRIM(acc.Name), '/', TRIM(con.Name), 123.45, 0xacc0) cname
173178
, FLAT(acc.Name)
174179
, (SELECT Id FROM con.Departments where uuu=con.Zzz and vvv=con.Id) qwerty
180+
, (select Id from r3.lkjh)
175181
FROM
176182
Contact con
177183
, con.Account acc

soql/parser/postprocess/normalize.go

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ import (
1010
)
1111

1212
type normalizeQueryContext struct {
13-
viewId int
14-
viewIdMap map[string]int
15-
columnId int
16-
columnIdMap map[string]int
17-
colIndexMap map[string]int
13+
viewId int
14+
viewIdMap map[string]int
15+
columnId int
16+
columnIdMap map[string]int
17+
colIndexMap map[string]int
18+
headObjDepthOffset int
19+
maxDepth int
1820
}
1921

2022
func (ctx *normalizeQueryContext) normalizeQuery(
@@ -249,6 +251,11 @@ func (ctx *normalizeQueryContext) normalizeQuery(
249251
} else {
250252
q.From[i].ViewId = viewId
251253
}
254+
255+
objDepth := len(q.From[i].Name) + ctx.headObjDepthOffset
256+
if ctx.maxDepth < objDepth {
257+
ctx.maxDepth = objDepth
258+
}
252259
}
253260

254261
// TODO: * check object graph when aggregation(group by)
@@ -311,9 +318,11 @@ func (ctx *normalizeQueryContext) normalizeQuery(
311318
savedViewIdMap := ctx.viewIdMap
312319
savedColumnIdMap := ctx.columnIdMap
313320
savedColIndexMap := ctx.colIndexMap
321+
savedHeadObjDepthOffset := ctx.headObjDepthOffset
314322
ctx.viewIdMap = make(map[string]int)
315323
ctx.columnIdMap = make(map[string]int)
316324
ctx.colIndexMap = make(map[string]int)
325+
ctx.headObjDepthOffset = len(q.From[0].Name)
317326

318327
if q.Where != nil {
319328
for i := 0; i < len(q.Where); i++ {
@@ -360,17 +369,20 @@ func (ctx *normalizeQueryContext) normalizeQuery(
360369
ctx.viewIdMap = savedViewIdMap
361370
ctx.columnIdMap = savedColumnIdMap
362371
ctx.colIndexMap = savedColIndexMap
372+
ctx.headObjDepthOffset = savedHeadObjDepthOffset
363373

364374
return nil
365375
}
366376

367377
func Normalize(q *SoqlQuery) error {
368378
ctx := normalizeQueryContext{
369-
viewId: 1,
370-
viewIdMap: make(map[string]int),
371-
columnId: 1,
372-
columnIdMap: make(map[string]int),
373-
colIndexMap: map[string]int{},
379+
viewId: 1,
380+
viewIdMap: make(map[string]int),
381+
columnId: 1,
382+
columnIdMap: make(map[string]int),
383+
colIndexMap: map[string]int{},
384+
headObjDepthOffset: 0,
385+
maxDepth: 0,
374386
}
375387

376388
if err := ctx.normalizeQuery(soqlQueryPlace_Primary, q, nil); err != nil {
@@ -379,6 +391,7 @@ func Normalize(q *SoqlQuery) error {
379391

380392
q.Meta.NextColumnId = ctx.columnId
381393
q.Meta.NextViewId = ctx.viewId
394+
q.Meta.MaxDepth = ctx.maxDepth
382395

383396
return nil
384397
}

soql/parser/types/types.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ type SoqlFieldInfo struct {
8787
NotSelected bool `json:"notSelected,omitempty"` // It appears only in parameters and conditional expressions.
8888
Aggregated bool `json:"aggregated,omitempty"` // It is an aggregation function result field or not
8989
Hints []SoqlQueryHint `json:"hints,omitempty"` // TODO: hints
90-
ColumnId int `json:"columnId,omitempty"` // (internal use) for all; 1-based; If 0, it is not set.; Unique column Id across all main and sub queries
90+
ColumnId int `json:"columnId,omitempty"` // Column unique id; 1-based; If 0, it is not set.; Unique column Id across all main and sub queries
9191
ColIndex int `json:"colIndex"` // Column index in the object
92-
ViewId int `json:"viewId,omitempty"` // TODO: (internal use) for SubQuery and Function; 1-based; If 0, it is not set.
92+
ViewId int `json:"viewId,omitempty"` // View (table/object) unique id; 1-based; If 0, it is not set.
9393
Key string `json:"key,omitempty"` // (internal use) Base64-encoded, dot-delimited Name field value
9494
}
9595

@@ -135,7 +135,8 @@ type SoqlObjectInfo struct {
135135
InnerJoin bool `json:"innerJoin,omitempty"` // When this object is on the left side, an inner join is performed.
136136
Hints []SoqlQueryHint `json:"hints,omitempty"` // TODO: hints
137137
PerObjectQuery *SoqlQuery `json:"perObjectQuery"` // A query that extracts only the filter and sort conditions and fields related to this object. A simple query, not including function calls, etc.
138-
ViewId int `json:"viewId,omitempty"` // TODO: (internal use) for SubQuery and Function; 1-based; If 0, it is not set.
138+
ViewId int `json:"viewId,omitempty"` // View (table/object) unique id; 1-based; If 0, it is not set.
139+
ParentViewId int `json:"parentViewId,omitempty"` // TODO:
139140
Key string `json:"key,omitempty"` // (internal use) Base64-encoded, dot-delimited Name field value
140141
}
141142

@@ -245,6 +246,7 @@ type SoqlQueryMeta struct {
245246
Version string `json:"version,omitempty"`
246247
Date time.Time `json:"date,omitempty"`
247248
Source string `json:"source,omitempty"`
249+
MaxDepth int `json:"maxDepth,omitempty"` // TODO:
248250
NextColumnId int `json:"nextColumnId,omitempty"`
249251
NextViewId int `json:"nextViewId,omitempty"`
250252
}

0 commit comments

Comments
 (0)