Skip to content
This repository has been archived by the owner on Jun 14, 2019. It is now read-only.

Feature request: support subquery as a condition value #7

Closed
damnever opened this issue Oct 24, 2017 · 4 comments
Closed

Feature request: support subquery as a condition value #7

damnever opened this issue Oct 24, 2017 · 4 comments
Labels

Comments

@damnever
Copy link
Contributor

damnever commented Oct 24, 2017

Expr seems not good enough..

subb := builder.Select("id").From("table_b").Where(builder.And(builder.Eq{"b": "a"}))
b := builder.Select("a, b").From("table_a").Where(
    builder.And(builder.Eq{
        "id":   23,
        "b_id": subb,
    }),
)
sql, args, err := b.ToSQL()
// args will contain the args from subquery:
//  subsql, subargs, err := subb.ToSQL()
//  args = append(args, subargs...)

fmt.Println(sql)
fmt.Println(args)
fmt.Println(err)

Expected output:

SELECT a, b FROM table_a WHERE id=? AND b_id=(SELECT id FROM table_b WHERE b=?)
[23 "a"]
<nil>
@lunny lunny added the feature label Oct 24, 2017
@damnever
Copy link
Contributor Author

damnever commented Oct 24, 2017

I found a solution, anyway, support the Builder as the value directly will be nice..

subsql, subargs, err := builder.Select("id").From("table_b").Where(builder.And(builder.Eq{"b": "a"})).ToSQL()

b := builder.Select("a, b").From("table_a").Where(
        builder.And(builder.Eq{
                "id": 23,
                "b_id": builder.Expr(subsql, subargs...),
        }),
)
sql, args, err := b.ToSQL()
fmt.Println(sql)
fmt.Println(args)
fmt.Println(err)

@damnever damnever reopened this Oct 24, 2017
@lunny
Copy link
Member

lunny commented Oct 24, 2017

That's a method but not a good idea. I think treat Builder as a subquery is a better idea for performance.

@damnever
Copy link
Contributor Author

The issue doesn't actually exists, my bad..

@lunny
Copy link
Member

lunny commented Oct 30, 2017

Yes, this feature has been implemented. See the test a4a881a

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants