func (s *ZStoreStub) handleQuery(req *zc.ZMsg, resp *zc.ZMsg) { payload, _ := req.GetPayload() if !req.CheckExists("zc-class", "zc-query") { resp.SetErr("invalid query request") log.Infof("invalid query request: %s", string(payload)) return } className := req.GetString("zc-class") if !s.checkClass(className) { resp.SetErr("invalid class name") log.Infof("invalid query request: %s", string(payload)) return } classStore := s.store[className] if classStore == nil { resp.SetAck() return } query := req.GetObject("zc-query") if !query.CheckExists("zc-eq") { resp.SetErr("no eq condition to find objects") return } eq := query.GetObject("zc-eq") selectKeys := query.GetStrings("zc-select") primaryKey := s.getPrimaryKey(className, eq) dataZO := classStore[primaryKey] if dataZO == nil { resp.SetAck() return } selectZO := zc.NewObject() if len(selectKeys) <= 0 { selectZO = dataZO } else { for _, key := range selectKeys { selectZO.Put(key, dataZO.Get(key)) } } resp.SetAck() resp.AddObject("zc-objects", selectZO) return }
func (s *ZStoreStub) handleFind(req *zc.ZMsg, resp *zc.ZMsg) { payload, _ := req.GetPayload() if !req.CheckExists("zc-class", "zc-find") { resp.SetErr("invalid find request") log.Infof("invalid find request: %s", string(payload)) return } className := req.GetString("zc-class") if !s.checkClass(className) { resp.SetErr("invalid class name") log.Infof("invalid find request: %s", string(payload)) return } classStore := s.store[className] if classStore == nil { resp.SetAck() return } find := req.GetObject("zc-find") zo := find.GetObject("zc-object") selectKeys := find.GetStrings("zc-select") primaryKey := s.getPrimaryKey(className, zo) dataZO := classStore[primaryKey] if dataZO == nil { resp.SetAck() return } if len(selectKeys) <= 0 { resp.SetAck() resp.Put("zc-object", dataZO) return } selectZO := zc.NewObject() for _, key := range selectKeys { selectZO.Put(key, dataZO.Get(key)) } resp.SetAck() resp.Put("zc-object", selectZO) return }
func TestStoreStubFindWithSelect(t *testing.T) { startStoreStub() // create and find all account := zc.NewObject() account.Put("accountid", "12345") account.Put("name", "lihailei") account.Put("age", 1) account.Put("email", "*****@*****.**") err := zc.Store("account").Create(account).Execute() if err != nil { t.Error(err) } foundAccount, err := zc.Store("account").Find("accountid", "12345").Execute() if err != nil { t.Error(err) } if foundAccount.GetString("accountid") != "12345" || foundAccount.GetString("name") != "lihailei" || foundAccount.GetInt("age") != 1 || foundAccount.GetString("email") != "*****@*****.**" { t.Error(foundAccount) } // find with select keys foundAccount, err = zc.Store("account").Find("accountid", "12345"). Select("accountid", "name", "age"). Execute() if err != nil { t.Error(err) } if foundAccount.GetString("accountid") != "12345" || foundAccount.GetString("name") != "lihailei" || foundAccount.Exists("email") || foundAccount.GetInt("age") != 1 { t.Error(foundAccount) } }
func TestStoreStubUsingZObject(t *testing.T) { startStoreStub() // create and find account := zc.NewObject() account.Put("accountid", "12345") account.Put("name", "lihailei") account.Put("age", 1) err := zc.Store("account").Create(account).Execute() if err != nil { t.Error(err) } foundAccount, err := zc.Store("account").Find("accountid", "12345").Execute() if err != nil { t.Error(err) } if foundAccount.GetString("accountid") != "12345" || foundAccount.GetString("name") != "lihailei" || foundAccount.GetInt("age") != 1 { t.Error(foundAccount) } // update and find account.Put("email", "*****@*****.**") account.Put("age", 2) err = zc.Store("account").Update(account).Execute() if err != nil { t.Error(err) } foundAccount, err = zc.Store("account").Find("accountid", "12345").Execute() if err != nil { t.Error(err) } if foundAccount.GetString("accountid") != "12345" || foundAccount.GetString("name") != "lihailei" || foundAccount.GetString("email") != "*****@*****.**" || foundAccount.GetInt("age") != 2 { t.Error(foundAccount) } // put and find newAccount := zc.NewObject() newAccount.Put("accountid", "12345") newAccount.Put("name", "lihailei") newAccount.Put("age", 3) err = zc.Store("account").Replace(newAccount).Execute() if err != nil { t.Error(err) } foundAccount, err = zc.Store("account").Find("accountid", "12345").Execute() if err != nil { t.Error(err) } if foundAccount.GetString("accountid") != "12345" || foundAccount.GetString("name") != "lihailei" || foundAccount.Exists("email") || foundAccount.GetInt("age") != 3 { t.Error(foundAccount) } }