canonical / sqlair

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

Change decode to return an error. #60

Closed Aflynn50 closed 1 year ago

Aflynn50 commented 1 year ago

This PR now changes Decode to return an error instead of a bool so that issues can be dealt with straight away. It now doesn't close the rows since an error could be handled and iteration could continue.

Old description:

This PR adds two lines in Decode to close the underlying sql.Rows if an error occurs in execution. This closes the underlying database connection.

If a call to sql.Rows.Next returns false the rows are automatically closed. Since we are stuck with this behaviour when making use of the underlying sql.Rows it makes sense that we are consistent with it between our Iter.Next and Iter.Decode which both return bool.

Moreover, if decode throws an error and the user does not do Iter.Close then they will get obscure errors from subsequent actions due to the leaking of the underlying a database connection. In the interest of making the users life as pain-free as possible it makes sense to close this connection proactivally where possible.

It is very much good practice to do Iter.Close after Next or Decode has returned false not least to check for the error, regardless of this change.