gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

util.go (3120B)


      1 // GoToSocial
      2 // Copyright (C) GoToSocial Authors admin@gotosocial.org
      3 // SPDX-License-Identifier: AGPL-3.0-or-later
      4 //
      5 // This program is free software: you can redistribute it and/or modify
      6 // it under the terms of the GNU Affero General Public License as published by
      7 // the Free Software Foundation, either version 3 of the License, or
      8 // (at your option) any later version.
      9 //
     10 // This program is distributed in the hope that it will be useful,
     11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13 // GNU Affero General Public License for more details.
     14 //
     15 // You should have received a copy of the GNU Affero General Public License
     16 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
     17 
     18 package bundb
     19 
     20 import (
     21 	"github.com/superseriousbusiness/gotosocial/internal/db"
     22 	"github.com/uptrace/bun"
     23 )
     24 
     25 // whereEmptyOrNull is a convenience function to return a bun WhereGroup that specifies
     26 // that the given column should be EITHER an empty string OR null.
     27 //
     28 // Use it as follows:
     29 //
     30 //	q = q.WhereGroup(" AND ", whereEmptyOrNull("whatever_column"))
     31 func whereEmptyOrNull(column string) func(*bun.SelectQuery) *bun.SelectQuery {
     32 	return func(q *bun.SelectQuery) *bun.SelectQuery {
     33 		return q.
     34 			WhereOr("? IS NULL", bun.Ident(column)).
     35 			WhereOr("? = ''", bun.Ident(column))
     36 	}
     37 }
     38 
     39 // whereNotEmptyAndNotNull is a convenience function to return a bun WhereGroup that specifies
     40 // that the given column should be NEITHER an empty string NOR null.
     41 //
     42 // Use it as follows:
     43 //
     44 //	q = q.WhereGroup(" AND ", whereNotEmptyAndNotNull("whatever_column"))
     45 func whereNotEmptyAndNotNull(column string) func(*bun.SelectQuery) *bun.SelectQuery {
     46 	return func(q *bun.SelectQuery) *bun.SelectQuery {
     47 		return q.
     48 			Where("? IS NOT NULL", bun.Ident(column)).
     49 			Where("? != ''", bun.Ident(column))
     50 	}
     51 }
     52 
     53 // updateWhere parses []db.Where and adds it to the given update query.
     54 func updateWhere(q *bun.UpdateQuery, where []db.Where) {
     55 	for _, w := range where {
     56 		query, args := parseWhere(w)
     57 		q = q.Where(query, args...)
     58 	}
     59 }
     60 
     61 // selectWhere parses []db.Where and adds it to the given select query.
     62 func selectWhere(q *bun.SelectQuery, where []db.Where) {
     63 	for _, w := range where {
     64 		query, args := parseWhere(w)
     65 		q = q.Where(query, args...)
     66 	}
     67 }
     68 
     69 // deleteWhere parses []db.Where and adds it to the given where query.
     70 func deleteWhere(q *bun.DeleteQuery, where []db.Where) {
     71 	for _, w := range where {
     72 		query, args := parseWhere(w)
     73 		q = q.Where(query, args...)
     74 	}
     75 }
     76 
     77 // parseWhere looks through the options on a single db.Where entry, and
     78 // returns the appropriate query string and arguments.
     79 func parseWhere(w db.Where) (query string, args []interface{}) {
     80 	if w.Not {
     81 		if w.Value == nil {
     82 			query = "? IS NOT NULL"
     83 			args = []interface{}{bun.Ident(w.Key)}
     84 			return
     85 		}
     86 
     87 		query = "? != ?"
     88 		args = []interface{}{bun.Ident(w.Key), w.Value}
     89 		return
     90 	}
     91 
     92 	if w.Value == nil {
     93 		query = "? IS NULL"
     94 		args = []interface{}{bun.Ident(w.Key)}
     95 		return
     96 	}
     97 
     98 	query = "? = ?"
     99 	args = []interface{}{bun.Ident(w.Key), w.Value}
    100 	return
    101 }