Skip to content

Commit 4ae3893

Browse files
committed
Drop superfluous utils functions
1 parent 83e6b58 commit 4ae3893

File tree

2 files changed

+0
-109
lines changed

2 files changed

+0
-109
lines changed

internal/utils/utils.go

Lines changed: 0 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -10,76 +10,8 @@ import (
1010
"github.com/jmoiron/sqlx"
1111
"github.com/pkg/errors"
1212
"slices"
13-
"strings"
1413
)
1514

16-
// BuildInsertStmtWithout builds an insert stmt without the provided column.
17-
func BuildInsertStmtWithout(db *database.DB, into interface{}, withoutColumn string) string {
18-
columns := db.BuildColumns(into)
19-
for i, column := range columns {
20-
if column == withoutColumn {
21-
// Event id is auto incremented, so just erase it from our insert columns
22-
columns = append(columns[:i], columns[i+1:]...)
23-
break
24-
}
25-
}
26-
27-
return fmt.Sprintf(
28-
`INSERT INTO "%s" ("%s") VALUES (%s)`,
29-
database.TableName(into), strings.Join(columns, `", "`),
30-
fmt.Sprintf(":%s", strings.Join(columns, ", :")),
31-
)
32-
}
33-
34-
// RunInTx allows running a function in a database transaction without requiring manual transaction handling.
35-
//
36-
// A new transaction is started on db which is then passed to fn. After fn returns, the transaction is
37-
// committed unless an error was returned. If fn returns an error, that error is returned, otherwise an
38-
// error is returned if a database operation fails.
39-
func RunInTx(ctx context.Context, db *database.DB, fn func(tx *sqlx.Tx) error) error {
40-
tx, err := db.BeginTxx(ctx, nil)
41-
if err != nil {
42-
return err
43-
}
44-
defer func() { _ = tx.Rollback() }()
45-
46-
err = fn(tx)
47-
if err != nil {
48-
return err
49-
}
50-
51-
return tx.Commit()
52-
}
53-
54-
// InsertAndFetchId executes the given query and fetches the last inserted ID.
55-
func InsertAndFetchId(ctx context.Context, tx *sqlx.Tx, stmt string, args any) (int64, error) {
56-
var lastInsertId int64
57-
if tx.DriverName() == database.PostgreSQL {
58-
preparedStmt, err := tx.PrepareNamedContext(ctx, stmt+" RETURNING id")
59-
if err != nil {
60-
return 0, err
61-
}
62-
defer func() { _ = preparedStmt.Close() }()
63-
64-
err = preparedStmt.Get(&lastInsertId, args)
65-
if err != nil {
66-
return 0, fmt.Errorf("failed to insert entry for type %T: %s", args, err)
67-
}
68-
} else {
69-
result, err := tx.NamedExecContext(ctx, stmt, args)
70-
if err != nil {
71-
return 0, fmt.Errorf("failed to insert entry for type %T: %s", args, err)
72-
}
73-
74-
lastInsertId, err = result.LastInsertId()
75-
if err != nil {
76-
return 0, fmt.Errorf("failed to fetch last insert id for type %T: %s", args, err)
77-
}
78-
}
79-
80-
return lastInsertId, nil
81-
}
82-
8315
// ForEachRow applies the provided restoreFunc callback for each successfully retrieved row of the specified type.
8416
// It will bulk SELECT the data from the database scoped to the specified ids and scans into the provided Row type.
8517
// Returns error on any database failure or fails to acquire the table semaphore.
@@ -139,25 +71,6 @@ func ToDBInt(value int64) types.Int {
13971
return val
14072
}
14173

142-
func RemoveIf[T any](slice []T, pred func(T) bool) []T {
143-
n := len(slice)
144-
145-
for i := 0; i < n; i++ {
146-
for i < n && pred(slice[i]) {
147-
n--
148-
slice[i], slice[n] = slice[n], slice[i]
149-
}
150-
}
151-
152-
return slice[:n]
153-
}
154-
155-
func RemoveNils[T any](slice []*T) []*T {
156-
return RemoveIf(slice, func(ptr *T) bool {
157-
return ptr == nil
158-
})
159-
}
160-
16174
// IterateOrderedMap implements iter.Seq2 to iterate over a map in the key's order.
16275
//
16376
// This function returns a func yielding key-value-pairs from a given map in the order of their keys, if their type

internal/utils/utils_test.go

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,6 @@ import (
55
"testing"
66
)
77

8-
func TestRemoveNils(t *testing.T) {
9-
var a, b, c, d int
10-
11-
tests := []struct {
12-
name string
13-
in []*int
14-
want []*int
15-
}{
16-
{"Empty", []*int{}, []*int{}},
17-
{"SingleKeep", []*int{&a}, []*int{&a}},
18-
{"SingleRemove", []*int{nil}, []*int{}},
19-
{"KeepOrder", []*int{&a, &b, &c, &d}, []*int{&a, &b, &c, &d}},
20-
{"Duplicates", []*int{&a, &b, &b}, []*int{&a, &b, &b}},
21-
{"Mixed", []*int{&a, nil, &b, nil, nil, &b, nil, &d}, []*int{&a, &b, &b, &d}},
22-
}
23-
for _, tt := range tests {
24-
t.Run(tt.name, func(t *testing.T) {
25-
assert.Equal(t, tt.want, RemoveNils(tt.in))
26-
})
27-
}
28-
}
29-
308
func TestIterateOrderedMap(t *testing.T) {
319
tests := []struct {
3210
name string

0 commit comments

Comments
 (0)