-
Notifications
You must be signed in to change notification settings - Fork 18k
cmd/vet: detect mismatch in no. of columns and expressions in sql queries #28611
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Sounds reasonable, and reminds me of the checks that already take place for The check ticks the correctness vet requirement, since a different number of arguments is definitely a bug. Precision shouldn't be a problem either, as it should be easy to avoid false positives. Frequency is always a bit more hard to prove - do you have any proof of people running into this issue besides your own experience? |
I don't. Maybe we can spread this bug around and let people +1 or -1 the issue and gauge reaction. |
This would be great for an external tool. I'm not sure it could properly lex SQL server, pg, MySQL, Oracle, both named and indexed(@ $ :), understand comments (vendors support different comment styles), properly handle :: symbols but reading :N vars. It would need to handle T-SQL and PL/SQL. As soon as the sql string was not a constant it would need to ignore the check. This would be fine in a limited case for an external tool. |
The query strings are interpreted by the driver and are database specific. PostgreSQL uses |
I didn't know that I agree that an external tool could be a good start, to gather data about the check's viability and usefulness. |
Yeah .. that's why I thought it wouldn't be feasible. Maybe I will create something just for postgres since I need it. Closing. Thanks for the input everyone. |
It seems like a good application for a static checker. SQL is a fairly widely used standard package, and SQL queries would benefit from detection similar to printf format strings. That said, SQL is much less frequently used than printf, and printf misformatting often goes undetected because it does not return an error, whereas a malformed SQL query is destined to return an error and such errors are invariably checked, so your checker will uncover relatively few problems in practice. If you proceed, I suggest you use the new golang.org/x/tools/go/analysis.Analyzer API so that the checker can be freely incorporated into vet or other tools. Take a look at the existing printf checker as a starting point. You should attempt to quickly skip the analyzer if the package does not import database/sql, or you should attempt to identify and check db.Exec wrapper functions similar to the way we identify and check printf wrapper functions in packages other than fmt. |
Awesome, thanks Alan ! |
FYI - I went ahead and made this https://github.com/agnivade/sqlargs. It works only for postgres queries as of now. @alandonovan - Any feedback will be appreciated ! I didn't use the P.S. I am thinking of writing a blog post on how to write a vet analyzer pass. |
Correct, facts are only needed in the situation you describe. In your scenario, you might want to use facts if you have functions that call db.Exec, and you want to check calls to them as if they were direct calls to db.Exec. Quick review:
|
Thanks for taking the time to review !
For the switch case, I had thought of it. Since I am actually checking for the inverse of the condition, doing FactTypes - yes I will remove that. |
What version of Go are you using (
go version
)?Consider a sql query like this -
Now, it let's say that you get a quick feature request to add a new column, you do -
Let's assume you are lazy programmer who doesn't write tests. So everything compiles fine and you deploy. Until you get this error in prod -
pq: INSERT has more target columns than expressions
All because you missed a
$3
.Alternatively, you might add the
$3
and forget to add thecol3
, in which case, the error is -pq: INSERT has more expressions than target columns
This can also happen while removing a particular column from a query. Another scenario can be when you forget to add/remove the
param3
, although that has been a rarity for me. It is also difficult to stumble into this in an UPDATE query since you will have values in the format ofcol=$1
pairs.But I have been bitten by the first 2 errors way too often, and wanted to check whether it's a good idea to have a vet check for this. Or whether it is feasible at all in the first place to do.
/cc @mvdan @kardianos
Please feel free to close if this doesn't make sense, or not feasible to do.
The text was updated successfully, but these errors were encountered: