/internal/database/database.go

https://github.com/go-shiori/shiori · Go · 82 lines · 44 code · 19 blank · 19 comment · 4 complexity · 9a2e92c8d0602314121d73670f9d5b97 MD5 · raw file

  1. package database
  2. import (
  3. "database/sql"
  4. "github.com/go-shiori/shiori/internal/model"
  5. )
  6. // OrderMethod is the order method for getting bookmarks
  7. type OrderMethod int
  8. const (
  9. // DefaultOrder is oldest to newest.
  10. DefaultOrder OrderMethod = iota
  11. // ByLastAdded is from newest addition to the oldest.
  12. ByLastAdded
  13. // ByLastModified is from latest modified to the oldest.
  14. ByLastModified
  15. )
  16. // GetBookmarksOptions is options for fetching bookmarks from database.
  17. type GetBookmarksOptions struct {
  18. IDs []int
  19. Tags []string
  20. ExcludedTags []string
  21. Keyword string
  22. WithContent bool
  23. OrderMethod OrderMethod
  24. Limit int
  25. Offset int
  26. }
  27. // GetAccountsOptions is options for fetching accounts from database.
  28. type GetAccountsOptions struct {
  29. Keyword string
  30. Owner bool
  31. }
  32. // DB is interface for accessing and manipulating data in database.
  33. type DB interface {
  34. // SaveBookmarks saves bookmarks data to database.
  35. SaveBookmarks(bookmarks ...model.Bookmark) ([]model.Bookmark, error)
  36. // GetBookmarks fetch list of bookmarks based on submitted options.
  37. GetBookmarks(opts GetBookmarksOptions) ([]model.Bookmark, error)
  38. // GetBookmarksCount get count of bookmarks in database.
  39. GetBookmarksCount(opts GetBookmarksOptions) (int, error)
  40. // DeleteBookmarks removes all record with matching ids from database.
  41. DeleteBookmarks(ids ...int) error
  42. // GetBookmark fetchs bookmark based on its ID or URL.
  43. GetBookmark(id int, url string) (model.Bookmark, bool)
  44. // SaveAccount saves new account in database
  45. SaveAccount(model.Account) error
  46. // GetAccounts fetch list of account (without its password) with matching keyword.
  47. GetAccounts(opts GetAccountsOptions) ([]model.Account, error)
  48. // GetAccount fetch account with matching username.
  49. GetAccount(username string) (model.Account, bool)
  50. // DeleteAccounts removes all record with matching usernames
  51. DeleteAccounts(usernames ...string) error
  52. // GetTags fetch list of tags and its frequency from database.
  53. GetTags() ([]model.Tag, error)
  54. // RenameTag change the name of a tag.
  55. RenameTag(id int, newName string) error
  56. // CreateNewID creates new id for specified table.
  57. CreateNewID(table string) (int, error)
  58. }
  59. func checkError(err error) {
  60. if err != nil && err != sql.ErrNoRows {
  61. panic(err)
  62. }
  63. }