Skip to content

Commit 14739a8

Browse files
committed
feature: WithOptimizerHint
1 parent 8b8a1c5 commit 14739a8

File tree

3 files changed

+64
-16
lines changed

3 files changed

+64
-16
lines changed

queries/qm/query_mods.go

+18
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,24 @@ func Select(columns ...string) QueryMod {
227227
}
228228
}
229229

230+
type optimizerHintQueryMod struct {
231+
optimizerHint string
232+
args []interface{}
233+
}
234+
235+
// Apply implements QueryMod.Apply.
236+
func (qm optimizerHintQueryMod) Apply(q *queries.Query) {
237+
queries.AppendOptimizerHint(q, qm.optimizerHint, qm.args...)
238+
}
239+
240+
// WithOptimizerHint adds an optimizer hint comment to the query
241+
func WithOptimizerHint(optimizerHint string, args ...interface{}) QueryMod {
242+
return optimizerHintQueryMod{
243+
optimizerHint: optimizerHint,
244+
args: args,
245+
}
246+
}
247+
230248
// Where allows you to specify a where clause for your statement. If multiple
231249
// Where statements are used they are combined with 'and'
232250
func Where(clause string, args ...interface{}) QueryMod {

queries/query.go

+32-16
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,23 @@ type Query struct {
3030
load []string
3131
loadMods map[string]Applicator
3232

33-
delete bool
34-
update map[string]interface{}
35-
withs []argClause
36-
selectCols []string
37-
count bool
38-
from []string
39-
joins []join
40-
where []where
41-
groupBy []string
42-
orderBy []argClause
43-
having []argClause
44-
limit *int
45-
offset int
46-
forlock string
47-
distinct string
48-
comment string
33+
delete bool
34+
update map[string]interface{}
35+
withs []argClause
36+
optimizerHints []string
37+
selectCols []string
38+
count bool
39+
from []string
40+
joins []join
41+
where []where
42+
groupBy []string
43+
orderBy []argClause
44+
having []argClause
45+
limit *int
46+
offset int
47+
forlock string
48+
distinct string
49+
comment string
4950

5051
// This field is a hack to allow a query to strip out the reference
5152
// to deleted at is null.
@@ -246,6 +247,16 @@ func GetSelect(q *Query) []string {
246247
return q.selectCols
247248
}
248249

250+
// SetOptimizerHints on the query.
251+
func SetOptimizerHints(q *Query, optimizerHints []string) {
252+
q.optimizerHints = optimizerHints
253+
}
254+
255+
// GetOptimizerHints from the query
256+
func GetOptimizerHints(q *Query) []string {
257+
return q.optimizerHints
258+
}
259+
249260
// SetDistinct on the query.
250261
func SetDistinct(q *Query, distinct string) {
251262
q.distinct = distinct
@@ -291,6 +302,11 @@ func AppendSelect(q *Query, columns ...string) {
291302
q.selectCols = append(q.selectCols, columns...)
292303
}
293304

305+
// AppendOptimizerHint on the query.
306+
func AppendOptimizerHint(q *Query, optimizerHint string, args ...interface{}) {
307+
q.optimizerHints = append(q.optimizerHints, fmt.Sprintf(optimizerHint, args...))
308+
}
309+
294310
// AppendFrom on the query.
295311
func AppendFrom(q *Query, from ...string) {
296312
q.from = append(q.from, from...)

queries/query_builders.go

+14
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ func buildSelectQuery(q *Query) (*bytes.Buffer, []interface{}) {
5454
writeCTEs(q, buf, &args)
5555

5656
buf.WriteString("SELECT ")
57+
writeOptimizerHints(q, buf)
5758

5859
if q.dialect.UseTopClause {
5960
if q.limit != nil && q.offset == 0 {
@@ -632,3 +633,16 @@ func writeCTEs(q *Query, buf *bytes.Buffer, args *[]interface{}) {
632633
fmt.Fprintf(buf, resp)
633634
strmangle.PutBuffer(withBuf)
634635
}
636+
637+
func writeOptimizerHints(q *Query, buf *bytes.Buffer) {
638+
if len(q.optimizerHints) == 0 {
639+
return
640+
}
641+
642+
buf.WriteString("/*+ ")
643+
for _, hint := range q.optimizerHints {
644+
buf.WriteString(hint)
645+
buf.WriteString(" ")
646+
}
647+
buf.WriteString("*/ ")
648+
}

0 commit comments

Comments
 (0)