diff --git a/main.go b/main.go index 89a84f4..e93316d 100644 --- a/main.go +++ b/main.go @@ -20,6 +20,10 @@ func init() { if err != nil { panic(err) } + err = models.InitTerms() + if err != nil { + panic(err) + } } func main() { diff --git a/models/globalInit.go b/models/globalInit.go index 03483e0..bcc6960 100644 --- a/models/globalInit.go +++ b/models/globalInit.go @@ -1,5 +1,7 @@ package models +import "fmt" + var Options = make(map[string]string) func InitOptions() error { @@ -19,6 +21,16 @@ func InitOptions() error { return nil } -func InitTerms() { - //terms,err := WpTermsM.SimplePagination() +func InitTerms() (err error) { + 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 } diff --git a/models/model.go b/models/model.go index 5cf0876..33e9b6c 100644 --- a/models/model.go +++ b/models/model.go @@ -22,13 +22,27 @@ func (w SqlBuilder) parseWhere(in ...[]interface{}) (string, []interface{}) { for _, ss := range w { if len(ss) == 2 { s.WriteString("`") - s.WriteString(ss[0]) + 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("`=? and ") args = append(args, ss[1]) } if len(ss) >= 3 { s.WriteString("`") - s.WriteString(ss[0]) + 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("`") s.WriteString(ss[1]) 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) { var r []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...) if err != nil { 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) { - sql = strings.Replace(sql, "%table%", r.Table(), -1) + sql = strings.Replace(sql, "{table}", r.Table(), -1) err = db.Db.Get(&r, sql, params...) return }