9
9
"strings"
10
10
11
11
"github.com/go-xorm/builder"
12
- "github.com/go-xorm/xorm"
13
12
)
14
13
15
14
// RepositoryList contains a list of repositories
@@ -116,25 +115,21 @@ type SearchRepoOptions struct {
116
115
// SearchRepositoryByName takes keyword and part of repository name to search,
117
116
// it returns results in given range and number of total results.
118
117
func SearchRepositoryByName (opts * SearchRepoOptions ) (repos RepositoryList , count int64 , err error ) {
119
- var (
120
- sess * xorm.Session
121
- cond = builder .NewCond ()
122
- )
123
-
124
- opts .Keyword = strings .ToLower (opts .Keyword )
125
-
118
+ var cond = builder .NewCond ()
126
119
if opts .Page <= 0 {
127
120
opts .Page = 1
128
121
}
129
122
130
- repos = make ([]* Repository , 0 , opts .PageSize )
131
-
132
123
if opts .Starred && opts .OwnerID > 0 {
133
124
cond = builder.Eq {
134
125
"star.uid" : opts .OwnerID ,
135
126
}
136
127
}
137
- cond = cond .And (builder.Like {"lower_name" , opts .Keyword })
128
+
129
+ opts .Keyword = strings .ToLower (opts .Keyword )
130
+ if opts .Keyword != "" {
131
+ cond = cond .And (builder.Like {"lower_name" , opts .Keyword })
132
+ }
138
133
139
134
// Append conditions
140
135
if ! opts .Starred && opts .OwnerID > 0 {
@@ -158,48 +153,51 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, coun
158
153
ownerIds = append (ownerIds , org .ID )
159
154
}
160
155
161
- cond = cond .Or (builder .And (builder.Like {"lower_name" , opts .Keyword }, builder .In ("owner_id" , ownerIds )))
162
-
156
+ searcherReposCond := builder .In ("owner_id" , ownerIds )
163
157
if opts .Collaborate {
164
- cond = cond .Or (builder .Expr (`id IN (SELECT repo_id FROM "access" WHERE access.user_id = ? AND owner_id != ?)` , opts . Searcher . ID , opts . Searcher . ID ) ,
165
- builder . And (builder. Like { "lower_name" , opts .Keyword }, builder. Eq { "is_private" : opts . Private } ))
158
+ searcherReposCond = searcherReposCond .Or (builder .Expr (`id IN (SELECT repo_id FROM "access" WHERE access.user_id = ? AND owner_id != ?)` ,
159
+ opts . Searcher . ID , opts .Searcher . ID ))
166
160
}
161
+ cond = cond .And (searcherReposCond )
167
162
}
168
163
169
164
if len (opts .OrderBy ) == 0 {
170
165
opts .OrderBy = "name ASC"
171
166
}
172
167
168
+ sess := x .NewSession ()
169
+ defer sess .Close ()
170
+
173
171
if opts .Starred && opts .OwnerID > 0 {
174
- sess = x .
175
- Join ("INNER" , "star" , "star.repo_id = repository.id" ).
176
- Where (cond )
177
- count , err = x .
172
+ count , err = sess .
178
173
Join ("INNER" , "star" , "star.repo_id = repository.id" ).
179
174
Where (cond ).
180
175
Count (new (Repository ))
181
176
if err != nil {
182
177
return nil , 0 , fmt .Errorf ("Count: %v" , err )
183
178
}
179
+
180
+ sess .Join ("INNER" , "star" , "star.repo_id = repository.id" )
184
181
} else {
185
- sess = x .Where (cond )
186
- count , err = x .
182
+ count , err = sess .
187
183
Where (cond ).
188
184
Count (new (Repository ))
189
185
if err != nil {
190
186
return nil , 0 , fmt .Errorf ("Count: %v" , err )
191
187
}
192
188
}
193
189
190
+ repos = make ([]* Repository , 0 , opts .PageSize )
194
191
if err = sess .
192
+ Where (cond ).
195
193
Limit (opts .PageSize , (opts .Page - 1 )* opts .PageSize ).
196
194
OrderBy (opts .OrderBy ).
197
195
Find (& repos ); err != nil {
198
196
return nil , 0 , fmt .Errorf ("Repo: %v" , err )
199
197
}
200
198
201
199
if ! opts .IsProfile {
202
- if err = repos .loadAttributes (x ); err != nil {
200
+ if err = repos .loadAttributes (sess ); err != nil {
203
201
return nil , 0 , fmt .Errorf ("LoadAttributes: %v" , err )
204
202
}
205
203
}
0 commit comments