Skip to content

Commit

Permalink
Drop superfluous utils functions
Browse files Browse the repository at this point in the history
  • Loading branch information
yhabteab committed May 27, 2024
1 parent 83e6b58 commit 4ae3893
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 109 deletions.
87 changes: 0 additions & 87 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,76 +10,8 @@ import (
"github.com/jmoiron/sqlx"
"github.com/pkg/errors"
"slices"
"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() == database.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
}

// ForEachRow applies the provided restoreFunc callback for each successfully retrieved row of the specified type.
// It will bulk SELECT the data from the database scoped to the specified ids and scans into the provided Row type.
// Returns error on any database failure or fails to acquire the table semaphore.
Expand Down Expand Up @@ -139,25 +71,6 @@ 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
})
}

// IterateOrderedMap implements iter.Seq2 to iterate over a map in the key's order.
//
// This function returns a func yielding key-value-pairs from a given map in the order of their keys, if their type
Expand Down
22 changes: 0 additions & 22 deletions internal/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,6 @@ import (
"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))
})
}
}

func TestIterateOrderedMap(t *testing.T) {
tests := []struct {
name string
Expand Down

0 comments on commit 4ae3893

Please sign in to comment.