bun icon indicating copy to clipboard operation
bun copied to clipboard

SelectQuery has ScanAndCount, but UpdateQuery, InsertQuery, DeleteQuery do not

Open TheWug opened this issue 2 years ago • 2 comments

As described by the title, SelectQuery has ScanAndCount, a convenience method for processing rows and counting them at the same time.

Normally, inserts/updates/deletes don't return rows and are invoked with Exec, which returns a row count, and therefore it's not necessary to do anything special to count the number of rows they operate on. But they can return rows if the RETURNING clause is used, in which case they need to be processed with Scan instead. However, they lack a corresponding ScanAndCount method, which limits the practical usefulness of this (it becomes difficult to e.g. check whether a given operation did anything significant, or operated on 0 rows because of the query's filter).

Can the ScanAndCount and any similar functions be be implemented for all query types which have the ability to produce rows?

TheWug avatar Jun 04 '23 00:06 TheWug

You can use res.RowsAffected as the count. We probably can support ScanAndCount as well...

vmihailenco avatar Jun 04 '23 14:06 vmihailenco

Scan() doesn't return an sql.Result, only an error. If I use Exec() will it actually capture the result rows? My impression was that it wouldn't but the docs aren't clear and I didn't try it.

TheWug avatar Jun 06 '23 06:06 TheWug