wp-go/model/querycondition.go

52 lines
953 B
Go
Raw Normal View History

package model
import (
"fmt"
"strings"
)
2023-02-21 11:49:13 +00:00
func BuildQuerySql[T Model](q *QueryCondition) (r string, args []any, err error) {
var rr T
w := ""
if q.where != nil {
w, args, err = q.where.ParseWhere(&q.in)
if err != nil {
return
}
}
h := ""
if q.having != nil {
hh, arg, er := q.having.ParseWhere(&q.in)
if er != nil {
err = er
return
}
args = append(args, arg...)
h = strings.Replace(hh, " where", " having", 1)
}
j := q.join.parseJoin()
groupBy := ""
if q.group != "" {
g := strings.Builder{}
g.WriteString(" group by ")
g.WriteString(q.group)
groupBy = g.String()
}
tp := "select %s from %s %s %s %s %s %s %s"
l := ""
if q.limit > 0 {
l = fmt.Sprintf(" limit %d", q.limit)
}
if q.offset > 0 {
l = fmt.Sprintf(" %s offset %d", l, q.offset)
}
table := rr.Table()
if q.from != "" {
table = q.from
2023-02-21 14:15:35 +00:00
}
r = fmt.Sprintf(tp, q.fields, table, j, w, groupBy, h, q.order.parseOrderBy(), l)
2023-02-21 14:15:35 +00:00
return
}