// 创建所有表结构 func createTables(db *orm.DB) error { // 创建表 tx, err := db.Begin() if err != nil { return err } err = tx.MultCreate( &Option{}, &Comment{}, &Tag{}, &Post{}, &Relationship{}, ) if err != nil { tx.Rollback() return err } err = tx.Commit() if err != nil { tx.Rollback() } return err }
func addTagsToSitemap(buf *bytes.Buffer, db *orm.DB, opt *app.Options) error { sql := "SELECT {id} AS ID, {name} AS Name, {title} AS Title, {description} AS Description FROM #tags" rows, err := db.Query(true, sql) if err != nil { return err } defer rows.Close() tags := make([]*front.Tag, 0, 100) if _, err := fetch.Obj(&tags, rows); err != nil { return err } now := time.Now().Unix() for _, tag := range tags { addItemToSitemap(buf, tag.Permalink(), opt.TagsChangefreq, now, opt.TagsPriority) } return nil }
func addPostsToSitemap(buf *bytes.Buffer, db *orm.DB, opt *app.Options) error { sql := `SELECT {id} AS ID, {name} AS Name, {title} AS Title, {summary} AS Summary, {content} AS Content, {created} AS Created, {modified} AS Modified FROM #posts WHERE {state}=?` rows, err := db.Query(true, sql, models.PostStatePublished) if err != nil { return err } defer rows.Close() posts := make([]*front.Post, 0, 100) if _, err := fetch.Obj(&posts, rows); err != nil { return err } for _, p := range posts { addItemToSitemap(buf, p.Permalink(), opt.PostsChangefreq, p.Modified, opt.PostsPriority) } return nil }
// 安装数据库和初始化默认的初始数据。 // options表的数据在options包中安装。 func Install(db *orm.DB) error { if db == nil { return errors.New("db==nil") } // 创建表 if err := createTables(db); err != nil { return err } // tags tag := &Tag{ Name: "default", Title: "默认标签", Description: "这是系统产生的默认标签", } if _, err := db.Insert(tag); err != nil { return err } // post now := time.Now().Unix() post := &Post{ Title: "第一篇日志", Content: "<p>这是你的第一篇日志</p>", State: PostStatePublished, Created: now, Modified: now, } if _, err := db.Insert(post); err != nil { return err } // comment comment := &Comment{ PostID: 1, Content: "<p>沙发</p>", AuthorName: "游客", State: CommentStateWaiting, } if _, err := db.Insert(comment); err != nil { return err } // relationship if _, err := db.Insert(&Relationship{TagID: 1, PostID: 1}); err != nil { return err } return nil }