From 425fc2d65558c7c7b6e97b65ea4c5bacfea0408d Mon Sep 17 00:00:00 2001 From: Mesut GENEZ Date: Fri, 20 Dec 2024 15:27:25 +0300 Subject: [PATCH] join --- README.md | 4 ++-- builder.go | 25 ++++++++++++++++--------- builder_test.go | 6 +++--- example/main.go | 4 ++-- 4 files changed, 23 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 375470d..9b097ee 100644 --- a/README.md +++ b/README.md @@ -40,11 +40,11 @@ Params: ["1", "loremipsum@lrmpsm.com"] ### 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", "=", "loremipsum@lrmpsm.com"). 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", "=", "loremipsum@lrmpsm.com"). Sql() ``` diff --git a/builder.go b/builder.go index 024be26..4447635 100644 --- a/builder.go +++ b/builder.go @@ -7,13 +7,6 @@ import ( "strings" ) -const ( - InnerJoin = "INNER" - LeftJoin = "LEFT" - RightJoin = "RIGHT" - FullJoin = "FULL" -) - type SQLDialect string const ( @@ -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 } diff --git a/builder_test.go b/builder_test.go index aeb26fe..f360042 100644 --- a/builder_test.go +++ b/builder_test.go @@ -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) } diff --git a/example/main.go b/example/main.go index 104cbee..8f6b641 100644 --- a/example/main.go +++ b/example/main.go @@ -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", "=", "loremipsum@lrmpsm.com").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", "=", "loremipsum@lrmpsm.com").Sql() fmt.Printf("Join Sql: \n%s\n\n", query)