Skip to content

Ksloveyuan/gorm-ex

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gorm-ex

Based on my best practice, extend gorm with some frequent used functions, e.g:

  • SaveOne()
  • Update()
  • GetOne()
  • GetList()
  • GetOrderedList()
  • GetPageRangeList()

Blogs

Gorm的使用心得和一些常用扩展(一)

Gorm的使用心得和一些常用扩展(二)

Examples

GetOne

query := Topic{TopicId: topicId}
result := Topic{}

if found, err := dw.GetOne(&result, query); !found {
	//not found
    if err != nil {
    	// has error
        return err
    }
}

GetList

query := InstInfo{
    Valid:1,
}
result:= make([]*InstInfo, 0)

if err := dw.GetList(&result, query); err != nil{
    // error handling
    return err
}
tids := []int{1, 2, 3, 4}
result := []*TuChart{}

if err :=dw.GetList(&result, "valid = 1 and tid in (?)", tids); err != nil{
    //error handling
    return err
}

GetOrderedList

It shares the same usage of GetList, except that there is one more order field.

query := InstInfo{
    Valid:1,
}
result:= make([]*InstInfo, 0)

if err := dw.GetOrderedList(&result,"create_time desc", query); err != nil{
    // error handling
    return err
}

GetPageRangeList

result := []*MpFeedInfo{}

if err :=dw.GetPageRangeList(&result, "update_time asc", limit, offset,
        "valid = 1 and update_time > ? and update_time < ?", startTime, endTime);err != nil{
    return err
}

SaveOne

Update All Fields, the object's primary_key, defined in gorm format definition, must have value.

instInfo  := InstInfo{
    InstId:req.InstId,
    Name:req.Name,
    Contact:req.Contact,
    Address:req.Address,
    Email:req.Email,
    Phone:req.Phone,
    OwnerId: req.OwnerId,
    Valid: req.Valid,
}

if err := dw.SaveOne(&instInfo); err != nil{
    // error handling
    return err
}

Update

Update partial Fields, if attrs is an object, it will ignore default value field; if attrs is map, it will ignore unchanged field.

if you intent to execute below sql:

update test.user set description = "A programmer" where id = 1

there are 4 ways to do it:

udateAttrs := User{Description: "A programmer"}
condition := User{Id: 1}
if err := dw.Update(&udateAttrs, condition); err != nil{
    // error handling
    return err
}
udateAttrs := User{Description: "A programmer"}
if err := dw.Update(&udateAttrs, "id = ?", 1); err != nil{
    // error handling
    return err
}
udateAttrs := NewUpdateAttrs("test.user")
udateAttrs["description"] = "A programmer"

if err := dw.Update(&udateAttrs, "id = ?", 1); err != nil{
    // error handling
    return err
}
udateAttrs := NewUpdateAttrs("test.user")
udateAttrs["description"] = "A programmer"
condition := User{Id: 1}

if err := dw.Update(&udateAttrs, condition); err != nil{
    // error handling
    return err
}

ExecuteSql

sql := "SELECT ta.*, tac.content FROM tucloud.tu_article ta inner join tucloud.tu_article_content tac on ta.article_id = tac.article_id and ta.valid = 1 and ta.article_id in (?)"
result := []*TuArticleWithContent{}

if err:= dw.ExecSql(&result, sql, tuArticleIds);err != nil{
    // error handling
    return err
}

About

Extend gorm with some frequently used functions

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages