From d9c69f6ce05daddf2f6d5d8c6c03b4c3d55df93a Mon Sep 17 00:00:00 2001 From: tobi <31960611+tsmethurst@users.noreply.github.com> Date: Wed, 5 Jul 2023 12:34:37 +0200 Subject: [PATCH] [chore/performance] Remove remaining 'whereEmptyOrNull' funcs (#1946) --- internal/db/bundb/account.go | 4 ++-- internal/db/bundb/admin.go | 4 ++-- internal/db/bundb/instance.go | 5 +++-- internal/db/bundb/media.go | 6 +++--- internal/db/bundb/timeline.go | 4 +++- internal/db/bundb/util.go | 28 ---------------------------- 6 files changed, 13 insertions(+), 38 deletions(-) diff --git a/internal/db/bundb/account.go b/internal/db/bundb/account.go index d8aee80f..17339732 100644 --- a/internal/db/bundb/account.go +++ b/internal/db/bundb/account.go @@ -388,8 +388,8 @@ func (a *accountDB) GetAccountLastPosted(ctx context.Context, accountID string, if webOnly { q = q. - WhereGroup(" AND ", whereEmptyOrNull("status.in_reply_to_uri")). - WhereGroup(" AND ", whereEmptyOrNull("status.boost_of_id")). + Where("? IS NULL", bun.Ident("status.in_reply_to_uri")). + Where("? IS NULL", bun.Ident("status.boost_of_id")). Where("? = ?", bun.Ident("status.visibility"), gtsmodel.VisibilityPublic). Where("? = ?", bun.Ident("status.federated"), true) } diff --git a/internal/db/bundb/admin.go b/internal/db/bundb/admin.go index 1b4c9876..ed595a95 100644 --- a/internal/db/bundb/admin.go +++ b/internal/db/bundb/admin.go @@ -102,7 +102,7 @@ func (a *adminDB) NewSignup(ctx context.Context, username string, reason string, NewSelect(). Model(acct). Where("? = ?", bun.Ident("account.username"), username). - WhereGroup(" AND ", whereEmptyOrNull("account.domain")). + Where("? IS NULL", bun.Ident("account.domain")). Scan(ctx); err != nil { err = a.conn.ProcessError(err) if err != db.ErrNoEntries { @@ -199,7 +199,7 @@ func (a *adminDB) CreateInstanceAccount(ctx context.Context) db.Error { TableExpr("? AS ?", bun.Ident("accounts"), bun.Ident("account")). Column("account.id"). Where("? = ?", bun.Ident("account.username"), username). - WhereGroup(" AND ", whereEmptyOrNull("account.domain")) + Where("? IS NULL", bun.Ident("account.domain")) exists, err := a.conn.Exists(ctx, q) if err != nil { diff --git a/internal/db/bundb/instance.go b/internal/db/bundb/instance.go index 4fa89863..95f6ad5b 100644 --- a/internal/db/bundb/instance.go +++ b/internal/db/bundb/instance.go @@ -39,8 +39,9 @@ func (i *instanceDB) CountInstanceUsers(ctx context.Context, domain string) (int Where("? IS NULL", bun.Ident("account.suspended_at")) if domain == config.GetHost() || domain == config.GetAccountDomain() { - // if the domain is *this* domain, just count where the domain field is null - q = q.WhereGroup(" AND ", whereEmptyOrNull("account.domain")) + // If the domain is *this* domain, just + // count where the domain field is null. + q = q.Where("? IS NULL", bun.Ident("account.domain")) } else { q = q.Where("? = ?", bun.Ident("account.domain"), domain) } diff --git a/internal/db/bundb/media.go b/internal/db/bundb/media.go index a9b60e3a..80a4f8bb 100644 --- a/internal/db/bundb/media.go +++ b/internal/db/bundb/media.go @@ -241,7 +241,7 @@ func (m *mediaDB) GetRemoteOlderThan(ctx context.Context, olderThan time.Time, l Column("media_attachment.id"). Where("? = ?", bun.Ident("media_attachment.cached"), true). Where("? < ?", bun.Ident("media_attachment.created_at"), olderThan). - WhereGroup(" AND ", whereNotEmptyAndNotNull("media_attachment.remote_url")). + Where("? IS NOT NULL", bun.Ident("media_attachment.remote_url")). Order("media_attachment.created_at DESC") if limit != 0 { @@ -261,8 +261,8 @@ func (m *mediaDB) CountRemoteOlderThan(ctx context.Context, olderThan time.Time) TableExpr("? AS ?", bun.Ident("media_attachments"), bun.Ident("media_attachment")). Column("media_attachment.id"). Where("? = ?", bun.Ident("media_attachment.cached"), true). - Where("? < ?", bun.Ident("media_attachment.created_at"), olderThan). - WhereGroup(" AND ", whereNotEmptyAndNotNull("media_attachment.remote_url")) + Where("? IS NOT NULL", bun.Ident("media_attachment.remote_url")). + Where("? < ?", bun.Ident("media_attachment.created_at"), olderThan) count, err := q.Count(ctx) if err != nil { diff --git a/internal/db/bundb/timeline.go b/internal/db/bundb/timeline.go index d492c958..0859f3ff 100644 --- a/internal/db/bundb/timeline.go +++ b/internal/db/bundb/timeline.go @@ -164,8 +164,10 @@ func (t *timelineDB) GetPublicTimeline(ctx context.Context, maxID string, sinceI NewSelect(). TableExpr("? AS ?", bun.Ident("statuses"), bun.Ident("status")). Column("status.id"). + // Public only. Where("? = ?", bun.Ident("status.visibility"), gtsmodel.VisibilityPublic). - WhereGroup(" AND ", whereEmptyOrNull("status.boost_of_id")). + // Ignore boosts. + Where("? IS NULL", bun.Ident("status.boost_of_id")). Order("status.id DESC") if maxID == "" { diff --git a/internal/db/bundb/util.go b/internal/db/bundb/util.go index a656a41c..06bb289d 100644 --- a/internal/db/bundb/util.go +++ b/internal/db/bundb/util.go @@ -22,34 +22,6 @@ import ( "github.com/uptrace/bun" ) -// whereEmptyOrNull is a convenience function to return a bun WhereGroup that specifies -// that the given column should be EITHER an empty string OR null. -// -// Use it as follows: -// -// q = q.WhereGroup(" AND ", whereEmptyOrNull("whatever_column")) -func whereEmptyOrNull(column string) func(*bun.SelectQuery) *bun.SelectQuery { - return func(q *bun.SelectQuery) *bun.SelectQuery { - return q. - WhereOr("? IS NULL", bun.Ident(column)). - WhereOr("? = ''", bun.Ident(column)) - } -} - -// whereNotEmptyAndNotNull is a convenience function to return a bun WhereGroup that specifies -// that the given column should be NEITHER an empty string NOR null. -// -// Use it as follows: -// -// q = q.WhereGroup(" AND ", whereNotEmptyAndNotNull("whatever_column")) -func whereNotEmptyAndNotNull(column string) func(*bun.SelectQuery) *bun.SelectQuery { - return func(q *bun.SelectQuery) *bun.SelectQuery { - return q. - Where("? IS NOT NULL", bun.Ident(column)). - Where("? != ''", bun.Ident(column)) - } -} - // updateWhere parses []db.Where and adds it to the given update query. func updateWhere(q *bun.UpdateQuery, where []db.Where) { for _, w := range where {