All SQLite access converted to use context manager and prepared statements #64
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request resolves issue #37.
Three classes were changed, but all were changed substantially.
Basically, the classes previously all had a pair of functions:
__openDB()
and__closeDB()
, and every function that used the database had to call first one then the other.__closeDB()
was not only tasked with closing the database, it also committed any outstanding transactions.There are two main problems with this.
__closeDB()
. (See, for example,Users.uidExists()
inusers.py
.) It is also not exception-safe - if an exception were to be thrown after a successful database command but before__closeDB()
, the effects of the command would basically be lost.Seen
class, but that's a whole other issue.)Generally, all functions in all three classes that actually accessed databases were converted from this form:
to this form:
All SQL statements were also pulled out of the code and placed separately at the top of the files. This will make it easier to figure out any changes that have to be made if the database structure changes.
This pull request also resolves several other minor issues.
A potential future improvement is a new module
database
, with adb_open()
function. Then there would be no need for thecontextlib
machinery anywhere else, leaving you with this: