Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add method join with subquery #247

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.2.2
)

go 1.13
24 changes: 24 additions & 0 deletions select.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,16 +286,40 @@ func (b SelectBuilder) JoinClause(pred interface{}, args ...interface{}) SelectB
return builder.Append(b, "Joins", newPart(pred, args...)).(SelectBuilder)
}

// JoinSelect sets a subquery into the Join clause of the query.
func (b SelectBuilder) JoinSelect(from SelectBuilder, alias string, pred interface{}, args ...interface{}) SelectBuilder {
// Prevent misnumbered parameters in nested selects (#183).
from = from.PlaceholderFormat(Question)
b = b.JoinClause("JOIN ")
return builder.Append(b, "Joins", Alias(from, alias), newPart(pred, args...)).(SelectBuilder)
}

// Join adds a JOIN clause to the query.
func (b SelectBuilder) Join(join string, rest ...interface{}) SelectBuilder {
return b.JoinClause("JOIN "+join, rest...)
}

// LeftJoinSelect sets a subquery into the Left Join clause of the query.
func (b SelectBuilder) LeftJoinSelect(from SelectBuilder, alias string, pred interface{}, args ...interface{}) SelectBuilder {
// Prevent misnumbered parameters in nested selects (#183).
from = from.PlaceholderFormat(Question)
b = b.JoinClause("LEFT JOIN ")
return builder.Append(b, "Joins", Alias(from, alias), newPart(pred, args...)).(SelectBuilder)
}

// LeftJoin adds a LEFT JOIN clause to the query.
func (b SelectBuilder) LeftJoin(join string, rest ...interface{}) SelectBuilder {
return b.JoinClause("LEFT JOIN "+join, rest...)
}

// RightJoinSelect sets a subquery into the Left Join clause of the query.
func (b SelectBuilder) RightJoinSelect(from SelectBuilder, alias string, pred interface{}, args ...interface{}) SelectBuilder {
// Prevent misnumbered parameters in nested selects (#183).
from = from.PlaceholderFormat(Question)
b = b.JoinClause("RIGHT JOIN ")
return builder.Append(b, "Joins", Alias(from, alias), newPart(pred, args...)).(SelectBuilder)
}

// RightJoin adds a RIGHT JOIN clause to the query.
func (b SelectBuilder) RightJoin(join string, rest ...interface{}) SelectBuilder {
return b.JoinClause("RIGHT JOIN "+join, rest...)
Expand Down
16 changes: 16 additions & 0 deletions select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,22 @@ func TestSelectBuilderNestedSelectJoin(t *testing.T) {
assert.Equal(t, args, expectedArgs)
}

func TestSelectBuilderNestedSelectJoinNew(t *testing.T) {

expectedSql := "SELECT * FROM bar JOIN (SELECT * FROM baz WHERE foo = ?) AS r ON bar.foo = r.foo"
expectedArgs := []interface{}{42}

nestedSelect := Select("*").From("baz").Where("foo = ?", 42)

b := Select("*").From("bar").JoinSelect(nestedSelect, "r", "ON bar.foo = r.foo")

sql, args, err := b.ToSql()
assert.NoError(t, err)

assert.Equal(t, expectedSql, sql)
assert.Equal(t, args, expectedArgs)
}

func TestSelectWithOptions(t *testing.T) {
sql, _, err := Select("*").From("foo").Distinct().Options("SQL_NO_CACHE").ToSql()

Expand Down