Move db related basic functions to models/db (#17075)

* Move db related basic functions to models/db

* Fix lint

* Fix lint

* Fix test

* Fix lint

* Fix lint

* revert unnecessary change

* Fix test

* Fix wrong replace string

* Use *Context

* Correct committer spelling and fix wrong replaced words

Co-authored-by: zeripath <art27@cantab.net>
This commit is contained in:
Lunny Xiao 2021-09-19 19:49:59 +08:00 committed by GitHub
parent 462306e263
commit a4bfef265d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
335 changed files with 4191 additions and 3654 deletions

View file

@ -8,6 +8,7 @@ import (
"math"
"strings"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/timeutil"
"github.com/go-enry/go-enry/v2"
@ -26,6 +27,10 @@ type LanguageStat struct {
CreatedUnix timeutil.TimeStamp `xorm:"INDEX CREATED"`
}
func init() {
db.RegisterModel(new(LanguageStat))
}
// LanguageStatList defines a list of language statistics
type LanguageStatList []*LanguageStat
@ -60,7 +65,7 @@ func (stats LanguageStatList) getLanguagePercentages() map[string]float32 {
return langPerc
}
func (repo *Repository) getLanguageStats(e Engine) (LanguageStatList, error) {
func (repo *Repository) getLanguageStats(e db.Engine) (LanguageStatList, error) {
stats := make(LanguageStatList, 0, 6)
if err := e.Where("`repo_id` = ?", repo.ID).Desc("`size`").Find(&stats); err != nil {
return nil, err
@ -70,12 +75,12 @@ func (repo *Repository) getLanguageStats(e Engine) (LanguageStatList, error) {
// GetLanguageStats returns the language statistics for a repository
func (repo *Repository) GetLanguageStats() (LanguageStatList, error) {
return repo.getLanguageStats(x)
return repo.getLanguageStats(db.DefaultContext().Engine())
}
// GetTopLanguageStats returns the top language statistics for a repository
func (repo *Repository) GetTopLanguageStats(limit int) (LanguageStatList, error) {
stats, err := repo.getLanguageStats(x)
stats, err := repo.getLanguageStats(db.DefaultContext().Engine())
if err != nil {
return nil, err
}
@ -107,7 +112,7 @@ func (repo *Repository) GetTopLanguageStats(limit int) (LanguageStatList, error)
// UpdateLanguageStats updates the language statistics for repository
func (repo *Repository) UpdateLanguageStats(commitID string, stats map[string]int64) error {
sess := x.NewSession()
sess := db.DefaultContext().NewSession()
if err := sess.Begin(); err != nil {
return err
}
@ -178,7 +183,7 @@ func (repo *Repository) UpdateLanguageStats(commitID string, stats map[string]in
// CopyLanguageStat Copy originalRepo language stat information to destRepo (use for forked repo)
func CopyLanguageStat(originalRepo, destRepo *Repository) error {
sess := x.NewSession()
sess := db.DefaultContext().NewSession()
defer sess.Close()
if err := sess.Begin(); err != nil {
return err