Skip to content

Commit

Permalink
allow multiple joins
Browse files Browse the repository at this point in the history
  • Loading branch information
lqs committed Aug 21, 2022
1 parent df26da7 commit 16e5836
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
4 changes: 3 additions & 1 deletion select.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type selectWithJoinOn interface {
toSelectWithContext
toSelectFinal
toUnionSelect
toSelectJoin
Where(conditions ...BooleanExpression) selectWithWhere
GroupBy(expressions ...Expression) selectWithGroupBy
OrderBy(orderBys ...OrderBy) selectWithOrder
Expand Down Expand Up @@ -431,8 +432,9 @@ func (s selectBase) buildSelectBase(sb *strings.Builder) error {
if err != nil {
return err
}
sb.WriteString(" ")
sb.WriteString(join.prefix)
sb.WriteString(" JOIN ")
sb.WriteString("JOIN ")
sb.WriteString(join.table.GetSQL(s.scope))
sb.WriteString(" ON ")
sb.WriteString(onSql)
Expand Down
17 changes: 14 additions & 3 deletions select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ var field2 = NewNumberField(table1, "field2")
var table2 = NewTable("table2")
var field3 = NewNumberField(table2, "field3")

var table3 = NewTable("table3")
var field4 = NewNumberField(table3, "field4")

func (t tTable1) GetFields() []Field {
return []Field{field1, field2}
}
Expand Down Expand Up @@ -51,9 +54,17 @@ func TestSelect(t *testing.T) {

db.SelectDistinct(field2).From(Table1).GetSQL()

db.Select(field1, field3).From(Table1).Join(table2).On(field1.Equals(field3)).GetSQL()
db.Select(field1, field3).From(Table1).LeftJoin(table2).On(field1.Equals(field3)).GetSQL()
db.Select(field1, field3).From(Table1).RightJoin(table2).On(field1.Equals(field3)).GetSQL()
_, _ = db.Select(field1, field3).From(Table1).Join(table2).On(field1.Equals(field3)).FetchFirst()
assertLastSql(t, "SELECT `table1`.`field1`, `table2`.`field3` FROM `table1` JOIN `table2` ON `table1`.`field1` = `table2`.`field3`")
_, _ = db.Select(field1, field3).From(Table1).LeftJoin(table2).On(field1.Equals(field3)).FetchFirst()
assertLastSql(t, "SELECT `table1`.`field1`, `table2`.`field3` FROM `table1` LEFT JOIN `table2` ON `table1`.`field1` = `table2`.`field3`")
_, _ = db.Select(field1, field3).From(Table1).RightJoin(table2).On(field1.Equals(field3)).FetchFirst()
assertLastSql(t, "SELECT `table1`.`field1`, `table2`.`field3` FROM `table1` RIGHT JOIN `table2` ON `table1`.`field1` = `table2`.`field3`")

_, _ = db.Select(field1, field3).From(Table1).
LeftJoin(table2).On(field1.Equals(field3)).
RightJoin(table3).On(field1.Equals(field4)).FetchFirst()
assertLastSql(t, "SELECT `table1`.`field1`, `table2`.`field3` FROM `table1` LEFT JOIN `table2` ON `table1`.`field1` = `table2`.`field3` RIGHT JOIN `table3` ON `table1`.`field1` = `table3`.`field4`")

db.Select(1).WithContext(context.Background())

Expand Down

0 comments on commit 16e5836

Please sign in to comment.