summaryrefslogtreecommitdiff
path: root/internal/db/bundb/util.go
diff options
context:
space:
mode:
authorLibravatar kim <89579420+NyaaaWhatsUpDoc@users.noreply.github.com>2024-02-07 14:43:27 +0000
committerLibravatar GitHub <noreply@github.com>2024-02-07 14:43:27 +0000
commit6738fd5bb0193daf3e2b524105ff690e8bfc32f4 (patch)
tree1c9b84846e21c737746f2a528170ad1d4bfb0a1c /internal/db/bundb/util.go
parent[bugfix] Ensure activities sender always = activities actor (#2608) (diff)
downloadgotosocial-6738fd5bb0193daf3e2b524105ff690e8bfc32f4.tar.xz
[feature/performance] sqlite pragma optimize on close (#2596)
* wrap database drivers in order to handle error processing, hooks, etc * remove dead code * add code comment, remove unused blank imports
Diffstat (limited to 'internal/db/bundb/util.go')
-rw-r--r--internal/db/bundb/util.go21
1 files changed, 21 insertions, 0 deletions
diff --git a/internal/db/bundb/util.go b/internal/db/bundb/util.go
index cee20bbe1..e2dd392dc 100644
--- a/internal/db/bundb/util.go
+++ b/internal/db/bundb/util.go
@@ -18,6 +18,8 @@
package bundb
import (
+ "context"
+ "database/sql"
"slices"
"strings"
@@ -113,6 +115,25 @@ func whereStartsLike(
)
}
+// exists checks the results of a SelectQuery for the existence of the data in question, masking ErrNoEntries errors.
+func exists(ctx context.Context, query *bun.SelectQuery) (bool, error) {
+ exists, err := query.Exists(ctx)
+ switch err {
+ case nil:
+ return exists, nil
+ case sql.ErrNoRows:
+ return false, nil
+ default:
+ return false, err
+ }
+}
+
+// notExists checks the results of a SelectQuery for the non-existence of the data in question, masking ErrNoEntries errors.
+func notExists(ctx context.Context, query *bun.SelectQuery) (bool, error) {
+ exists, err := exists(ctx, query)
+ return !exists, err
+}
+
// loadPagedIDs loads a page of IDs from given SliceCache by `key`, resorting to `loadDESC` if required. Uses `page` to sort + page resulting IDs.
// NOTE: IDs returned from `cache` / `loadDESC` MUST be in descending order, otherwise paging will not work correctly / return things out of order.
func loadPagedIDs(cache *cache.SliceCache[string], key string, page *paging.Page, loadDESC func() ([]string, error)) ([]string, error) {