Compare commits
3 Commits
2a68b73773
...
efeeb8b675
Author | SHA1 | Date | |
---|---|---|---|
|
efeeb8b675 | ||
|
4aa54b1ca7 | ||
|
2aacb682e1 |
|
@ -95,3 +95,34 @@ func TestBuilder_WriteString(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func BenchmarkBuilder_SprintfXX(b *testing.B) {
|
||||||
|
s := NewBuilder()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
s.Sprintf("%s %s %s", "a", "b", "c")
|
||||||
|
_ = s.String()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkSPrintfXX(b *testing.B) {
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
_ = fmt.Sprintf("%s %s %s", "a", "b", "c")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStrJoinXX(b *testing.B) {
|
||||||
|
s := strings.Builder{}
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
s.WriteString("a ")
|
||||||
|
s.WriteString("b ")
|
||||||
|
s.WriteString("c ")
|
||||||
|
_ = s.String()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func BenchmarkBuilderJoinXX(b *testing.B) {
|
||||||
|
s := NewBuilder()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
s.WriteString("a ", "b ", "c")
|
||||||
|
_ = s.String()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,17 +1,10 @@
|
||||||
package common
|
package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"github.com/fthvgb1/wp-go/helper/maps"
|
|
||||||
"github.com/fthvgb1/wp-go/helper/slice"
|
"github.com/fthvgb1/wp-go/helper/slice"
|
||||||
str "github.com/fthvgb1/wp-go/helper/strings"
|
|
||||||
"github.com/fthvgb1/wp-go/internal/cmd/reload"
|
"github.com/fthvgb1/wp-go/internal/cmd/reload"
|
||||||
"github.com/fthvgb1/wp-go/internal/pkg/config"
|
|
||||||
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
|
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
|
||||||
"github.com/fthvgb1/wp-go/internal/pkg/logs"
|
"github.com/fthvgb1/wp-go/internal/pkg/logs"
|
||||||
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
|
||||||
"github.com/fthvgb1/wp-go/internal/plugins"
|
|
||||||
"github.com/fthvgb1/wp-go/internal/wpconfig"
|
"github.com/fthvgb1/wp-go/internal/wpconfig"
|
||||||
"github.com/gin-contrib/sessions"
|
"github.com/gin-contrib/sessions"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
@ -30,6 +23,7 @@ type Handle struct {
|
||||||
Templ string
|
Templ string
|
||||||
Class []string
|
Class []string
|
||||||
ThemeMods wpconfig.ThemeMods
|
ThemeMods wpconfig.ThemeMods
|
||||||
|
Plugins []HandlePluginFn[*Handle]
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHandle(c *gin.Context, scene int, theme string) *Handle {
|
func NewHandle(c *gin.Context, scene int, theme string) *Handle {
|
||||||
|
@ -47,13 +41,6 @@ func NewHandle(c *gin.Context, scene int, theme string) *Handle {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handle) GetPassword() {
|
|
||||||
pw := h.Session.Get("post_password")
|
|
||||||
if pw != nil {
|
|
||||||
h.Password = pw.(string)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *Handle) AutoCal(name string, fn func() string) {
|
func (h *Handle) AutoCal(name string, fn func() string) {
|
||||||
v, ok := reload.GetStr(name)
|
v, ok := reload.GetStr(name)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
@ -63,63 +50,30 @@ func (h *Handle) AutoCal(name string, fn func() string) {
|
||||||
h.GinH[name] = v
|
h.GinH[name] = v
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *IndexHandle) ExecListPagePlugin(m map[string]Plugin[models.Posts, *Handle], calls ...func(*models.Posts)) {
|
|
||||||
|
|
||||||
pluginConf := config.GetConfig().ListPagePlugins
|
|
||||||
|
|
||||||
plugin := GetListPostPlugins(pluginConf, m)
|
|
||||||
|
|
||||||
i.GinH["posts"] = slice.Map(i.Posts, PluginFn[models.Posts, *Handle](plugin, i.Handle, Defaults(calls...)))
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func ListPostPlugins() map[string]Plugin[models.Posts, *Handle] {
|
|
||||||
return maps.Copy(pluginFns)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Defaults(call ...func(*models.Posts)) Fn[models.Posts] {
|
|
||||||
return func(posts models.Posts) models.Posts {
|
|
||||||
for _, fn := range call {
|
|
||||||
fn(&posts)
|
|
||||||
}
|
|
||||||
return posts
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func Default[T any](t T) T {
|
func Default[T any](t T) T {
|
||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
|
|
||||||
func ProjectTitle(t models.Posts) models.Posts {
|
func (h *Handle) GetPassword() {
|
||||||
if t.PostPassword != "" {
|
pw := h.Session.Get("post_password")
|
||||||
plugins.PasswordProjectTitle(&t)
|
if pw != nil {
|
||||||
|
h.Password = pw.(string)
|
||||||
}
|
}
|
||||||
return t
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetListPostPlugins(name []string, m map[string]Plugin[models.Posts, *Handle]) []Plugin[models.Posts, *Handle] {
|
func (h *Handle) ExecHandlePlugin() {
|
||||||
return slice.FilterAndMap(name, func(t string) (Plugin[models.Posts, *Handle], bool) {
|
if len(h.Plugins) > 0 {
|
||||||
v, ok := m[t]
|
HandlePlugin(h.Plugins, h)
|
||||||
if ok {
|
|
||||||
return v, true
|
|
||||||
}
|
}
|
||||||
logs.ErrPrintln(errors.New(str.Join("插件", t, "不存在")), "")
|
|
||||||
return nil, false
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func DigestsAndOthers(ctx context.Context, calls ...func(*models.Posts)) Fn[models.Posts] {
|
type HandleFn[T any] func(T)
|
||||||
return func(post models.Posts) models.Posts {
|
|
||||||
if post.PostExcerpt != "" {
|
type HandlePluginFn[T any] func(HandleFn[T], T) HandleFn[T]
|
||||||
plugins.PostExcerpt(&post)
|
|
||||||
} else {
|
// HandlePlugin 方便把功能写在其它包里
|
||||||
plugins.Digest(ctx, &post, config.GetConfig().DigestWordCount)
|
func HandlePlugin[T any](fns []HandlePluginFn[T], h T) HandleFn[T] {
|
||||||
}
|
return slice.ReverseReduce(fns, func(t HandlePluginFn[T], r HandleFn[T]) HandleFn[T] {
|
||||||
if len(calls) > 0 {
|
return t(r, h)
|
||||||
for _, call := range calls {
|
}, func(t T) {})
|
||||||
call(&post)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return post
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -117,5 +117,6 @@ func (d *DetailHandle) Details() {
|
||||||
d.C.HTML(d.Code, d.Templ, d.GinH)
|
d.C.HTML(d.Code, d.Templ, d.GinH)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
d.ExecHandlePlugin()
|
||||||
d.Render()
|
d.Render()
|
||||||
}
|
}
|
||||||
|
|
|
@ -130,5 +130,6 @@ func (i *IndexHandle) Indexs() {
|
||||||
i.C.HTML(i.Code, i.Templ, i.GinH)
|
i.C.HTML(i.Code, i.Templ, i.GinH)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
i.ExecHandlePlugin()
|
||||||
i.Render()
|
i.Render()
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,13 @@
|
||||||
package common
|
package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"github.com/fthvgb1/wp-go/helper/maps"
|
||||||
"github.com/fthvgb1/wp-go/helper/slice"
|
"github.com/fthvgb1/wp-go/helper/slice"
|
||||||
|
str "github.com/fthvgb1/wp-go/helper/strings"
|
||||||
"github.com/fthvgb1/wp-go/internal/pkg/config"
|
"github.com/fthvgb1/wp-go/internal/pkg/config"
|
||||||
|
"github.com/fthvgb1/wp-go/internal/pkg/logs"
|
||||||
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
||||||
"github.com/fthvgb1/wp-go/internal/plugins"
|
"github.com/fthvgb1/wp-go/internal/plugins"
|
||||||
)
|
)
|
||||||
|
@ -46,3 +51,60 @@ func Digest(next Fn[models.Posts], h *Handle, post models.Posts) models.Posts {
|
||||||
}
|
}
|
||||||
return post
|
return post
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ListPostPlugins() map[string]Plugin[models.Posts, *Handle] {
|
||||||
|
return maps.Copy(pluginFns)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ProjectTitle(t models.Posts) models.Posts {
|
||||||
|
if t.PostPassword != "" {
|
||||||
|
plugins.PasswordProjectTitle(&t)
|
||||||
|
}
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetListPostPlugins(name []string, m map[string]Plugin[models.Posts, *Handle]) []Plugin[models.Posts, *Handle] {
|
||||||
|
return slice.FilterAndMap(name, func(t string) (Plugin[models.Posts, *Handle], bool) {
|
||||||
|
v, ok := m[t]
|
||||||
|
if ok {
|
||||||
|
return v, true
|
||||||
|
}
|
||||||
|
logs.ErrPrintln(errors.New(str.Join("插件", t, "不存在")), "")
|
||||||
|
return nil, false
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func DigestsAndOthers(ctx context.Context, calls ...func(*models.Posts)) Fn[models.Posts] {
|
||||||
|
return func(post models.Posts) models.Posts {
|
||||||
|
if post.PostExcerpt != "" {
|
||||||
|
plugins.PostExcerpt(&post)
|
||||||
|
} else {
|
||||||
|
plugins.Digest(ctx, &post, config.GetConfig().DigestWordCount)
|
||||||
|
}
|
||||||
|
if len(calls) > 0 {
|
||||||
|
for _, call := range calls {
|
||||||
|
call(&post)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return post
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *IndexHandle) ExecListPagePlugin(m map[string]Plugin[models.Posts, *Handle], calls ...func(*models.Posts)) {
|
||||||
|
|
||||||
|
pluginConf := config.GetConfig().ListPagePlugins
|
||||||
|
|
||||||
|
plugin := GetListPostPlugins(pluginConf, m)
|
||||||
|
|
||||||
|
i.GinH["posts"] = slice.Map(i.Posts, PluginFn[models.Posts, *Handle](plugin, i.Handle, Defaults(calls...)))
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func Defaults(call ...func(*models.Posts)) Fn[models.Posts] {
|
||||||
|
return func(posts models.Posts) models.Posts {
|
||||||
|
for _, fn := range call {
|
||||||
|
fn(&posts)
|
||||||
|
}
|
||||||
|
return posts
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,20 +1,18 @@
|
||||||
package model
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"github.com/fthvgb1/wp-go/helper/slice"
|
"github.com/fthvgb1/wp-go/helper/slice"
|
||||||
|
str "github.com/fthvgb1/wp-go/helper/strings"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (w SqlBuilder) parseField(ss []string, s *strings.Builder) {
|
func (w SqlBuilder) parseField(ss []string, s *strings.Builder) {
|
||||||
if strings.Contains(ss[0], ".") && !strings.Contains(ss[0], "(") {
|
if strings.Contains(ss[0], ".") && !strings.Contains(ss[0], "(") {
|
||||||
s.WriteString("`")
|
x := slice.Map(strings.Split(ss[0], "."), func(t string) string {
|
||||||
sx := strings.Split(ss[0], ".")
|
return str.Join("`", t, "`")
|
||||||
s.WriteString(sx[0])
|
})
|
||||||
s.WriteString("`.`")
|
s.WriteString(strings.Join(x, "."))
|
||||||
s.WriteString(sx[1])
|
|
||||||
s.WriteString("`")
|
|
||||||
} else if !strings.Contains(ss[0], ".") && !strings.Contains(ss[0], "(") {
|
} else if !strings.Contains(ss[0], ".") && !strings.Contains(ss[0], "(") {
|
||||||
s.WriteString("`")
|
s.WriteString("`")
|
||||||
s.WriteString(ss[0])
|
s.WriteString(ss[0])
|
||||||
|
@ -27,13 +25,9 @@ func (w SqlBuilder) parseField(ss []string, s *strings.Builder) {
|
||||||
func (w SqlBuilder) parseIn(ss []string, s *strings.Builder, c *int, args *[]any, in *[][]any) (t bool) {
|
func (w SqlBuilder) parseIn(ss []string, s *strings.Builder, c *int, args *[]any, in *[][]any) (t bool) {
|
||||||
if slice.IsContained(ss[1], []string{"in", "not in"}) && len(*in) > 0 {
|
if slice.IsContained(ss[1], []string{"in", "not in"}) && len(*in) > 0 {
|
||||||
s.WriteString(" (")
|
s.WriteString(" (")
|
||||||
for _, p := range (*in)[*c] {
|
x := strings.TrimRight(strings.Repeat("?,", len((*in)[*c])), ",")
|
||||||
s.WriteString("?,")
|
s.WriteString(x)
|
||||||
*args = append(*args, p)
|
*args = append(*args, (*in)[*c]...)
|
||||||
}
|
|
||||||
sx := s.String()
|
|
||||||
s.Reset()
|
|
||||||
s.WriteString(strings.TrimRight(sx, ","))
|
|
||||||
s.WriteString(")")
|
s.WriteString(")")
|
||||||
*c++
|
*c++
|
||||||
t = true
|
t = true
|
||||||
|
@ -82,11 +76,15 @@ func (w SqlBuilder) ParseWhere(in *[][]any) (string, []any, error) {
|
||||||
args := make([]any, 0, len(w))
|
args := make([]any, 0, len(w))
|
||||||
c := 0
|
c := 0
|
||||||
for _, ss := range w {
|
for _, ss := range w {
|
||||||
if len(ss) == 2 {
|
switch len(ss) {
|
||||||
|
case 1:
|
||||||
|
s.WriteString(ss[0])
|
||||||
|
s.WriteString(" and ")
|
||||||
|
case 2:
|
||||||
w.parseField(ss, &s)
|
w.parseField(ss, &s)
|
||||||
s.WriteString("=? and ")
|
s.WriteString("=? and ")
|
||||||
args = append(args, ss[1])
|
args = append(args, ss[1])
|
||||||
} else if len(ss) >= 3 && len(ss) < 5 {
|
case 3, 4:
|
||||||
w.parseField(ss, &s)
|
w.parseField(ss, &s)
|
||||||
s.WriteString(ss[1])
|
s.WriteString(ss[1])
|
||||||
if w.parseIn(ss, &s, &c, &args, in) {
|
if w.parseIn(ss, &s, &c, &args, in) {
|
||||||
|
@ -98,7 +96,9 @@ func (w SqlBuilder) ParseWhere(in *[][]any) (string, []any, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", nil, err
|
return "", nil, err
|
||||||
}
|
}
|
||||||
} else if len(ss) >= 5 && len(ss)%5 == 0 {
|
}
|
||||||
|
|
||||||
|
if len(ss) >= 5 && len(ss)%5 == 0 {
|
||||||
j := len(ss) / 5
|
j := len(ss) / 5
|
||||||
for i := 0; i < j; i++ {
|
for i := 0; i < j; i++ {
|
||||||
start := i * 5
|
start := i * 5
|
||||||
|
@ -108,7 +108,9 @@ func (w SqlBuilder) ParseWhere(in *[][]any) (string, []any, error) {
|
||||||
st = strings.TrimRight(st, "and ")
|
st = strings.TrimRight(st, "and ")
|
||||||
s.Reset()
|
s.Reset()
|
||||||
s.WriteString(st)
|
s.WriteString(st)
|
||||||
s.WriteString(fmt.Sprintf(" %s ", ss[start]))
|
s.WriteString(" ")
|
||||||
|
s.WriteString(ss[start])
|
||||||
|
s.WriteString(" ")
|
||||||
}
|
}
|
||||||
if i == 0 {
|
if i == 0 {
|
||||||
s.WriteString("( ")
|
s.WriteString("( ")
|
||||||
|
@ -148,7 +150,7 @@ func (w SqlBuilder) ParseWhere(in *[][]any) (string, []any, error) {
|
||||||
func (w SqlBuilder) parseOrderBy() string {
|
func (w SqlBuilder) parseOrderBy() string {
|
||||||
s := strings.Builder{}
|
s := strings.Builder{}
|
||||||
for _, ss := range w {
|
for _, ss := range w {
|
||||||
if len(ss) == 2 && ss[0] != "" && slice.IsContained(ss[1], []string{"asc", "desc"}) {
|
if len(ss) == 2 && ss[0] != "" && slice.IsContained(strings.ToLower(ss[1]), []string{"asc", "desc"}) {
|
||||||
s.WriteString(" ")
|
s.WriteString(" ")
|
||||||
s.WriteString(ss[0])
|
s.WriteString(ss[0])
|
||||||
s.WriteString(" ")
|
s.WriteString(" ")
|
||||||
|
|
|
@ -510,7 +510,7 @@ func Test_findScanner(t *testing.T) {
|
||||||
func BenchmarkSqlxQueryXX(b *testing.B) {
|
func BenchmarkSqlxQueryXX(b *testing.B) {
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
var r []options
|
var r []options
|
||||||
err := ddb.Select(&r, "select * from wp_options where option_id<100")
|
err := ddb.Select(&r, "select * from wp_options where option_id<100 and option_id>50")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
@ -523,7 +523,7 @@ func BenchmarkScannerXX(b *testing.B) {
|
||||||
err := findScanner[options](glob, ctx, func(t options) {
|
err := findScanner[options](glob, ctx, func(t options) {
|
||||||
r = append(r, t)
|
r = append(r, t)
|
||||||
//fmt.Println(t)
|
//fmt.Println(t)
|
||||||
}, Conditions(Where(SqlBuilder{{"option_id", "<", "100", "int"}})))
|
}, Conditions(Where(SqlBuilder{{"option_id<100"}, {"option_id>50"}})))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
@ -532,7 +532,9 @@ func BenchmarkScannerXX(b *testing.B) {
|
||||||
|
|
||||||
func BenchmarkFindsXX(b *testing.B) {
|
func BenchmarkFindsXX(b *testing.B) {
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
_, err := finds[options](glob, ctx, Conditions(Where(SqlBuilder{{"option_id", "<", "100", "int"}})))
|
_, err := finds[options](glob, ctx, Conditions(
|
||||||
|
Where(SqlBuilder{{"option_id<100"}, {"option_id>50"}})),
|
||||||
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user