Beispiel #1
0
func testCreatedJsonTime(engine *xorm.Engine, t *testing.T) {
	di5 := new(MyJsonTime)
	err := engine.Sync2(di5)
	if err != nil {
		t.Fatal(err)
	}
	ci5 := &MyJsonTime{}
	_, err = engine.Insert(ci5)
	if err != nil {
		t.Fatal(err)
	}
	has, err := engine.Desc("(id)").Get(di5)
	if err != nil {
		t.Fatal(err)
	}
	if !has {
		t.Fatal(xorm.ErrNotExist)
	}
	if time.Time(ci5.Created).Unix() != time.Time(di5.Created).Unix() {
		t.Fatal("should equal:", time.Time(ci5.Created).Unix(), time.Time(di5.Created).Unix())
	}
	fmt.Println("ci5:", ci5, "di5:", di5)

	var dis = make([]MyJsonTime, 0)
	err = engine.Find(&dis)
	if err != nil {
		t.Fatal(err)
	}
}
Beispiel #2
0
func limit(engine *xorm.Engine, t *testing.T) {
	users := make([]Userinfo, 0)
	err := engine.Limit(2, 1).Find(&users)
	if err != nil {
		t.Error(err)
		panic(err)
	}
	fmt.Println(users)

	err = engine.DropTables(new(Limit))
	if err != nil {
		t.Error(err)
		panic(err)
	}

	err = engine.Sync2(new(Limit))
	if err != nil {
		t.Error(err)
		panic(err)
	}

	_, err = engine.Insert(&Limit{Name: "1"})
	if err != nil {
		t.Error(err)
		panic(err)
	}

	// TODO: support limit for update
	/*_, err = engine.Limit(1).Update(&Limit{Name: "2"})
	if err != nil {
		t.Error(err)
		panic(err)
	}*/
}
Beispiel #3
0
func NewXormAuth(orm *xorm.Engine, allowAnony bool, perm os.FileMode, encryptFunc func(string) string) (*XormAuth, error) {
	err := orm.Sync2(new(User))
	if err != nil {
		return nil, err
	}

	return &XormAuth{allowAnony, os.ModePerm, orm, encryptFunc}, nil
}
Beispiel #4
0
func setCommentUpdatedWithCreated(x *xorm.Engine) (err error) {
	type Comment struct {
		UpdatedUnix int64
	}

	if err = x.Sync2(new(Comment)); err != nil {
		return fmt.Errorf("Sync2: %v", err)
	} else if _, err = x.Exec("UPDATE comment SET updated_unix = created_unix"); err != nil {
		return fmt.Errorf("set update_unix: %v", err)
	}
	return nil
}
Beispiel #5
0
func testInsertDefault2(engine *xorm.Engine, t *testing.T) {
	di := new(DefaultInsert2)
	err := engine.Sync2(di)
	if err != nil {
		t.Error(err)
	}

	var di2 = DefaultInsert2{Name: "test"}
	_, err = engine.Omit(engine.ColumnMapper.Obj2Table("CheckTime")).Insert(&di2)
	if err != nil {
		t.Error(err)
	}

	has, err := engine.Desc("(id)").Get(di)
	if err != nil {
		t.Error(err)
	}
	if !has {
		err = errors.New("error with no data")
		t.Error(err)
		panic(err)
	}

	has, err = engine.NoAutoCondition().Desc("(id)").Get(&di2)
	if err != nil {
		t.Error(err)
	}

	if !has {
		err = errors.New("error with no data")
		t.Error(err)
		panic(err)
	}

	if *di != di2 {
		err = fmt.Errorf("%v is not equal to %v", di, di2)
		t.Error(err)
		panic(err)
	}

	/*if di2.Updated.Unix() != di.Updated.Unix() {
		err = errors.New("updated should equal")
		t.Error(err, di.Updated, di2.Updated)
		panic(err)
	}
	if di2.Created.Unix() != di.Created.Unix() {
		err = errors.New("created should equal")
		t.Error(err, di.Created, di2.Created)
		panic(err)
	}*/
}
Beispiel #6
0
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)
}
Beispiel #7
0
func creatTables(Engine *xorm.Engine) error {

	if err := Engine.Sync2(new(User), new(Category), new(Node), new(Topic), new(Reply), new(Kvs), new(File)); err != nil {
		fmt.Println("Database struct sync failed")
		fmt.Println("Engine.Sync ERRORS:", err)
		return err
	} else {
		fmt.Println("Database struct sync successfully")
	}

	//用户等级划分:正数是普通用户,负数是管理员各种等级划分,为0则尚未注册
	if GetUserByRole(-1000).Role != -1000 {
		AddUser("*****@*****.**", "root", "系统默认管理员", helper.EncryptHash("rootpass", nil), -1000)
		fmt.Println("Default User:root,Password:rootpass")

		if GetAllTopic(0, 0, "id") == nil {
			//分類默認數據
			AddCategory("Category!", "This is Category!")

			AddNode("Node!", "This is Node!", 1, 1)
			SetTopic(0, 1, 1, 1, 0, "Topic Title", `<p>This is Topic!</p>`, "root", "")

		}
	}

	if GetKV("author") != "Insion" {
		SetKV("author", "Insion")
		SetKV("title", "Zenpress")
		SetKV("title_en", "Zenpress")
		SetKV("keywords", "Zenpress,")
		SetKV("description", "Zenpress,")

		SetKV("company", "Zenpress")
		SetKV("copyright", "2013 - 2015 Copyright Zenpress .All Right Reserved")
		SetKV("site_email", "*****@*****.**")

		SetKV("tweibo", "http://t.qq.com/yours")
		SetKV("sweibo", "http://weibo.com/yours")
	}
	return nil
}
Beispiel #8
0
func testInsertDefault(engine *xorm.Engine, t *testing.T) {
	di := new(DefaultInsert)
	err := engine.Sync2(di)
	if err != nil {
		t.Error(err)
	}

	var di2 = DefaultInsert{Name: "test"}
	_, err = engine.Omit(engine.ColumnMapper.Obj2Table("Status")).Insert(&di2)
	if err != nil {
		t.Error(err)
	}

	has, err := engine.Desc("(id)").Get(di)
	if err != nil {
		t.Error(err)
	}
	if !has {
		err = errors.New("error with no data")
		t.Error(err)
		panic(err)
	}
	if di.Status != -1 {
		err = errors.New("inserted error data")
		t.Error(err)
		panic(err)
	}
	if di2.Updated.Unix() != di.Updated.Unix() {
		err = errors.New("updated should equal")
		t.Error(err, di.Updated, di2.Updated)
		panic(err)
	}
	if di2.Created.Unix() != di.Created.Unix() {
		err = errors.New("created should equal")
		t.Error(err, di.Created, di2.Created)
		panic(err)
	}

	testInsertDefault2(engine, t)
}
Beispiel #9
0
func testUpdateUpdated(engine *xorm.Engine, t *testing.T) {
	di := new(UpdatedUpdate)
	err := engine.Sync2(di)
	if err != nil {
		t.Fatal(err)
	}

	_, err = engine.Insert(&UpdatedUpdate{})
	if err != nil {
		t.Fatal(err)
	}

	ci := &UpdatedUpdate{}
	_, err = engine.Id(1).Update(ci)
	if err != nil {
		t.Fatal(err)
	}

	has, err := engine.Id(1).Get(di)
	if err != nil {
		t.Fatal(err)
	}
	if !has {
		t.Fatal(xorm.ErrNotExist)
	}
	if ci.Updated.Unix() != di.Updated.Unix() {
		t.Fatal("should equal:", ci, di)
	}
	fmt.Println("ci:", ci, "di:", di)

	di2 := new(UpdatedUpdate2)
	err = engine.Sync2(di2)
	if err != nil {
		t.Fatal(err)
	}

	_, err = engine.Insert(&UpdatedUpdate2{})
	if err != nil {
		t.Fatal(err)
	}
	ci2 := &UpdatedUpdate2{}
	_, err = engine.Id(1).Update(ci2)
	if err != nil {
		t.Fatal(err)
	}
	has, err = engine.Id(1).Get(di2)
	if err != nil {
		t.Fatal(err)
	}
	if !has {
		t.Fatal(xorm.ErrNotExist)
	}
	if ci2.Updated != di2.Updated {
		t.Fatal("should equal:", ci2, di2)
	}
	fmt.Println("ci2:", ci2, "di2:", di2)

	di3 := new(UpdatedUpdate3)
	err = engine.Sync2(di3)
	if err != nil {
		t.Fatal(err)
	}

	_, err = engine.Insert(&UpdatedUpdate3{})
	if err != nil {
		t.Fatal(err)
	}
	ci3 := &UpdatedUpdate3{}
	_, err = engine.Id(1).Update(ci3)
	if err != nil {
		t.Fatal(err)
	}

	has, err = engine.Id(1).Get(di3)
	if err != nil {
		t.Fatal(err)
	}
	if !has {
		t.Fatal(xorm.ErrNotExist)
	}
	if ci3.Updated != di3.Updated {
		t.Fatal("should equal:", ci3, di3)
	}
	fmt.Println("ci3:", ci3, "di3:", di3)

	di4 := new(UpdatedUpdate4)
	err = engine.Sync2(di4)
	if err != nil {
		t.Fatal(err)
	}

	_, err = engine.Insert(&UpdatedUpdate4{})
	if err != nil {
		t.Fatal(err)
	}

	ci4 := &UpdatedUpdate4{}
	_, err = engine.Id(1).Update(ci4)
	if err != nil {
		t.Fatal(err)
	}

	has, err = engine.Id(1).Get(di4)
	if err != nil {
		t.Fatal(err)
	}
	if !has {
		t.Fatal(xorm.ErrNotExist)
	}
	if ci4.Updated != di4.Updated {
		t.Fatal("should equal:", ci4, di4)
	}
	fmt.Println("ci4:", ci4, "di4:", di4)

	di5 := new(UpdatedUpdate5)
	err = engine.Sync2(di5)
	if err != nil {
		t.Fatal(err)
	}

	_, err = engine.Insert(&UpdatedUpdate5{})
	if err != nil {
		t.Fatal(err)
	}
	ci5 := &UpdatedUpdate5{}
	_, err = engine.Id(1).Update(ci5)
	if err != nil {
		t.Fatal(err)
	}

	has, err = engine.Id(1).Get(di5)
	if err != nil {
		t.Fatal(err)
	}
	if !has {
		t.Fatal(xorm.ErrNotExist)
	}
	if ci5.Updated.Unix() != di5.Updated.Unix() {
		t.Fatal("should equal:", ci5, di5)
	}
	fmt.Println("ci5:", ci5, "di5:", di5)
}
Beispiel #10
0
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
}
Beispiel #11
0
func testInsertCreated(engine *xorm.Engine, t *testing.T) {
	di := new(CreatedInsert)
	err := engine.Sync2(di)
	if err != nil {
		t.Fatal(err)
	}
	ci := &CreatedInsert{}
	_, err = engine.Insert(ci)
	if err != nil {
		t.Fatal(err)
	}

	has, err := engine.Desc("(id)").Get(di)
	if err != nil {
		t.Fatal(err)
	}
	if !has {
		t.Fatal(xorm.ErrNotExist)
	}
	if ci.Created.Unix() != di.Created.Unix() {
		t.Fatal("should equal:", ci, di)
	}
	fmt.Println("ci:", ci, "di:", di)

	di2 := new(CreatedInsert2)
	err = engine.Sync2(di2)
	if err != nil {
		t.Fatal(err)
	}
	ci2 := &CreatedInsert2{}
	_, err = engine.Insert(ci2)
	if err != nil {
		t.Fatal(err)
	}
	has, err = engine.Desc("(id)").Get(di2)
	if err != nil {
		t.Fatal(err)
	}
	if !has {
		t.Fatal(xorm.ErrNotExist)
	}
	if ci2.Created != di2.Created {
		t.Fatal("should equal:", ci2, di2)
	}
	fmt.Println("ci2:", ci2, "di2:", di2)

	di3 := new(CreatedInsert3)
	err = engine.Sync2(di3)
	if err != nil {
		t.Fatal(err)
	}
	ci3 := &CreatedInsert3{}
	_, err = engine.Insert(ci3)
	if err != nil {
		t.Fatal(err)
	}
	has, err = engine.Desc("(id)").Get(di3)
	if err != nil {
		t.Fatal(err)
	}
	if !has {
		t.Fatal(xorm.ErrNotExist)
	}
	if ci3.Created != di3.Created {
		t.Fatal("should equal:", ci3, di3)
	}
	fmt.Println("ci3:", ci3, "di3:", di3)

	di4 := new(CreatedInsert4)
	err = engine.Sync2(di4)
	if err != nil {
		t.Fatal(err)
	}
	ci4 := &CreatedInsert4{}
	_, err = engine.Insert(ci4)
	if err != nil {
		t.Fatal(err)
	}
	has, err = engine.Desc("(id)").Get(di4)
	if err != nil {
		t.Fatal(err)
	}
	if !has {
		t.Fatal(xorm.ErrNotExist)
	}
	if ci4.Created != di4.Created {
		t.Fatal("should equal:", ci4, di4)
	}
	fmt.Println("ci4:", ci4, "di4:", di4)

	di5 := new(CreatedInsert5)
	err = engine.Sync2(di5)
	if err != nil {
		t.Fatal(err)
	}
	ci5 := &CreatedInsert5{}
	_, err = engine.Insert(ci5)
	if err != nil {
		t.Fatal(err)
	}
	has, err = engine.Desc("(id)").Get(di5)
	if err != nil {
		t.Fatal(err)
	}
	if !has {
		t.Fatal(xorm.ErrNotExist)
	}
	if ci5.Created.Unix() != di5.Created.Unix() {
		t.Fatal("should equal:", ci5, di5)
	}
	fmt.Println("ci5:", ci5, "di5:", di5)
}