From 9cb43ed13274d4efc26975dbfb00b0c87d4d3c3d Mon Sep 17 00:00:00 2001 From: xing Date: Sat, 5 Nov 2022 20:59:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 1 - helper/func.go | 20 +++++ helper/func_test.go | 48 ++++++++++ models/model.go | 207 ------------------------------------------ models/query.go | 214 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 282 insertions(+), 208 deletions(-) create mode 100644 models/query.go diff --git a/Dockerfile b/Dockerfile index 20df9f0..5de12cf 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,7 +2,6 @@ FROM golang:latest COPY ./ /go/src/wp-go WORKDIR /go/src/wp-go #ENV GOPROXY="https://goproxy.cn" -#ENV CGO=0 RUN go build -tags netgo FROM alpine:latest diff --git a/helper/func.go b/helper/func.go index 158df07..5f6c116 100644 --- a/helper/func.go +++ b/helper/func.go @@ -288,3 +288,23 @@ func SimpleSort[T any](arr []T, fn func(i, j T) bool) { sort.Sort(slice) return } + +func Min[T IntNumber | ~float64 | ~float32](a ...T) T { + min := a[0] + for _, t := range a { + if min > t { + min = t + } + } + return min +} + +func Max[T IntNumber | ~float64 | ~float32](a ...T) T { + max := a[0] + for _, t := range a { + if max < t { + max = t + } + } + return max +} diff --git a/helper/func_test.go b/helper/func_test.go index fd0e676..0649b66 100644 --- a/helper/func_test.go +++ b/helper/func_test.go @@ -660,3 +660,51 @@ func TestSimpleSort(t *testing.T) { }) } } + +func TestMin(t *testing.T) { + type args struct { + a []int + } + tests := []struct { + name string + args args + want int + }{ + { + name: "t1", + args: args{a: []int{1, 2, 3}}, + want: 1, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := Min(tt.args.a...); got != tt.want { + t.Errorf("Min() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestMax(t *testing.T) { + type args struct { + a []int + } + tests := []struct { + name string + args args + want int + }{ + { + name: "t1", + args: args{a: []int{1, 2, 3}}, + want: 3, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := Max(tt.args.a...); got != tt.want { + t.Errorf("Max() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/models/model.go b/models/model.go index 8cc534b..28a7f50 100644 --- a/models/model.go +++ b/models/model.go @@ -2,12 +2,9 @@ package models import ( "fmt" - "github/fthvgb1/wp-go/db" "github/fthvgb1/wp-go/helper" - "math/rand" "strconv" "strings" - "time" ) var _ ParseWhere = SqlBuilder{} @@ -180,207 +177,3 @@ func (w SqlBuilder) parseJoin() string { } return s.String() } - -func SimplePagination[T Model](where ParseWhere, fields, group string, page, pageSize int, order SqlBuilder, join SqlBuilder, having SqlBuilder, in ...[]any) (r []T, total int, err error) { - var rr T - var w string - var args []any - if where != nil { - w, args, err = where.ParseWhere(&in) - if err != nil { - return r, total, err - } - } - - h := "" - if having != nil { - hh, arg, err := having.ParseWhere(&in) - if err != nil { - return r, total, err - } - args = append(args, arg...) - h = strings.Replace(hh, " where", " having", 1) - } - - n := struct { - N int `db:"n" json:"n"` - }{} - groupBy := "" - if group != "" { - g := strings.Builder{} - g.WriteString(" group by ") - g.WriteString(group) - groupBy = g.String() - } - if having != nil { - tm := map[string]struct{}{} - for _, s := range strings.Split(group, ",") { - tm[s] = struct{}{} - } - for _, ss := range having { - if _, ok := tm[ss[0]]; !ok { - group = fmt.Sprintf("%s,%s", group, ss[0]) - } - } - group = strings.Trim(group, ",") - } - j := join.parseJoin() - 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 { - tpx := "select count(*) n from (select %s from %s %s %s %s %s ) %s" - rand.Seed(int64(time.Now().Nanosecond())) - sq := fmt.Sprintf(tpx, group, rr.Table(), j, w, groupBy, h, fmt.Sprintf("table%d", rand.Int())) - 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 - } - tp := "select %s from %s %s %s %s %s %s limit %d,%d" - sql := fmt.Sprintf(tp, fields, rr.Table(), j, w, groupBy, h, order.parseOrderBy(), offset, pageSize) - err = db.Db.Select(&r, sql, args...) - if err != nil { - return - } - return -} - -func FindOneById[T Model, I helper.IntNumber](id I) (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 -} - -func FirstOne[T Model](where ParseWhere, fields string, order SqlBuilder, in ...[]any) (T, error) { - var r T - var w string - var args []any - var err error - if where != nil { - w, args, err = where.ParseWhere(&in) - if err != nil { - return r, err - } - } - tp := "select %s from %s %s %s" - sql := fmt.Sprintf(tp, fields, r.Table(), w, order.parseOrderBy()) - err = db.Db.Get(&r, sql, args...) - if err != nil { - return r, err - } - return r, nil -} - -func LastOne[T Model](where ParseWhere, fields string, in ...[]any) (T, error) { - var r T - var w string - var args []any - var err error - if where != nil { - w, args, err = where.ParseWhere(&in) - if err != nil { - return r, err - } - } - tp := "select %s from %s %s order by %s desc limit 1" - 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 SimpleFind[T Model](where ParseWhere, fields string, in ...[]any) ([]T, error) { - var r []T - var rr T - var err error - var args []any - var w string - if where != nil { - w, args, err = where.ParseWhere(&in) - if err != nil { - return r, err - } - } - tp := "select %s from %s %s" - 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 Select[T Model](sql string, params ...any) ([]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 -} - -func Find[T Model](where ParseWhere, fields, group string, order SqlBuilder, join SqlBuilder, having SqlBuilder, limit int, in ...[]any) (r []T, err error) { - var rr T - w := "" - var args []any - if where != nil { - w, args, err = where.ParseWhere(&in) - if err != nil { - return r, err - } - } - h := "" - if having != nil { - hh, arg, err := having.ParseWhere(&in) - if err != nil { - return r, err - } - args = append(args, arg...) - h = strings.Replace(hh, " where", " having", 1) - } - - j := join.parseJoin() - 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 %s" - l := "" - if limit > 0 { - l = fmt.Sprintf(" limit %d", limit) - } - sql := fmt.Sprintf(tp, fields, rr.Table(), j, w, groupBy, h, order.parseOrderBy(), l) - err = db.Db.Select(&r, sql, args...) - return -} - -func Get[T Model](sql string, params ...any) (r T, err error) { - sql = strings.Replace(sql, "{table}", r.Table(), -1) - err = db.Db.Get(&r, sql, params...) - return -} diff --git a/models/query.go b/models/query.go new file mode 100644 index 0000000..3cc65aa --- /dev/null +++ b/models/query.go @@ -0,0 +1,214 @@ +package models + +import ( + "fmt" + "github/fthvgb1/wp-go/db" + "github/fthvgb1/wp-go/helper" + "math/rand" + "strings" + "time" +) + +func SimplePagination[T Model](where ParseWhere, fields, group string, page, pageSize int, order SqlBuilder, join SqlBuilder, having SqlBuilder, in ...[]any) (r []T, total int, err error) { + var rr T + var w string + var args []any + if where != nil { + w, args, err = where.ParseWhere(&in) + if err != nil { + return r, total, err + } + } + + h := "" + if having != nil { + hh, arg, err := having.ParseWhere(&in) + if err != nil { + return r, total, err + } + args = append(args, arg...) + h = strings.Replace(hh, " where", " having", 1) + } + + n := struct { + N int `db:"n" json:"n"` + }{} + groupBy := "" + if group != "" { + g := strings.Builder{} + g.WriteString(" group by ") + g.WriteString(group) + groupBy = g.String() + } + if having != nil { + tm := map[string]struct{}{} + for _, s := range strings.Split(group, ",") { + tm[s] = struct{}{} + } + for _, ss := range having { + if _, ok := tm[ss[0]]; !ok { + group = fmt.Sprintf("%s,%s", group, ss[0]) + } + } + group = strings.Trim(group, ",") + } + j := join.parseJoin() + 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 { + tpx := "select count(*) n from (select %s from %s %s %s %s %s ) %s" + rand.Seed(int64(time.Now().Nanosecond())) + sq := fmt.Sprintf(tpx, group, rr.Table(), j, w, groupBy, h, fmt.Sprintf("table%d", rand.Int())) + 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 + } + tp := "select %s from %s %s %s %s %s %s limit %d,%d" + sql := fmt.Sprintf(tp, fields, rr.Table(), j, w, groupBy, h, order.parseOrderBy(), offset, pageSize) + err = db.Db.Select(&r, sql, args...) + if err != nil { + return + } + return +} + +func FindOneById[T Model, I helper.IntNumber](id I) (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 +} + +func FirstOne[T Model](where ParseWhere, fields string, order SqlBuilder, in ...[]any) (T, error) { + var r T + var w string + var args []any + var err error + if where != nil { + w, args, err = where.ParseWhere(&in) + if err != nil { + return r, err + } + } + tp := "select %s from %s %s %s" + sql := fmt.Sprintf(tp, fields, r.Table(), w, order.parseOrderBy()) + err = db.Db.Get(&r, sql, args...) + if err != nil { + return r, err + } + return r, nil +} + +func LastOne[T Model](where ParseWhere, fields string, in ...[]any) (T, error) { + var r T + var w string + var args []any + var err error + if where != nil { + w, args, err = where.ParseWhere(&in) + if err != nil { + return r, err + } + } + tp := "select %s from %s %s order by %s desc limit 1" + 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 SimpleFind[T Model](where ParseWhere, fields string, in ...[]any) ([]T, error) { + var r []T + var rr T + var err error + var args []any + var w string + if where != nil { + w, args, err = where.ParseWhere(&in) + if err != nil { + return r, err + } + } + tp := "select %s from %s %s" + 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 Select[T Model](sql string, params ...any) ([]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 +} + +func Find[T Model](where ParseWhere, fields, group string, order SqlBuilder, join SqlBuilder, having SqlBuilder, limit int, in ...[]any) (r []T, err error) { + var rr T + w := "" + var args []any + if where != nil { + w, args, err = where.ParseWhere(&in) + if err != nil { + return r, err + } + } + h := "" + if having != nil { + hh, arg, err := having.ParseWhere(&in) + if err != nil { + return r, err + } + args = append(args, arg...) + h = strings.Replace(hh, " where", " having", 1) + } + + j := join.parseJoin() + 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 %s" + l := "" + if limit > 0 { + l = fmt.Sprintf(" limit %d", limit) + } + sql := fmt.Sprintf(tp, fields, rr.Table(), j, w, groupBy, h, order.parseOrderBy(), l) + err = db.Db.Select(&r, sql, args...) + return +} + +func Get[T Model](sql string, params ...any) (r T, err error) { + sql = strings.Replace(sql, "{table}", r.Table(), -1) + err = db.Db.Get(&r, sql, params...) + return +}