コード例 #1
0
ファイル: initDB.go プロジェクト: ohko/gmdd
func runInit(ctx *cli.Context) {

	pass := ctx.String("password")
	if pass == "" {
		fmt.Println("Please password: --password ******")
		return
	}

	libs.DB.Exec("SET FOREIGN_KEY_CHECKS = 0")

	// drop
	if err := libs.DB.DropTables(
		new(model.Category),
		new(model.Tag),
		new(model.Md),
		new(model.MdTag),
		new(model.User),
	); err != nil {
		panic(err.Error())
	}

	// create
	if err := libs.DB.Sync2(
		new(model.Category),
		new(model.Tag),
		new(model.Md),
		new(model.MdTag),
		new(model.User),
	); err != nil {
		panic(err.Error())
	}

	pass_md5 := md5.New()
	pass_md5.Write([]byte(pass))

	user := new(model.User)
	user.User = "******"
	user.Pass = hex.EncodeToString(pass_md5.Sum(nil))
	libs.DB.Insert(user)

	cat := new(model.Category)
	cat.Id = 1
	cat.Name = "default"
	libs.DB.Insert(cat)

	libs.DB.Exec("ALTER TABLE `" + libs.Conf["PREFIX"] + "md` ADD FOREIGN KEY (`category_id`) REFERENCES `" + libs.Conf["PREFIX"] + "category` (`id`)")
	libs.DB.Exec("ALTER TABLE `" + libs.Conf["PREFIX"] + "md_tag` ADD FOREIGN KEY (`md_id`) REFERENCES `" + libs.Conf["PREFIX"] + "md` (`id`)")
	libs.DB.Exec("ALTER TABLE `" + libs.Conf["PREFIX"] + "md_tag` ADD FOREIGN KEY (`tag_id`) REFERENCES `" + libs.Conf["PREFIX"] + "tag` (`id`)")
	libs.DB.Exec("SET FOREIGN_KEY_CHECKS = 1")
}
コード例 #2
0
ファイル: import.go プロジェクト: ohko/gmdd
func saveToDB(category, title, content string) string {

	// find category id
	category_id := 1
	cat := new(model.Category)
	if has, _ := libs.DB.Where("name=?", category).Get(cat); !has {
		cat.Name = category
		libs.DB.Insert(cat)
	}
	category_id = cat.Id

	// make md
	md := new(model.Md)
	md.Title = title
	md.Content = content
	md.CategoryId = category_id
	md.Draft = "N"

	// insert
	if _, err := libs.DB.Insert(md); err == nil {
		return fmt.Sprintf("%d.%s [%s]", md.Id, md.Title, category)
	}
	return "error"
}