-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package sqlx | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
type JoinClause struct { | ||
method *string | ||
table string | ||
where *WhereClause | ||
} | ||
|
||
func LeftJoin(table string, predicate any) *JoinClause { | ||
method := "LEFT" | ||
return &JoinClause{&method, table, Where(predicate)} | ||
} | ||
|
||
func LeftOuterJoin(table string, predicate any) *JoinClause { | ||
method := "LEFT OUTER" | ||
return &JoinClause{&method, table, Where(predicate)} | ||
} | ||
|
||
func (self *JoinClause) And(predicate any) *JoinClause { | ||
self.where.And(predicate) | ||
return self | ||
} | ||
|
||
func (self *JoinClause) Or(predicate any) *JoinClause { | ||
self.where.Or(predicate) | ||
return self | ||
} | ||
|
||
func (self JoinClause) Sql() string { | ||
parts := []string{} | ||
|
||
if self.method != nil { | ||
parts = append(parts, *self.method) | ||
} | ||
|
||
parts = append(parts, "JOIN", self.table, "ON") | ||
parts = append(parts, self.where.Sql()) | ||
return strings.Join(parts, " ") | ||
} | ||
|
||
func (self JoinClause) SqlPretty(indent string) string { | ||
parts := []string{} | ||
|
||
if self.method != nil { | ||
parts = append(parts, fmt.Sprintf("%s JOIN %s", *self.method, self.table)) | ||
} else { | ||
parts = append(parts, fmt.Sprintf("JOIN %s", self.table)) | ||
} | ||
|
||
lines := strings.Split(self.where.SqlPretty(indent), "\n") | ||
parts = append(parts, indent+"ON "+lines[0]) | ||
|
||
for _, line := range lines[1:] { | ||
parts = append(parts, indent+line) | ||
} | ||
|
||
return strings.Join(parts, "\n") | ||
} | ||
|
||
func (self *JoinClause) setDepth(_ uint) { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
SELECT * FROM a LEFT JOIN b ON a.id = b.id AND b.deleted_at IS NULL; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
SELECT | ||
* | ||
FROM a | ||
LEFT JOIN b | ||
ON a.id = b.id | ||
AND b.deleted_at IS NULL; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
SELECT * FROM a LEFT OUTER JOIN b ON a.id = b.id AND b.deleted_at IS NULL; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
SELECT | ||
* | ||
FROM a | ||
LEFT OUTER JOIN b | ||
ON a.id = b.id | ||
AND b.deleted_at IS NULL; |