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 Jan 16, 2024
1 parent 7339071 commit 996fd95
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 120 deletions.
92 changes: 0 additions & 92 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
@@ -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}}
Expand All @@ -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
})
}
28 changes: 0 additions & 28 deletions internal/utils/utils_test.go

This file was deleted.

0 comments on commit 996fd95

Please sign in to comment.