查询换个写法
This commit is contained in:
parent
304bb96140
commit
3b6c5832ff
21
go.mod
21
go.mod
|
@ -3,13 +3,28 @@ module github/fthvgb1/wp-go
|
|||
go 1.18
|
||||
|
||||
require (
|
||||
github.com/gin-gonic/gin v1.8.1
|
||||
github.com/go-sql-driver/mysql v1.6.0
|
||||
github.com/jmoiron/sqlx v1.3.5
|
||||
gopkg.in/yaml.v2 v2.4.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/kr/pretty v0.3.0 // indirect
|
||||
github.com/rogpeppe/go-internal v1.8.0 // indirect
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
|
||||
github.com/gin-contrib/sse v0.1.0 // indirect
|
||||
github.com/go-playground/locales v0.14.0 // indirect
|
||||
github.com/go-playground/universal-translator v0.18.0 // indirect
|
||||
github.com/go-playground/validator/v10 v10.10.0 // indirect
|
||||
github.com/goccy/go-json v0.9.7 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/leodido/go-urn v1.2.1 // indirect
|
||||
github.com/mattn/go-isatty v0.0.14 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
|
||||
github.com/ugorji/go/codec v1.2.7 // indirect
|
||||
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 // indirect
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 // indirect
|
||||
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069 // indirect
|
||||
golang.org/x/text v0.3.6 // indirect
|
||||
google.golang.org/protobuf v1.28.0 // indirect
|
||||
)
|
||||
|
|
13
main.go
13
main.go
|
@ -1,9 +1,9 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github/fthvgb1/wp-go/db"
|
||||
"github/fthvgb1/wp-go/models"
|
||||
"github/fthvgb1/wp-go/route"
|
||||
"github/fthvgb1/wp-go/vars"
|
||||
)
|
||||
|
||||
|
@ -16,14 +16,15 @@ func init() {
|
|||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
err = models.InitOptions()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
T, t, err := models.WpPostsM.SimplePagination(nil, "wp_posts.ID,b.meta_id post_author", 4, 2, nil, models.SqlBuilder{{
|
||||
"left join", "wp_postmeta b", "b.post_id=wp_posts.ID",
|
||||
}})
|
||||
err := route.SetupRouter().Run(":8082")
|
||||
if err != nil {
|
||||
return
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println(T, t)
|
||||
}
|
||||
|
|
24
models/globalInit.go
Normal file
24
models/globalInit.go
Normal file
|
@ -0,0 +1,24 @@
|
|||
package models
|
||||
|
||||
var Options = make(map[string]string)
|
||||
|
||||
func InitOptions() error {
|
||||
ops, err := SimpleFind[WpOptions](SqlBuilder{{"autoload", "yes"}}, "option_name, option_value")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(ops) == 0 {
|
||||
ops, err = SimpleFind[WpOptions](nil, "option_name, option_value")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
for _, options := range ops {
|
||||
Options[options.OptionName] = options.OptionValue
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func InitTerms() {
|
||||
//terms,err := WpTermsM.SimplePagination()
|
||||
}
|
|
@ -8,22 +8,11 @@ import (
|
|||
"strings"
|
||||
)
|
||||
|
||||
type mod interface {
|
||||
type model interface {
|
||||
PrimaryKey() string
|
||||
Table() string
|
||||
}
|
||||
|
||||
type model[T mod] struct {
|
||||
}
|
||||
|
||||
func (m model[T]) PrimaryKey() string {
|
||||
return "id"
|
||||
}
|
||||
|
||||
func (m model[T]) Table() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
type SqlBuilder [][]string
|
||||
|
||||
func (w SqlBuilder) parseWhere(in ...[]interface{}) (string, []interface{}) {
|
||||
|
@ -115,24 +104,7 @@ func (w SqlBuilder) parseJoin() string {
|
|||
return s.String()
|
||||
}
|
||||
|
||||
func (m model[T]) Find(where SqlBuilder, fields string, order SqlBuilder, join SqlBuilder, limit int, in ...[]interface{}) (r []T, err error) {
|
||||
var rr T
|
||||
w, args := where.parseWhere(in...)
|
||||
j := join.parseJoin()
|
||||
tp := "select %s from %s %s %s %s %s"
|
||||
l := ""
|
||||
if limit > 0 {
|
||||
l = fmt.Sprintf(" limit %d", limit)
|
||||
}
|
||||
sql := fmt.Sprintf(tp, fields, rr.Table(), j, w, order.parseOrderBy(), l)
|
||||
err = db.Db.Select(&r, sql, args...)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (m model[T]) SimplePagination(where SqlBuilder, fields string, page, pageSize int, order SqlBuilder, join SqlBuilder, in ...[]interface{}) (r []T, total int, err error) {
|
||||
func SimplePagination[T model](where SqlBuilder, fields string, page, pageSize int, order SqlBuilder, join SqlBuilder, in ...[]interface{}) (r []T, total int, err error) {
|
||||
var rr T
|
||||
w, args := where.parseWhere(in...)
|
||||
n := struct {
|
||||
|
@ -165,7 +137,7 @@ func (m model[T]) SimplePagination(where SqlBuilder, fields string, page, pageSi
|
|||
return
|
||||
}
|
||||
|
||||
func (m model[T]) FindOneById(id int) (T, error) {
|
||||
func FindOneById[T model](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)
|
||||
|
@ -175,7 +147,7 @@ func (m model[T]) FindOneById(id int) (T, error) {
|
|||
return r, nil
|
||||
}
|
||||
|
||||
func (m model[T]) FirstOne(where SqlBuilder, fields string, in ...[]interface{}) (T, error) {
|
||||
func FirstOne[T model](where SqlBuilder, fields string, in ...[]interface{}) (T, error) {
|
||||
var r T
|
||||
w, args := where.parseWhere(in...)
|
||||
tp := "select %s from %s %s"
|
||||
|
@ -187,7 +159,7 @@ func (m model[T]) FirstOne(where SqlBuilder, fields string, in ...[]interface{})
|
|||
return r, nil
|
||||
}
|
||||
|
||||
func (m model[T]) LastOne(where SqlBuilder, fields string, in ...[]interface{}) (T, error) {
|
||||
func LastOne[T model](where SqlBuilder, fields string, in ...[]interface{}) (T, error) {
|
||||
var r T
|
||||
w, args := where.parseWhere(in...)
|
||||
tp := "select %s from %s %s order by %s desc limit 1"
|
||||
|
@ -199,7 +171,7 @@ func (m model[T]) LastOne(where SqlBuilder, fields string, in ...[]interface{})
|
|||
return r, nil
|
||||
}
|
||||
|
||||
func (m model[T]) SimpleFind(where SqlBuilder, fields string, in ...[]interface{}) ([]T, error) {
|
||||
func SimpleFind[T model](where SqlBuilder, fields string, in ...[]interface{}) ([]T, error) {
|
||||
var r []T
|
||||
var rr T
|
||||
w, args := where.parseWhere(in...)
|
||||
|
@ -212,17 +184,7 @@ func (m model[T]) SimpleFind(where SqlBuilder, fields string, in ...[]interface{
|
|||
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) {
|
||||
func Select[T model](sql string, params ...interface{}) ([]T, error) {
|
||||
var r []T
|
||||
var rr T
|
||||
sql = strings.Replace(sql, "%table%", rr.Table(), -1)
|
||||
|
@ -232,3 +194,23 @@ func (m model[T]) Select(sql string, params ...interface{}) ([]T, error) {
|
|||
}
|
||||
return r, nil
|
||||
}
|
||||
|
||||
func Find[T model](where SqlBuilder, fields string, order SqlBuilder, join SqlBuilder, limit int, in ...[]interface{}) (r []T, err error) {
|
||||
var rr T
|
||||
w, args := where.parseWhere(in...)
|
||||
j := join.parseJoin()
|
||||
tp := "select %s from %s %s %s %s %s"
|
||||
l := ""
|
||||
if limit > 0 {
|
||||
l = fmt.Sprintf(" limit %d", limit)
|
||||
}
|
||||
sql := fmt.Sprintf(tp, fields, rr.Table(), j, w, order.parseOrderBy(), l)
|
||||
err = db.Db.Select(&r, sql, args...)
|
||||
return
|
||||
}
|
||||
|
||||
func Get[T model](sql string, params ...interface{}) (r T, err error) {
|
||||
sql = strings.Replace(sql, "%table%", r.Table(), -1)
|
||||
err = db.Db.Get(&r, sql, params...)
|
||||
return
|
||||
}
|
||||
|
|
|
@ -2,10 +2,7 @@ package models
|
|||
|
||||
import "time"
|
||||
|
||||
var WpCommentsM = WpComments{}
|
||||
|
||||
type WpComments struct {
|
||||
model[WpComments]
|
||||
CommentId uint64 `gorm:"column:comment_ID" db:"comment_ID" json:"comment_ID" form:"comment_ID"`
|
||||
CommentPostId uint64 `gorm:"column:comment_post_ID" db:"comment_post_ID" json:"comment_post_ID" form:"comment_post_ID"`
|
||||
CommentAuthor string `gorm:"column:comment_author" db:"comment_author" json:"comment_author" form:"comment_author"`
|
||||
|
|
|
@ -1,9 +1,6 @@
|
|||
package models
|
||||
|
||||
var WpOptionsM = WpOptions{}
|
||||
|
||||
type WpOptions struct {
|
||||
model[WpOptions]
|
||||
OptionId uint64 `gorm:"column:option_id" db:"option_id" json:"option_id" form:"option_id"`
|
||||
OptionName string `gorm:"column:option_name" db:"option_name" json:"option_name" form:"option_name"`
|
||||
OptionValue string `gorm:"column:option_value" db:"option_value" json:"option_value" form:"option_value"`
|
||||
|
|
|
@ -2,10 +2,7 @@ package models
|
|||
|
||||
import "time"
|
||||
|
||||
var WpPostsM = WpPosts{}
|
||||
|
||||
type WpPosts struct {
|
||||
model[WpPosts]
|
||||
Id uint64 `gorm:"column:ID" db:"ID" json:"ID" form:"ID"`
|
||||
PostAuthor uint64 `gorm:"column:post_author" db:"post_author" json:"post_author" form:"post_author"`
|
||||
PostDate time.Time `gorm:"column:post_date" db:"post_date" json:"post_date" form:"post_date"`
|
||||
|
|
|
@ -1,9 +1,6 @@
|
|||
package models
|
||||
|
||||
var WpTermTaxonomyM = WpTermTaxonomy{}
|
||||
|
||||
type WpTermTaxonomy struct {
|
||||
model[WpTermTaxonomy]
|
||||
TermTaxonomyId uint64 `gorm:"column:term_taxonomy_id" db:"term_taxonomy_id" json:"term_taxonomy_id" form:"term_taxonomy_id"`
|
||||
TermId uint64 `gorm:"column:term_id" db:"term_id" json:"term_id" form:"term_id"`
|
||||
Taxonomy string `gorm:"column:taxonomy" db:"taxonomy" json:"taxonomy" form:"taxonomy"`
|
||||
|
|
|
@ -1,9 +1,6 @@
|
|||
package models
|
||||
|
||||
var WpTermsM = WpTerms{}
|
||||
|
||||
type WpTerms struct {
|
||||
model[WpTerms]
|
||||
TermId uint64 `gorm:"column:term_id" db:"term_id" json:"term_id" form:"term_id"`
|
||||
Name string `gorm:"column:name" db:"name" json:"name" form:"name"`
|
||||
Slug string `gorm:"column:slug" db:"slug" json:"slug" form:"slug"`
|
||||
|
|
12
route/actions.go
Normal file
12
route/actions.go
Normal file
|
@ -0,0 +1,12 @@
|
|||
package route
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func index(c *gin.Context) {
|
||||
c.HTML(http.StatusOK, "index.html", gin.H{
|
||||
"title": "Main website",
|
||||
})
|
||||
}
|
16
route/route.go
Normal file
16
route/route.go
Normal file
|
@ -0,0 +1,16 @@
|
|||
package route
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func SetupRouter() *gin.Engine {
|
||||
// Disable Console Color
|
||||
// gin.DisableConsoleColor()
|
||||
r := gin.Default()
|
||||
r.LoadHTMLGlob("templates/*")
|
||||
// Ping test
|
||||
r.GET("/", index)
|
||||
|
||||
return r
|
||||
}
|
12
templates/index.html
Normal file
12
templates/index.html
Normal file
|
@ -0,0 +1,12 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>{{ .title }}</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>
|
||||
{{ .title }}
|
||||
</h1>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue
Block a user