Skip to content

Commit

Permalink
base join
Browse files Browse the repository at this point in the history
  • Loading branch information
aacebo committed Nov 22, 2024
1 parent 5704917 commit 3a11beb
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
4 changes: 4 additions & 0 deletions sqlx/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ type JoinClause struct {
where *WhereClause
}

func Join(table string, predicate any) *JoinClause {
return &JoinClause{nil, table, Where(predicate)}
}

func LeftJoin(table string, predicate any) *JoinClause {
method := "LEFT"
return &JoinClause{&method, table, Where(predicate)}
Expand Down
32 changes: 32 additions & 0 deletions sqlx/select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,22 @@ func TestSelect(t *testing.T) {
})

t.Run("join", func(t *testing.T) {
t.Run("join", func(t *testing.T) {
expected, err := os.ReadFile("./testcases/select/join.sql")

if err != nil {
t.Fatal(err)
}

sql := sqlx.Select("*").From("a").Join(
sqlx.Join("b", "a.id = b.id").And("b.deleted_at IS NULL"),
).Sql()

if sql != strings.TrimSuffix(string(expected), "\n") {
t.Fatalf(sql)
}
})

t.Run("left", func(t *testing.T) {
expected, err := os.ReadFile("./testcases/select/left_join.sql")

Expand Down Expand Up @@ -434,6 +450,22 @@ func TestSelect(t *testing.T) {
})

t.Run("join", func(t *testing.T) {
t.Run("join", func(t *testing.T) {
expected, err := os.ReadFile("./testcases/select/join_pretty.sql")

if err != nil {
t.Fatal(err)
}

sql := sqlx.Select("*").From("a").Join(
sqlx.Join("b", "a.id = b.id").And("b.deleted_at IS NULL"),
).SqlPretty(" ")

if sql != strings.TrimSuffix(string(expected), "\n") {
t.Fatalf(sql)
}
})

t.Run("left", func(t *testing.T) {
expected, err := os.ReadFile("./testcases/select/left_join_pretty.sql")

Expand Down
1 change: 1 addition & 0 deletions sqlx/testcases/select/join.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT * FROM a JOIN b ON a.id = b.id AND b.deleted_at IS NULL;
6 changes: 6 additions & 0 deletions sqlx/testcases/select/join_pretty.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
SELECT
*
FROM a
JOIN b
ON a.id = b.id
AND b.deleted_at IS NULL;

0 comments on commit 3a11beb

Please sign in to comment.