Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor DB code with Database(Holder|Helper) #245

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from

Conversation

vedgy
Copy link
Contributor

@vedgy vedgy commented Apr 11, 2021

See the commit messages for details.

vedgy added 10 commits April 11, 2021 16:59
Including only what is used should reduce compilation time.

The added includes were necessary to make the respective translation
units compile after a thorough cleanup of data_base_management.h
includes. This is how the complete include and forward-declaration
sections should look like in data_base_management.h:

'#'include <QThread> // (prevent '#' from starting a git comment line)

class QSqlDatabase;
class QSqlQuery;
class QSqlRecord;
class QString;
class QStringList;

Subsequently I had to partly restore the unnecessary includes by moving
them from data_base_management.cpp into the header due to the huge
number of compilation errors I am not prepared to deal with right now.
Even an implicitly shared class is cheaper to pass by reference than by
value, as evidenced by Qt APIs themselves.
All other similar updating functions take db as the last argument.
Take QString arguments by value only if they are unconditionally moved
from.

Fix typos in DBHelper::reassignOrderTo* member function names.
These classes will allow to improve exception and early-return safety as
well as simplify code that calls QSqlDatabase::removeDatabase().
The returned value is always the same and is not used by the caller.

Remove unused variables from this function.
DatabaseHolder is preferable to DatabaseHelper here for these reasons:
1. DataBaseManagement uses DBHelper only once, so DatabaseHelper's
advantage is tiny here.
2. DataBaseManagement uses the wrapped QSqlDatabase object a lot.
DatabaseHolder's operators make these uses convenient and less verbose.
3. In YACReader code a QSqlDatabase object is almost always obtained via
DataBaseManagement::loadDatabase(). DatabaseHelper provides only one
constructor that adds a QSqlDatabase connection in this way to prevent
usage mistakes. By contrast, DataBaseManagement always adds QSqlDatabase
connections in other ways, so DatabaseHelper cannot be used as is.
This change eliminates the following warning from the output of
YACReaderLibrary built in Debug mode:
  QSqlDatabasePrivate::addDatabase: duplicate connection name
  '/path/to/.yacreaderlibrary<thread id>', old connection removed.
This change shows how code is simplified by using DatabaseHelper. It
also allows to test DatabaseHelper. Eventually DatabaseHelper (or rarely
DatabaseHolder) should replace almost all explicit
QSqlDatabase::removeDatabase() calls.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant