aboutsummaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2025-03-04 10:25:20 -0800
committerGitHub <noreply@github.com>2025-03-04 18:25:20 +0000
commit6c8fb8d455cfe25d5aa966674624bce99fba1735 (patch)
treefdc466b99aa508a8738960200d2f0a1bbaf0b620 /models
parent75e85c25c1674627ef750bf7114d3b0b7c0d15aa (diff)
downloadgitea-6c8fb8d455cfe25d5aa966674624bce99fba1735.tar.gz
gitea-6c8fb8d455cfe25d5aa966674624bce99fba1735.zip
Small refactor to reduce unnecessary database queries and remove duplicated functions (#33779)
Diffstat (limited to 'models')
-rw-r--r--models/actions/schedule.go9
-rw-r--r--models/actions/schedule_spec_list.go2
-rw-r--r--models/db/context.go3
-rw-r--r--models/issues/issue.go3
-rw-r--r--models/issues/label.go9
-rw-r--r--models/organization/team_list.go3
-rw-r--r--models/project/column.go3
-rw-r--r--models/repo/repo.go3
-rw-r--r--models/repo/repo_list.go5
-rw-r--r--models/user/user_list.go4
10 files changed, 32 insertions, 12 deletions
diff --git a/models/actions/schedule.go b/models/actions/schedule.go
index e2cc32eedc..fcdc7c2a4c 100644
--- a/models/actions/schedule.go
+++ b/models/actions/schedule.go
@@ -43,15 +43,12 @@ func init() {
// GetSchedulesMapByIDs returns the schedules by given id slice.
func GetSchedulesMapByIDs(ctx context.Context, ids []int64) (map[int64]*ActionSchedule, error) {
schedules := make(map[int64]*ActionSchedule, len(ids))
+ if len(ids) == 0 {
+ return schedules, nil
+ }
return schedules, db.GetEngine(ctx).In("id", ids).Find(&schedules)
}
-// GetReposMapByIDs returns the repos by given id slice.
-func GetReposMapByIDs(ctx context.Context, ids []int64) (map[int64]*repo_model.Repository, error) {
- repos := make(map[int64]*repo_model.Repository, len(ids))
- return repos, db.GetEngine(ctx).In("id", ids).Find(&repos)
-}
-
// CreateScheduleTask creates new schedule task.
func CreateScheduleTask(ctx context.Context, rows []*ActionSchedule) error {
// Return early if there are no rows to insert
diff --git a/models/actions/schedule_spec_list.go b/models/actions/schedule_spec_list.go
index f7dac72f8b..e26b2c1120 100644
--- a/models/actions/schedule_spec_list.go
+++ b/models/actions/schedule_spec_list.go
@@ -32,7 +32,7 @@ func (specs SpecList) LoadSchedules(ctx context.Context) error {
}
repoIDs := specs.GetRepoIDs()
- repos, err := GetReposMapByIDs(ctx, repoIDs)
+ repos, err := repo_model.GetRepositoriesMapByIDs(ctx, repoIDs)
if err != nil {
return err
}
diff --git a/models/db/context.go b/models/db/context.go
index 51627712b1..4b98796ef0 100644
--- a/models/db/context.go
+++ b/models/db/context.go
@@ -289,6 +289,9 @@ func FindIDs(ctx context.Context, tableName, idCol string, cond builder.Cond) ([
// DecrByIDs decreases the given column for entities of the "bean" type with one of the given ids by one
// Timestamps of the entities won't be updated
func DecrByIDs(ctx context.Context, ids []int64, decrCol string, bean any) error {
+ if len(ids) == 0 {
+ return nil
+ }
_, err := GetEngine(ctx).Decr(decrCol).In("id", ids).NoAutoCondition().NoAutoTime().Update(bean)
return err
}
diff --git a/models/issues/issue.go b/models/issues/issue.go
index 7e72bb776c..5204f27faf 100644
--- a/models/issues/issue.go
+++ b/models/issues/issue.go
@@ -595,6 +595,9 @@ func GetIssueByID(ctx context.Context, id int64) (*Issue, error) {
// If keepOrder is true, the order of the returned issues will be the same as the given IDs.
func GetIssuesByIDs(ctx context.Context, issueIDs []int64, keepOrder ...bool) (IssueList, error) {
issues := make([]*Issue, 0, len(issueIDs))
+ if len(issueIDs) == 0 {
+ return issues, nil
+ }
if err := db.GetEngine(ctx).In("id", issueIDs).Find(&issues); err != nil {
return nil, err
diff --git a/models/issues/label.go b/models/issues/label.go
index b9d24bbe99..8a5d9321cc 100644
--- a/models/issues/label.go
+++ b/models/issues/label.go
@@ -299,6 +299,9 @@ func GetLabelByID(ctx context.Context, labelID int64) (*Label, error) {
// GetLabelsByIDs returns a list of labels by IDs
func GetLabelsByIDs(ctx context.Context, labelIDs []int64, cols ...string) ([]*Label, error) {
labels := make([]*Label, 0, len(labelIDs))
+ if len(labelIDs) == 0 {
+ return labels, nil
+ }
return labels, db.GetEngine(ctx).Table("label").
In("id", labelIDs).
Asc("name").
@@ -375,6 +378,9 @@ func BuildLabelNamesIssueIDsCondition(labelNames []string) *builder.Builder {
// it silently ignores label IDs that do not belong to the repository.
func GetLabelsInRepoByIDs(ctx context.Context, repoID int64, labelIDs []int64) ([]*Label, error) {
labels := make([]*Label, 0, len(labelIDs))
+ if len(labelIDs) == 0 {
+ return labels, nil
+ }
return labels, db.GetEngine(ctx).
Where("repo_id = ?", repoID).
In("id", labelIDs).
@@ -447,6 +453,9 @@ func GetLabelInOrgByID(ctx context.Context, orgID, labelID int64) (*Label, error
// it silently ignores label IDs that do not belong to the organization.
func GetLabelsInOrgByIDs(ctx context.Context, orgID int64, labelIDs []int64) ([]*Label, error) {
labels := make([]*Label, 0, len(labelIDs))
+ if len(labelIDs) == 0 {
+ return labels, nil
+ }
return labels, db.GetEngine(ctx).
Where("org_id = ?", orgID).
In("id", labelIDs).
diff --git a/models/organization/team_list.go b/models/organization/team_list.go
index 6f2a922e95..0274f9c5ba 100644
--- a/models/organization/team_list.go
+++ b/models/organization/team_list.go
@@ -133,5 +133,8 @@ func GetTeamsByOrgIDs(ctx context.Context, orgIDs []int64) (TeamList, error) {
func GetTeamsByIDs(ctx context.Context, teamIDs []int64) (map[int64]*Team, error) {
teams := make(map[int64]*Team, len(teamIDs))
+ if len(teamIDs) == 0 {
+ return teams, nil
+ }
return teams, db.GetEngine(ctx).Where(builder.In("`id`", teamIDs)).Find(&teams)
}
diff --git a/models/project/column.go b/models/project/column.go
index 5f581b5880..77ff5ef83e 100644
--- a/models/project/column.go
+++ b/models/project/column.go
@@ -336,6 +336,9 @@ func UpdateColumnSorting(ctx context.Context, cl ColumnList) error {
func GetColumnsByIDs(ctx context.Context, projectID int64, columnsIDs []int64) (ColumnList, error) {
columns := make([]*Column, 0, 5)
+ if len(columnsIDs) == 0 {
+ return columns, nil
+ }
if err := db.GetEngine(ctx).
Where("project_id =?", projectID).
In("id", columnsIDs).
diff --git a/models/repo/repo.go b/models/repo/repo.go
index d42792faa2..13473699f3 100644
--- a/models/repo/repo.go
+++ b/models/repo/repo.go
@@ -839,6 +839,9 @@ func GetRepositoryByID(ctx context.Context, id int64) (*Repository, error) {
// GetRepositoriesMapByIDs returns the repositories by given id slice.
func GetRepositoriesMapByIDs(ctx context.Context, ids []int64) (map[int64]*Repository, error) {
repos := make(map[int64]*Repository, len(ids))
+ if len(ids) == 0 {
+ return repos, nil
+ }
return repos, db.GetEngine(ctx).In("id", ids).Find(&repos)
}
diff --git a/models/repo/repo_list.go b/models/repo/repo_list.go
index 9bed2e9197..02c228e8a0 100644
--- a/models/repo/repo_list.go
+++ b/models/repo/repo_list.go
@@ -21,11 +21,6 @@ import (
"xorm.io/builder"
)
-// FindReposMapByIDs find repos as map
-func FindReposMapByIDs(ctx context.Context, repoIDs []int64, res map[int64]*Repository) error {
- return db.GetEngine(ctx).In("id", repoIDs).Find(&res)
-}
-
// RepositoryListDefaultPageSize is the default number of repositories
// to load in memory when running administrative tasks on all (or almost
// all) of them.
diff --git a/models/user/user_list.go b/models/user/user_list.go
index c66d59f0d9..4241905058 100644
--- a/models/user/user_list.go
+++ b/models/user/user_list.go
@@ -11,6 +11,10 @@ import (
func GetUsersMapByIDs(ctx context.Context, userIDs []int64) (map[int64]*User, error) {
userMaps := make(map[int64]*User, len(userIDs))
+ if len(userIDs) == 0 {
+ return userMaps, nil
+ }
+
left := len(userIDs)
for left > 0 {
limit := db.DefaultMaxInSize