Skip to content

Commit 4e1b83a

Browse files
committed
Add force index query mod for select query.
Fix eager loading casting for nested levels.
1 parent f892107 commit 4e1b83a

4 files changed

Lines changed: 32 additions & 3 deletions

File tree

queries/eager_load.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,16 @@ func (l loadRelationshipState) loadRelationshipsRecurse(depth int, obj reflect.V
206206
}
207207

208208
bkind := kindStruct
209-
if reflect.Indirect(loadedObject).Kind() != reflect.Struct {
209+
if derefed := reflect.Indirect(loadedObject); derefed.Kind() != reflect.Struct {
210210
bkind = kindPtrSliceStruct
211-
loadedObject = loadedObject.Addr()
211+
212+
// Convert away any helper slice types
213+
// elemType is *elem (from []*elem or helperSliceType)
214+
// sliceType is *[]*elem
215+
elemType := derefed.Type().Elem()
216+
sliceType := reflect.PtrTo(reflect.SliceOf(elemType))
217+
218+
loadedObject = loadedObject.Addr().Convert(sliceType)
212219
}
213220
return l.loadRelationships(depth+1, loadedObject.Interface(), bkind)
214221
}
@@ -241,6 +248,9 @@ func collectLoaded(key string, loadingFrom reflect.Value) (reflect.Value, bindKi
241248
if loadedType.Elem().Kind() == reflect.Struct {
242249
bkind = kindStruct
243250
loadedType = reflect.SliceOf(loadedType)
251+
} else {
252+
// Ensure that we get rid of all the helper "XSlice" types
253+
loadedType = reflect.SliceOf(loadedType.Elem())
244254
}
245255

246256
collection := reflect.MakeSlice(loadedType, 0, 0)

queries/qm/query_mods.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,13 @@ func From(from string) QueryMod {
123123
}
124124
}
125125

126+
func ForceIndex( index string) QueryMod {
127+
return func(q *queries.Query) {
128+
queries.SetForceIndex(q, index)
129+
}
130+
}
131+
132+
126133
// Limit the number of returned rows
127134
func Limit(limit int) QueryMod {
128135
return func(q *queries.Query) {

queries/query.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type Query struct {
3030
selectCols []string
3131
count bool
3232
from []string
33+
forceindex string
3334
joins []join
3435
where []where
3536
in []in
@@ -263,6 +264,11 @@ func SetLastWhereAsOr(q *Query) {
263264
q.where[len(q.where)-1].orSeparator = true
264265
}
265266

267+
// SetForceIndex sets the index to be used by the query
268+
func SetForceIndex(q *Query, index string){
269+
q.forceindex = index
270+
}
271+
266272
// SetLastInAsOr sets the or separator for the tail "IN" in the slice
267273
func SetLastInAsOr(q *Query) {
268274
if len(q.in) == 0 {

queries/query_builders.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,13 @@ func buildSelectQuery(q *Query) (*bytes.Buffer, []interface{}) {
7676
buf.WriteByte(')')
7777
}
7878

79-
fmt.Fprintf(buf, " FROM %s", strings.Join(strmangle.IdentQuoteSlice(q.dialect.LQ, q.dialect.RQ, q.from), ", "))
79+
if len(q.forceindex) > 0 {
80+
fmt.Fprintf(buf, " FROM %s FORCE INDEX (%s)", strings.Join(strmangle.IdentQuoteSlice(q.dialect.LQ, q.dialect.RQ, q.from), ", "),q.forceindex)
81+
82+
}else{
83+
fmt.Fprintf(buf, " FROM %s", strings.Join(strmangle.IdentQuoteSlice(q.dialect.LQ, q.dialect.RQ, q.from), ", "))
84+
85+
}
8086

8187
if len(q.joins) > 0 {
8288
argsLen := len(args)

0 commit comments

Comments
 (0)