From 996fd9505295c02fc680d3e2b1e2ea2c3cd366db Mon Sep 17 00:00:00 2001 From: Yonas Habteab Date: Tue, 16 Jan 2024 14:13:05 +0100 Subject: [PATCH] Drop superfluous utils functions --- internal/utils/utils.go | 92 ------------------------------------ internal/utils/utils_test.go | 28 ----------- 2 files changed, 120 deletions(-) delete mode 100644 internal/utils/utils_test.go diff --git a/internal/utils/utils.go b/internal/utils/utils.go index 1b11ad79f..a3ab3f3ad 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -1,83 +1,10 @@ package utils import ( - "context" "database/sql" - "fmt" - "github.com/icinga/icinga-go-library/database" - "github.com/icinga/icinga-go-library/driver" "github.com/icinga/icinga-go-library/types" - "github.com/jmoiron/sqlx" - "strings" ) -// BuildInsertStmtWithout builds an insert stmt without the provided column. -func BuildInsertStmtWithout(db *database.DB, into interface{}, withoutColumn string) string { - columns := db.BuildColumns(into) - for i, column := range columns { - if column == withoutColumn { - // Event id is auto incremented, so just erase it from our insert columns - columns = append(columns[:i], columns[i+1:]...) - break - } - } - - return fmt.Sprintf( - `INSERT INTO "%s" ("%s") VALUES (%s)`, - database.TableName(into), strings.Join(columns, `", "`), - fmt.Sprintf(":%s", strings.Join(columns, ", :")), - ) -} - -// RunInTx allows running a function in a database transaction without requiring manual transaction handling. -// -// A new transaction is started on db which is then passed to fn. After fn returns, the transaction is -// committed unless an error was returned. If fn returns an error, that error is returned, otherwise an -// error is returned if a database operation fails. -func RunInTx(ctx context.Context, db *database.DB, fn func(tx *sqlx.Tx) error) error { - tx, err := db.BeginTxx(ctx, nil) - if err != nil { - return err - } - defer func() { _ = tx.Rollback() }() - - err = fn(tx) - if err != nil { - return err - } - - return tx.Commit() -} - -// InsertAndFetchId executes the given query and fetches the last inserted ID. -func InsertAndFetchId(ctx context.Context, tx *sqlx.Tx, stmt string, args any) (int64, error) { - var lastInsertId int64 - if tx.DriverName() == driver.PostgreSQL { - preparedStmt, err := tx.PrepareNamedContext(ctx, stmt+" RETURNING id") - if err != nil { - return 0, err - } - defer func() { _ = preparedStmt.Close() }() - - err = preparedStmt.Get(&lastInsertId, args) - if err != nil { - return 0, fmt.Errorf("failed to insert entry for type %T: %s", args, err) - } - } else { - result, err := tx.NamedExecContext(ctx, stmt, args) - if err != nil { - return 0, fmt.Errorf("failed to insert entry for type %T: %s", args, err) - } - - lastInsertId, err = result.LastInsertId() - if err != nil { - return 0, fmt.Errorf("failed to fetch last insert id for type %T: %s", args, err) - } - } - - return lastInsertId, nil -} - // ToDBString transforms the given string to types.String. func ToDBString(value string) types.String { str := types.String{NullString: sql.NullString{String: value}} @@ -97,22 +24,3 @@ func ToDBInt(value int64) types.Int { return val } - -func RemoveIf[T any](slice []T, pred func(T) bool) []T { - n := len(slice) - - for i := 0; i < n; i++ { - for i < n && pred(slice[i]) { - n-- - slice[i], slice[n] = slice[n], slice[i] - } - } - - return slice[:n] -} - -func RemoveNils[T any](slice []*T) []*T { - return RemoveIf(slice, func(ptr *T) bool { - return ptr == nil - }) -} diff --git a/internal/utils/utils_test.go b/internal/utils/utils_test.go deleted file mode 100644 index 15108999e..000000000 --- a/internal/utils/utils_test.go +++ /dev/null @@ -1,28 +0,0 @@ -package utils - -import ( - "github.com/stretchr/testify/assert" - "testing" -) - -func TestRemoveNils(t *testing.T) { - var a, b, c, d int - - tests := []struct { - name string - in []*int - want []*int - }{ - {"Empty", []*int{}, []*int{}}, - {"SingleKeep", []*int{&a}, []*int{&a}}, - {"SingleRemove", []*int{nil}, []*int{}}, - {"KeepOrder", []*int{&a, &b, &c, &d}, []*int{&a, &b, &c, &d}}, - {"Duplicates", []*int{&a, &b, &b}, []*int{&a, &b, &b}}, - {"Mixed", []*int{&a, nil, &b, nil, nil, &b, nil, &d}, []*int{&a, &b, &b, &d}}, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - assert.Equal(t, tt.want, RemoveNils(tt.in)) - }) - } -}