Skip to content

Commit

Permalink
Fix bug in JOIN USING
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenafamo committed Nov 16, 2023
1 parent 4b60617 commit ff7112d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
13 changes: 7 additions & 6 deletions clause/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,21 @@ func (j Join) WriteSQL(w io.Writer, d bob.Dialect, start int) ([]any, error) {
args = append(args, onArgs...)

for k, col := range j.Using {
if k != 0 {
w.Write([]byte("USING("))
if k == 0 {
w.Write([]byte(" USING("))
} else {
w.Write([]byte(", "))
}

if k == len(j.Using)-1 {
w.Write([]byte(")"))
}

_, err = expr.Quote(col).WriteSQL(w, d, 1) // start does not matter
if err != nil {
return nil, err
}

if k == len(j.Using)-1 {
w.Write([]byte(") "))
}

}

return args, nil
Expand Down
8 changes: 8 additions & 0 deletions dialect/psql/select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@ func TestSelect(t *testing.T) {
sm.Limit(psql.Arg(10)),
),
},
"join using": {
ExpectedSQL: "SELECT id FROM test1 LEFT JOIN test2 USING (id)",
Query: psql.Select(
sm.Columns("id"),
sm.From("test1"),
sm.LeftJoin("test2").Using("id"),
),
},
}

testutils.RunTests(t, examples, formatter)
Expand Down
18 changes: 18 additions & 0 deletions website/docs/query-builder/psql/examples/select.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,21 @@ psql.Select(
sm.Limit(psql.Arg(10)),
)
```

## Join Using

SQL:

```sql
SELECT id FROM test1 LEFT JOIN test2 USING (id)
```

Code:

```go
psql.Select(
sm.Columns("id"),
sm.From("test1"),
sm.LeftJoin("test2").Using("id"),
)
```

0 comments on commit ff7112d

Please sign in to comment.