[chore/performance] Remove remaining 'whereEmptyOrNull' funcs (#1946)

This commit is contained in:
tobi 2023-07-05 12:34:37 +02:00 committed by GitHub
parent 3d16962173
commit d9c69f6ce0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 13 additions and 38 deletions

View file

@ -388,8 +388,8 @@ func (a *accountDB) GetAccountLastPosted(ctx context.Context, accountID string,
if webOnly { if webOnly {
q = q. q = q.
WhereGroup(" AND ", whereEmptyOrNull("status.in_reply_to_uri")). Where("? IS NULL", bun.Ident("status.in_reply_to_uri")).
WhereGroup(" AND ", whereEmptyOrNull("status.boost_of_id")). Where("? IS NULL", bun.Ident("status.boost_of_id")).
Where("? = ?", bun.Ident("status.visibility"), gtsmodel.VisibilityPublic). Where("? = ?", bun.Ident("status.visibility"), gtsmodel.VisibilityPublic).
Where("? = ?", bun.Ident("status.federated"), true) Where("? = ?", bun.Ident("status.federated"), true)
} }

View file

@ -102,7 +102,7 @@ func (a *adminDB) NewSignup(ctx context.Context, username string, reason string,
NewSelect(). NewSelect().
Model(acct). Model(acct).
Where("? = ?", bun.Ident("account.username"), username). Where("? = ?", bun.Ident("account.username"), username).
WhereGroup(" AND ", whereEmptyOrNull("account.domain")). Where("? IS NULL", bun.Ident("account.domain")).
Scan(ctx); err != nil { Scan(ctx); err != nil {
err = a.conn.ProcessError(err) err = a.conn.ProcessError(err)
if err != db.ErrNoEntries { 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")). TableExpr("? AS ?", bun.Ident("accounts"), bun.Ident("account")).
Column("account.id"). Column("account.id").
Where("? = ?", bun.Ident("account.username"), username). 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) exists, err := a.conn.Exists(ctx, q)
if err != nil { if err != nil {

View file

@ -39,8 +39,9 @@ func (i *instanceDB) CountInstanceUsers(ctx context.Context, domain string) (int
Where("? IS NULL", bun.Ident("account.suspended_at")) Where("? IS NULL", bun.Ident("account.suspended_at"))
if domain == config.GetHost() || domain == config.GetAccountDomain() { if domain == config.GetHost() || domain == config.GetAccountDomain() {
// if the domain is *this* domain, just count where the domain field is null // If the domain is *this* domain, just
q = q.WhereGroup(" AND ", whereEmptyOrNull("account.domain")) // count where the domain field is null.
q = q.Where("? IS NULL", bun.Ident("account.domain"))
} else { } else {
q = q.Where("? = ?", bun.Ident("account.domain"), domain) q = q.Where("? = ?", bun.Ident("account.domain"), domain)
} }

View file

@ -241,7 +241,7 @@ func (m *mediaDB) GetRemoteOlderThan(ctx context.Context, olderThan time.Time, l
Column("media_attachment.id"). Column("media_attachment.id").
Where("? = ?", bun.Ident("media_attachment.cached"), true). Where("? = ?", bun.Ident("media_attachment.cached"), true).
Where("? < ?", bun.Ident("media_attachment.created_at"), olderThan). 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") Order("media_attachment.created_at DESC")
if limit != 0 { 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")). TableExpr("? AS ?", bun.Ident("media_attachments"), bun.Ident("media_attachment")).
Column("media_attachment.id"). Column("media_attachment.id").
Where("? = ?", bun.Ident("media_attachment.cached"), true). Where("? = ?", bun.Ident("media_attachment.cached"), true).
Where("? < ?", bun.Ident("media_attachment.created_at"), olderThan). Where("? IS NOT NULL", bun.Ident("media_attachment.remote_url")).
WhereGroup(" AND ", whereNotEmptyAndNotNull("media_attachment.remote_url")) Where("? < ?", bun.Ident("media_attachment.created_at"), olderThan)
count, err := q.Count(ctx) count, err := q.Count(ctx)
if err != nil { if err != nil {

View file

@ -164,8 +164,10 @@ func (t *timelineDB) GetPublicTimeline(ctx context.Context, maxID string, sinceI
NewSelect(). NewSelect().
TableExpr("? AS ?", bun.Ident("statuses"), bun.Ident("status")). TableExpr("? AS ?", bun.Ident("statuses"), bun.Ident("status")).
Column("status.id"). Column("status.id").
// Public only.
Where("? = ?", bun.Ident("status.visibility"), gtsmodel.VisibilityPublic). 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") Order("status.id DESC")
if maxID == "" { if maxID == "" {

View file

@ -22,34 +22,6 @@ import (
"github.com/uptrace/bun" "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. // updateWhere parses []db.Where and adds it to the given update query.
func updateWhere(q *bun.UpdateQuery, where []db.Where) { func updateWhere(q *bun.UpdateQuery, where []db.Where) {
for _, w := range where { for _, w := range where {