func find(engine *xorm.Engine, t *testing.T) { users := make([]Userinfo, 0) err := engine.Find(&users) if err != nil { t.Error(err) panic(err) } for _, user := range users { fmt.Println(user) } users2 := make([]Userinfo, 0) userinfo := engine.TableMapper.Obj2Table("Userinfo") err = engine.Sql("select * from " + engine.Quote(userinfo)).Find(&users2) if err != nil { t.Error(err) panic(err) } }
func convertDateToUnix(x *xorm.Engine) (err error) { type Bean struct { ID int64 `xorm:"pk autoincr"` Created time.Time Updated time.Time Merged time.Time Deadline time.Time ClosedDate time.Time NextUpdate time.Time } var tables = []struct { name string cols []string bean interface{} }{ {"action", []string{"created"}, new(TAction)}, {"notice", []string{"created"}, new(TNotice)}, {"comment", []string{"created"}, new(TComment)}, {"issue", []string{"deadline", "created", "updated"}, new(TIssue)}, {"milestone", []string{"deadline", "closed_date"}, new(TMilestone)}, {"attachment", []string{"created"}, new(TAttachment)}, {"login_source", []string{"created", "updated"}, new(TLoginSource)}, {"pull_request", []string{"merged"}, new(TPull)}, {"release", []string{"created"}, new(TRelease)}, {"repository", []string{"created", "updated"}, new(TRepo)}, {"mirror", []string{"updated", "next_update"}, new(TMirror)}, {"public_key", []string{"created", "updated"}, new(TPublicKey)}, {"deploy_key", []string{"created", "updated"}, new(TDeployKey)}, {"access_token", []string{"created", "updated"}, new(TAccessToken)}, {"user", []string{"created", "updated"}, new(TUser)}, {"webhook", []string{"created", "updated"}, new(TWebhook)}, } for _, table := range tables { log.Info("Converting table: %s", table.name) if err = x.Sync2(table.bean); err != nil { return fmt.Errorf("Sync [table: %s]: %v", table.name, err) } offset := 0 for { beans := make([]*Bean, 0, 100) if err = x.Sql(fmt.Sprintf("SELECT * FROM `%s` ORDER BY id ASC LIMIT 100 OFFSET %d", table.name, offset)).Find(&beans); err != nil { return fmt.Errorf("select beans [table: %s, offset: %d]: %v", table.name, offset, err) } log.Trace("Table [%s]: offset: %d, beans: %d", table.name, offset, len(beans)) if len(beans) == 0 { break } offset += 100 baseSQL := "UPDATE `" + table.name + "` SET " for _, bean := range beans { valSQLs := make([]string, 0, len(table.cols)) for _, col := range table.cols { fieldSQL := "" fieldSQL += col + "_unix = " switch col { case "deadline": if bean.Deadline.IsZero() { continue } fieldSQL += com.ToStr(bean.Deadline.UTC().Unix()) case "created": fieldSQL += com.ToStr(bean.Created.UTC().Unix()) case "updated": fieldSQL += com.ToStr(bean.Updated.UTC().Unix()) case "closed_date": fieldSQL += com.ToStr(bean.ClosedDate.UTC().Unix()) case "merged": fieldSQL += com.ToStr(bean.Merged.UTC().Unix()) case "next_update": fieldSQL += com.ToStr(bean.NextUpdate.UTC().Unix()) } valSQLs = append(valSQLs, fieldSQL) } if len(valSQLs) == 0 { continue } if _, err = x.Exec(baseSQL + strings.Join(valSQLs, ",") + " WHERE id = " + com.ToStr(bean.ID)); err != nil { return fmt.Errorf("update bean [table: %s, id: %d]: %v", table.name, bean.ID, err) } } } } return nil }
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) }