Skip to content

Commit

Permalink
feat: returning id
Browse files Browse the repository at this point in the history
  • Loading branch information
dyaksa committed Jul 17, 2024
1 parent 8873a45 commit 6220ee0
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions crypto/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type TextHeap struct {
Hash string
}

func InsertWithHeap[T Entity](c *crypto.Crypto, ctx context.Context, tx *sql.Tx, tableName string, entity T) (a T, err error) {
func InsertWithHeap[T Entity](c *crypto.Crypto, ctx context.Context, tx *sql.Tx, tableName string, entity any, generic T) (a T, err error) {
entityValue := reflect.ValueOf(entity)
entityType := entityValue.Type()
var fieldNames []string
Expand Down Expand Up @@ -104,15 +104,15 @@ func InsertWithHeap[T Entity](c *crypto.Crypto, ctx context.Context, tx *sql.Tx,
placeholders = append(placeholders, "$"+fmt.Sprint(len(placeholders)+1))
}

query := fmt.Sprintf("INSERT INTO %s (%s) VALUES (%s) RETURNING (%s)", tableName, strings.Join(fieldNames, ", "), strings.Join(placeholders, ", "), strings.Join(fieldNames, ", "))
query := fmt.Sprintf("INSERT INTO %s (%s) VALUES (%s) RETURNING id", tableName, strings.Join(fieldNames, ", "), strings.Join(placeholders, ", "))

// stmt, err := tx.PrepareContext(ctx, query)
// if err != nil {
// return a, fmt.Errorf("failed to prepare statement: %w", err)
// }
// defer stmt.Close()
stmt, err := tx.PrepareContext(ctx, query)
if err != nil {
return a, fmt.Errorf("failed to prepare statement: %w", err)
}
defer stmt.Close()

err = tx.QueryRowContext(ctx, query, args...).Scan(&a)
err = stmt.QueryRowContext(ctx, args...).Scan(&a)
if err != nil {
return a, fmt.Errorf("failed to execute statement: %w", err)
}
Expand Down

0 comments on commit 6220ee0

Please sign in to comment.