#Unified function for db.Query and db.Exec

4 messages · Page 1 of 1 (latest)

urban valley
#

If I am given an SQL query string, but I don't know beforehand whether it's a SELECT query (which requires using db.Query()) or a non-SELECT query (like INSERT, UPDATE, DELETE, or DDL, which requires using db.Exec()), is it possible for me to automatically determine which one to use (db.Query() or db.Exec())?

Is there a single function in Golang (without needing to inspect the query) that I can use which will return the correct result, whether it's for a SELECT query (which returns rows) or a non-SELECT query (which affects rows but doesn't return them)?

waxen dome
#

what return type would you be looking for in this hypothetical function?

#

a function's signature can't change at runtime, so either it would need to return something that doesn't exist in Exec, or not return values that do in Query

uncut zenith
#

Since there is a difference in what possible queries may do, it will be better to have one function per type of action and some sort of wrapper one to decide which one of them to use based on the string? Also, where are those strings coming from? There could be a vulnerability for SQL injection.
Anyways, I am curious, what is the use case of this?