wp-go/models/model.go

305 lines
7.0 KiB
Go
Raw Normal View History

2022-08-27 13:21:05 +00:00
package models
import (
"fmt"
"github/fthvgb1/wp-go/db"
2022-08-28 13:27:41 +00:00
"github/fthvgb1/wp-go/helper"
2022-09-16 10:06:50 +00:00
"math/rand"
2022-08-31 03:44:58 +00:00
"strconv"
2022-08-27 13:21:05 +00:00
"strings"
2022-09-16 10:06:50 +00:00
"time"
2022-08-27 13:21:05 +00:00
)
2022-09-22 09:21:41 +00:00
var _ ParseWhere = SqlBuilder{}
2022-09-08 02:54:37 +00:00
type Model interface {
2022-08-27 13:21:05 +00:00
PrimaryKey() string
Table() string
}
2022-09-08 02:54:37 +00:00
type ParseWhere interface {
2022-09-20 14:37:31 +00:00
ParseWhere(in ...[]any) (string, []any)
2022-09-08 02:54:37 +00:00
}
2022-08-28 13:27:41 +00:00
type SqlBuilder [][]string
2022-08-27 13:21:05 +00:00
2022-09-16 09:59:17 +00:00
func (w SqlBuilder) parseField(ss []string, s *strings.Builder) {
if strings.Contains(ss[0], ".") && !strings.Contains(ss[0], "(") {
s.WriteString("`")
sx := strings.Split(ss[0], ".")
s.WriteString(sx[0])
s.WriteString("`.`")
s.WriteString(sx[1])
s.WriteString("`")
} else if !strings.Contains(ss[0], ".") && !strings.Contains(ss[0], "(") {
s.WriteString("`")
s.WriteString(ss[0])
s.WriteString("`")
} else {
s.WriteString(ss[0])
}
}
2022-09-20 14:37:31 +00:00
func (w SqlBuilder) parseIn(ss []string, s *strings.Builder, c *int, args *[]any, in [][]any) (t bool) {
2022-09-21 13:49:14 +00:00
if helper.IsContainInArr(ss[1], []string{"in", "not in"}) && len(in) > 0 {
2022-09-16 14:32:05 +00:00
s.WriteString(" (")
for _, p := range in[*c] {
s.WriteString("?,")
*args = append(*args, p)
}
sx := s.String()
s.Reset()
s.WriteString(strings.TrimRight(sx, ","))
s.WriteString(")")
*c++
t = true
}
return t
}
2022-09-20 14:37:31 +00:00
func (w SqlBuilder) parseType(ss []string, args *[]any) {
2022-09-16 14:32:05 +00:00
if len(ss) == 4 && ss[3] == "int" {
i, _ := strconv.Atoi(ss[2])
*args = append(*args, i)
} else if len(ss) == 4 && ss[3] == "float" {
i, _ := strconv.ParseFloat(ss[2], 64)
*args = append(*args, i)
} else {
*args = append(*args, ss[2])
}
}
2022-09-20 14:37:31 +00:00
func (w SqlBuilder) ParseWhere(in ...[]any) (string, []any) {
2022-08-27 13:21:05 +00:00
var s strings.Builder
2022-09-20 14:37:31 +00:00
args := make([]any, 0, len(w))
2022-08-31 03:44:58 +00:00
c := 0
2022-08-27 13:21:05 +00:00
for _, ss := range w {
if len(ss) == 2 {
2022-09-16 09:59:17 +00:00
w.parseField(ss, &s)
2022-09-16 06:46:51 +00:00
s.WriteString("=? and ")
2022-08-27 13:21:05 +00:00
args = append(args, ss[1])
2022-09-16 14:32:05 +00:00
} else if len(ss) >= 3 && len(ss) < 5 {
2022-09-16 09:59:17 +00:00
w.parseField(ss, &s)
2022-08-27 13:21:05 +00:00
s.WriteString(ss[1])
2022-09-16 14:32:05 +00:00
if w.parseIn(ss, &s, &c, &args, in) {
2022-08-31 03:44:58 +00:00
s.WriteString(" and ")
continue
}
2022-08-28 13:27:41 +00:00
s.WriteString(" ? and ")
2022-09-16 15:33:05 +00:00
w.parseType(ss, &args)
2022-09-16 14:32:05 +00:00
} else if len(ss) >= 5 && len(ss)%5 == 0 {
j := len(ss) / 5
for i := 0; i < j; i++ {
start := i * 5
end := start + 5
2022-09-16 15:03:55 +00:00
st := s.String()
if strings.Contains(st, "and ") && ss[start] == "or" {
st = strings.TrimRight(st, "and ")
s.Reset()
s.WriteString(st)
s.WriteString(fmt.Sprintf(" %s ", ss[start]))
}
if i == 0 {
s.WriteString("( ")
}
w.parseField(ss[start+1:end], &s)
2022-09-16 15:54:32 +00:00
s.WriteString(ss[start+2])
2022-09-16 15:03:55 +00:00
if w.parseIn(ss[start+1:end], &s, &c, &args, in) {
s.WriteString(" and ")
continue
2022-09-16 14:32:05 +00:00
}
2022-09-16 15:03:55 +00:00
s.WriteString(" ? and ")
2022-09-16 15:33:05 +00:00
w.parseType(ss[start+1:end], &args)
2022-08-31 03:44:58 +00:00
}
2022-09-16 15:54:32 +00:00
st := s.String()
st = strings.TrimRight(st, "and ")
s.Reset()
s.WriteString(st)
s.WriteString(") and ")
2022-08-27 13:21:05 +00:00
}
}
2022-08-28 13:59:42 +00:00
ss := strings.TrimRight(s.String(), "and ")
if ss != "" {
s.Reset()
s.WriteString(" where ")
s.WriteString(ss)
ss = s.String()
}
return ss, args
2022-08-27 13:21:05 +00:00
}
2022-08-28 13:27:41 +00:00
func (w SqlBuilder) parseOrderBy() string {
s := strings.Builder{}
for _, ss := range w {
if len(ss) == 2 && ss[0] != "" && helper.IsContainInArr(ss[1], []string{"asc", "desc"}) {
2022-09-15 14:35:32 +00:00
s.WriteString(" ")
2022-08-28 13:27:41 +00:00
s.WriteString(ss[0])
2022-09-15 14:35:32 +00:00
s.WriteString(" ")
2022-08-28 13:27:41 +00:00
s.WriteString(ss[1])
s.WriteString(",")
}
}
2022-08-28 13:59:42 +00:00
ss := strings.TrimRight(s.String(), ",")
if ss != "" {
s.Reset()
s.WriteString(" order by ")
s.WriteString(ss)
ss = s.String()
}
return ss
2022-08-28 13:27:41 +00:00
}
2022-08-29 04:10:10 +00:00
func (w SqlBuilder) parseJoin() string {
s := strings.Builder{}
for _, ss := range w {
l := len(ss)
for j := 0; j < l; j++ {
s.WriteString(" ")
if (l == 4 && j == 3) || (l == 3 && j == 2) {
s.WriteString("on ")
}
s.WriteString(ss[j])
s.WriteString(" ")
}
}
return s.String()
}
2022-08-28 13:27:41 +00:00
2022-09-20 14:37:31 +00:00
func SimplePagination[T Model](where ParseWhere, fields, group string, page, pageSize int, order SqlBuilder, join SqlBuilder, in ...[]any) (r []T, total int, err error) {
2022-08-28 13:27:41 +00:00
var rr T
2022-09-08 02:54:37 +00:00
w, args := where.ParseWhere(in...)
2022-08-28 13:27:41 +00:00
n := struct {
N int `db:"n" json:"n"`
}{}
2022-09-16 09:59:17 +00:00
groupBy := ""
if group != "" {
g := strings.Builder{}
g.WriteString(" group by ")
g.WriteString(group)
groupBy = g.String()
}
2022-08-29 04:10:10 +00:00
j := join.parseJoin()
2022-09-16 09:59:17 +00:00
if group == "" {
tpx := "select count(*) n from %s %s %s limit 1"
sq := fmt.Sprintf(tpx, rr.Table(), j, w)
err = db.Db.Get(&n, sq, args...)
} else {
2022-09-16 10:06:50 +00:00
tpx := "select count(*) n from (select %s from %s %s %s %s ) %s"
rand.Seed(int64(time.Now().Nanosecond()))
sq := fmt.Sprintf(tpx, group, rr.Table(), j, w, groupBy, fmt.Sprintf("table%d", rand.Int()))
2022-09-16 09:59:17 +00:00
err = db.Db.Get(&n, sq, args...)
}
2022-08-28 13:27:41 +00:00
if err != nil {
return
}
if n.N == 0 {
return
}
total = n.N
offset := 0
if page > 1 {
offset = (page - 1) * pageSize
}
if offset >= total {
return
}
2022-09-16 09:59:17 +00:00
tp := "select %s from %s %s %s %s %s limit %d,%d"
sql := fmt.Sprintf(tp, fields, rr.Table(), j, w, groupBy, order.parseOrderBy(), offset, pageSize)
2022-08-28 13:27:41 +00:00
err = db.Db.Select(&r, sql, args...)
if err != nil {
return
}
return
}
2022-09-08 02:54:37 +00:00
func FindOneById[T Model](id int) (T, error) {
2022-08-27 13:21:05 +00:00
var r T
sql := fmt.Sprintf("select * from `%s` where `%s`=?", r.Table(), r.PrimaryKey())
err := db.Db.Get(&r, sql, id)
if err != nil {
return r, err
}
return r, nil
}
2022-09-20 14:37:31 +00:00
func FirstOne[T Model](where ParseWhere, fields string, order SqlBuilder, in ...[]any) (T, error) {
2022-08-27 13:21:05 +00:00
var r T
2022-09-08 02:54:37 +00:00
w, args := where.ParseWhere(in...)
2022-09-19 12:15:10 +00:00
tp := "select %s from %s %s %s"
sql := fmt.Sprintf(tp, fields, r.Table(), w, order.parseOrderBy())
2022-08-27 13:21:05 +00:00
err := db.Db.Get(&r, sql, args...)
if err != nil {
return r, err
}
return r, nil
}
2022-09-20 14:37:31 +00:00
func LastOne[T Model](where ParseWhere, fields string, in ...[]any) (T, error) {
2022-08-28 13:27:41 +00:00
var r T
2022-09-08 02:54:37 +00:00
w, args := where.ParseWhere(in...)
2022-08-28 13:59:42 +00:00
tp := "select %s from %s %s order by %s desc limit 1"
2022-08-28 13:27:41 +00:00
sql := fmt.Sprintf(tp, fields, r.Table(), w, r.PrimaryKey())
err := db.Db.Get(&r, sql, args...)
if err != nil {
return r, err
}
return r, nil
}
2022-09-20 14:37:31 +00:00
func SimpleFind[T Model](where ParseWhere, fields string, in ...[]any) ([]T, error) {
2022-08-27 13:21:05 +00:00
var r []T
var rr T
2022-09-08 02:54:37 +00:00
w, args := where.ParseWhere(in...)
2022-08-28 13:59:42 +00:00
tp := "select %s from %s %s"
2022-08-27 13:21:05 +00:00
sql := fmt.Sprintf(tp, fields, rr.Table(), w)
err := db.Db.Select(&r, sql, args...)
if err != nil {
return r, err
}
return r, nil
}
2022-09-20 14:37:31 +00:00
func Select[T Model](sql string, params ...any) ([]T, error) {
2022-08-27 13:21:05 +00:00
var r []T
var rr T
2022-09-05 01:49:14 +00:00
sql = strings.Replace(sql, "{table}", rr.Table(), -1)
2022-08-27 13:21:05 +00:00
err := db.Db.Select(&r, sql, params...)
if err != nil {
return r, err
}
return r, nil
}
2022-09-01 02:31:11 +00:00
2022-09-20 14:37:31 +00:00
func Find[T Model](where ParseWhere, fields, group string, order SqlBuilder, join SqlBuilder, limit int, in ...[]any) (r []T, err error) {
2022-09-01 02:31:11 +00:00
var rr T
2022-09-15 14:35:32 +00:00
w := ""
2022-09-20 14:37:31 +00:00
var args []any
2022-09-15 14:35:32 +00:00
if where != nil {
w, args = where.ParseWhere(in...)
}
2022-09-01 02:31:11 +00:00
j := join.parseJoin()
2022-09-14 13:30:59 +00:00
groupBy := ""
if group != "" {
g := strings.Builder{}
g.WriteString(" group by ")
g.WriteString(group)
groupBy = g.String()
}
tp := "select %s from %s %s %s %s %s %s"
2022-09-01 02:31:11 +00:00
l := ""
if limit > 0 {
l = fmt.Sprintf(" limit %d", limit)
}
2022-09-14 13:30:59 +00:00
sql := fmt.Sprintf(tp, fields, rr.Table(), j, w, groupBy, order.parseOrderBy(), l)
2022-09-01 02:31:11 +00:00
err = db.Db.Select(&r, sql, args...)
return
}
2022-09-20 14:37:31 +00:00
func Get[T Model](sql string, params ...any) (r T, err error) {
2022-09-05 01:49:14 +00:00
sql = strings.Replace(sql, "{table}", r.Table(), -1)
2022-09-01 02:31:11 +00:00
err = db.Db.Get(&r, sql, params...)
return
}