gorm_ex

package module
Version: v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 17, 2019 License: MIT Imports: 6 Imported by: 0

README

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
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DBExtension

type DBExtension struct {
	*gorm.DB
	// contains filtered or unexported fields
}

func NewDBWrapper

func NewDBWrapper(db *gorm.DB) *DBExtension

func (*DBExtension) Count

func (dw *DBExtension) Count(count *int, query interface{}) error

func (*DBExtension) CountBy

func (dw *DBExtension) CountBy(count *int, byField string, query interface{}) error

func (*DBExtension) ExecSql

func (dw *DBExtension) ExecSql(result interface{}, sql string, args ...interface{}) error

func (*DBExtension) GetFirstNRecords

func (dw *DBExtension) GetFirstNRecords(result interface{}, order string, limit int, query interface{}, args ...interface{}) error

func (*DBExtension) GetList

func (dw *DBExtension) GetList(result interface{}, query interface{}, args ...interface{}) error

func (*DBExtension) GetOne

func (dw *DBExtension) GetOne(result interface{}, query interface{}, args ...interface{}) (found bool, err error)

func (*DBExtension) GetOrderedList

func (dw *DBExtension) GetOrderedList(result interface{}, order string, query interface{}, args ...interface{}) error

func (*DBExtension) GetPageRangeList

func (dw *DBExtension) GetPageRangeList(result interface{}, order string, limit, offset int, query interface{}, args ...interface{}) error

func (*DBExtension) SaveOne

func (dw *DBExtension) SaveOne(value TableNameAble) error

Update All Fields

func (*DBExtension) SetDB

func (dw *DBExtension) SetDB(db *gorm.DB)

func (*DBExtension) SetLogger

func (dw *DBExtension) SetLogger(logger DBLogger)

func (*DBExtension) Update

func (dw *DBExtension) Update(attrs interface{}, query interface{}, args ...interface{}) error

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

type DBLogger

type DBLogger interface {
	LogInfoc(category, message string)
	LogWarnc(category string, err error, message string)
	LogErrorc(category string, err error, message string)
}

type TableNameAble

type TableNameAble interface {
	TableName() string
}

type UpdateAttrs

type UpdateAttrs map[string]interface{}

func NewUpdateAttrs

func NewUpdateAttrs(tableName string) UpdateAttrs

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL