This commit is contained in:
xing 2022-09-05 09:49:14 +08:00
parent 3b6c5832ff
commit 819f24460e
3 changed files with 36 additions and 6 deletions

View File

@ -20,6 +20,10 @@ func init() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
err = models.InitTerms()
if err != nil {
panic(err)
}
} }
func main() { func main() {

View File

@ -1,5 +1,7 @@
package models package models
import "fmt"
var Options = make(map[string]string) var Options = make(map[string]string)
func InitOptions() error { func InitOptions() error {
@ -19,6 +21,16 @@ func InitOptions() error {
return nil return nil
} }
func InitTerms() { func InitTerms() (err error) {
//terms,err := WpTermsM.SimplePagination() var themes []interface{}
themes = append(themes, "wp_theme")
var name []interface{}
name = append(name, "twentyfifteen")
terms, err := Find[WpTerms](SqlBuilder{{
"tt.taxonomy", "in", "",
}, {"t.name", "in", ""}}, "t.term_id", nil, SqlBuilder{{
"t", "inner join", "wp_term_taxonomy tt", "t.term_id = tt.term_id",
}}, 1, themes, name)
fmt.Println(terms, err)
return
} }

View File

@ -22,13 +22,27 @@ func (w SqlBuilder) parseWhere(in ...[]interface{}) (string, []interface{}) {
for _, ss := range w { for _, ss := range w {
if len(ss) == 2 { if len(ss) == 2 {
s.WriteString("`") s.WriteString("`")
if strings.Contains(ss[0], ".") {
sx := strings.Split(ss[0], ".")
s.WriteString(sx[0])
s.WriteString("`.`")
s.WriteString(sx[1])
} else {
s.WriteString(ss[0]) s.WriteString(ss[0])
}
s.WriteString("`=? and ") s.WriteString("`=? and ")
args = append(args, ss[1]) args = append(args, ss[1])
} }
if len(ss) >= 3 { if len(ss) >= 3 {
s.WriteString("`") s.WriteString("`")
if strings.Contains(ss[0], ".") {
sx := strings.Split(ss[0], ".")
s.WriteString(sx[0])
s.WriteString("`.`")
s.WriteString(sx[1])
} else {
s.WriteString(ss[0]) s.WriteString(ss[0])
}
s.WriteString("`") s.WriteString("`")
s.WriteString(ss[1]) s.WriteString(ss[1])
if ss[1] == "in" && len(in) > 0 { if ss[1] == "in" && len(in) > 0 {
@ -187,7 +201,7 @@ func SimpleFind[T model](where SqlBuilder, fields string, in ...[]interface{}) (
func Select[T model](sql string, params ...interface{}) ([]T, error) { func Select[T model](sql string, params ...interface{}) ([]T, error) {
var r []T var r []T
var rr T var rr T
sql = strings.Replace(sql, "%table%", rr.Table(), -1) sql = strings.Replace(sql, "{table}", rr.Table(), -1)
err := db.Db.Select(&r, sql, params...) err := db.Db.Select(&r, sql, params...)
if err != nil { if err != nil {
return r, err return r, err
@ -210,7 +224,7 @@ func Find[T model](where SqlBuilder, fields string, order SqlBuilder, join SqlBu
} }
func Get[T model](sql string, params ...interface{}) (r T, err error) { func Get[T model](sql string, params ...interface{}) (r T, err error) {
sql = strings.Replace(sql, "%table%", r.Table(), -1) sql = strings.Replace(sql, "{table}", r.Table(), -1)
err = db.Db.Get(&r, sql, params...) err = db.Db.Get(&r, sql, params...)
return return
} }