appview/db: add db helpers to count entries #542

merged
opened by oppi.li targeting master from push-mvmrzuxwmzvs
Changed files
+79 -68
appview
+4 -4
appview/db/follow.go
··· 56 56 } 57 57 58 58 type FollowStats struct { 59 - Followers int 60 - Following int 59 + Followers int64 60 + Following int64 61 61 } 62 62 63 63 func GetFollowerFollowingCount(e Execer, did string) (FollowStats, error) { 64 - followers, following := 0, 0 64 + var followers, following int64 65 65 err := e.QueryRow( 66 66 `SELECT 67 67 COUNT(CASE WHEN subject_did = ? THEN 1 END) AS followers, ··· 122 122 123 123 for rows.Next() { 124 124 var did string 125 - var followers, following int 125 + var followers, following int64 126 126 if err := rows.Scan(&did, &followers, &following); err != nil { 127 127 return nil, err 128 128 }
+25 -64
appview/db/repos.go
··· 2 2 3 3 import ( 4 4 "database/sql" 5 + "errors" 5 6 "fmt" 6 7 "log" 7 8 "slices" ··· 38 39 return p 39 40 } 40 41 41 - func GetAllRepos(e Execer, limit int) ([]Repo, error) { 42 - var repos []Repo 43 - 44 - rows, err := e.Query( 45 - `select did, name, knot, rkey, description, created, source 46 - from repos 47 - order by created desc 48 - limit ? 49 - `, 50 - limit, 51 - ) 52 - if err != nil { 53 - return nil, err 54 - } 55 - defer rows.Close() 56 - 57 - for rows.Next() { 58 - var repo Repo 59 - err := scanRepo( 60 - rows, &repo.Did, &repo.Name, &repo.Knot, &repo.Rkey, &repo.Description, &repo.Created, &repo.Source, 61 - ) 62 - if err != nil { 63 - return nil, err 64 - } 65 - repos = append(repos, repo) 66 - } 67 - 68 - if err := rows.Err(); err != nil { 69 - return nil, err 70 - } 71 - 72 - return repos, nil 73 - } 74 - 75 42 func GetRepos(e Execer, limit int, filters ...filter) ([]Repo, error) { 76 43 repoMap := make(map[syntax.ATURI]*Repo) 77 44 ··· 318 285 return repos, nil 319 286 } 320 287 288 + func CountRepos(e Execer, filters ...filter) (int64, error) { 289 + var conditions []string 290 + var args []any 291 + for _, filter := range filters { 292 + conditions = append(conditions, filter.Condition()) 293 + args = append(args, filter.Arg()...) 294 + } 295 + 296 + whereClause := "" 297 + if conditions != nil { 298 + whereClause = " where " + strings.Join(conditions, " and ") 299 + } 300 + 301 + repoQuery := fmt.Sprintf(`select count(1) from repos %s`, whereClause) 302 + var count int64 303 + err := e.QueryRow(repoQuery, args...).Scan(&count) 304 + 305 + if !errors.Is(err, sql.ErrNoRows) && err != nil { 306 + return 0, err 307 + } 308 + 309 + return count, nil 310 + } 311 + 321 312 func GetAllReposByDid(e Execer, did string) ([]Repo, error) { 322 313 var repos []Repo 323 314 ··· 570 561 IssueCount IssueCount 571 562 PullCount PullCount 572 563 } 573 - 574 - func scanRepo(rows *sql.Rows, did, name, knot, rkey, description *string, created *time.Time, source *string) error { 575 - var createdAt string 576 - var nullableDescription sql.NullString 577 - var nullableSource sql.NullString 578 - if err := rows.Scan(did, name, knot, rkey, &nullableDescription, &createdAt, &nullableSource); err != nil { 579 - return err 580 - } 581 - 582 - if nullableDescription.Valid { 583 - *description = nullableDescription.String 584 - } else { 585 - *description = "" 586 - } 587 - 588 - createdAtTime, err := time.Parse(time.RFC3339, createdAt) 589 - if err != nil { 590 - *created = time.Now() 591 - } else { 592 - *created = createdAtTime 593 - } 594 - 595 - if nullableSource.Valid { 596 - *source = nullableSource.String 597 - } else { 598 - *source = "" 599 - } 600 - 601 - return nil 602 - }
+26
appview/db/star.go
··· 1 1 package db 2 2 3 3 import ( 4 + "database/sql" 5 + "errors" 4 6 "fmt" 5 7 "log" 6 8 "strings" ··· 183 185 return stars, nil 184 186 } 185 187 188 + func CountStars(e Execer, filters ...filter) (int64, error) { 189 + var conditions []string 190 + var args []any 191 + for _, filter := range filters { 192 + conditions = append(conditions, filter.Condition()) 193 + args = append(args, filter.Arg()...) 194 + } 195 + 196 + whereClause := "" 197 + if conditions != nil { 198 + whereClause = " where " + strings.Join(conditions, " and ") 199 + } 200 + 201 + repoQuery := fmt.Sprintf(`select count(1) from stars %s`, whereClause) 202 + var count int64 203 + err := e.QueryRow(repoQuery, args...).Scan(&count) 204 + 205 + if !errors.Is(err, sql.ErrNoRows) && err != nil { 206 + return 0, err 207 + } 208 + 209 + return count, nil 210 + } 211 + 186 212 func GetAllStars(e Execer, limit int) ([]Star, error) { 187 213 var stars []Star 188 214
+24
appview/db/strings.go
··· 206 206 return all, nil 207 207 } 208 208 209 + func CountStrings(e Execer, filters ...filter) (int64, error) { 210 + var conditions []string 211 + var args []any 212 + for _, filter := range filters { 213 + conditions = append(conditions, filter.Condition()) 214 + args = append(args, filter.Arg()...) 215 + } 216 + 217 + whereClause := "" 218 + if conditions != nil { 219 + whereClause = " where " + strings.Join(conditions, " and ") 220 + } 221 + 222 + repoQuery := fmt.Sprintf(`select count(1) from strings %s`, whereClause) 223 + var count int64 224 + err := e.QueryRow(repoQuery, args...).Scan(&count) 225 + 226 + if !errors.Is(err, sql.ErrNoRows) && err != nil { 227 + return 0, err 228 + } 229 + 230 + return count, nil 231 + } 232 + 209 233 func DeleteString(e Execer, filters ...filter) error { 210 234 var conditions []string 211 235 var args []any