Gorp - Go’s “ORM”
I hate writing SQL. Always have. I find the SQL Language repulsive, and reading other developer’s “clever” SQL makes me ill. It is just about the ugliest syntax I have ever seen. By now you are getting the idea why something like GORP would appeal to me. Gorp creates a mapping between structs and sql database tables which allows for various dialects of sql database. AWESOME! I don’t have to write SQL.
Example of gorp.v1
Using gorp is easy. Below is a toy example of a sql schema, and the corresponding gorp instructions:
The above is fairly readable, and understandable, and didn’t take any sql in code which is great. There are clear understandable helper methods such as Insert, Update, Delete, Get which covers a variety of common needs. Insert, Update, Delete are variadic which allows for multiple arguments, which is also excellent.
What is the Catch?
Well, in our example car insertion, what would you expect the “Old Beater” car record to have for a Created time? I would expect it to be whatever the database time was at insertion. Wrong!
If you look in the gorp code for the Insert bindings you can see gorp is being really smart and iterating over ALL of the columns to generate the sql code for the insertion. This is unfortunate as the query ends up looking like this:
Which makes our Created time become NULL in the database, instead of what we were trying to get, which was the sql NOW() value…
It is important to understand that gorp is doing an EXACT mapping, and not omitting nil values from the query.
If we look at the Insert bindings again, we also notice that the variadic is being looped over, and each parameter is being inserted one at a time into the database. This isn’t very awesome for large numbers of parameters, as our generated sql looks like this:
Whereas we might have wanted them all to be included in one sql statement like this:
This causes extreme slowness if there are many items that are to be inserted at the same time. I believe a few tweaks to gorp’s insert/update/delete bindings could make these problems go away, but might introduce other issues.
Though I am pointing out some issues I have seen in Gorp, I am not going to dismiss it as a decent code base for doing database mappings in golang. I feel like with time it will become better, but I do think there is no substitute for well thought out sql queries in applications. I have come across many instances where I needed to do some task in sql that isn’t a base primitive in Gorp, and still end up including sql in my code bases, but, since I have been leaning on Gorp I end up getting sloppy and put all of the custom sql statements required in in-opportune places in the code…