Skip to content

Commit

Permalink
add remaining joins
Browse files Browse the repository at this point in the history
  • Loading branch information
aacebo committed Nov 22, 2024
1 parent 667e658 commit 9f38daf
Show file tree
Hide file tree
Showing 10 changed files with 176 additions and 0 deletions.
20 changes: 20 additions & 0 deletions sqlx/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,26 @@ func LeftOuterJoin(table string, predicate any) *JoinClause {
return &JoinClause{&method, table, Where(predicate)}
}

func RightJoin(table string, predicate any) *JoinClause {
method := "RIGHT"
return &JoinClause{&method, table, Where(predicate)}
}

func RightOuterJoin(table string, predicate any) *JoinClause {
method := "RIGHT OUTER"
return &JoinClause{&method, table, Where(predicate)}
}

func FullOuterJoin(table string, predicate any) *JoinClause {
method := "FULL OUTER"
return &JoinClause{&method, table, Where(predicate)}
}

func CrossJoin(table string, predicate any) *JoinClause {
method := "CROSS"
return &JoinClause{&method, table, Where(predicate)}
}

func (self *JoinClause) And(predicate any) *JoinClause {
self.where.And(predicate)
return self
Expand Down
128 changes: 128 additions & 0 deletions sqlx/select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,70 @@ func TestSelect(t *testing.T) {
t.Fatalf(sql)
}
})

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

t.Run("group by", func(t *testing.T) {
Expand Down Expand Up @@ -497,6 +561,70 @@ func TestSelect(t *testing.T) {
t.Fatalf(sql)
}
})

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

t.Run("group by", func(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions sqlx/testcases/select/join/cross_join.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT * FROM a CROSS JOIN b ON a.id = b.id AND b.deleted_at IS NULL;
6 changes: 6 additions & 0 deletions sqlx/testcases/select/join/cross_join_pretty.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
SELECT
*
FROM a
CROSS JOIN b
ON a.id = b.id
AND b.deleted_at IS NULL;
1 change: 1 addition & 0 deletions sqlx/testcases/select/join/full_outer_join.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT * FROM a FULL OUTER JOIN b ON a.id = b.id AND b.deleted_at IS NULL;
6 changes: 6 additions & 0 deletions sqlx/testcases/select/join/full_outer_join_pretty.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
SELECT
*
FROM a
FULL OUTER JOIN b
ON a.id = b.id
AND b.deleted_at IS NULL;
1 change: 1 addition & 0 deletions sqlx/testcases/select/join/right_join.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT * FROM a RIGHT JOIN b ON a.id = b.id AND b.deleted_at IS NULL;
6 changes: 6 additions & 0 deletions sqlx/testcases/select/join/right_join_pretty.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
SELECT
*
FROM a
RIGHT JOIN b
ON a.id = b.id
AND b.deleted_at IS NULL;
1 change: 1 addition & 0 deletions sqlx/testcases/select/join/right_outer_join.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT * FROM a RIGHT OUTER JOIN b ON a.id = b.id AND b.deleted_at IS NULL;
6 changes: 6 additions & 0 deletions sqlx/testcases/select/join/right_outer_join_pretty.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
SELECT
*
FROM a
RIGHT OUTER JOIN b
ON a.id = b.id
AND b.deleted_at IS NULL;

0 comments on commit 9f38daf

Please sign in to comment.