func table(engine *xorm.Engine, t *testing.T) { err := engine.DropTables("user_user") if err != nil { t.Error(err) panic(err) } err = engine.Table("user_user").CreateTable(&Userinfo{}) if err != nil { t.Error(err) panic(err) } }
func joinCount2(engine *xorm.Engine, t *testing.T) { err := engine.Sync2(new(History), new(Resource), new(NewUser)) if err != nil { t.Error(err) panic(err) } var where = "`history`.`deleted_at` > '0001-01-01 00:00:00'" count, err := engine.Table("history").Join("LEFT", "resource", "`resource`.`rid`=`history`.`rid`").Join("LEFT", "user", "`user`.`uid`=`history`.`uid`").Where(where).Count(new(History)) if err != nil { t.Error(err) panic(err) } fmt.Println(count) }
func tableOp(engine *xorm.Engine, t *testing.T) { user := Userinfo{Username: "******", Departname: "dev", Alias: "lunny", Created: time.Now()} tableName := fmt.Sprintf("user_%v", len(user.Username)) cnt, err := engine.Table(tableName).Insert(&user) if err != nil { t.Error(err) panic(err) } if cnt != 1 { err = errors.New("insert not returned 1") t.Error(err) panic(err) return } has, err := engine.Table(tableName).Get(&Userinfo{Username: "******"}) if err != nil { t.Error(err) panic(err) } if !has { err = errors.New("Get has return false") t.Error(err) panic(err) return } users := make([]Userinfo, 0) err = engine.Table(tableName).Find(&users) if err != nil { t.Error(err) panic(err) } id := user.Uid cnt, err = engine.Table(tableName).Id(id).Update(&Userinfo{Username: "******"}) if err != nil { t.Error(err) panic(err) } _, err = engine.Table(tableName).Id(id).Delete(&Userinfo{}) if err != nil { t.Error(err) panic(err) } }
func updateSameMapper(engine *xorm.Engine, t *testing.T) { var ori Userinfo has, err := engine.Get(&ori) if err != nil { t.Error(err) panic(err) } if !has { t.Error(errors.New("not exist")) panic(errors.New("not exist")) } // update by id user := Userinfo{Username: "******", Height: 1.2} cnt, err := engine.Id(ori.Uid).Update(&user) if err != nil { t.Error(err) panic(err) } if cnt != 1 { err = errors.New("update not returned 1") t.Error(err) panic(err) return } condi := Condi{"Username": "******", "Departname": ""} cnt, err = engine.Table(&user).Id(ori.Uid).Update(&condi) if err != nil { t.Error(err) panic(err) } if cnt != 1 { err = errors.New("update not returned 1") t.Error(err) panic(err) return } cnt, err = engine.Update(&Userinfo{Username: "******"}, &user) if err != nil { t.Error(err) panic(err) } total, err := engine.Count(&user) if err != nil { t.Error(err) panic(err) } if cnt != total { err = errors.New("insert not returned 1") t.Error(err) panic(err) return } err = engine.Sync(&Article{}) if err != nil { t.Error(err) panic(err) } defer func() { err = engine.DropTables(&Article{}) if err != nil { t.Error(err) panic(err) } }() a := &Article{0, "1", "2", "3", "4", "5", 2} cnt, err = engine.Insert(a) if err != nil { t.Error(err) panic(err) } if cnt != 1 { err = errors.New(fmt.Sprintf("insert not returned 1 but %d", cnt)) t.Error(err) panic(err) } if a.Id == 0 { err = errors.New("insert returned id is 0") t.Error(err) panic(err) } cnt, err = engine.Id(a.Id).Update(&Article{Name: "6"}) if err != nil { t.Error(err) panic(err) } if cnt != 1 { err = errors.New(fmt.Sprintf("insert not returned 1 but %d", cnt)) t.Error(err) panic(err) return } col1 := &UpdateAllCols{} err = engine.Sync(col1) if err != nil { t.Error(err) panic(err) } _, err = engine.Insert(col1) if err != nil { t.Error(err) panic(err) } col2 := &UpdateAllCols{col1.Id, true, "", nil} _, err = engine.Id(col2.Id).AllCols().Update(col2) if err != nil { t.Error(err) panic(err) } col3 := &UpdateAllCols{} has, err = engine.Id(col2.Id).Get(col3) if err != nil { t.Error(err) panic(err) } if !has { err = errors.New(fmt.Sprintf("cannot get id %d", col2.Id)) t.Error(err) panic(err) return } if *col2 != *col3 { err = errors.New(fmt.Sprintf("col2 should eq col3")) t.Error(err) panic(err) return } { col1 := &UpdateMustCols{} err = engine.Sync(col1) if err != nil { t.Error(err) panic(err) } _, err = engine.Insert(col1) if err != nil { t.Error(err) panic(err) } col2 := &UpdateMustCols{col1.Id, true, ""} boolStr := engine.ColumnMapper.Obj2Table("Bool") stringStr := engine.ColumnMapper.Obj2Table("String") _, err = engine.Id(col2.Id).MustCols(boolStr, stringStr).Update(col2) if err != nil { t.Error(err) panic(err) } col3 := &UpdateMustCols{} has, err := engine.Id(col2.Id).Get(col3) if err != nil { t.Error(err) panic(err) } if !has { err = errors.New(fmt.Sprintf("cannot get id %d", col2.Id)) t.Error(err) panic(err) return } if *col2 != *col3 { err = errors.New(fmt.Sprintf("col2 should eq col3")) t.Error(err) panic(err) return } } { col1 := &UpdateIncr{} err = engine.Sync(col1) if err != nil { t.Error(err) panic(err) } _, err = engine.Insert(col1) if err != nil { t.Error(err) panic(err) } cnt, err := engine.Id(col1.Id).Incr("`Cnt`").Update(col1) if err != nil { t.Error(err) panic(err) } if cnt != 1 { err = errors.New("update incr failed") t.Error(err) panic(err) } newCol := new(UpdateIncr) has, err := engine.Id(col1.Id).Get(newCol) if err != nil { t.Error(err) panic(err) } if !has { err = errors.New("has incr failed") t.Error(err) panic(err) } if 1 != newCol.Cnt { err = errors.New("incr failed") t.Error(err) panic(err) } } }
func update(engine *xorm.Engine, t *testing.T) { var ori Userinfo has, err := engine.Get(&ori) if err != nil { t.Error(err) panic(err) } if !has { t.Error(errors.New("not exist")) panic(errors.New("not exist")) } // update by id user := Userinfo{Username: "******", Height: 1.2} cnt, err := engine.Id(ori.Uid).Update(&user) if err != nil { t.Error(err) panic(err) } if cnt != 1 { err = errors.New("update not returned 1") t.Error(err) panic(err) return } condi := Condi{"username": "******", "departname": ""} cnt, err = engine.Table(&user).Id(ori.Uid).Update(&condi) if err != nil { t.Error(err) panic(err) } if cnt != 1 { err = errors.New("update not returned 1") t.Error(err) panic(err) return } cnt, err = engine.Update(&Userinfo{Username: "******"}, &user) if err != nil { t.Error(err) panic(err) } total, err := engine.Count(&user) if err != nil { t.Error(err) panic(err) } if cnt != total { err = errors.New("insert not returned 1") t.Error(err) panic(err) return } // nullable update { user := &Userinfo{Username: "******", Height: 180.5} _, err := engine.Insert(user) if err != nil { t.Error(err) panic(err) } userID := user.Uid has, err := engine.Id(userID). And("username = ?", user.Username). And("height = ?", user.Height). And("departname = ?", ""). And("detail_id = ?", 0). And("is_man = ?", 0). And("created IS NOT NULL"). Get(&Userinfo{}) if err != nil { t.Error(err) panic(err) } if !has { err = errors.New("cannot insert properly") t.Error(err) panic(err) } updatedUser := &Userinfo{Username: "******"} cnt, err = engine.Id(userID). Nullable("height", "departname", "is_man", "created"). Update(updatedUser) if err != nil { t.Error(err) panic(err) } if cnt != 1 { err = errors.New("update not returned 1") t.Error(err) panic(err) } has, err = engine.Id(userID). And("username = ?", updatedUser.Username). And("height IS NULL"). And("departname IS NULL"). And("is_man IS NULL"). And("created IS NULL"). And("detail_id = ?", 0). Get(&Userinfo{}) if err != nil { t.Error(err) panic(err) } if !has { err = errors.New("cannot update with null properly") t.Error(err) panic(err) } cnt, err = engine.Id(userID).Delete(&Userinfo{}) if err != nil { t.Error(err) panic(err) } if cnt != 1 { err = errors.New("delete not returned 1") t.Error(err) panic(err) } } err = engine.StoreEngine("Innodb").Sync2(&Article{}) if err != nil { t.Error(err) panic(err) } defer func() { err = engine.DropTables(&Article{}) if err != nil { t.Error(err) panic(err) } }() a := &Article{0, "1", "2", "3", "4", "5", 2} cnt, err = engine.Insert(a) if err != nil { t.Error(err) panic(err) } if cnt != 1 { err = errors.New(fmt.Sprintf("insert not returned 1 but %d", cnt)) t.Error(err) panic(err) } if a.Id == 0 { err = errors.New("insert returned id is 0") t.Error(err) panic(err) } cnt, err = engine.Id(a.Id).Update(&Article{Name: "6"}) if err != nil { t.Error(err) panic(err) } if cnt != 1 { err = errors.New(fmt.Sprintf("insert not returned 1 but %d", cnt)) t.Error(err) panic(err) return } var s = "test" col1 := &UpdateAllCols{Ptr: &s} err = engine.Sync(col1) if err != nil { t.Error(err) panic(err) } _, err = engine.Insert(col1) if err != nil { t.Error(err) panic(err) } col2 := &UpdateAllCols{col1.Id, true, "", nil} _, err = engine.Id(col2.Id).AllCols().Update(col2) if err != nil { t.Error(err) panic(err) } col3 := &UpdateAllCols{} has, err = engine.Id(col2.Id).Get(col3) if err != nil { t.Error(err) panic(err) } if !has { err = errors.New(fmt.Sprintf("cannot get id %d", col2.Id)) t.Error(err) panic(err) return } if *col2 != *col3 { err = errors.New(fmt.Sprintf("col2 should eq col3")) t.Error(err) panic(err) return } { col1 := &UpdateMustCols{} err = engine.Sync(col1) if err != nil { t.Error(err) panic(err) } _, err = engine.Insert(col1) if err != nil { t.Error(err) panic(err) } col2 := &UpdateMustCols{col1.Id, true, ""} boolStr := engine.ColumnMapper.Obj2Table("Bool") stringStr := engine.ColumnMapper.Obj2Table("String") _, err = engine.Id(col2.Id).MustCols(boolStr, stringStr).Update(col2) if err != nil { t.Error(err) panic(err) } col3 := &UpdateMustCols{} has, err := engine.Id(col2.Id).Get(col3) if err != nil { t.Error(err) panic(err) } if !has { err = errors.New(fmt.Sprintf("cannot get id %d", col2.Id)) t.Error(err) panic(err) return } if *col2 != *col3 { err = errors.New(fmt.Sprintf("col2 should eq col3")) t.Error(err) panic(err) return } } { col1 := &UpdateIncr{} err = engine.Sync(col1) if err != nil { t.Error(err) panic(err) } _, err = engine.Insert(col1) if err != nil { t.Error(err) panic(err) } cnt, err := engine.Id(col1.Id).Incr("cnt").Update(col1) if err != nil { t.Error(err) panic(err) } if cnt != 1 { err = errors.New("update incr failed") t.Error(err) panic(err) } newCol := new(UpdateIncr) has, err := engine.Id(col1.Id).Get(newCol) if err != nil { t.Error(err) panic(err) } if !has { err = errors.New("has incr failed") t.Error(err) panic(err) } if 1 != newCol.Cnt { err = fmt.Errorf("incr failed %v %v %v", newCol.Cnt, newCol, col1) t.Error(err) panic(err) } } testUpdateUpdated(engine, t) }
func testPointerData(engine *xorm.Engine, t *testing.T) { err := engine.DropTables(&NullData{}) if err != nil { t.Error(err) panic(err) } err = engine.CreateTables(&NullData{}) if err != nil { t.Error(err) panic(err) } nullData := NullData{ StringPtr: new(string), StringPtr2: new(string), BoolPtr: new(bool), BytePtr: new(byte), UintPtr: new(uint), Uint8Ptr: new(uint8), Uint16Ptr: new(uint16), Uint32Ptr: new(uint32), Uint64Ptr: new(uint64), IntPtr: new(int), Int8Ptr: new(int8), Int16Ptr: new(int16), Int32Ptr: new(int32), Int64Ptr: new(int64), RunePtr: new(rune), Float32Ptr: new(float32), Float64Ptr: new(float64), // Complex64Ptr: new(complex64), // Complex128Ptr: new(complex128), TimePtr: new(time.Time), } *nullData.StringPtr = "abc" *nullData.StringPtr2 = "123" *nullData.BoolPtr = true *nullData.BytePtr = 1 *nullData.UintPtr = 1 *nullData.Uint8Ptr = 1 *nullData.Uint16Ptr = 1 *nullData.Uint32Ptr = 1 *nullData.Uint64Ptr = 1 *nullData.IntPtr = -1 *nullData.Int8Ptr = -1 *nullData.Int16Ptr = -1 *nullData.Int32Ptr = -1 *nullData.Int64Ptr = -1 *nullData.RunePtr = 1 *nullData.Float32Ptr = -1.2 *nullData.Float64Ptr = -1.1 // *nullData.Complex64Ptr = 123456789012345678901234567890 // *nullData.Complex128Ptr = 123456789012345678901234567890123456789012345678901234567890 *nullData.TimePtr = time.Now() cnt, err := engine.Insert(&nullData) fmt.Println(nullData.Id) if err != nil { t.Error(err) panic(err) } if cnt != 1 { err = errors.New("insert not returned 1") t.Error(err) panic(err) return } if nullData.Id <= 0 { err = errors.New("not return id error") t.Error(err) panic(err) } // verify get values nullDataGet := NullData{} has, err := engine.Id(nullData.Id).Get(&nullDataGet) if err != nil { t.Error(err) panic(err) } else if !has { t.Error(errors.New("ID not found")) } if *nullDataGet.StringPtr != *nullData.StringPtr { t.Error(errors.New(fmt.Sprintf("inserted value unmatch: [%v]", *nullDataGet.StringPtr))) } if *nullDataGet.StringPtr2 != *nullData.StringPtr2 { t.Error(errors.New(fmt.Sprintf("inserted value unmatch: [%v]", *nullDataGet.StringPtr2))) } if *nullDataGet.BoolPtr != *nullData.BoolPtr { t.Error(errors.New(fmt.Sprintf("inserted value unmatch: [%t]", *nullDataGet.BoolPtr))) } if *nullDataGet.UintPtr != *nullData.UintPtr { t.Error(errors.New(fmt.Sprintf("inserted value unmatch: [%v]", *nullDataGet.UintPtr))) } if *nullDataGet.Uint8Ptr != *nullData.Uint8Ptr { t.Error(errors.New(fmt.Sprintf("inserted value unmatch: [%v]", *nullDataGet.Uint8Ptr))) } if *nullDataGet.Uint16Ptr != *nullData.Uint16Ptr { t.Error(errors.New(fmt.Sprintf("inserted value unmatch: [%v]", *nullDataGet.Uint16Ptr))) } if *nullDataGet.Uint32Ptr != *nullData.Uint32Ptr { t.Error(errors.New(fmt.Sprintf("inserted value unmatch: [%v]", *nullDataGet.Uint32Ptr))) } if *nullDataGet.Uint64Ptr != *nullData.Uint64Ptr { t.Error(errors.New(fmt.Sprintf("inserted value unmatch: [%v]", *nullDataGet.Uint64Ptr))) } if *nullDataGet.IntPtr != *nullData.IntPtr { t.Error(errors.New(fmt.Sprintf("inserted value unmatch: [%v]", *nullDataGet.IntPtr))) } if *nullDataGet.Int8Ptr != *nullData.Int8Ptr { t.Error(errors.New(fmt.Sprintf("inserted value unmatch: [%v]", *nullDataGet.Int8Ptr))) } if *nullDataGet.Int16Ptr != *nullData.Int16Ptr { t.Error(errors.New(fmt.Sprintf("inserted value unmatch: [%v]", *nullDataGet.Int16Ptr))) } if *nullDataGet.Int32Ptr != *nullData.Int32Ptr { t.Error(errors.New(fmt.Sprintf("inserted value unmatch: [%v]", *nullDataGet.Int32Ptr))) } if *nullDataGet.Int64Ptr != *nullData.Int64Ptr { t.Error(errors.New(fmt.Sprintf("inserted value unmatch: [%v]", *nullDataGet.Int64Ptr))) } if *nullDataGet.RunePtr != *nullData.RunePtr { t.Error(errors.New(fmt.Sprintf("inserted value unmatch: [%v]", *nullDataGet.RunePtr))) } if *nullDataGet.Float32Ptr != *nullData.Float32Ptr { t.Error(errors.New(fmt.Sprintf("inserted value unmatch: [%v]", *nullDataGet.Float32Ptr))) } if *nullDataGet.Float64Ptr != *nullData.Float64Ptr { t.Error(errors.New(fmt.Sprintf("inserted value unmatch: [%v]", *nullDataGet.Float64Ptr))) } // if *nullDataGet.Complex64Ptr != *nullData.Complex64Ptr { // t.Error(errors.New(fmt.Sprintf("inserted value unmatch: [%v]", *nullDataGet.Complex64Ptr))) // } // if *nullDataGet.Complex128Ptr != *nullData.Complex128Ptr { // t.Error(errors.New(fmt.Sprintf("inserted value unmatch: [%v]", *nullDataGet.Complex128Ptr))) // } /*if (*nullDataGet.TimePtr).Unix() != (*nullData.TimePtr).Unix() { t.Error(errors.New(fmt.Sprintf("inserted value unmatch: [%v]:[%v]", *nullDataGet.TimePtr, *nullData.TimePtr))) } else { // !nashtsai! mymysql driver will failed this test case, due the time is roundup to nearest second, I would considered this is a bug in mymysql driver fmt.Printf("time value: [%v]:[%v]", *nullDataGet.TimePtr, *nullData.TimePtr) fmt.Println() }*/ // -- // using instance type should just work too nullData2Get := NullData2{} tableName := engine.TableMapper.Obj2Table("NullData") has, err = engine.Table(tableName).Id(nullData.Id).Get(&nullData2Get) if err != nil { t.Error(err) panic(err) } else if !has { t.Error(errors.New("ID not found")) } if nullData2Get.StringPtr != *nullData.StringPtr { t.Error(errors.New(fmt.Sprintf("inserted value unmatch: [%v]", nullData2Get.StringPtr))) } if nullData2Get.StringPtr2 != *nullData.StringPtr2 { t.Error(errors.New(fmt.Sprintf("inserted value unmatch: [%v]", nullData2Get.StringPtr2))) } if nullData2Get.BoolPtr != *nullData.BoolPtr { t.Error(errors.New(fmt.Sprintf("inserted value unmatch: [%t]", nullData2Get.BoolPtr))) } if nullData2Get.UintPtr != *nullData.UintPtr { t.Error(errors.New(fmt.Sprintf("inserted value unmatch: [%v]", nullData2Get.UintPtr))) } if nullData2Get.Uint8Ptr != *nullData.Uint8Ptr { t.Error(errors.New(fmt.Sprintf("inserted value unmatch: [%v]", nullData2Get.Uint8Ptr))) } if nullData2Get.Uint16Ptr != *nullData.Uint16Ptr { t.Error(errors.New(fmt.Sprintf("inserted value unmatch: [%v]", nullData2Get.Uint16Ptr))) } if nullData2Get.Uint32Ptr != *nullData.Uint32Ptr { t.Error(errors.New(fmt.Sprintf("inserted value unmatch: [%v]", nullData2Get.Uint32Ptr))) } if nullData2Get.Uint64Ptr != *nullData.Uint64Ptr { t.Error(errors.New(fmt.Sprintf("inserted value unmatch: [%v]", nullData2Get.Uint64Ptr))) } if nullData2Get.IntPtr != *nullData.IntPtr { t.Error(errors.New(fmt.Sprintf("inserted value unmatch: [%v]", nullData2Get.IntPtr))) } if nullData2Get.Int8Ptr != *nullData.Int8Ptr { t.Error(errors.New(fmt.Sprintf("inserted value unmatch: [%v]", nullData2Get.Int8Ptr))) } if nullData2Get.Int16Ptr != *nullData.Int16Ptr { t.Error(errors.New(fmt.Sprintf("inserted value unmatch: [%v]", nullData2Get.Int16Ptr))) } if nullData2Get.Int32Ptr != *nullData.Int32Ptr { t.Error(errors.New(fmt.Sprintf("inserted value unmatch: [%v]", nullData2Get.Int32Ptr))) } if nullData2Get.Int64Ptr != *nullData.Int64Ptr { t.Error(errors.New(fmt.Sprintf("inserted value unmatch: [%v]", nullData2Get.Int64Ptr))) } if nullData2Get.RunePtr != *nullData.RunePtr { t.Error(errors.New(fmt.Sprintf("inserted value unmatch: [%v]", nullData2Get.RunePtr))) } if nullData2Get.Float32Ptr != *nullData.Float32Ptr { t.Error(errors.New(fmt.Sprintf("inserted value unmatch: [%v]", nullData2Get.Float32Ptr))) } if nullData2Get.Float64Ptr != *nullData.Float64Ptr { t.Error(errors.New(fmt.Sprintf("inserted value unmatch: [%v]", nullData2Get.Float64Ptr))) } // if nullData2Get.Complex64Ptr != *nullData.Complex64Ptr { // t.Error(errors.New(fmt.Sprintf("inserted value unmatch: [%v]", nullData2Get.Complex64Ptr))) // } // if nullData2Get.Complex128Ptr != *nullData.Complex128Ptr { // t.Error(errors.New(fmt.Sprintf("inserted value unmatch: [%v]", nullData2Get.Complex128Ptr))) // } /*if nullData2Get.TimePtr.Unix() != (*nullData.TimePtr).Unix() { t.Error(errors.New(fmt.Sprintf("inserted value unmatch: [%v]:[%v]", nullData2Get.TimePtr, *nullData.TimePtr))) } else { // !nashtsai! mymysql driver will failed this test case, due the time is roundup to nearest second, I would considered this is a bug in mymysql driver fmt.Printf("time value: [%v]:[%v]", nullData2Get.TimePtr, *nullData.TimePtr) fmt.Println() }*/ // -- }
func testExtends(engine *xorm.Engine, t *testing.T) { err := engine.DropTables(&tempUser2{}) if err != nil { t.Error(err) panic(err) } err = engine.CreateTables(&tempUser2{}) if err != nil { t.Error(err) panic(err) } tu := &tempUser2{tempUser{0, "extends"}, "dev depart"} _, err = engine.Insert(tu) if err != nil { t.Error(err) panic(err) } tu2 := &tempUser2{} _, err = engine.Get(tu2) if err != nil { t.Error(err) panic(err) } tu3 := &tempUser2{tempUser{0, "extends update"}, ""} _, err = engine.Id(tu2.Id).Update(tu3) if err != nil { t.Error(err) panic(err) } err = engine.DropTables(&tempUser3{}) if err != nil { t.Error(err) panic(err) } err = engine.CreateTables(&tempUser3{}) if err != nil { t.Error(err) panic(err) } tu4 := &tempUser3{&tempUser{0, "extends"}, "dev depart"} _, err = engine.Insert(tu4) if err != nil { t.Error(err) panic(err) } tu5 := &tempUser3{} _, err = engine.Get(tu5) if err != nil { t.Error(err) panic(err) } if tu5.Temp == nil { err = errors.New("error get data extends") t.Error(err) panic(err) } if tu5.Temp.Id != 1 || tu5.Temp.Username != "extends" || tu5.Departname != "dev depart" { err = errors.New("error get data extends") t.Error(err) panic(err) } tu6 := &tempUser3{&tempUser{0, "extends update"}, ""} _, err = engine.Id(tu5.Temp.Id).Update(tu6) if err != nil { t.Error(err) panic(err) } users := make([]tempUser3, 0) err = engine.Find(&users) if err != nil { t.Error(err) panic(err) } if len(users) != 1 { err = errors.New("error get data not 1") t.Error(err) panic(err) } var info UserAndDetail qt := engine.Quote engine.Update(&Userinfo{Detail: Userdetail{Id: 1}}) ui := engine.TableMapper.Obj2Table("Userinfo") ud := engine.TableMapper.Obj2Table("Userdetail") uiid := engine.TableMapper.Obj2Table("Id") udid := "detail_id" sql := fmt.Sprintf("select * from %s, %s where %s.%s = %s.%s", qt(ui), qt(ud), qt(ui), qt(udid), qt(ud), qt(uiid)) b, err := engine.Sql(sql).Get(&info) if err != nil { t.Error(err) panic(err) } if !b { err = errors.New("should has lest one record") t.Error(err) panic(err) } if info.Userinfo.Uid == 0 || info.Userdetail.Id == 0 { err = errors.New("all of the id should has value") t.Error(err) panic(err) } fmt.Println(info) fmt.Println("----join--info2") var info2 UserAndDetail b, err = engine.Table(&Userinfo{}).Join("LEFT", qt(ud), qt(ui)+"."+qt("detail_id")+" = "+qt(ud)+"."+qt(uiid)).Get(&info2) if err != nil { t.Error(err) panic(err) } if !b { err = errors.New("should has lest one record") t.Error(err) panic(err) } if info2.Userinfo.Uid == 0 || info2.Userdetail.Id == 0 { err = errors.New("all of the id should has value") t.Error(err) panic(err) } fmt.Println(info2) fmt.Println("----join--infos2") var infos2 = make([]UserAndDetail, 0) err = engine.Table(&Userinfo{}).Join("LEFT", qt(ud), qt(ui)+"."+qt("detail_id")+" = "+qt(ud)+"."+qt(uiid)).Find(&infos2) if err != nil { t.Error(err) panic(err) } fmt.Println(infos2) }
func testExtends2(engine *xorm.Engine, t *testing.T) { err := engine.DropTables(&Message{}, &MessageUser{}) if err != nil { t.Error(err) panic(err) } err = engine.CreateTables(&Message{}, &MessageUser{}) if err != nil { t.Error(err) panic(err) } var sender = MessageUser{Name: "sender"} var receiver = MessageUser{Name: "receiver"} _, err = engine.Insert(&sender, &receiver) if err != nil { t.Error(err) panic(err) } msg := Message{ Title: "test", Content: "test", Uid: sender.Id, ToUid: receiver.Id, } _, err = engine.Insert(&msg) if err != nil { t.Error(err) panic(err) } var mapper = engine.TableMapper.Obj2Table userTableName := mapper("MessageUser") msgTableName := mapper("Message") list := make([]MessageExtend, 0) err = engine.Table(msgTableName).Join("LEFT", []string{userTableName, "sender"}, "`sender`.`"+mapper("Id")+"`=`"+msgTableName+"`.`"+mapper("Uid")+"`"). Join("LEFT", []string{userTableName, "receiver"}, "`receiver`.`"+mapper("Id")+"`=`"+msgTableName+"`.`"+mapper("ToUid")+"`"). Find(&list) if err != nil { t.Error(err) panic(err) } if len(list) != 1 { err = errors.New(fmt.Sprintln("should have 1 message, got", len(list))) t.Error(err) panic(err) } if list[0].Message.Id != msg.Id { err = errors.New(fmt.Sprintln("should message equal", list[0].Message, msg)) t.Error(err) panic(err) } if list[0].Sender.Id != sender.Id { err = errors.New(fmt.Sprintln("should sender equal", list[0].Sender, sender)) t.Error(err) panic(err) } if list[0].Receiver.Id != receiver.Id { err = errors.New(fmt.Sprintln("should receiver equal", list[0].Receiver, receiver)) t.Error(err) panic(err) } }