wp-go/models/model.go

194 lines
4.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-08-27 13:21:05 +00:00
"strings"
)
type mod interface {
PrimaryKey() string
Table() string
}
type model[T mod] struct {
}
2022-08-28 14:03:52 +00:00
func (m model[T]) PrimaryKey() string {
return "id"
}
func (m model[T]) Table() string {
return ""
}
2022-08-28 13:27:41 +00:00
type SqlBuilder [][]string
2022-08-27 13:21:05 +00:00
2022-08-28 13:27:41 +00:00
func (w SqlBuilder) parseWhere() (string, []interface{}) {
2022-08-27 13:21:05 +00:00
var s strings.Builder
args := make([]interface{}, 0, len(w))
for _, ss := range w {
if len(ss) == 2 {
s.WriteString("`")
s.WriteString(ss[0])
s.WriteString("`=? and ")
args = append(args, ss[1])
}
if len(ss) == 3 {
s.WriteString("`")
s.WriteString(ss[0])
s.WriteString("`")
s.WriteString(ss[1])
2022-08-28 13:27:41 +00:00
s.WriteString(" ? and ")
2022-08-27 13:21:05 +00:00
args = append(args, ss[2])
}
}
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"}) {
s.WriteString(" `")
s.WriteString(ss[0])
s.WriteString("` ")
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-08-29 04:10:10 +00:00
func (m model[T]) SimplePagination(where SqlBuilder, fields string, page, pageSize int, order SqlBuilder, join SqlBuilder) (r []T, total int, err error) {
2022-08-28 13:27:41 +00:00
var rr T
w, args := where.parseWhere()
n := struct {
N int `db:"n" json:"n"`
}{}
2022-08-29 04:10:10 +00:00
j := join.parseJoin()
tpx := "select count(*) n from %s %s %s limit 1"
sq := fmt.Sprintf(tpx, rr.Table(), j, w)
2022-08-28 13:27:41 +00:00
err = db.Db.Get(&n, sq, args...)
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-08-29 04:10:10 +00:00
tp := "select %s from %s %s %s %s limit %d,%d"
sql := fmt.Sprintf(tp, fields, rr.Table(), j, w, 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-08-27 13:21:05 +00:00
func (m model[T]) FindOneById(id int) (T, error) {
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-08-28 13:27:41 +00:00
func (m model[T]) FirstOne(where SqlBuilder, fields string) (T, error) {
2022-08-27 13:21:05 +00:00
var r T
w, args := where.parseWhere()
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, r.Table(), w)
err := db.Db.Get(&r, sql, args...)
if err != nil {
return r, err
}
return r, nil
}
2022-08-28 13:27:41 +00:00
func (m model[T]) LastOne(where SqlBuilder, fields string) (T, error) {
var r T
w, args := where.parseWhere()
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
}
func (m model[T]) FindMany(where SqlBuilder, fields string) ([]T, error) {
2022-08-27 13:21:05 +00:00
var r []T
var rr T
w, args := where.parseWhere()
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
}
func (m model[T]) Get(sql string, params ...interface{}) (T, error) {
var r T
sql = strings.Replace(sql, "%table%", r.Table(), -1)
err := db.Db.Get(&r, sql, params...)
if err != nil {
return r, err
}
return r, nil
}
func (m model[T]) Select(sql string, params ...interface{}) ([]T, error) {
var r []T
var rr T
sql = strings.Replace(sql, "%table%", rr.Table(), -1)
err := db.Db.Select(&r, sql, params...)
if err != nil {
return r, err
}
return r, nil
}