func GetAllTopicByCidNid(cid int64, nid int64, offset int, limit int, ctype int64, path string) (allt []*Topic) { q, _ := ConnDb() defer q.Close() switch { case path == "asc": if ctype != 0 { condition := qbs.NewCondition("cid=?", cid).And("nid=?", nid).And("ctype=?", ctype) q.Condition(condition).Offset(offset).Limit(limit).FindAll(&allt) } else { condition := qbs.NewCondition("cid=?", cid).And("nid=?", nid) q.Condition(condition).Offset(offset).Limit(limit).FindAll(&allt) } default: if ctype != 0 { condition := qbs.NewCondition("cid=?", cid).And("nid=?", nid).And("ctype=?", ctype) q.Condition(condition).Offset(offset).Limit(limit).OrderByDesc(path).OrderByDesc("views").OrderByDesc("reply_count").OrderByDesc("created").FindAll(&allt) } else { condition := qbs.NewCondition("cid=?", cid).And("nid=?", nid) q.Condition(condition).Offset(offset).Limit(limit).OrderByDesc(path).OrderByDesc("views").OrderByDesc("reply_count").OrderByDesc("created").FindAll(&allt) } } return allt }
func FindUserByCondition(q *qbs.Qbs) (*User, error) { user := new(User) condition1 := qbs.NewCondition("id > ?", 100).Or("id < ?", 50).OrEqual("id", 75) condition2 := qbs.NewCondition("name != ?", "Red").And("name != ?", "Black") condition1.AndCondition(condition2) err := q.Condition(condition1).Find(user) return user, err }
func getPkgInfoWithQ(path, tag string, q *qbs.Qbs) (*hv.PkgInfo, error) { // Check path length to reduce connect times. if len(path) == 0 { return nil, errors.New("models.getPkgInfoWithQ -> Empty path as not found.") } pinfo := new(hv.PkgInfo) q.WhereEqual("import_path", path).Find(pinfo) proPath := utils.GetProjectPath(path) if utils.IsGoRepoPath(path) { proPath = "code.google.com/p/go" } beego.Trace("models.getPkgInfoWithQ -> proPath:", proPath) ptag := new(PkgTag) cond := qbs.NewCondition("path = ?", proPath).And("tag = ?", tag) err := q.Condition(cond).Find(ptag) if err != nil { pinfo.Ptag = "ptag" return pinfo, errors.New( fmt.Sprintf("models.getPkgInfoWithQ( %s:%s ) -> 'PkgTag': %s", path, tag, err)) } pinfo.Vcs = ptag.Vcs pinfo.Tags = ptag.Tags // Only 'PkgInfo' cannot prove that package exists, // we have to check 'PkgDecl' as well in case it was deleted by mistake. pdecl := new(PkgDecl) cond = qbs.NewCondition("pid = ?", pinfo.Id).And("tag = ?", tag) err = q.Condition(cond).Find(pdecl) if err != nil { // Basically, error means not found, so we set 'pinfo.PkgVer' to 0 // because server uses it to decide whether force update. pinfo.PkgVer = 0 pinfo.Ptag = "ptag" return pinfo, errors.New( fmt.Sprintf("models.getPkgInfoWithQ( %s:%s ) -> 'PkgDecl': %s", path, tag, err)) } docPath := path + utils.TagSuffix("-", tag) if !com.IsExist("." + utils.DocsJsPath + docPath + ".js") { pinfo.PkgVer = 0 pinfo.Ptag = "ptag" return pinfo, errors.New( fmt.Sprintf("models.getPkgInfoWithQ( %s:%s ) -> JS: File not found", path, tag)) } return pinfo, nil }
// SavePkgDoc saves readered readme.md file data. func SavePkgDoc(path string, readmes map[string][]byte) { q := connDb() defer q.Close() for lang, data := range readmes { if len(data) == 0 { continue } if data[0] == '\n' { data = data[1:] } pdoc := new(PkgDoc) cond := qbs.NewCondition("path = ?", path).And("lang = ?", lang).And("type = ?", "rm") q.Condition(cond).Find(pdoc) pdoc.Path = path pdoc.Lang = lang pdoc.Type = "rm" pdoc.Doc = base32.StdEncoding.EncodeToString(handleIllegalChars(data)) _, err := q.Save(pdoc) if err != nil { beego.Error("models.SavePkgDoc -> readme:", err) } } }
// GetPkgInfo returns package information. func GetPkgInfo(path, tag string) (*PkgInfo, error) { // Check path length to reduce connect times. if len(path) == 0 { return nil, errors.New("models.GetPkgInfo -> Empty path as not found.") } // Connect to database. q := connDb() defer q.Close() pinfo := new(PkgInfo) err := q.WhereEqual("path", path).Find(pinfo) if err != nil { return pinfo, errors.New("models.GetPkgInfo -> " + err.Error()) } pdecl := new(PkgDecl) cond := qbs.NewCondition("pid = ?", pinfo.Id).And("tag = ?", tag) err = q.Condition(cond).Find(pdecl) if err != nil { pinfo.Etag = "" err = errors.New("models.GetPkgInfo -> " + err.Error()) } return pinfo, err }
func GetAllNodeByCid(cid int64, offset int, limit int, ctype int64, path string) (alln []*Node) { //排序首先是热值优先,然后是时间优先。 q, _ := ConnDb() defer q.Close() switch { case path == "asc": if ctype != 0 { condition := qbs.NewCondition("pid=?", cid).And("ctype=?", ctype) q.Condition(condition).Offset(offset).Limit(limit).FindAll(&alln) } else { if cid == 0 { q.Offset(offset).Limit(limit).FindAll(&alln) } else { q.WhereEqual("pid", cid).Offset(offset).Limit(limit).FindAll(&alln) } } case path == "views" || path == "topic_count": if ctype != 0 { condition := qbs.NewCondition("pid=?", cid).And("ctype=?", ctype) q.Condition(condition).OrderByDesc(path).Offset(offset).Limit(limit).FindAll(&alln) } else { if cid == 0 { q.OrderByDesc(path).Offset(offset).Limit(limit).FindAll(&alln) } else { q.WhereEqual("pid", cid).OrderByDesc(path).Offset(offset).Limit(limit).FindAll(&alln) } } default: if ctype != 0 { condition := qbs.NewCondition("pid=?", cid).And("ctype=?", ctype) q.Condition(condition).Offset(offset).Limit(limit).OrderByDesc(path).OrderByDesc("views").OrderByDesc("topic_count").OrderByDesc("created").FindAll(&alln) } else { if cid == 0 { q.Offset(offset).Limit(limit).OrderByDesc(path).OrderByDesc("views").OrderByDesc("topic_count").OrderByDesc("created").FindAll(&alln) } else { q.WhereEqual("pid", cid).Offset(offset).Limit(limit).OrderByDesc(path).OrderByDesc("views").OrderByDesc("topic_count").OrderByDesc("created").FindAll(&alln) } } } return alln }
// SearchRawDoc returns results for raw page, // which are package that import path and synopsis contains keyword. func SearchRawDoc(key string, isMatchSub bool) (pkgInfos []*PkgInfo, err error) { // Connect to database. q := connDb() defer q.Db.Close() // Check if need to match sub-packages. if isMatchSub { condition := qbs.NewCondition("pro_name != ?", "Go") condition2 := qbs.NewCondition("path like ?", "%"+key+"%").Or("synopsis like ?", "%"+key+"%") err = q.Condition(condition).Condition(condition2).Limit(50).OrderByDesc("views").FindAll(&pkgInfos) return pkgInfos, err } condition := qbs.NewCondition("pro_name like ?", "%"+key+"%").Or("synopsis like ?", "%"+key+"%") err = q.Condition(condition).Limit(50).OrderByDesc("views").FindAll(&pkgInfos) return pkgInfos, err }
// SearchPkg returns packages that import path and synopsis contains keyword. func SearchPkg(key string) []*hv.PkgInfo { q := connDb() defer q.Close() var pinfos []*hv.PkgInfo cond := qbs.NewCondition("import_path like ?", "%"+key+"%").Or("synopsis like ?", "%"+key+"%") q.Limit(200).Condition(cond).OrderByDesc("rank").FindAll(&pinfos) return pinfos }
//查询明细信息 func (this *EpisodeController) QueryDetail(id int64) revel.Result { episode := new(models.Episode) condition := qbs.NewCondition("id = ?", id) this.q.Condition(condition).Find(episode) user := FindUserById(this.q, episode.Author) episode.User = user this.RenderArgs["episode"] = episode return this.RenderTemplate("article/detailEpisode.html") }
// GetGoRepo returns packages in go standard library. func GetGoRepo() ([]*PkgInfo, error) { // Connect to database. q := connDb() defer q.Db.Close() var pkgInfos []*PkgInfo condition := qbs.NewCondition("pro_name = ?", "Go") err := q.Condition(condition).OrderBy("path").FindAll(&pkgInfos) return pkgInfos, err }
// LoadPkgDoc loads project introduction documentation. func LoadPkgDoc(path, lang, docType string) (doc string) { // Connect to database. q := connDb() defer q.Close() pdoc := new(PkgDoc) cond := qbs.NewCondition("path = ?", path).And("lang = ?", lang).And("type = ?", docType) err := q.Condition(cond).Find(pdoc) if err == nil { return pdoc.Doc } cond = qbs.NewCondition("path = ?", path).And("lang = ?", "en").And("type = ?", docType) err = q.Condition(cond).Find(pdoc) if err == nil { return pdoc.Doc } return doc }
// SearchDoc returns packages that import path contains keyword. func SearchDoc(key string) ([]*PkgInfo, error) { // Connect to database. q := connDb() defer q.Db.Close() var pkgInfos []*PkgInfo condition := qbs.NewCondition("path like ?", "%"+key+"%") err := q.Condition(condition).OrderBy("path").FindAll(&pkgInfos) return pkgInfos, err }
func (c *Category) HasName() bool { q, err := qbs.GetQbs() if err != nil { fmt.Println(err) } defer q.Close() category := new(Category) condition := qbs.NewCondition("name = ?", c.Name) if c.Id > 0 { condition = qbs.NewCondition("name = ?", c.Name).And("id != ?", c.Id) } err = q.Condition(condition).Find(category) if category.Id > 0 { return true } return false }
// SearchPkg returns packages that import path and synopsis contains keyword. func SearchPkg(key string) []*PkgInfo { q := connDb() defer q.Close() var pinfos []*PkgInfo cond := qbs.NewCondition("path like ?", "%"+key+"%").Or("synopsis like ?", "%"+key+"%") q.OmitFields("ProName", "IsCmd", "Tags", "Views", "ViewedTime", "Created", "Etag", "Labels", "ImportedNum", "ImportPid", "Note"). Limit(200).Condition(cond).OrderByDesc("rank").FindAll(&pinfos) return pinfos }
// SavePkgDoc saves readered readme.md file data. func SavePkgDoc(path, lang string, docBys []byte) { // Connect to database. q := connDb() defer q.Close() // Reader readme. doc := string(docBys) if len(doc) == 0 { return } if doc[0] == '\n' { doc = doc[1:] } // Remove title and `==========`. doc = doc[strings.Index(doc, "\n")+1:] if len(doc) == 0 { return } if doc[0] == '=' { doc = doc[strings.Index(doc, "\n")+1:] } // Find all picture path of build system. HAVE BUG!!! for _, m := range buildPicPattern.FindAllString(doc, -1) { start := strings.Index(m, "http") end := strings.Index(m, ")") if (start > -1) && (end > -1) && (start < end) { picPath := m[start:end] doc = strings.Replace(doc, m, "![]("+picPath+")", 1) } } doc = string(blackfriday.MarkdownCommon([]byte(doc))) doc = strings.Replace(doc, "h3>", "h5>", -1) doc = strings.Replace(doc, "h2>", "h4>", -1) doc = strings.Replace(doc, "h1>", "h3>", -1) doc = strings.Replace(doc, "<center>", "", -1) doc = strings.Replace(doc, "</center>", "", -1) doc = "<div style='display:block; padding: 3px; border:1px solid #4F4F4F;'>" + doc + "</div>" pdoc := new(PkgDoc) cond := qbs.NewCondition("path = ?", path).And("lang = ?", lang).And("type = ?", "rm") q.Condition(cond).Find(pdoc) pdoc.Path = path pdoc.Lang = lang pdoc.Type = "rm" pdoc.Doc = base32.StdEncoding.EncodeToString([]byte(doc)) _, err := q.Save(pdoc) if err != nil { beego.Error("models.SavePkgDoc -> readme:", err) } }
// SaveProject save package information, declaration, documentation to database, and update import information. func SaveProject(pinfo *PkgInfo, pdecl *PkgDecl, pdoc *PkgDoc, imports []string) error { // Connect to database. q := connDb() defer q.Close() // Save package information. info := new(PkgInfo) err := q.WhereEqual("path", pinfo.Path).Find(info) if err == nil { pinfo.Id = info.Id } _, err = q.Save(pinfo) if err != nil { beego.Error("models.SaveProject -> Information:", err) } // Save package declaration if pdecl != nil { decl := new(PkgDecl) cond := qbs.NewCondition("path = ?", pinfo.Path).And("tag = ?", pdecl.Tag) err = q.Condition(cond).Find(decl) if err == nil { pdecl.Id = decl.Id } _, err = q.Save(pdecl) if err != nil { beego.Error("models.SaveProject -> Declaration:", err) } } // Save package documentation if pdoc != nil && len(pdoc.Doc) > 0 { _, err = q.Save(pdoc) if err != nil { beego.Error("models.SaveProject -> Documentation:", err) } } // Don't need to check standard library. if imports != nil && !utils.IsGoRepoPath(pinfo.Path) { // Update import information. for _, v := range imports { if !utils.IsGoRepoPath(v) { // Only count non-standard library. updateImportInfo(q, v, int(pinfo.Id), true) } } } return nil }
func GetImports(pid, tag string) []*hv.PkgInfo { q := connDb() defer q.Close() pdecl := new(PkgDecl) cond := qbs.NewCondition("pid = ?", pid).And("tag = ?", tag) err := q.Condition(cond).Find(pdecl) if err != nil { return nil } return getGroupPkgInfoWithQ(q, strings.Split(pdecl.Imports, "|")) }
// SearchDoc returns packages information that contain keyword func SearchDoc(key string) ([]*PkgInfo, error) { // Connect to database. q, err := connDb() if err != nil { beego.Error("models.SearchDoc():", err) } defer q.Db.Close() var pkgInfos []*PkgInfo condition := qbs.NewCondition("path like ?", "%"+key+"%") err = q.Condition(condition).OrderBy("path").FindAll(&pkgInfos) return pkgInfos, err }
// GetGoRepo returns packages in go standard library. func GetGoRepo() ([]*PkgInfo, error) { // Connect to database. q, err := connDb() if err != nil { beego.Error("models.GetGoRepo():", err) } defer q.Db.Close() var pkgInfos []*PkgInfo condition := qbs.NewCondition("pro_name = ?", "Go") err = q.Condition(condition).OrderBy("path").FindAll(&pkgInfos) return pkgInfos, err }
func SearchTopic(content string, offset int, limit int, path string) (allt []*Topic) { //排序首先是热值优先,然后是时间优先。 if content != "" { q, _ := ConnDb() defer q.Close() keyword := "%" + content + "%" condition := qbs.NewCondition("title like ?", keyword).Or("content like ?", keyword) q.Condition(condition).Offset(offset).Limit(limit).OrderByDesc(path).OrderByDesc("views").OrderByDesc("reply_count").OrderByDesc("created").FindAll(&allt) //q.Where("title like ?", keyword).Offset(offset).Limit(limit).OrderByDesc(path).OrderByDesc("created").FindAll(&allt) return allt } return nil }
func CheckSignin(name, password string) *User { q, err := qbs.GetQbs() if err != nil { fmt.Println(err) } defer q.Close() user := new(User) condition := qbs.NewCondition("name = ?", name).And( "hashed_password = ?", EncryptPassword(password)) q.Condition(condition).Find(user) return user }
// LoadProject returns package declaration. func LoadProject(pid int64, tag string) (*PkgDecl, error) { // Check path length to reduce connect times. if pid == 0 { return nil, errors.New("models.LoadProject -> Zero id.") } q := connDb() defer q.Close() pdecl := new(PkgDecl) cond := qbs.NewCondition("pid = ?", pid).And("tag = ?", tag) err := q.Condition(cond).Find(pdecl) return pdecl, err }
func GetAllTopicByNid(nodeid int64, offset int, limit int, ctype int64, path string) (allt []*Topic) { //排序首先是热值优先,然后是时间优先。 q, _ := ConnDb() defer q.Close() switch { case path == "asc": if nodeid == 0 { //q.Offset(offset).Limit(limit).OrderByDesc(path).OrderByDesc("views").OrderByDesc("reply_count").OrderByDesc("created").FindAll(&allt) return nil } else { if ctype != 0 { condition := qbs.NewCondition("nid=?", nodeid).And("ctype=?", ctype) q.Condition(condition).Offset(offset).Limit(limit).FindAll(&allt) } else { q.Where("nid=?", nodeid).Offset(offset).Limit(limit).FindAll(&allt) } } default: if nodeid == 0 { //q.Offset(offset).Limit(limit).OrderByDesc(path).OrderByDesc("views").OrderByDesc("reply_count").OrderByDesc("created").FindAll(&allt) return nil } else { if ctype != 0 { condition := qbs.NewCondition("nid=?", nodeid).And("ctype=?", ctype) q.Condition(condition).Offset(offset).Limit(limit).OrderByDesc(path).OrderByDesc("views").OrderByDesc("reply_count").OrderByDesc("created").FindAll(&allt) } else { q.Where("nid=?", nodeid).Offset(offset).Limit(limit).OrderByDesc(path).OrderByDesc("views").OrderByDesc("reply_count").OrderByDesc("created").FindAll(&allt) } } } return allt }
// SavePkgExam saves user examples. func SavePkgExam(gist *PkgExam) error { q := connDb() defer q.Close() // Check if corresponding package exists. pinfo := new(hv.PkgInfo) err := q.WhereEqual("import_path", gist.Path).Find(pinfo) if err != nil { return errors.New( fmt.Sprintf("models.SavePkgExam( %s ) -> Package does not exist", gist.Path)) } pexam := new(PkgExam) cond := qbs.NewCondition("path = ?", gist.Path).And("gist = ?", gist.Gist) err = q.Condition(cond).Find(pexam) if err == nil { // Check if refresh too frequently(within in 5 minutes). if pexam.Created.Add(5 * time.Minute).UTC().After(time.Now().UTC()) { return errors.New( fmt.Sprintf("models.SavePkgExam( %s ) -> Refresh too frequently(within in 5 minutes)", gist.Path)) } gist.Id = pexam.Id } gist.Created = time.Now().UTC() _, err = q.Save(gist) if err != nil { return errors.New( fmt.Sprintf("models.SavePkgExam( %s ) -> %s", gist.Path, err)) } // Delete 'PkgDecl' in order to generate new page. cond = qbs.NewCondition("pid = ?", pinfo.Id).And("tag = ?", "") q.Condition(cond).Delete(new(PkgDecl)) return nil }
// GetIndexPageInfo returns all data that used for index page. // One function is for reducing database connect times. func GetIndexPageInfo() (totalNum int64, popPkgs, importedPkgs, WFPros, ORMPros, DBDPros, GUIPros, NETPros []*PkgInfo, err error) { // Connect to database. q := connDb() defer q.Db.Close() totalNum = q.Count(&PkgInfo{}) err = q.Offset(25).Limit(39).OrderByDesc("views").FindAll(&popPkgs) if err != nil { beego.Error("models.GetIndexPageInfo(): popPkgs:", err) } err = q.Limit(20).OrderByDesc("imported_num").OrderByDesc("views").FindAll(&importedPkgs) condition := qbs.NewCondition("tags like ?", "%$wf|%") err = q.Limit(10).Condition(condition).OrderByDesc("views").FindAll(&WFPros) condition = qbs.NewCondition("tags like ?", "%$orm|%") err = q.Limit(10).Condition(condition).OrderByDesc("views").FindAll(&ORMPros) condition = qbs.NewCondition("tags like ?", "%$dbd|%") err = q.Limit(10).Condition(condition).OrderByDesc("views").FindAll(&DBDPros) condition = qbs.NewCondition("tags like ?", "%$gui|%") err = q.Limit(10).Condition(condition).OrderByDesc("views").FindAll(&GUIPros) condition = qbs.NewCondition("tags like ?", "%$net|%") err = q.Limit(10).Condition(condition).OrderByDesc("views").FindAll(&NETPros) return totalNum, popPkgs, importedPkgs, WFPros, ORMPros, DBDPros, GUIPros, NETPros, nil }
// LoadPkgDoc loads project introduction documentation. func LoadPkgDoc(path, lang, docType string) (doc string) { q := connDb() defer q.Close() if len(lang) < 2 { return "" } lang = lang[:2] pdoc := new(PkgDoc) cond := qbs.NewCondition("path = ?", path).And("lang = ?", lang).And("type = ?", docType) err := q.Condition(cond).Find(pdoc) if err == nil { return pdoc.Doc } cond = qbs.NewCondition("path = ?", path).And("lang = ?", "en").And("type = ?", docType) err = q.Condition(cond).Find(pdoc) if err == nil { return pdoc.Doc } return doc }
// LoadProject gets package declaration from database. func LoadProject(path, tag string) (*PkgDecl, error) { // Check path length to reduce connect times. if len(path) == 0 { return nil, errors.New("models.LoadProject -> Empty path as not found.") } // Connect to database. q := connDb() defer q.Close() pdecl := new(PkgDecl) cond := qbs.NewCondition("path = ?", path).And("tag = ?", tag) err := q.Condition(cond).Find(pdecl) return pdecl, err }
// GetGoRepo returns packages in go standard library. func GetGoRepo() ([]*PkgInfo, error) { // Connect to database. q := connDb() defer q.Close() var pkgInfos []*PkgInfo condition := qbs.NewCondition("pro_name = ?", "Go") err := q.Condition(condition).OrderBy("path").FindAll(&pkgInfos) infos := make([]*PkgInfo, 0, 30) for _, v := range pkgInfos { if strings.Index(v.Path, ".") == -1 { infos = append(infos, v) } } return infos, err }
// SavePkgExam saves user examples to database. func SavePkgExam(gist *PkgExam) { // Connect to database. q := connDb() defer q.Close() pexam := new(PkgExam) cond := qbs.NewCondition("path = ?", gist.Path).And("gist = ?", gist.Gist) err := q.Condition(cond).Find(pexam) if err == nil { gist.Id = pexam.Id } gist.Created = time.Now().UTC() _, err = q.Save(gist) if err != nil { beego.Error("models.SavePkgExam -> ", gist.Path, err) } }
func main() { var ( q *qbs.Qbs ) q = SetupDb() defer q.Close() //START SETUP OMIT q.Save(&User{ Id: 1, FirstName: "John", LastName: "Doe", Age: 24, }) q.Save(&User{ Id: 2, FirstName: "Jane", LastName: "Doe", Age: 52, }) q.Save(&User{ Id: 3, FirstName: "Joe", LastName: "Shmoe", Age: 10, }) //END SETUP OMIT //START CODE OMIT var users []*User q.Condition(qbs.NewCondition("last_name = ?", "Doe").Or("age < ?", 12)).Limit(2).OrderByDesc("age").FindAll(&users) for _, user := range users { fmt.Printf("%+v,", user) } fmt.Println() //END CODE OMIT }