From 387ce31ec11dd0d94d86a7018453203cdb991620 Mon Sep 17 00:00:00 2001 From: Yonas Habteab Date: Thu, 19 Oct 2023 17:31:16 +0200 Subject: [PATCH] Drop `utils` package --- internal/utils/utils.go | 99 ------------------------------------ internal/utils/utils_test.go | 28 ---------- 2 files changed, 127 deletions(-) delete mode 100644 internal/utils/utils.go delete mode 100644 internal/utils/utils_test.go diff --git a/internal/utils/utils.go b/internal/utils/utils.go deleted file mode 100644 index 7bf6b53b0..000000000 --- a/internal/utils/utils.go +++ /dev/null @@ -1,99 +0,0 @@ -package utils - -import ( - "context" - "database/sql" - "fmt" - "github.com/icinga/icingadb/pkg/driver" - "github.com/icinga/icingadb/pkg/icingadb" - "github.com/icinga/icingadb/pkg/types" - "github.com/icinga/icingadb/pkg/utils" - "github.com/jmoiron/sqlx" - "strings" -) - -// BuildInsertStmtWithout builds an insert stmt without the provided column. -func BuildInsertStmtWithout(db *icingadb.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)`, - utils.TableName(into), strings.Join(columns, `", "`), - fmt.Sprintf(":%s", strings.Join(columns, ", :")), - ) -} - -// 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}} - if value != "" { - str.Valid = true - } - - return str -} - -// ToDBInt transforms the given value to types.Int. -func ToDBInt(value int64) types.Int { - val := types.Int{NullInt64: sql.NullInt64{Int64: value}} - if value != 0 { - val.Valid = true - } - - 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)) - }) - } -}