@@ -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
0 commit comments