A website inspired by Last.fm that will keep track of your listening statistics
lastfm music statistics
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add option to database insert helper to add `OR IGNORE` to insert statement

oscar345 727b9bbc 61d9ac20

+25 -3
+25 -3
pkg/database/insert.go
··· 9 9 "strings" 10 10 ) 11 11 12 - func Insert(ctx context.Context, db *sql.DB, table string, values map[string]any) (int, error) { 12 + type insertOptions struct { 13 + ignore bool 14 + } 15 + 16 + type InsertOption func(*insertOptions) 17 + 18 + func WithIgnore() InsertOption { 19 + return func(o *insertOptions) { 20 + o.ignore = true 21 + } 22 + } 23 + 24 + func Insert(ctx context.Context, db *sql.DB, table string, values map[string]any, opts ...InsertOption) (int, error) { 13 25 var id int 14 26 statement := /*sql*/ ` 15 - INSERT INTO %s (%s) VALUES (%s) RETURNING id 27 + INSERT %s INTO %s (%s) VALUES (%s) RETURNING id 16 28 ` 17 29 30 + options := insertOptions{} 31 + for _, opt := range opts { 32 + opt(&options) 33 + } 34 + 35 + ignore := "" 36 + if options.ignore { 37 + ignore = "OR IGNORE" 38 + } 39 + 18 40 columns := slices.Collect(maps.Keys(values)) 19 41 placeholders := strings.Join(slices.Repeat([]string{"?"}, len(columns)), ", ") 20 - statement = fmt.Sprintf(statement, table, columns, placeholders) 42 + statement = fmt.Sprintf(statement, ignore, table, columns, placeholders) 21 43 args := slices.Collect(maps.Values(values)) 22 44 23 45 return QueryOne(ctx, db, statement, args, func(r *sql.Rows) (int, error) {