|
| 1 | +#ifndef QX_SQLDATABASE_H |
| 2 | +#define QX_SQLDATABASE_H |
| 3 | + |
| 4 | +// Shared Lib Support |
| 5 | +#include "qx/sql/qx_sql_export.h" |
| 6 | + |
| 7 | +// Qt Includes |
| 8 | +#include <QSqlDatabase> |
| 9 | +#include <QReadWriteLock> |
| 10 | +#include <QUuid> |
| 11 | + |
| 12 | +// Intra-component Includes |
| 13 | +#include "qx/sql/qx-sqlquery.h" |
| 14 | + |
| 15 | +// Extra-component Includes |
| 16 | + |
| 17 | +using namespace Qt::StringLiterals; |
| 18 | + |
| 19 | +class QThread; |
| 20 | + |
| 21 | +namespace Qx |
| 22 | +{ |
| 23 | + |
| 24 | +/* Careful use of the thread safe static functions of QSqlDatabase here allow us to avoid |
| 25 | + * needing our own lock and connection tracker, AND ensure that a lambda can be used to |
| 26 | + * handle connection closures due to thread-termination without having a potential dangling |
| 27 | + * reference to the this pointer in the capture, thereby avoiding the overhead of needing |
| 28 | + * to make this a QObject in order to disconnect when this is destroyed. |
| 29 | + * |
| 30 | + * This introduces a small amount of overhead, particularly when it comes to closing all |
| 31 | + * connections at destruction, but the added simplicity is worth it.. |
| 32 | + */ |
| 33 | + |
| 34 | +class QX_SQL_EXPORT SqlDatabase |
| 35 | +{ |
| 36 | +//-Class Variables----------------------------------------------------------------------------------------------- |
| 37 | +private: |
| 38 | + static inline const QString ID_NAMESPACE = u"Qx::SqlDatabase"_s; |
| 39 | + |
| 40 | +//-Instance Variables----------------------------------------------------------------------------------------------- |
| 41 | +private: |
| 42 | + // NOTE: These are not const to allow move semantics, but DO NOT write to them after construction, or else this |
| 43 | + // isn't thread safe. |
| 44 | + QString mDatabaseName; /* TODO: Have opt in ctor var for shared connections in the same thread; that is, |
| 45 | + * shared instances don't salt their pointer and reuse the same connection as long |
| 46 | + * as they're in the same thread, then non-shared instances (default) will only use |
| 47 | + * the same connection through the same instance, as is now. Perhaps specify this by |
| 48 | + * an optional connection name input that defaults to empty, which will cause this |
| 49 | + * class to use the db name as part of the connection name by default, then if the |
| 50 | + * connection name is different, that is used instead of the |
| 51 | + */ |
| 52 | + QString mDriver; |
| 53 | + QString mId; |
| 54 | + |
| 55 | +//-Constructor------------------------------------------------------------------------------------------------- |
| 56 | +public: |
| 57 | + // TODO: Instead of passing only the driver and db name, allow passing a struct that olds those |
| 58 | + // and all of the stuff there are setters for in QSqlDatabase so that we can allow full customization |
| 59 | + // before/at construction time, but dont need to allow setting the values after and therefore don't need |
| 60 | + // our own mutex. |
| 61 | + explicit SqlDatabase(const QString& databaseName, const QString& driver); |
| 62 | + SqlDatabase(const SqlDatabase& other); |
| 63 | + SqlDatabase(SqlDatabase&& other); |
| 64 | + |
| 65 | +//-Destructor------------------------------------------------------------------------------------------------- |
| 66 | +public: |
| 67 | + ~SqlDatabase(); |
| 68 | + |
| 69 | +//-Class Functions------------------------------------------------------------------------------------------------------ |
| 70 | +private: |
| 71 | + static QString connectionName(QStringView id, const QThread* thread); |
| 72 | + static void closeConnection(const QString& connectionName); |
| 73 | + static void closeConnection(QStringView id, const QThread* thread); |
| 74 | + |
| 75 | +//-Instance Functions------------------------------------------------------------------------------------------------------ |
| 76 | +private: |
| 77 | + void closeAllConnections(); |
| 78 | + QString connectionName(const QThread* thread) const; |
| 79 | + |
| 80 | +public: |
| 81 | + SqlError database(QSqlDatabase& db, bool connect = true); |
| 82 | + QString driver() const; |
| 83 | + QString databaseName() const; |
| 84 | + bool isConnected() const; |
| 85 | + |
| 86 | + // SQL |
| 87 | + template<sql_string First, sql_string ...Rest> |
| 88 | + SqlDqlQuery SELECT(First&& fs, Rest&&... s) |
| 89 | + { |
| 90 | + SqlDqlQuery q(*this); |
| 91 | + q.SELECT(std::forward<First>(fs), std::forward(s)...); |
| 92 | + return q; |
| 93 | + } |
| 94 | + |
| 95 | + template<QxSql::sql_struct... Structs> |
| 96 | + SqlDqlQuery SELECT() |
| 97 | + { |
| 98 | + SqlDqlQuery q(*this); |
| 99 | + q.SELECT<Structs...>(); |
| 100 | + return q; |
| 101 | + } |
| 102 | + |
| 103 | + template<sql_string First, sql_string ...Rest> |
| 104 | + SqlDqlQuery SELECT_DISTINCT(First&& fs, Rest&&... s) |
| 105 | + { |
| 106 | + SqlDqlQuery q(*this); |
| 107 | + q.SELECT_DISTINCT(std::forward<First>(fs), std::forward(s)...); |
| 108 | + return q; |
| 109 | + } |
| 110 | + |
| 111 | + template<QxSql::sql_struct... Structs> |
| 112 | + SqlDqlQuery SELECT_DISTINCT() |
| 113 | + { |
| 114 | + SqlDqlQuery q(*this); |
| 115 | + q.SELECT<Structs...>(); |
| 116 | + return q; |
| 117 | + } |
| 118 | + |
| 119 | +//-Operators------------------------------------------------------------------------------------------------------ |
| 120 | +public: |
| 121 | + SqlDatabase& operator=(const SqlDatabase& other); |
| 122 | + SqlDatabase& operator=(SqlDatabase&& other); |
| 123 | +}; |
| 124 | + |
| 125 | +} |
| 126 | + |
| 127 | +#endif // QX_SQLDATABASE_H |
0 commit comments