canonical / sqlair

Friendly type mapping for SQL databases
Apache License 2.0
16 stars 8 forks source link

fix: add error for unused types in prepare and refactor bindtypes/arginfo #153

Open Aflynn50 opened 4 weeks ago

Aflynn50 commented 4 weeks ago

When doing sqlair.Prepare, SQLair will not throw an error if you pass extra types that are not used in the query. This refactor is motivated by the need to add this error.

The BindTypes function used ArgInfo from the typeinfo package to get information about the arguments. The ArgInfo object has methods that allow BindTypes to pass simply the name of the type needed and they will return all the relevent information about the type or an error message if it is not found. The typeinfo package is only concerned with getting information about types, and does not do any storing of state.

A simple (but wrong) way to track which types have been used during prepare would be to add state to this ArgInfo object, and when BindTypes was finished, it could be queried to see if all types had been used. However, this would add state to ArgInfo

Another way to do this would be to keep track of the arguments used in BindTypes directly, however, the level of abstraction here is wrong, several layers have to be unwrapped to get the type information.

The solution I have landed upon is adding an extra layer in-between ArgInfo and the bindTypes functions on the typed expressions. This follows a similar pattern to the query builder in the BindInputs stage. The ArgInfo object arguably had too much responsibility considering that it was designed only to be concerned with types. Therefore, it was natural to transfer some of this to the new typedExprBulider layer which is now responsible for finding the arguments in the map returned from GenerateArgInfo and also with the keeping track of outputs and arguments used.