remove unused code
This commit is contained in:
parent
c6dff63ed3
commit
40fdd4079d
|
@ -1,361 +0,0 @@
|
|||
// GoToSocial
|
||||
// Copyright (C) GoToSocial Authors admin@gotosocial.org
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package bundb
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"database/sql/driver"
|
||||
"time"
|
||||
_ "unsafe" // linkname shenanigans
|
||||
|
||||
pgx "github.com/jackc/pgx/v5/stdlib"
|
||||
"github.com/ncruces/go-sqlite3"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/db/sqlite"
|
||||
)
|
||||
|
||||
var (
|
||||
// global wrapped gts driver instances.
|
||||
gtsPostgresDriver = &PostgreSQLDriver{}
|
||||
gtsSQLiteDriver = &SQLiteDriver{}
|
||||
|
||||
// global PostgreSQL driver instances.
|
||||
postgresDriver = pgx.GetDefaultDriver()
|
||||
|
||||
// global SQLite3 driver instance.
|
||||
sqliteDriver = &sqlite.Driver{
|
||||
Init: func(c *sqlite3.Conn) error {
|
||||
return c.BusyHandler(busyHandler)
|
||||
},
|
||||
}
|
||||
|
||||
// check the postgres connection
|
||||
// conforms to our conn{} interface.
|
||||
// (note SQLite doesn't export their
|
||||
// conn type, and gets checked in
|
||||
// tests very regularly anywho).
|
||||
_ conn = (*pgx.Conn)(nil)
|
||||
)
|
||||
|
||||
func init() {
|
||||
sql.Register("pgx-gts", gtsPostgresDriver)
|
||||
sql.Register("sqlite-gts", gtsSQLiteDriver)
|
||||
}
|
||||
|
||||
// PostgreSQLDriver is our own wrapper around the
|
||||
// pgx/stdlib.Driver{} type in order to wrap further
|
||||
// SQL driver types with our own err processing.
|
||||
type PostgreSQLDriver struct{}
|
||||
|
||||
func (d *PostgreSQLDriver) Open(name string) (driver.Conn, error) {
|
||||
c, err := postgresDriver.Open(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &PostgreSQLConn{conn: c.(conn)}, nil
|
||||
}
|
||||
|
||||
type PostgreSQLConn struct{ conn }
|
||||
|
||||
func (c *PostgreSQLConn) Begin() (driver.Tx, error) {
|
||||
return c.BeginTx(context.Background(), driver.TxOptions{})
|
||||
}
|
||||
|
||||
func (c *PostgreSQLConn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {
|
||||
tx, err := c.conn.BeginTx(ctx, opts)
|
||||
err = processPostgresError(err)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &PostgreSQLTx{tx}, nil
|
||||
}
|
||||
|
||||
func (c *PostgreSQLConn) Prepare(query string) (driver.Stmt, error) {
|
||||
return c.PrepareContext(context.Background(), query)
|
||||
}
|
||||
|
||||
func (c *PostgreSQLConn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) {
|
||||
st, err := c.conn.PrepareContext(ctx, query)
|
||||
err = processPostgresError(err)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &PostgreSQLStmt{stmt: st.(stmt)}, nil
|
||||
}
|
||||
|
||||
func (c *PostgreSQLConn) Exec(query string, args []driver.Value) (driver.Result, error) {
|
||||
return c.ExecContext(context.Background(), query, toNamedValues(args))
|
||||
}
|
||||
|
||||
func (c *PostgreSQLConn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
|
||||
result, err := c.conn.ExecContext(ctx, query, args)
|
||||
err = processPostgresError(err)
|
||||
return result, err
|
||||
}
|
||||
|
||||
func (c *PostgreSQLConn) Query(query string, args []driver.Value) (driver.Rows, error) {
|
||||
return c.QueryContext(context.Background(), query, toNamedValues(args))
|
||||
}
|
||||
|
||||
func (c *PostgreSQLConn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
|
||||
rows, err := c.conn.QueryContext(ctx, query, args)
|
||||
err = processPostgresError(err)
|
||||
return rows, err
|
||||
}
|
||||
|
||||
func (c *PostgreSQLConn) Close() error {
|
||||
return c.conn.Close()
|
||||
}
|
||||
|
||||
type PostgreSQLTx struct{ driver.Tx }
|
||||
|
||||
func (tx *PostgreSQLTx) Commit() error {
|
||||
err := tx.Tx.Commit()
|
||||
return processPostgresError(err)
|
||||
}
|
||||
|
||||
func (tx *PostgreSQLTx) Rollback() error {
|
||||
err := tx.Tx.Rollback()
|
||||
return processPostgresError(err)
|
||||
}
|
||||
|
||||
type PostgreSQLStmt struct{ stmt }
|
||||
|
||||
func (stmt *PostgreSQLStmt) Exec(args []driver.Value) (driver.Result, error) {
|
||||
return stmt.ExecContext(context.Background(), toNamedValues(args))
|
||||
}
|
||||
|
||||
func (stmt *PostgreSQLStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
|
||||
res, err := stmt.stmt.ExecContext(ctx, args)
|
||||
err = processPostgresError(err)
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (stmt *PostgreSQLStmt) Query(args []driver.Value) (driver.Rows, error) {
|
||||
return stmt.QueryContext(context.Background(), toNamedValues(args))
|
||||
}
|
||||
|
||||
func (stmt *PostgreSQLStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
|
||||
rows, err := stmt.stmt.QueryContext(ctx, args)
|
||||
err = processPostgresError(err)
|
||||
return rows, err
|
||||
}
|
||||
|
||||
// SQLiteDriver is our own wrapper around the
|
||||
// sqlite.Driver{} type in order to wrap further
|
||||
// SQL driver types with our own functionality,
|
||||
// e.g. hooks, retries and err processing.
|
||||
type SQLiteDriver struct{}
|
||||
|
||||
func (d *SQLiteDriver) Open(name string) (driver.Conn, error) {
|
||||
cc, err := d.OpenConnector(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return cc.Connect(context.Background())
|
||||
}
|
||||
|
||||
func (d *SQLiteDriver) OpenConnector(name string) (driver.Connector, error) {
|
||||
cc, err := sqliteDriver.OpenConnector(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &SQLiteConnector{cc}, nil
|
||||
}
|
||||
|
||||
type SQLiteConnector struct{ driver.Connector }
|
||||
|
||||
func (c *SQLiteConnector) Driver() driver.Driver { return gtsSQLiteDriver }
|
||||
|
||||
func (c *SQLiteConnector) Connect(ctx context.Context) (driver.Conn, error) {
|
||||
conn, err := c.Connector.Connect(ctx)
|
||||
err = processSQLiteError(err)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &SQLiteConn{conn.(sqlite.ConnIface)}, nil
|
||||
}
|
||||
|
||||
type SQLiteConn struct{ sqlite.ConnIface }
|
||||
|
||||
func (c *SQLiteConn) Begin() (driver.Tx, error) {
|
||||
return c.BeginTx(context.Background(), driver.TxOptions{})
|
||||
}
|
||||
|
||||
func (c *SQLiteConn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {
|
||||
tx, err := c.ConnIface.BeginTx(ctx, opts)
|
||||
err = processSQLiteError(err)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &SQLiteTx{tx}, nil
|
||||
}
|
||||
|
||||
func (c *SQLiteConn) Prepare(query string) (driver.Stmt, error) {
|
||||
return c.PrepareContext(context.Background(), query)
|
||||
}
|
||||
|
||||
func (c *SQLiteConn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) {
|
||||
st, err := c.ConnIface.PrepareContext(ctx, query)
|
||||
err = processSQLiteError(err)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &SQLiteStmt{st.(stmt)}, nil
|
||||
}
|
||||
|
||||
func (c *SQLiteConn) Exec(query string, args []driver.Value) (driver.Result, error) {
|
||||
return c.ExecContext(context.Background(), query, toNamedValues(args))
|
||||
}
|
||||
|
||||
func (c *SQLiteConn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
|
||||
st, err := c.PrepareContext(ctx, query)
|
||||
err = processSQLiteError(err)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
stmt := st.(*SQLiteStmt)
|
||||
result, err := stmt.ExecContext(ctx, args)
|
||||
err = processSQLiteError(err)
|
||||
return result, err
|
||||
}
|
||||
|
||||
func (c *SQLiteConn) Query(query string, args []driver.Value) (driver.Rows, error) {
|
||||
return c.QueryContext(context.Background(), query, toNamedValues(args))
|
||||
}
|
||||
|
||||
func (c *SQLiteConn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
|
||||
st, err := c.PrepareContext(ctx, query)
|
||||
err = processSQLiteError(err)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
stmt := st.(*SQLiteStmt)
|
||||
rows, err := stmt.QueryContext(ctx, args)
|
||||
err = processSQLiteError(err)
|
||||
return rows, err
|
||||
}
|
||||
|
||||
func (c *SQLiteConn) Close() error {
|
||||
// see: https://www.sqlite.org/pragma.html#pragma_optimize
|
||||
const onClose = "PRAGMA analysis_limit=1000; PRAGMA optimize;"
|
||||
_, _ = c.ExecContext(context.Background(), onClose, nil)
|
||||
err := c.ConnIface.Close()
|
||||
err = processSQLiteError(err)
|
||||
return err
|
||||
}
|
||||
|
||||
type SQLiteTx struct{ driver.Tx }
|
||||
|
||||
func (tx *SQLiteTx) Commit() error {
|
||||
err := tx.Tx.Commit()
|
||||
err = processSQLiteError(err)
|
||||
return err
|
||||
}
|
||||
|
||||
func (tx *SQLiteTx) Rollback() error {
|
||||
err := tx.Tx.Rollback()
|
||||
err = processSQLiteError(err)
|
||||
return err
|
||||
}
|
||||
|
||||
type SQLiteStmt struct{ sqlite.StmtIface }
|
||||
|
||||
func (stmt *SQLiteStmt) Exec(args []driver.Value) (driver.Result, error) {
|
||||
return stmt.ExecContext(context.Background(), toNamedValues(args))
|
||||
}
|
||||
|
||||
func (stmt *SQLiteStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
|
||||
res, err := stmt.StmtIface.ExecContext(ctx, args)
|
||||
err = processSQLiteError(err)
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (stmt *SQLiteStmt) Query(args []driver.Value) (driver.Rows, error) {
|
||||
return stmt.QueryContext(context.Background(), toNamedValues(args))
|
||||
}
|
||||
|
||||
func (stmt *SQLiteStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
|
||||
rows, err := stmt.StmtIface.QueryContext(ctx, args)
|
||||
err = processSQLiteError(err)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &SQLiteRows{rows.(sqlite.RowsIface)}, nil
|
||||
}
|
||||
|
||||
type SQLiteRows struct{ sqlite.RowsIface }
|
||||
|
||||
func (r *SQLiteRows) Next(dest []driver.Value) error {
|
||||
err := r.RowsIface.Next(dest)
|
||||
err = processSQLiteError(err)
|
||||
return err
|
||||
}
|
||||
|
||||
type conn interface {
|
||||
driver.Conn
|
||||
driver.ConnPrepareContext
|
||||
driver.ExecerContext
|
||||
driver.QueryerContext
|
||||
driver.ConnBeginTx
|
||||
}
|
||||
|
||||
type stmt interface {
|
||||
driver.Stmt
|
||||
driver.StmtExecContext
|
||||
driver.StmtQueryContext
|
||||
}
|
||||
|
||||
type rows interface {
|
||||
driver.Rows
|
||||
}
|
||||
|
||||
// busyHandler performs a busy backoff against given context,
|
||||
// returning false if the backoff is too long or ctx is done.
|
||||
func busyHandler(ctx context.Context, i int) (retry bool) {
|
||||
backoff := 2 * time.Millisecond * (1 << (2*i + 1))
|
||||
if backoff > 5*time.Minute {
|
||||
return false
|
||||
}
|
||||
donech := ctx.Done()
|
||||
if donech == nil {
|
||||
time.Sleep(backoff)
|
||||
return true
|
||||
}
|
||||
select {
|
||||
case <-donech:
|
||||
return false
|
||||
case <-time.After(backoff):
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
// toNamedValues converts older driver.Value types to driver.NamedValue types.
|
||||
func toNamedValues(args []driver.Value) []driver.NamedValue {
|
||||
if args == nil {
|
||||
return nil
|
||||
}
|
||||
args2 := make([]driver.NamedValue, len(args))
|
||||
for i := range args {
|
||||
args2[i] = driver.NamedValue{
|
||||
Ordinal: i + 1,
|
||||
Value: args[i],
|
||||
}
|
||||
}
|
||||
return args2
|
||||
}
|
|
@ -29,11 +29,4 @@ var (
|
|||
|
||||
// ErrAlreadyExists is returned when a conflict was encountered in the db when doing an insert.
|
||||
ErrAlreadyExists = errors.New("already exists")
|
||||
|
||||
// ErrTemporarilyBusy should be returned when the database wants to indicate you should retry.
|
||||
ErrTemporarilyBusy = errors.New("temporarily busy")
|
||||
|
||||
// ErrBusyTimeout is returned if the database connection indicates the connection is too busy
|
||||
// to complete the supplied query. This is generally intended to be handled internally by the DB.
|
||||
ErrBusyTimeout = errors.New("busy timeout")
|
||||
)
|
||||
|
|
|
@ -43,10 +43,6 @@ func TestMultiError(t *testing.T) {
|
|||
t.Error("should be db.ErrAlreadyExists")
|
||||
}
|
||||
|
||||
if errors.Is(err, db.ErrBusyTimeout) {
|
||||
t.Error("should not be db.ErrBusyTimeout")
|
||||
}
|
||||
|
||||
errString := err.Error()
|
||||
expected := `sql: no rows in result set
|
||||
oopsie woopsie we did a fucky wucky etc
|
||||
|
|
Loading…
Reference in New Issue