Skip to content

Commit b664cfb

Browse files
committed
WIP
1 parent 16a00bd commit b664cfb

File tree

11 files changed

+855
-51
lines changed

11 files changed

+855
-51
lines changed

lib/sql/CMakeLists.txt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
#================= Add Component ==========================
22
qx_add_component("Sql"
33
HEADERS_API
4-
qx-common-sql.h
4+
qx-sqldatabase.h
5+
qx-sqlerror.h
6+
qx-sqlquery.h
7+
qx-sqlstring.h
58
IMPLEMENTATION
6-
qx-common-sql.cpp
9+
qx-sqldatabase.cpp
10+
qx-sqlerror.cpp
11+
qx-sqlquery.cpp
12+
qx-sqlstring.cpp
713
#DOC_ONLY
814
LINKS
915
PUBLIC

lib/sql/include/qx/sql/qx-common-sql.h

Lines changed: 0 additions & 27 deletions
This file was deleted.
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
35+
36+
class QX_SQL_EXPORT SqlDatabase
37+
{
38+
//-Class Variables-----------------------------------------------------------------------------------------------
39+
private:
40+
static inline const QString ID_NAMESPACE = u"Qx::SqlDatabase"_s;
41+
42+
//-Instance Variables-----------------------------------------------------------------------------------------------
43+
private:
44+
// NOTE: These are not const to allow move semantics, but DO NOT write to them after construction, or else this
45+
// isn't thread safe.
46+
QString mDatabaseName; /* TODO: Have opt in ctor var for shared connections in the same thread; that is,
47+
* shared instances don't salt their pointer and reuse the same connection as long
48+
* as they're in the same thread, then non-shared instances (default) will only use
49+
* the same connection through the same instance, as is now. Perhaps specify this by
50+
* an optional connection name input that defaults to empty, which will cause this
51+
* class to use the db name as part of the connection name by default, then if the
52+
* connection name is different, that is used instead of the
53+
*/
54+
QString mDriver;
55+
QString mId;
56+
57+
//-Constructor-------------------------------------------------------------------------------------------------
58+
public:
59+
// TODO: Instead of passing only the driver and db name, allow passing a struct that olds those
60+
// and all of the stuff there are setters for in QSqlDatabase so that we can allow full customization
61+
// before/at construction time, but dont need to allow setting the values after and therefore don't need
62+
// our own mutex.
63+
explicit SqlDatabase(const QString& databaseName, const QString& driver);
64+
SqlDatabase(const SqlDatabase& other);
65+
SqlDatabase(SqlDatabase&& other);
66+
67+
//-Destructor-------------------------------------------------------------------------------------------------
68+
public:
69+
~SqlDatabase();
70+
71+
//-Class Functions------------------------------------------------------------------------------------------------------
72+
private:
73+
static QString connectionName(QStringView id, const QThread* thread);
74+
static void closeConnection(const QString& connectionName);
75+
static void closeConnection(QStringView id, const QThread* thread);
76+
77+
//-Instance Functions------------------------------------------------------------------------------------------------------
78+
private:
79+
void closeAllConnections();
80+
QString connectionName(const QThread* thread) const;
81+
82+
public:
83+
QSqlError database(QSqlDatabase& db);
84+
85+
// SQL
86+
template<typename First, typename ...Rest>
87+
SqlDqlQuery SELECT(First&& fs, Rest&&... s) { return SqlDqlQuery(false, *this, std::forward<First>(fs), std::forward<Rest>(s)...); }
88+
template<typename First, typename ...Rest>
89+
SqlDqlQuery SELECT_DISTINCT(First&& fs, Rest&&... s) { return SqlDqlQuery(true, *this, std::forward<First>(fs), std::forward<Rest>(s)...); }
90+
91+
//-Operators------------------------------------------------------------------------------------------------------
92+
public:
93+
SqlDatabase& operator=(const SqlDatabase& other);
94+
SqlDatabase& operator=(SqlDatabase&& other);
95+
};
96+
97+
}
98+
99+
#endif // QX_SQLDATABASE_H
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#ifndef QX_SQLERROR_H
2+
#define QX_SQLERROR_H
3+
4+
// Shared Lib Support
5+
#include "qx/sql/qx_sql_export.h"
6+
7+
// Extra-component Includes
8+
#include "qx/core/qx-abstracterror.h"
9+
10+
namespace Qx
11+
{
12+
13+
class QX_SQL_EXPORT SqlError final : public AbstractError<"Qx::SqlError", 7>
14+
{
15+
friend class Db;
16+
//-Class Enums-------------------------------------------------------------
17+
public:
18+
enum Form
19+
{
20+
NoError
21+
};
22+
23+
//-Class Variables-------------------------------------------------------------
24+
private:
25+
static inline const QHash<Form, QString> ERR_STRINGS{
26+
{NoError, u"No error occurred."_s},
27+
};
28+
29+
//-Instance Variables-------------------------------------------------------------
30+
private:
31+
Form mForm;
32+
QString mCause;
33+
QString mDetails;
34+
35+
//-Class Constructor-------------------------------------------------------------
36+
public:
37+
SqlError();
38+
SqlError(Form f, const QString& c, const QString& d = {});
39+
40+
//-Instance Functions-------------------------------------------------------------
41+
private:
42+
quint32 deriveValue() const override;
43+
QString derivePrimary() const override;
44+
QString deriveSecondary() const override;
45+
QString deriveDetails() const override;
46+
47+
public:
48+
bool isValid() const;
49+
Form form() const;
50+
QString cause() const;
51+
QString details() const;
52+
};
53+
54+
}
55+
56+
#endif // QX_SQLERROR_H

0 commit comments

Comments
 (0)