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