Skip to content

Commit 6220ee0

Browse files
committed
feat: returning id
1 parent 8873a45 commit 6220ee0

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

crypto/query/query.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type TextHeap struct {
3333
Hash string
3434
}
3535

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

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

109-
// stmt, err := tx.PrepareContext(ctx, query)
110-
// if err != nil {
111-
// return a, fmt.Errorf("failed to prepare statement: %w", err)
112-
// }
113-
// defer stmt.Close()
109+
stmt, err := tx.PrepareContext(ctx, query)
110+
if err != nil {
111+
return a, fmt.Errorf("failed to prepare statement: %w", err)
112+
}
113+
defer stmt.Close()
114114

115-
err = tx.QueryRowContext(ctx, query, args...).Scan(&a)
115+
err = stmt.QueryRowContext(ctx, args...).Scan(&a)
116116
if err != nil {
117117
return a, fmt.Errorf("failed to execute statement: %w", err)
118118
}

0 commit comments

Comments
 (0)