mirror of https://github.com/shunfei/cronsun
parent
007b63d10e
commit
5a25adf84f
@ -1,4 +1,8 @@
|
||||
{
|
||||
"Host": "192.168.11.16:3000",
|
||||
"Database": "cronsun"
|
||||
"Hosts": [
|
||||
"192.168.11.16:3000"
|
||||
],
|
||||
"Database": "cronsun",
|
||||
"#Timeout": "connect timeout duration/second",
|
||||
"Timeout": 15
|
||||
}
|
@ -0,0 +1,81 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gopkg.in/mgo.v2"
|
||||
"gopkg.in/mgo.v2/bson"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Hosts []string
|
||||
UserName string
|
||||
Password string
|
||||
Database string
|
||||
Timeout time.Duration // second
|
||||
}
|
||||
|
||||
type Mdb struct {
|
||||
*Config
|
||||
*mgo.Session
|
||||
}
|
||||
|
||||
func NewMdb(c *Config) (*Mdb, error) {
|
||||
m := &Mdb{
|
||||
Config: c,
|
||||
}
|
||||
return m, m.connect()
|
||||
}
|
||||
|
||||
func (m *Mdb) connect() error {
|
||||
// url: [mongodb://][user:pass@]host1[:port1][,host2[:port2],...][/database][?options]
|
||||
url := strings.Join(m.Config.Hosts, ",")
|
||||
if len(m.Config.UserName) > 0 && len(m.Config.Password) > 0 {
|
||||
url = m.Config.UserName + ":" + m.Config.Password + "@" + url
|
||||
}
|
||||
|
||||
if len(m.Config.Database) > 0 {
|
||||
url += "/" + m.Config.Database
|
||||
}
|
||||
|
||||
session, err := mgo.DialWithTimeout(url, m.Config.Timeout)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
m.Session = session
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Mdb) WithC(collection string, job func(*mgo.Collection) error) error {
|
||||
s := m.Session.New()
|
||||
err := job(s.DB(m.Config.Database).C(collection))
|
||||
s.Close()
|
||||
return err
|
||||
}
|
||||
|
||||
func (self *Mdb) Upsert(collection string, selector interface{}, change interface{}) error {
|
||||
return self.WithC(collection, func(c *mgo.Collection) error {
|
||||
_, err := c.Upsert(selector, change)
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
func (self *Mdb) Insert(collection string, data ...interface{}) error {
|
||||
return self.WithC(collection, func(c *mgo.Collection) error {
|
||||
return c.Insert(data...)
|
||||
})
|
||||
}
|
||||
|
||||
func (self *Mdb) FindId(collection string, id interface{}, result interface{}) error {
|
||||
return self.WithC(collection, func(c *mgo.Collection) error {
|
||||
return c.Find(bson.M{"_id": id}).One(result)
|
||||
})
|
||||
}
|
||||
|
||||
func (self *Mdb) FindOne(collection string, query interface{}, result interface{}) error {
|
||||
return self.WithC(collection, func(c *mgo.Collection) error {
|
||||
return c.Find(query).One(result)
|
||||
})
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
package mid
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"gopkg.in/mgo.v2"
|
||||
"gopkg.in/mgo.v2/bson"
|
||||
)
|
||||
|
||||
var (
|
||||
field = &Field{
|
||||
Id: "seq",
|
||||
Collection: "_id",
|
||||
}
|
||||
)
|
||||
|
||||
type Field struct {
|
||||
Id string
|
||||
Collection string
|
||||
}
|
||||
|
||||
// 如果不设置,则用默认设置
|
||||
func SetFieldName(id, collection string) {
|
||||
field.Id = id
|
||||
field.Collection = collection
|
||||
}
|
||||
|
||||
//使collection 为 name 的 id 自增 1 并返回当前 id 的值
|
||||
func AutoInc(c *mgo.Collection, name string) (id int, err error) {
|
||||
return incr(c, name, 1)
|
||||
}
|
||||
|
||||
//批量申请一段id
|
||||
func ApplyBatchIds(c *mgo.Collection, name string, amount int) (id int, err error) {
|
||||
return incr(c, name, amount)
|
||||
}
|
||||
|
||||
func incr(c *mgo.Collection, name string, step int) (id int, err error) {
|
||||
result := make(map[string]interface{})
|
||||
change := mgo.Change{
|
||||
Update: bson.M{"$inc": bson.M{field.Id: step}},
|
||||
Upsert: true,
|
||||
ReturnNew: true,
|
||||
}
|
||||
_, err = c.Find(bson.M{field.Collection: name}).Apply(change, result)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
id, ok := result[field.Id].(int)
|
||||
if ok {
|
||||
return
|
||||
}
|
||||
id64, ok := result[field.Id].(int64)
|
||||
if !ok {
|
||||
err = fmt.Errorf("%s is ont int or int64", field.Id)
|
||||
return
|
||||
}
|
||||
id = int(id64)
|
||||
return
|
||||
}
|
@ -1,13 +1,13 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"sunteng/commons/db/imgo"
|
||||
"github.com/shunfei/cronsun/models/db"
|
||||
)
|
||||
|
||||
var (
|
||||
mgoDB *imgo.Mdb
|
||||
mgoDB *db.Mdb
|
||||
)
|
||||
|
||||
func GetDb() *imgo.Mdb {
|
||||
func GetDb() *db.Mdb {
|
||||
return mgoDB
|
||||
}
|
||||
|
Loading…
Reference in new issue