Skip to content

Commit

Permalink
join
Browse files Browse the repository at this point in the history
  • Loading branch information
mstgnz committed Dec 20, 2024
1 parent 34ababc commit 425fc2d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 16 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ Params: ["1", "[email protected]"]
### join
```go
gb.Table("users as u").Select("u.firstname", "u.lastname", "a.address").
Join("INNER", "address as a", "a.user_id=u.id").
Join("address as a", "a.user_id","=","u.id").
Where("u.email", "=", "[email protected]").
Prepare()
gb.Table("users as u").Select("u.firstname", "u.lastname", "a.address").
Join("INNER", "address as a", "a.user_id=u.id").
Join("address as a", "a.user_id","=","u.id").
Where("u.email", "=", "[email protected]").
Sql()
```
Expand Down
25 changes: 16 additions & 9 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@ import (
"strings"
)

const (
InnerJoin = "INNER"
LeftJoin = "LEFT"
RightJoin = "RIGHT"
FullJoin = "FULL"
)

type SQLDialect string

const (
Expand Down Expand Up @@ -203,8 +196,22 @@ func (gb *GoBuilder) Having(condition string, args ...any) *GoBuilder {
}

// Join adds a JOIN clause
func (gb *GoBuilder) Join(joinType, table, condition string) *GoBuilder {
join := fmt.Sprintf("%s JOIN %s ON %s", joinType, table, condition)
func (gb *GoBuilder) Join(table, first, operator, last string) *GoBuilder {
join := fmt.Sprintf("INNER JOIN %s ON %s %s %s", table, first, operator, last)
gb.joinClauses = append(gb.joinClauses, join)
return gb
}

// LeftJoin adds a LEFT JOIN clause
func (gb *GoBuilder) LeftJoin(table, first, operator, last string) *GoBuilder {
join := fmt.Sprintf("LEFT JOIN %s ON %s %s %s", table, first, operator, last)
gb.joinClauses = append(gb.joinClauses, join)
return gb
}

// RightJoin adds a RIGHT JOIN clause
func (gb *GoBuilder) RightJoin(table, first, operator, last string) *GoBuilder {
join := fmt.Sprintf("RIGHT JOIN %s ON %s %s %s", table, first, operator, last)
gb.joinClauses = append(gb.joinClauses, join)
return gb
}
Expand Down
6 changes: 3 additions & 3 deletions builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,16 +441,16 @@ func TestSql_Having(t *testing.T) {
}

func TestSql_Join(t *testing.T) {
queryExpected = "INNER JOIN users ON roles"
queryExpected = "INNER JOIN users ON users.id = roles.user_id"
paramsExpected := []any{}
query, params = gb.Join("INNER", "users", "roles").Prepare()
query, params = gb.Join("users", "users.id", "=", "roles.user_id").Prepare()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
if !reflect.DeepEqual(paramsExpected, params) {
t.Errorf("paramsExpected = %v, params %v", paramsExpected, params)
}
query = gb.Join("INNER", "users", "roles").Sql()
query = gb.Join("users", "users.id", "=", "roles.user_id").Sql()
if !reflect.DeepEqual(queryExpected, query) {
t.Errorf("queryExpected = %v, query %v", queryExpected, query)
}
Expand Down
4 changes: 2 additions & 2 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ func main() {
fmt.Printf("Where Or Where Sql: \n%s\n\n", query)

query, params = gb.Table("users as u").Select("u.firstname", "u.lastname", "a.address").
Join("INNER", "address as a", "a.user_id=u.id").
Join("address as a", "a.user_id", "=", "u.id").
Where("u.email", "=", "[email protected]").Prepare()
fmt.Printf("Join Prepare: \n%s\t%v\n", query, params)
query = gb.Table("users as u").Select("u.firstname", "u.lastname", "a.address").
Join("INNER", "address as a", "a.user_id=u.id").
Join("address as a", "a.user_id", "=", "u.id").
Where("u.email", "=", "[email protected]").Sql()
fmt.Printf("Join Sql: \n%s\n\n", query)

Expand Down

0 comments on commit 425fc2d

Please sign in to comment.