Skip to content

Commit

Permalink
Merge pull request #22 from twharmon/left-joins
Browse files Browse the repository at this point in the history
Add left joins
  • Loading branch information
twharmon authored Aug 26, 2020
2 parents f056b77 + 4e6532c commit e31536b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
10 changes: 8 additions & 2 deletions count_query.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gosql

import (
"fmt"
"strings"
)

Expand Down Expand Up @@ -41,7 +42,13 @@ func (cq *CountQuery) OrWhere(condition string, args ...interface{}) *CountQuery

// Join joins another table to this query.
func (cq *CountQuery) Join(join string) *CountQuery {
cq.joins = append(cq.joins, join)
cq.joins = append(cq.joins, fmt.Sprintf(" join %s", join))
return cq
}

// LeftJoin joins another table to this query.
func (cq *CountQuery) LeftJoin(join string) *CountQuery {
cq.joins = append(cq.joins, fmt.Sprintf(" left join %s", join))
return cq
}

Expand All @@ -63,7 +70,6 @@ func (cq *CountQuery) String() string {
q.WriteString(cq.table)

for _, join := range cq.joins {
q.WriteString(" join ")
q.WriteString(join)
}
for i, where := range cq.wheres {
Expand Down
10 changes: 8 additions & 2 deletions delete_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gosql

import (
"database/sql"
"fmt"
"strings"
)

Expand Down Expand Up @@ -38,7 +39,13 @@ func (dq *DeleteQuery) OrWhere(condition string, args ...interface{}) *DeleteQue

// Join joins another table to this query.
func (dq *DeleteQuery) Join(join string) *DeleteQuery {
dq.joins = append(dq.joins, join)
dq.joins = append(dq.joins, fmt.Sprintf(" join %s", join))
return dq
}

// LeftJoin joins another table to this query.
func (dq *DeleteQuery) LeftJoin(join string) *DeleteQuery {
dq.joins = append(dq.joins, fmt.Sprintf(" left join %s", join))
return dq
}

Expand All @@ -53,7 +60,6 @@ func (dq *DeleteQuery) String() string {
q.WriteString("delete from ")
q.WriteString(dq.table)
for _, join := range dq.joins {
q.WriteString(" join ")
q.WriteString(join)
}
for i, where := range dq.wheres {
Expand Down
9 changes: 7 additions & 2 deletions select_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ type SelectQuery struct {

// Join joins another table to this query.
func (sq *SelectQuery) Join(join string) *SelectQuery {
sq.joins = append(sq.joins, join)
sq.joins = append(sq.joins, fmt.Sprintf(" join %s", join))
return sq
}

// LeftJoin joins another table to this query.
func (sq *SelectQuery) LeftJoin(join string) *SelectQuery {
sq.joins = append(sq.joins, fmt.Sprintf(" left join %s", join))
return sq
}

Expand Down Expand Up @@ -268,7 +274,6 @@ func (sq *SelectQuery) String() string {
q.WriteString(" from ")
q.WriteString(sq.model.table)
for _, join := range sq.joins {
q.WriteString(" join ")
q.WriteString(join)
}
for i, where := range sq.wheres {
Expand Down
10 changes: 8 additions & 2 deletions update_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gosql

import (
"database/sql"
"fmt"
"strings"
)

Expand Down Expand Up @@ -47,7 +48,13 @@ func (uq *UpdateQuery) Set(set string, args ...interface{}) *UpdateQuery {

// Join joins another table to this query.
func (uq *UpdateQuery) Join(join string) *UpdateQuery {
uq.joins = append(uq.joins, join)
uq.joins = append(uq.joins, fmt.Sprintf(" join %s", join))
return uq
}

// LeftJoin joins another table to this query.
func (uq *UpdateQuery) LeftJoin(join string) *UpdateQuery {
uq.joins = append(uq.joins, fmt.Sprintf(" left join %s", join))
return uq
}

Expand All @@ -64,7 +71,6 @@ func (uq *UpdateQuery) String() string {
q.WriteString("update ")
q.WriteString(uq.table)
for _, join := range uq.joins {
q.WriteString(" join ")
q.WriteString(join)
}
for i, set := range uq.sets {
Expand Down

0 comments on commit e31536b

Please sign in to comment.