Commit df86ec3e authored by Amos Wenger's avatar Amos Wenger

Add joins to search

parent 379580a0
Pipeline #11195 passed with stage
in 59 seconds
......@@ -3,10 +3,18 @@ package hades
import (
"fmt"
"strings"
"github.com/go-xorm/builder"
)
type join struct {
joinTable string
joinCond string
}
type Search struct {
orders []string
joins []join
offset *int64
limit *int64
}
......@@ -26,6 +34,14 @@ func (s Search) Offset(offset int64) Search {
return s
}
func (s Search) Join(joinTable string, joinCond string) Search {
s.joins = append(s.joins, join{
joinTable: joinTable,
joinCond: joinCond,
})
return s
}
func (s Search) Apply(sql string) string {
if len(s.orders) > 0 {
sql = fmt.Sprintf("%s ORDER BY %s", sql, strings.Join(s.orders, ", "))
......@@ -46,9 +62,16 @@ func (s Search) Apply(sql string) string {
sql = fmt.Sprintf("%s OFFSET %d", sql, *s.offset)
}
}
return sql
}
func (s Search) ApplyJoins(b *builder.Builder) {
for _, j := range s.joins {
b.InnerJoin(j.joinTable, j.joinCond)
}
}
func (s Search) String() string {
return s.Apply("")
}
......@@ -31,7 +31,10 @@ func (c *Context) Select(conn *sqlite.Conn, result interface{}, cond builder.Con
ms := scope.GetModelStruct()
columns, fields := c.selectFields(ms)
query, args, err := builder.Select(columns...).From(ms.TableName).Where(cond).ToSQL()
b := builder.Select(columns...).From(ms.TableName).Where(cond)
search.ApplyJoins(b)
query, args, err := b.ToSQL()
if err != nil {
return err
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment