From what I can tell, there are only two types of results the Go database/sql
interface library expects back - a row or an array of rows. However, there is at least one more type of result - a single column.
DB.column('SELECT COUNT(*) FROM `user` WHERE `banned` IS NOT NULL')
Is there any way to handle this - or do I just have to fetch a row and then access the COUNT(*)
from that?
Yes you fetch a one column row but is that so hard ?
var count int
row := db.QueryRow("SELECT COUNT(*) FROM `user` WHERE `banned` IS NOT NULL")
err := row.Scan(&count)
Note that this may be compacted if you find it too verbose (you may remove the row variable).
I think that other similar systems in other languages (for example JDBC) don't offer natively this shortcut either.
I find easier to handle an API that I can memorize and browse rather than an API which has all the utilities I might be willing to use to remove one line in my code.
For the record, a SQL Server stored procedure returns all of the following (at the same time):
Within a rowset, all rows have the same number (one or more) of named, typed columns. The column names do not have to be distinct within a rowset.
SQL Server does not recognize any special cases, like a single rowset with a single row or a single column; or a single output parameter.
Other database systems are slightly different.