pop icon indicating copy to clipboard operation
pop copied to clipboard

Consider making Tx an interface to facilitate other implementations of the store interface

Open maplebed opened this issue 6 years ago • 0 comments

I am trying to provide wrappers for common libraries to make it easier to send instrumentation information about the library's use to a 3rd party platform. The struct types in both sql and sqlx make this difficult, but I was happy to see that pop has a store interface. Implementing this interface as a wrapper around sqlx that emits information about the database calls looks like it can work very well. I have a proof of concept wrapper and an example that uses it to demonstrate what I mean.

The challenge comes partly with NamedStmt (though its use in pop seems limited, if I'm reading it right) and mostly with the Transaction calls. Because the store interface's Transaction function returns a *Tx type and the *Tx type embeds sqlx.Tx, it is impossible to get information about a transaction except for its initial creation. Any following statements that happen directly on the Tx type (including commits and rollbacks) are impossible to capture as a wrapper.

If the pop.Tx type were an interface instead of a struct type, I could implement a wrapper that passes through all future sql statements that happen during the transaction.

While the full surface of a sqlx.Tx is rather large (25 functions), the number of functions in the pop.Tx interface could be significantly smaller, in the same way that the store interface is significantly smaller than the full sqlx surface.

maplebed avatar Aug 28 '18 16:08 maplebed