func (q *Query) Count(collectionName interface{}) (int, error) { if q.dao.Err != nil { return -1, q.dao.Err } if q.dao.db == nil { q.dao.Connect() if q.dao.Err != nil { return -1, q.dao.Err } defer q.dao.Close() } log4go.Debug("count") c := q.dao.db.C(getCollectionName(collectionName)) var mgoQuery *mgo.Query if q.dao.isID(q.queries...) { log4go.Debug("query id: %s", q.queries[0]) if str, ok := q.queries[0].(string); ok { mgoQuery = c.FindId(bson.ObjectIdHex(str)) } else { mgoQuery = c.FindId(q.queries[0]) } } else { selector := q.dao.getM(q.queries...) log4go.Debug("queries: %s", selector) mgoQuery = c.Find(selector) } var n int n, q.dao.Err = mgoQuery.Count() log4go.Debug(n) return n, q.dao.Err }
func TestGet(t *testing.T) { var user User err := mgox.Dao().Get().Result(&user) if handleError(t, err) { return } log4go.Debug(user) id := user.Id user = User{} err = mgox.Dao().Get(id).Result(&user) if handleError(t, err) { return } log4go.Debug(user) user = User{} err = mgox.Dao().Get("name", "yaosxi2").Result(&user) if handleError(t, err) { return } log4go.Debug(user) var users []User err = mgox.Dao().Find().Result(&users) if handleError(t, err) { return } }
func TestFind(t *testing.T) { var users []User err := mgox.Dao().Find().Result(&users) if handleError(t, err) { return } log4go.Debug(users) p := mgox.Page{Count: 1} err = mgox.Dao().Find().Page(&p).Sort("age", "-name").Result(&users) if handleError(t, err) { return } log4go.Debug(users) var user User err = mgox.Dao().Find(users[0].Id).Result(&user) if handleError(t, err) { return } log4go.Debug(user) user = User{} err = mgox.Dao().Find("name", "yaosxi2").Result(&user) if handleError(t, err) { return } log4go.Debug(user) }
func TestLast(t *testing.T) { var user User err := mgox.Dao().Find("name", "yaosxi").IgnoreNFE().Last(&user) if handleError(t, err) { return } log4go.Debug(user.Name) user = User{} err = mgox.Dao().Find("name", "name").IgnoreNFE().Last(&user) if handleError(t, err) { return } log4go.Debug(user.Name) }
func (d *dao) Close() { if d.db != nil { d.db.Session.Close() d.db = nil log4go.Debug("Closed DB connection succssfully") } }
func init() { db := new(PropertyReader) db.init("conf/mgox.properties") DBConfig.Host = db.m["host"] DBConfig.Database = db.m["database"] DBConfig.Username = db.m["username"] DBConfig.Password = db.m["password"] log4go.Debug(fmt.Sprintf("host=%s,database=%s,username=%s", DBConfig.Host, DBConfig.Database, DBConfig.Username)) }
func TestCount(t *testing.T) { n, err := mgox.Dao().Find().Count(UserCollectionName) if handleError(t, err) { return } if n != 4 { log4go.Debug(n) t.Fail() } }
func TestExist(t *testing.T) { b, err := mgox.Dao().Find().Exist("user") if handleError(t, err) { return } if !b { log4go.Debug(b) t.Fail() return } b, err = mgox.Dao().Find("56597ab9f918ad09b4000001").Exist("user") if handleError(t, err) { return } if !b { log4go.Debug(b) t.Fail() return } b, err = mgox.Dao().Find("name", "yaosxi").Exist("user") if handleError(t, err) { return } if !b { log4go.Debug(b) t.Fail() return } b, err = mgox.Dao().Find(bson.M{"name": "yaosxi"}).Exist("user") if handleError(t, err) { return } if !b { log4go.Debug(b) t.Fail() return } }
func TestConnection(t *testing.T) { dao := mgox.Dao().Connect() defer dao.Close() var user = new(User) dao.Find().IgnoreNFE().First(user) log4go.Debug(user.Id) dao.Find().IgnoreNFE().First(user) err := dao.Find().IgnoreNFE().First(user) if handleError(t, err) { return } }
func (d *dao) Remove(collectionName interface{}, selector interface{}) error { if d.Err != nil { return d.Err } if d.db == nil { d.Connect() if d.Err != nil { return d.Err } defer d.Close() } log4go.Debug("remove") c := d.db.C(getCollectionName(collectionName)) var id bson.ObjectId if strId, ok := selector.(string); ok { if strId == "" { d.Err = errors.New("id can't be empty") return d.Err } id = bson.ObjectIdHex(strId) } else if oId, ok := selector.(bson.ObjectId); ok { if oId == "" { d.Err = errors.New("id can't be empty") return d.Err } id = oId } else if _, ok := selector.(bson.M); ok { } else if _, ok := selector.(map[string]interface{}); ok { } else { d.Err = errors.New("unrecognized selector for remove") return d.Err } if id != "" { d.Err = c.RemoveId(id) } else { _, d.Err = c.RemoveAll(selector) } return d.Err }
func (q *Query) Last(result interface{}) error { if q.dao.Err != nil { return q.dao.Err } if q.dao.db == nil { q.dao.Connect() if q.dao.Err != nil { return q.dao.Err } defer q.dao.Close() } count, _ := q.Count(result) if q.dao.Err != nil { return q.dao.Err } if count == 0 { count = 1 } log4go.Debug("last") q.page = &Page{Cursor: count - 1, Count: 1} return q.Result(result) }
func (q *Query) First(result interface{}) error { log4go.Debug("first") q.page = &Page{Count: 1} return q.Result(result) }
func (d *dao) Insert(docs ...interface{}) error { if d.Err != nil { return d.Err } for i, _ := range docs { if docs[i] == nil { d.Err = errors.New("cannot insert empty document") return d.Err } mType := reflect.TypeOf(docs[i]) mValue := reflect.ValueOf(docs[i]) mType, mValue = getElem(mType, mValue) if mType.Kind() == reflect.Struct && mValue.IsValid() { field := mValue.FieldByName("Id") if field.IsValid() && field.String() == "" && field.CanSet() { field.Set(reflect.ValueOf(bson.NewObjectId())) } now := reflect.ValueOf(time.Now()) field = mValue.FieldByName("FirstCreated") if field.CanSet() { field.Set(now) } field = mValue.FieldByName("LastModified") if field.CanSet() { field.Set(now) } // This will force mongo db store localcreated field in a local time , not UTC time. field = mValue.FieldByName("LocalCreated") if field.CanSet() { _, offset := time.Now().Local().Zone() // zoneName,offset:=time.Now().Local().Zone() // println("zone name is:%s,offset is:%d",zoneName,offset) newtime := time.Now().Add(time.Duration(offset * 1000000000)) // fmt.Println("newtime:",newtime) nowlocal := reflect.ValueOf(newtime) field.Set(nowlocal) } if d.uid != nil { field = mValue.FieldByName("FirstCreator") if field.CanSet() { field.Set(reflect.ValueOf(d.uid)) } field = mValue.FieldByName("LastModifier") if field.CanSet() { field.Set(reflect.ValueOf(d.uid)) } } } else if mType.Kind() == reflect.Map { m, ok := docs[i].(bson.M) if !ok { m, _ = docs[i].(map[string]interface{}) } if len(m) > 0 { if _, ok := m["_id"]; !ok { m["_id"] = bson.NewObjectId() } now := time.Now() m["firstcreated"] = now m["lastmodified"] = now if d.uid != nil { m["firstcreator"] = d.uid m["lastmodifier"] = d.uid } } } } if d.db == nil { d.Connect() if d.Err != nil { return d.Err } defer d.Close() } log4go.Debug("insert") d.Err = d.db.C(getCollectionName(docs[0])).Insert(docs...) return d.Err }
func (d *dao) update(operator string, collectionName interface{}, selector interface{}, updates ...interface{}) error { if d.Err != nil { return d.Err } if len(updates) == 0 { d.Err = errors.New("updates can't be empty") return d.Err } isStruct := false mType := reflect.TypeOf(updates[0]) mValue := reflect.ValueOf(updates[0]) mType, mValue = getElem(mType, mValue) if mType.Kind() == reflect.Struct && mValue.IsValid() { isStruct = true field := mValue.FieldByName("LastModified") if field.CanSet() { field.Set(reflect.ValueOf(time.Now())) } if d.uid != nil { field := mValue.FieldByName("LastModifier") if field.CanSet() { field.Set(reflect.ValueOf(d.uid)) } } } var update bson.M if !isStruct { update = d.getM(updates...) if operator == "$inc" || operator == "$push" { m := bson.M{"lastmodified": time.Now()} if d.uid != nil { m["lastmodifier"] = d.uid } update = bson.M{"$set": m, operator: update} } else { if operator == "$set" || operator == "$update" { update["lastmodified"] = time.Now() if d.uid != nil { update["lastmodifier"] = d.uid } } if operator != "update" { update = bson.M{operator: update} } } } if d.db == nil { d.Connect() if d.Err != nil { return d.Err } defer d.Close() } _collectionName := getCollectionName(collectionName) c := d.db.C(_collectionName) var id bson.ObjectId if strId, ok := selector.(string); ok { if strId == "" { d.Err = errors.New("id can't be empty") return d.Err } id = bson.ObjectIdHex(strId) } else if oId, ok := selector.(bson.ObjectId); ok { if oId == "" { d.Err = errors.New("id can't be empty") return d.Err } id = oId } else if _, ok := selector.(bson.M); ok { } else if _, ok := selector.(map[string]interface{}); ok { } else { d.Err = errors.New("unrecognized selector for update") return d.Err } if isStruct { if id != "" { log4go.Debug(fmt.Sprintf("[%s]collection=%s,id=%s,struct=%s", operator, _collectionName, id, updates[0])) d.Err = c.UpdateId(id, updates[0]) } else { log4go.Debug(fmt.Sprintf("[%s]collection=%s,selector=%s,struct=%s", operator, _collectionName, selector, updates[0])) _, d.Err = c.UpdateAll(selector, updates[0]) } } else { if id != "" { log4go.Debug(fmt.Sprintf("id=%s", id)) log4go.Debug(fmt.Sprintf("[%s]collection=%s,id=%s,update=%s", operator, _collectionName, id, update)) d.Err = c.UpdateId(id, update) } else { log4go.Debug(fmt.Sprintf("[%s]collection=%s,selector=%s,struct=%s", operator, _collectionName, selector, update)) _, d.Err = c.UpdateAll(selector, update) } } return d.Err }