urls_storage.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright (c) [2022] [巴拉迪维 BaratSemet]
  2. // [ohUrlShortener] is licensed under Mulan PSL v2.
  3. // You can use this software according to the terms and conditions of the Mulan PSL v2.
  4. // You may obtain a copy of Mulan PSL v2 at:
  5. // http://license.coscl.org.cn/MulanPSL2
  6. // THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
  7. // See the Mulan PSL v2 for more details.
  8. package storage
  9. import (
  10. "fmt"
  11. "ohurlshortener/core"
  12. "ohurlshortener/utils"
  13. )
  14. var MaxInsertCount = 1000
  15. // UpdateShortUrl 更新短链接
  16. func UpdateShortUrl(shortUrl core.ShortUrl) error {
  17. query := `UPDATE public.short_urls SET short_url = :short_url, dest_url = :dest_url, is_valid = :is_valid, memo = :memo WHERE id = :id`
  18. return DbNamedExec(query, shortUrl)
  19. }
  20. // DeleteShortUrl 删除短链接
  21. func DeleteShortUrl(shortUrl core.ShortUrl) error {
  22. query := `DELETE from public.short_urls WHERE short_url = :short_url`
  23. return DbNamedExec(query, shortUrl)
  24. }
  25. // DeleteShortUrlWithAccessLogs 删除短链接以及其访问日志
  26. func DeleteShortUrlWithAccessLogs(shortUrl core.ShortUrl) error {
  27. query1 := fmt.Sprintf(`DELETE from public.short_urls WHERE short_url = '%s'`, shortUrl.ShortUrl)
  28. query2 := fmt.Sprintf(`DELETE from public.access_logs WHERE short_url = '%s'`, shortUrl.ShortUrl)
  29. return DbExecTx(query1, query2)
  30. } // end of Transaction Action
  31. // FindShortUrl 根据短链接查找短链接信息
  32. func FindShortUrl(url string) (core.ShortUrl, error) {
  33. found := core.ShortUrl{}
  34. query := `SELECT * FROM public.short_urls WHERE short_url = $1`
  35. err := DbGet(query, &found, url)
  36. return found, err
  37. }
  38. // FindAllShortUrls 查找所有短链接
  39. func FindAllShortUrls() ([]core.ShortUrl, error) {
  40. found := []core.ShortUrl{}
  41. query := `SELECT * FROM public.short_urls ORDER BY created_at DESC`
  42. err := DbSelect(query, &found)
  43. return found, err
  44. }
  45. // FindAllShortUrls 查找所有短链接
  46. func FindAllShortUrlsByPage(page, size int) ([]core.ShortUrl, error) {
  47. found := []core.ShortUrl{}
  48. if page < 0 {
  49. return found, nil
  50. }
  51. offset := (page - 1) * size
  52. query := `SELECT * FROM public.short_urls ORDER BY id DESC LIMIT $1 OFFSET $2`
  53. err := DbSelect(query, &found, size, offset)
  54. return found, err
  55. }
  56. // FindPagedShortUrls 分页查找短链接
  57. func FindPagedShortUrls(url string, page int, size int) ([]core.ShortUrl, error) {
  58. found := []core.ShortUrl{}
  59. offset := (page - 1) * size
  60. query := "SELECT * FROM public.short_urls u ORDER BY u.id DESC LIMIT $1 OFFSET $2"
  61. if !utils.EmptyString(url) {
  62. query := "SELECT * FROM public.short_urls u WHERE u.short_url = $1 ORDER BY u.id DESC LIMIT $2 OFFSET $3"
  63. var foundUrl core.ShortUrl
  64. err := DbGet(query, &foundUrl, url, size, offset)
  65. if !foundUrl.IsEmpty() {
  66. found = append(found, foundUrl)
  67. }
  68. return found, err
  69. }
  70. return found, DbSelect(query, &found, size, offset)
  71. }
  72. // InsertShortUrl 插入短链接
  73. func InsertShortUrl(url core.ShortUrl) error {
  74. query := `INSERT INTO public.short_urls (short_url, dest_url, created_at, is_valid, memo,open_type)
  75. VALUES(:short_url,:dest_url,:created_at,:is_valid,:memo,:open_type)`
  76. return DbNamedExec(query, url)
  77. }
  78. func splitLogsArray(array []core.AccessLog, size int) [][]core.AccessLog {
  79. var chunks [][]core.AccessLog
  80. for {
  81. if len(array) <= 0 {
  82. break
  83. }
  84. if len(array) < size {
  85. size = len(array)
  86. }
  87. chunks = append(chunks, array[0:size])
  88. array = array[size:]
  89. }
  90. return chunks
  91. }