From 17d28d79138e172871d7e3bd50f452026db4c9d0 Mon Sep 17 00:00:00 2001 From: yxh Date: Fri, 27 Mar 2020 23:37:22 +0800 Subject: [PATCH] =?UTF-8?q?gf=E6=A1=86=E6=9E=B6=E5=8D=87=E7=BA=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/model/admin/cms_news/cms_news.go | 2 +- app/model/admin/cms_news/cms_news_entity.go | 5 + app/model/admin/cms_news/cms_news_model.go | 143 ++++++++++---------- go.mod | 2 +- go.sum | 4 +- test/demo2_test.go | 10 +- 6 files changed, 88 insertions(+), 78 deletions(-) diff --git a/app/model/admin/cms_news/cms_news.go b/app/model/admin/cms_news/cms_news.go index 2d92968..0f44508 100644 --- a/app/model/admin/cms_news/cms_news.go +++ b/app/model/admin/cms_news/cms_news.go @@ -223,7 +223,7 @@ func ListByPage(req *ReqListSearchParams) (total, page int, list gdb.Result, err //通过文章id获取文章信息 func GetById(id int) (news *Entity, err error) { - news, err = Model.FindOne("id", id) + news, err = Model.FindOne(id) if err != nil { g.Log().Error(err) } diff --git a/app/model/admin/cms_news/cms_news_entity.go b/app/model/admin/cms_news/cms_news_entity.go index 5deeee0..7f112a4 100644 --- a/app/model/admin/cms_news/cms_news_entity.go +++ b/app/model/admin/cms_news/cms_news_entity.go @@ -45,6 +45,11 @@ func (r *Entity) Insert() (result sql.Result, err error) { return Model.Data(r).Insert() } +// InsertIgnore does "INSERT IGNORE INTO ..." statement for inserting current object into table. +func (r *Entity) InsertIgnore() (result sql.Result, err error) { + return Model.Data(r).InsertIgnore() +} + // Replace does "REPLACE...INTO..." statement for inserting current object into table. // If there's already another same record in the table (it checks using primary key or unique index), // it deletes it and insert this one. diff --git a/app/model/admin/cms_news/cms_news_model.go b/app/model/admin/cms_news/cms_news_model.go index 977a1a9..ff7c4bd 100644 --- a/app/model/admin/cms_news/cms_news_model.go +++ b/app/model/admin/cms_news/cms_news_model.go @@ -8,12 +8,13 @@ import ( "database/sql" "github.com/gogf/gf/database/gdb" "github.com/gogf/gf/frame/g" + "github.com/gogf/gf/frame/gmvc" "time" ) // arModel is a active record design model for table cms_news operations. type arModel struct { - M *gdb.Model + gmvc.M } var ( @@ -21,6 +22,52 @@ var ( Table = "cms_news" // Model is the model object of cms_news. Model = &arModel{g.DB("default").Table(Table).Safe()} + // Columns defines and stores column names for table cms_news. + Columns = struct { + Id string // + NewsFormat string // 内容格式;1:html;2:md + UserId string // 发表者用户id + NewsStatus string // 状态;1:已发布;0:未发布; + IsTop string // 是否置顶;1:置顶;0:不置顶 + Recommended string // 是否推荐;1:推荐;0:不推荐 + NewsHits string // 查看数 + NewsLike string // 点赞数 + CreateTime string // 创建时间 + UpdateTime string // 更新时间 + PublishedTime string // 发布时间 + DeleteTime string // 删除时间 + NewsTitle string // post标题 + NewsKeywords string // seo keywords + NewsExcerpt string // post摘要 + NewsSource string // 转载文章的来源 + NewsContent string // 文章内容 + Thumbnail string // 缩略图 + IsJump string // 是否跳转地址 + JumpUrl string // 跳转地址 + IsPushed string // 是否已推送 + }{ + Id: "id", + NewsFormat: "news_format", + UserId: "user_id", + NewsStatus: "news_status", + IsTop: "is_top", + Recommended: "recommended", + NewsHits: "news_hits", + NewsLike: "news_like", + CreateTime: "create_time", + UpdateTime: "update_time", + PublishedTime: "published_time", + DeleteTime: "delete_time", + NewsTitle: "news_title", + NewsKeywords: "news_keywords", + NewsExcerpt: "news_excerpt", + NewsSource: "news_source", + NewsContent: "news_content", + Thumbnail: "thumbnail", + IsJump: "is_jump", + JumpUrl: "jump_url", + IsPushed: "is_pushed", + } ) // FindOne is a convenience method for Model.FindOne. @@ -41,6 +88,12 @@ func FindValue(fieldsAndWhere ...interface{}) (gdb.Value, error) { return Model.FindValue(fieldsAndWhere...) } +// FindArray is a convenience method for Model.FindArray. +// See Model.FindArray. +func FindArray(fieldsAndWhere ...interface{}) ([]gdb.Value, error) { + return Model.FindArray(fieldsAndWhere...) +} + // FindCount is a convenience method for Model.FindCount. // See Model.FindCount. func FindCount(where ...interface{}) (int, error) { @@ -52,6 +105,11 @@ func Insert(data ...interface{}) (result sql.Result, err error) { return Model.Insert(data...) } +// InsertIgnore is a convenience method for Model.InsertIgnore. +func InsertIgnore(data ...interface{}) (result sql.Result, err error) { + return Model.InsertIgnore(data...) +} + // Replace is a convenience method for Model.Replace. func Replace(data ...interface{}) (result sql.Result, err error) { return Model.Replace(data...) @@ -207,8 +265,8 @@ func (m *arModel) Batch(batch int) *arModel { // control the cache like changing the or clearing the cache with specified . // // Note that, the cache feature is disabled if the model is operating on a transaction. -func (m *arModel) Cache(expire time.Duration, name ...string) *arModel { - return &arModel{m.M.Cache(expire, name...)} +func (m *arModel) Cache(duration time.Duration, name ...string) *arModel { + return &arModel{m.M.Cache(duration, name...)} } // Data sets the operation data for the model. @@ -222,53 +280,6 @@ func (m *arModel) Data(data ...interface{}) *arModel { return &arModel{m.M.Data(data...)} } -// Insert does "INSERT INTO ..." statement for the model. -// The optional parameter is the same as the parameter of Model.Data function, -// see Model.Data. -func (m *arModel) Insert(data ...interface{}) (result sql.Result, err error) { - return m.M.Insert(data...) -} - -// Replace does "REPLACE INTO ..." statement for the model. -// The optional parameter is the same as the parameter of Model.Data function, -// see Model.Data. -func (m *arModel) Replace(data ...interface{}) (result sql.Result, err error) { - return m.M.Replace(data...) -} - -// Save does "INSERT INTO ... ON DUPLICATE KEY UPDATE..." statement for the model. -// It updates the record if there's primary or unique index in the saving data, -// or else it inserts a new record into the table. -// -// The optional parameter is the same as the parameter of Model.Data function, -// see Model.Data. -func (m *arModel) Save(data ...interface{}) (result sql.Result, err error) { - return m.M.Save(data...) -} - -// Update does "UPDATE ... " statement for the model. -// -// If the optional parameter is given, the dataAndWhere[0] is the updated -// data field, and dataAndWhere[1:] is treated as where condition fields. -// Also see Model.Data and Model.Where functions. -func (m *arModel) Update(dataAndWhere ...interface{}) (result sql.Result, err error) { - return m.M.Update(dataAndWhere...) -} - -// Delete does "DELETE FROM ... " statement for the model. -// The optional parameter is the same as the parameter of Model.Where function, -// see Model.Where. -func (m *arModel) Delete(where ...interface{}) (result sql.Result, err error) { - return m.M.Delete(where...) -} - -// Count does "SELECT COUNT(x) FROM ..." statement for the model. -// The optional parameter is the same as the parameter of Model.Where function, -// see Model.Where. -func (m *arModel) Count(where ...interface{}) (int, error) { - return m.M.Count(where...) -} - // All does "SELECT FROM ..." statement for the model. // It retrieves the records from table and returns the result as []*Entity. // It returns nil if there's no record retrieved with the given conditions from table. @@ -304,16 +315,6 @@ func (m *arModel) One(where ...interface{}) (*Entity, error) { return entity, nil } -// Value retrieves a specified record value from table and returns the result as interface type. -// It returns nil if there's no record found with the given conditions from table. -// -// If the optional parameter is given, the fieldsAndWhere[0] is the selected fields -// and fieldsAndWhere[1:] is treated as where condition fields. -// Also see Model.Fields and Model.Where functions. -func (m *arModel) Value(fieldsAndWhere ...interface{}) (gdb.Value, error) { - return m.M.Value(fieldsAndWhere...) -} - // FindOne retrieves and returns a single Record by Model.WherePri and Model.One. // Also see Model.WherePri and Model.One. func (m *arModel) FindOne(where ...interface{}) (*Entity, error) { @@ -342,18 +343,6 @@ func (m *arModel) FindAll(where ...interface{}) ([]*Entity, error) { return entities, nil } -// FindValue retrieves and returns single field value by Model.WherePri and Model.Value. -// Also see Model.WherePri and Model.Value. -func (m *arModel) FindValue(fieldsAndWhere ...interface{}) (gdb.Value, error) { - return m.M.FindValue(fieldsAndWhere...) -} - -// FindCount retrieves and returns the record number by Model.WherePri and Model.Count. -// Also see Model.WherePri and Model.Count. -func (m *arModel) FindCount(where ...interface{}) (int, error) { - return m.M.FindCount(where...) -} - // Chunk iterates the table with given size and callback function. func (m *arModel) Chunk(limit int, callback func(entities []*Entity, err error) bool) { m.M.Chunk(limit, func(result gdb.Result, err error) bool { @@ -365,3 +354,13 @@ func (m *arModel) Chunk(limit int, callback func(entities []*Entity, err error) return callback(entities, err) }) } + +// LockUpdate sets the lock for update for current operation. +func (m *arModel) LockUpdate() *arModel { + return &arModel{m.M.LockUpdate()} +} + +// LockShared sets the lock in share mode for current operation. +func (m *arModel) LockShared() *arModel { + return &arModel{m.M.LockShared()} +} diff --git a/go.mod b/go.mod index ef94ee1..a76b730 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ require ( github.com/casbin/casbin/v2 v2.1.2 github.com/go-ole/go-ole v1.2.4 // indirect github.com/goflyfox/gtoken v1.3.12 - github.com/gogf/gf v1.11.6 + github.com/gogf/gf v1.11.7 github.com/mojocn/base64Captcha v1.3.0 github.com/mssola/user_agent v0.5.1 github.com/shirou/gopsutil v2.20.2+incompatible diff --git a/go.sum b/go.sum index 3843a9e..9a9fe57 100644 --- a/go.sum +++ b/go.sum @@ -26,8 +26,8 @@ github.com/goflyfox/gtoken v1.3.12 h1:ewet3ZzkfBIuOKkJ2T9uErheDAu/TxnyrmMmEaof2F github.com/goflyfox/gtoken v1.3.12/go.mod h1:KPGDYrhvNzcfqFJz4/rE6x0SyURAuAxfRNhwMxGbFxk= github.com/gogf/gf v1.11.5 h1:e6HB9x1QZ6EFD3eEWXDCsFeVs7KxNtmWQRVNh2c0+bQ= github.com/gogf/gf v1.11.5/go.mod h1:iuHZkqyEfxFtpwRYboAU7409O/sfdy79YTpY8si332I= -github.com/gogf/gf v1.11.6 h1:UfYtAQkhXmZtYFMGant1Tdh8azZrWUJrYTGAyS3FloA= -github.com/gogf/gf v1.11.6/go.mod h1:q6WSyX5jyJbUki7l66lpfsXvf2GeDEOgXNRz9Nb61qo= +github.com/gogf/gf v1.11.7 h1:rHt9g7OP3NPkkgQpN73zA9TJ4ItlQGworY1Jxb6mgQE= +github.com/gogf/gf v1.11.7/go.mod h1:q6WSyX5jyJbUki7l66lpfsXvf2GeDEOgXNRz9Nb61qo= github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g= github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k= github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= diff --git a/test/demo2_test.go b/test/demo2_test.go index bb8ae90..dac8337 100644 --- a/test/demo2_test.go +++ b/test/demo2_test.go @@ -1,7 +1,7 @@ package test import ( - "github.com/gogf/gf/database/gdb" + "fmt" "testing" ) @@ -10,5 +10,11 @@ func TestDemo2(t *testing.T) { } func test21(t *testing.T) { - gdb.Instance() + c := "AB" + r := []rune(c) + num := 0 + for k, v := range r { + num += int(v) - 65 + k*26 + } + fmt.Println(num) }