package infra

import (
	"database/sql"
	"fmt"
)

// DBExec executes a non-SELECT statement (INSERT, UPDATE, DELETE, DDL) and
// returns the number of rows affected. For statements that don't return rows
// affected (e.g. DDL on some drivers), the count may be 0.
func DBExec(db *sql.DB, query string, args ...any) (int64, error) {
	result, err := db.Exec(query, args...)
	if err != nil {
		return 0, fmt.Errorf("db_exec: %w", err)
	}
	n, err := result.RowsAffected()
	if err != nil {
		// Some drivers don't support RowsAffected; treat as 0.
		return 0, nil
	}
	return n, nil
}
