Skip to content

Commit

Permalink
Add support for optional index hints on join tables (#255)
Browse files Browse the repository at this point in the history
  • Loading branch information
austintaylor authored Jul 20, 2023
1 parent 36b0b6a commit f65b178
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
8 changes: 7 additions & 1 deletion join.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const (
full
)

func join(t joinType, table interface{}, on interface{}) Builder {
func join(t joinType, table interface{}, on interface{}, indexHints []Builder) Builder {
return BuildFunc(func(d Dialect, buf Buffer) error {
buf.WriteString(" ")
switch t {
Expand All @@ -28,6 +28,12 @@ func join(t joinType, table interface{}, on interface{}) Builder {
buf.WriteString(placeholder)
buf.WriteValue(table)
}
for _, hint := range indexHints {
buf.WriteString(" ")
if err := hint.Build(d, buf); err != nil {
return err
}
}
buf.WriteString(" ON ")
switch on := on.(type) {
case string:
Expand Down
16 changes: 8 additions & 8 deletions select.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,29 +370,29 @@ func (b *SelectStmt) Comment(comment string) *SelectStmt {

// Join add inner-join.
// on can be Builder or string.
func (b *SelectStmt) Join(table, on interface{}) *SelectStmt {
b.JoinTable = append(b.JoinTable, join(inner, table, on))
func (b *SelectStmt) Join(table, on interface{}, indexHints ...Builder) *SelectStmt {
b.JoinTable = append(b.JoinTable, join(inner, table, on, indexHints))
return b
}

// LeftJoin add left-join.
// on can be Builder or string.
func (b *SelectStmt) LeftJoin(table, on interface{}) *SelectStmt {
b.JoinTable = append(b.JoinTable, join(left, table, on))
func (b *SelectStmt) LeftJoin(table, on interface{}, indexHints ...Builder) *SelectStmt {
b.JoinTable = append(b.JoinTable, join(left, table, on, indexHints))
return b
}

// RightJoin add right-join.
// on can be Builder or string.
func (b *SelectStmt) RightJoin(table, on interface{}) *SelectStmt {
b.JoinTable = append(b.JoinTable, join(right, table, on))
func (b *SelectStmt) RightJoin(table, on interface{}, indexHints ...Builder) *SelectStmt {
b.JoinTable = append(b.JoinTable, join(right, table, on, indexHints))
return b
}

// FullJoin add full-join.
// on can be Builder or string.
func (b *SelectStmt) FullJoin(table, on interface{}) *SelectStmt {
b.JoinTable = append(b.JoinTable, join(full, table, on))
func (b *SelectStmt) FullJoin(table, on interface{}, indexHints ...Builder) *SelectStmt {
b.JoinTable = append(b.JoinTable, join(full, table, on, indexHints))
return b
}

Expand Down
6 changes: 3 additions & 3 deletions select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import (
"testing"

"github.com/lib/pq"
"github.com/stretchr/testify/require"

"github.com/gocraft/dbr/v2/dialect"
"github.com/stretchr/testify/require"
)

func TestSelectStmt(t *testing.T) {
buf := NewBuffer()
builder := Select("a", "b").
From(Select("a").From("table")).
LeftJoin("table2", "table.a1 = table.a2").
LeftJoin("table2", "table.a1 = table.a2", UseIndex("idx_table2")).
Distinct().
Where(Eq("c", 1)).
GroupBy("d").
Expand All @@ -29,7 +29,7 @@ func TestSelectStmt(t *testing.T) {
err := builder.Build(dialect.MySQL, buf)
require.NoError(t, err)
require.Equal(t, "/* SELECT TEST */\nSELECT DISTINCT a, b FROM ? USE INDEX FOR GROUP BY(`idx_c_d`) USE INDEX(idx_e_f) IGNORE INDEX(`idx_a_b`) "+
"LEFT JOIN `table2` ON table.a1 = table.a2 WHERE (`c` = ?) GROUP BY d HAVING (`e` = ?) ORDER BY f ASC LIMIT 3 OFFSET 4 FOR UPDATE", buf.String())
"LEFT JOIN `table2` USE INDEX(`idx_table2`) ON table.a1 = table.a2 WHERE (`c` = ?) GROUP BY d HAVING (`e` = ?) ORDER BY f ASC LIMIT 3 OFFSET 4 FOR UPDATE", buf.String())
// two functions cannot be compared
require.Equal(t, 3, len(buf.Value()))
}
Expand Down

0 comments on commit f65b178

Please sign in to comment.