gtsocial-umbx

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

basic.go (4274B)


      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 	"context"
     22 	"errors"
     23 
     24 	"github.com/superseriousbusiness/gotosocial/internal/db"
     25 	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
     26 	"github.com/superseriousbusiness/gotosocial/internal/log"
     27 	"github.com/uptrace/bun"
     28 )
     29 
     30 type basicDB struct {
     31 	conn *DBConn
     32 }
     33 
     34 func (b *basicDB) Put(ctx context.Context, i interface{}) db.Error {
     35 	_, err := b.conn.NewInsert().Model(i).Exec(ctx)
     36 	return b.conn.ProcessError(err)
     37 }
     38 
     39 func (b *basicDB) GetByID(ctx context.Context, id string, i interface{}) db.Error {
     40 	q := b.conn.
     41 		NewSelect().
     42 		Model(i).
     43 		Where("id = ?", id)
     44 
     45 	err := q.Scan(ctx)
     46 	return b.conn.ProcessError(err)
     47 }
     48 
     49 func (b *basicDB) GetWhere(ctx context.Context, where []db.Where, i interface{}) db.Error {
     50 	if len(where) == 0 {
     51 		return errors.New("no queries provided")
     52 	}
     53 
     54 	q := b.conn.NewSelect().Model(i)
     55 
     56 	selectWhere(q, where)
     57 
     58 	err := q.Scan(ctx)
     59 	return b.conn.ProcessError(err)
     60 }
     61 
     62 func (b *basicDB) GetAll(ctx context.Context, i interface{}) db.Error {
     63 	q := b.conn.
     64 		NewSelect().
     65 		Model(i)
     66 
     67 	err := q.Scan(ctx)
     68 	return b.conn.ProcessError(err)
     69 }
     70 
     71 func (b *basicDB) DeleteByID(ctx context.Context, id string, i interface{}) db.Error {
     72 	q := b.conn.
     73 		NewDelete().
     74 		Model(i).
     75 		Where("id = ?", id)
     76 
     77 	_, err := q.Exec(ctx)
     78 	return b.conn.ProcessError(err)
     79 }
     80 
     81 func (b *basicDB) DeleteWhere(ctx context.Context, where []db.Where, i interface{}) db.Error {
     82 	if len(where) == 0 {
     83 		return errors.New("no queries provided")
     84 	}
     85 
     86 	q := b.conn.
     87 		NewDelete().
     88 		Model(i)
     89 
     90 	deleteWhere(q, where)
     91 
     92 	_, err := q.Exec(ctx)
     93 	return b.conn.ProcessError(err)
     94 }
     95 
     96 func (b *basicDB) UpdateByID(ctx context.Context, i interface{}, id string, columns ...string) db.Error {
     97 	q := b.conn.
     98 		NewUpdate().
     99 		Model(i).
    100 		Column(columns...).
    101 		Where("? = ?", bun.Ident("id"), id)
    102 
    103 	_, err := q.Exec(ctx)
    104 	return b.conn.ProcessError(err)
    105 }
    106 
    107 func (b *basicDB) UpdateWhere(ctx context.Context, where []db.Where, key string, value interface{}, i interface{}) db.Error {
    108 	q := b.conn.NewUpdate().Model(i)
    109 
    110 	updateWhere(q, where)
    111 
    112 	q = q.Set("? = ?", bun.Ident(key), value)
    113 
    114 	_, err := q.Exec(ctx)
    115 	return b.conn.ProcessError(err)
    116 }
    117 
    118 func (b *basicDB) CreateTable(ctx context.Context, i interface{}) db.Error {
    119 	_, err := b.conn.NewCreateTable().Model(i).IfNotExists().Exec(ctx)
    120 	return err
    121 }
    122 
    123 func (b *basicDB) CreateAllTables(ctx context.Context) db.Error {
    124 	models := []interface{}{
    125 		&gtsmodel.Account{},
    126 		&gtsmodel.Application{},
    127 		&gtsmodel.Block{},
    128 		&gtsmodel.DomainBlock{},
    129 		&gtsmodel.EmailDomainBlock{},
    130 		&gtsmodel.Follow{},
    131 		&gtsmodel.FollowRequest{},
    132 		&gtsmodel.MediaAttachment{},
    133 		&gtsmodel.Mention{},
    134 		&gtsmodel.Status{},
    135 		&gtsmodel.StatusToEmoji{},
    136 		&gtsmodel.StatusToTag{},
    137 		&gtsmodel.StatusFave{},
    138 		&gtsmodel.StatusBookmark{},
    139 		&gtsmodel.StatusMute{},
    140 		&gtsmodel.Tag{},
    141 		&gtsmodel.User{},
    142 		&gtsmodel.Emoji{},
    143 		&gtsmodel.Instance{},
    144 		&gtsmodel.Notification{},
    145 		&gtsmodel.RouterSession{},
    146 		&gtsmodel.Token{},
    147 		&gtsmodel.Client{},
    148 	}
    149 	for _, i := range models {
    150 		if err := b.CreateTable(ctx, i); err != nil {
    151 			return err
    152 		}
    153 	}
    154 	return nil
    155 }
    156 
    157 func (b *basicDB) DropTable(ctx context.Context, i interface{}) db.Error {
    158 	_, err := b.conn.NewDropTable().Model(i).IfExists().Exec(ctx)
    159 	return b.conn.ProcessError(err)
    160 }
    161 
    162 func (b *basicDB) IsHealthy(ctx context.Context) db.Error {
    163 	return b.conn.PingContext(ctx)
    164 }
    165 
    166 func (b *basicDB) Stop(ctx context.Context) db.Error {
    167 	log.Info(ctx, "closing db connection")
    168 	return b.conn.Close()
    169 }