gomodel, 轻量级快速orm的库帮助与数据库交互

分享于 

5分钟阅读

GitHub

  繁體 雙語
A lightweight, fast, orm-like library helps interactive with database
  • 源代码名称:gomodel
  • 源代码网址:http://www.github.com/cosiner/gomodel
  • gomodel源代码文档
  • gomodel源代码下载
  • Git URL:
    git://www.github.com/cosiner/gomodel.git
    Git Clone代码到本地:
    git clone http://www.github.com/cosiner/gomodel
    Subversion代码到本地:
    $ svn co --depth empty http://www.github.com/cosiner/gomodel
    Checked out revision 1.
    $ cd repo
    $ svn up trunk
    
    gomodel wercker statusGoDoc

    gomodel提供了另一种与数据库交互的方法。
    代替反射,使用,代表 CRUD。sql/,缓存的字段,并生成模型代码,高性能。

    安装
    
    
    
    
    $ go get github.com/cosiner/gomodel
    
    
    $ cd/path/of/gomodel/cmd/gomodel
    
    
    $ go install # it will install the gomodel binary file to your $GOPATH/bin
    
    
    $ gomodel -cp # copy model.tmpl to default path $HOME/.config/go/model.tmpl
    
    
     # or just put it to your model package, gomodel will search it first 
    
    
    $ # if need customed template, just copy model.tmpl to your models directory
    
    
    
    

    gomodel工具和结构 SQL转换。

    这里有一个详细的示例用户跟随。

    用户
    
    
    
    
    var (
    
    
     ErrDuplicateUserName = httperrs.Conflict.NewS("user name already exists")
    
    
     ErrNoUser = httperrs.NotFound.NewS("user not found")
    
    
    )
    
    
    
    type User struct {
    
    
     Id int64
    
    
     Name string
    
    
     Age int
    
    
    
     Followings int
    
    
     Followers int
    
    
    }
    
    
    
    func (u *User) Add() error {
    
    
     u.Followings = 0
    
    
     u.Followers = 0
    
    
    
     id, err := DB.Insert(u, userFieldsExcpId, gomodel.RES_ID)
    
    
     err = dberrs.DuplicateKeyError(err, UserNameCol, ErrDuplicateUserName)
    
    
     if err == nil {
    
    
     u.Id = id
    
    
     }
    
    
    
     return err
    
    
    }
    
    
    
    func DeleteUserById(id int64) error {
    
    
     c, err := DB.ArgsDelete(userInstance, USER_ID, id)
    
    
    
     return dberrs.NoAffects(c, err, ErrNoUser)
    
    
    }
    
    
    
    func (u *User) Update() error {
    
    
     c, err := DB.Update(u, USER_NAME|USER_AGE, USER_ID)
    
    
    
     return dberrs.NoAffects(c, err, ErrNoUser)
    
    
    }
    
    
    
    func (u *User) ById() error {
    
    
     err := DB.One(u, userFieldsExcpId, USER_ID)
    
    
    
     return dberrs.NoRows(err, ErrNoUser)
    
    
    }
    
    
    
    func UsersByAge(age, start, count int) ([]User, error) {
    
    
     users := userStore{
    
    
     Fields: userFieldsAll,
    
    
     }
    
    
    
     err := DB.ArgsLimit(&users, userInstance, userFieldsAll, USER_AGE, age, start, count)
    
    
     return users.Values, dberrs.NoRows(err, ErrNoUser)
    
    
    }
    
    
    
    func AllUsersByAge(age int) ([]User, error) {
    
    
     users := userStore{
    
    
     Fields: userFieldsAll,
    
    
     }
    
    
    
     err := DB.ArgsAll(&users, userInstance, userFieldsAll, USER_AGE, age)
    
    
     return users.Values, dberrs.NoRows(err, ErrNoUser)
    
    
    }
    
    
    
    

    
    
    
    
    var (
    
    
     ErrFollowed = httperrs.Conflict.NewS("already followed")
    
    
     ErrNonFollow = httperrs.NotFound.NewS("non follow")
    
    
    )
    
    
    
    type Follow struct {
    
    
     UserId int64 `table:"user_follow"`
    
    
     FollowUserId int64
    
    
    }
    
    
    
    //gomodel insertUserFollowSQL = [
    
    
    // INSERT INTO Follow(UserId, FollowUserId)
    
    
    // SELECT?,? FROM DUAL
    
    
    // WHERE EXISTS(SELECT Id FROM User WHERE Id=?)
    
    
    //]
    
    
    func (f *Follow) Add() error {
    
    
     return f.txDo(DB, func(tx gomodel.Tx, f *Follow) error {
    
    
     c, err := tx.UpdateById(insertUserFollowSQL, gomodel.FieldVals(f, followFieldsAll, f.FollowUserId)...)
    
    
    
     err = dberrs.NoAffects(c, err, ErrNoUser)
    
    
     err = dberrs.DuplicateKeyError(err, dberrs.PRIMARY_KEY, ErrFollowed)
    
    
    
     return f.updateFollowInfo(tx, err, 1)
    
    
     })
    
    
    }
    
    
    
    func (f *Follow) Delete() error {
    
    
     return f.txDo(DB, func(tx gomodel.Tx, f *Follow) error {
    
    
     c, err := tx.Delete(f, followFieldsAll)
    
    
     err = dberrs.NoAffects(c, err, ErrNonFollow)
    
    
    
     return f.updateFollowInfo(tx, err, -1)
    
    
     })
    
    
    }
    
    
    
    func (f *Follow) updateFollowInfo(tx gomodel.Tx, err error, c int) error {
    
    
     if err == nil {
    
    
     _, err = tx.ArgsIncrBy(userInstance, USER_FOLLOWINGS, USER_ID, c, f.UserId)
    
    
     if err == nil {
    
    
     _, err = tx.ArgsIncrBy(userInstance, USER_FOLLOWERS, USER_ID, c, f.FollowUserId)
    
    
     }
    
    
     }
    
    
     return err
    
    
    }
    
    
    
    
    
    许可证

    MIT。


    数据  INT  Light  fast  LIKE  交互  
    相关文章