Skip to content

Commit 54d3c7c

Browse files
authored
Merge pull request HerikLyma#26 from wehnersteffensielaff/multidatabasecapabilitySecondtry
Multidatabasecapability secondtry
2 parents e071c4c + e2b6d8a commit 54d3c7c

File tree

9 files changed

+127
-30
lines changed

9 files changed

+127
-30
lines changed

CPPWebFramework/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ set(CPP_WebFrameWork_SRCS
3737
cwf/sqlquerymanager.cpp
3838
cwf/cstlcompilerobject.h
3939
cwf/variant.h
40-
cwf/dbstorage.h
40+
4141
)
4242

4343
add_library(CPPWebFramework SHARED ${CPP_WebFrameWork_SRCS} )
@@ -50,5 +50,5 @@ target_link_libraries(
5050
Qt5::Sql
5151

5252
)
53-
53+
INSTALL(TARGETS CPPWebFramework LIBRARY DESTINATION lib COMPONENT CPPWebFramework)
5454
add_definitions(-DCPPWEBFRAMEWORK_LIBRARY)

CPPWebFramework/cwf/constants.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#ifndef CONSTANTS_H
99
#define CONSTANTS_H
1010

11+
// clazy:excludeall=non-pod-global-static
1112
#include <QString>
1213
#include <QByteArray>
1314
#include "cppwebframework_global.h"

CPPWebFramework/cwf/cppwebapplication.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
#include "cppwebapplication.h"
99
#include "cppwebcontroller.h"
10-
10+
// clazy:excludeall=qgetenv
1111
CWF_BEGIN_NAMESPACE
1212

1313
QPair<QString, qint64> getFileAndMaxSize()

CPPWebFramework/cwf/cstlcompilerattributes.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include "constants.h"
1010
#include <QVariant>
1111
#include "metaclassparser.h"
12-
12+
// clazy:excludeall=connect-not-normalized
1313
CWF_BEGIN_NAMESPACE
1414

1515
CSTLCompilerAttributes::CSTLCompilerAttributes(QMap<QString, QObject *> &objects) : objects(objects)

CPPWebFramework/cwf/cstlcompilerobject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
#ifndef CSTLCOMPILEROBJECT_H
99
#define CSTLCOMPILEROBJECT_H
10-
10+
// clazy:excludeall=const-signal-or-slot
1111
#include <QObject>
1212
#include "cppwebframework_global.h"
1313

CPPWebFramework/cwf/httpreadrequest.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,13 @@ void HttpReadRequest::run()
6565
{
6666
if(socket->waitForReadyRead())
6767
{
68-
QByteArray req(socket->readAll());
68+
QByteArray req;
69+
try {
70+
req = socket->readAll();
71+
} catch (const std::bad_alloc & e) {
72+
qDebug() << e.what() << "\n";
73+
}
74+
6975

7076
HttpParser parser(req);
7177
if(parser.valid)
@@ -139,9 +145,15 @@ bool HttpReadRequest::readBody(HttpParser &parser, Request &request, Response &r
139145
if(socket->waitForReadyRead(10))
140146
{
141147
if (content.size() > maxUploadFile) {
142-
socket->readAll();
148+
socket->readAll();// what if the internal buffer throws a
143149
} else {
144-
content += socket->readAll();
150+
try {
151+
content += socket->readAll();
152+
} catch (const std::bad_alloc &e) {
153+
qDebug() << e.what() << "\n";
154+
break;
155+
}
156+
145157
}
146158
}
147159

CPPWebFramework/cwf/request.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include "response.h"
1212
#include "urlencoder.h"
1313
#include <QUuid>
14-
14+
// clazy:excludeall=connect-not-normalized
1515
CWF_BEGIN_NAMESPACE
1616

1717
Request::Request(QTcpSocket &socket,

CPPWebFramework/cwf/sqldatabasestorage.h

Lines changed: 104 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,63 @@
77

88
#ifndef SQLDATABASESTORAGE_H
99
#define SQLDATABASESTORAGE_H
10-
10+
#include <iostream>
1111
#include <QUuid>
1212
#include <QDebug>
1313
#include <QSqlError>
1414
#include <QSqlDatabase>
1515
#include <QThreadStorage>
1616
#include "cppwebframework_global.h"
1717

18+
#if __cplusplus >= 201703L
19+
#include <optional>
20+
#endif
21+
#if __cplusplus == 201402L
22+
#include <experimental/optional>
23+
#endif
1824
CWF_BEGIN_NAMESPACE
1925
/**
2026
* @brief The SqlDatabaseStorage class allows you to reuse connections made to the database through the QSqlDatabase class within QThreadPool.
2127
*/
28+
2229
class CPPWEBFRAMEWORKSHARED_EXPORT SqlDatabaseStorage
2330
{
2431
class Database
2532
{
2633
friend class SqlDatabaseStorage;
27-
QSqlDatabase *db = nullptr;
34+
// Map database connection names the library user uses to database connection names
35+
// which are valid for this thread
36+
std::map<QString, QSqlDatabase*> DatabaseConnections; //Unique Name to Heap allocated Connection
37+
std::map<QString, QString> trivialNameToUniqueID; //Names given by User to Unique Name
38+
//#if __cplusplus == 201402L
39+
// std::experimental::optional<QSqlDatabase> DBConnection; //C++ 14 Compatablity
40+
//#endif
41+
//#if __cplusplus >= 201703L
42+
// std::optional<QSqlDatabase> DBConnection;
43+
//#endif
2844
public:
2945
Database() = default;
3046
Database(Database &other)
3147
{
32-
db = other.db;
33-
other.db = nullptr;
48+
DatabaseConnections = other.DatabaseConnections;
49+
trivialNameToUniqueID = other.trivialNameToUniqueID;
50+
qDebug() << "Database Constructed";
3451
}
3552
~Database()
3653
{
37-
if(db)
38-
{
39-
const QString conName(db->connectionName());
40-
db->close();
41-
delete db;
42-
QSqlDatabase::removeDatabase(conName);
54+
qDebug() << "Database Destructed";
55+
if (!DatabaseConnections.empty()) {
56+
// const QString conName(DBConnection->connectionName());
57+
58+
// Remove all Database connnections this thread has and which will become invalid
59+
for (auto const & ConnectionNamePair : DatabaseConnections) {
60+
auto DBConnection = ConnectionNamePair.second;
61+
DBConnection->close();
62+
delete DBConnection;
63+
qDebug() << "" << ConnectionNamePair.first
64+
<< "" << ConnectionNamePair.second;
65+
QSqlDatabase::removeDatabase(ConnectionNamePair.first);
66+
}
4367
}
4468
}
4569
};
@@ -50,6 +74,7 @@ class CPPWEBFRAMEWORKSHARED_EXPORT SqlDatabaseStorage
5074
QString password;
5175
int port;
5276
QThreadStorage<Database> pool;
77+
5378
public:
5479
/**
5580
* @brief This constructor receives informations to create a connection to the database.
@@ -103,20 +128,79 @@ class CPPWEBFRAMEWORKSHARED_EXPORT SqlDatabaseStorage
103128
*/
104129
QSqlDatabase &getDatabase()
105130
{
106-
if(!pool.hasLocalData())
131+
if(!pool.hasLocalData()) //Pool has no Local Data -> create DataBaseConnection
107132
{
108133
Database database;
109-
database.db = new QSqlDatabase(QSqlDatabase::addDatabase(type, QUuid::createUuid().toString()));
110-
database.db->setHostName(hostName);
111-
database.db->setDatabaseName(databaseName);
112-
database.db->setPort(port);
113-
database.db->setUserName(userName);
114-
database.db->setPassword(password);
115-
if(!database.db->open())
116-
qDebug() << database.db->lastError().text();
134+
QString UniqueID = QUuid::createUuid().toString();
135+
auto DBConnection = new QSqlDatabase(QSqlDatabase::addDatabase(type, UniqueID));
136+
DBConnection->setHostName(hostName);
137+
DBConnection->setDatabaseName(databaseName);
138+
DBConnection->setPort(port);
139+
DBConnection->setUserName(userName);
140+
DBConnection->setPassword(password);
141+
if (!DBConnection->open()) {
142+
qDebug() << DBConnection->lastError().text();
143+
std::cout << "Database not openable \t"
144+
<< databaseName.toStdString()
145+
<< "\n";
146+
}
147+
117148
pool.setLocalData(database);
149+
pool.localData().DatabaseConnections.insert({UniqueID, DBConnection});
150+
pool.localData().trivialNameToUniqueID.insert({databaseName, UniqueID});
151+
} else { //Pool has Local Data
152+
auto IteratorToUserDatabaseName = pool.localData().trivialNameToUniqueID.find(databaseName);
153+
QString NameOfDBConnectionToThread;
154+
QString UniqueConnectionName;
155+
156+
if (IteratorToUserDatabaseName != pool.localData().trivialNameToUniqueID.end()) {
157+
// this thread has a Connection to the Database
158+
// if (PublicDBName->second == pool.localData().DBConnection->connectionName()) {
159+
// //already right connection
160+
// } else {
161+
// //set the right connection
162+
// NameOfDBConnectionToThread = IteratorToUserDatabaseName->first;
163+
// UniqueConnectionName = IteratorToUserDatabaseName->second;
164+
// //pool.localData().DBConnection->close();
165+
// pool.localData().DBConnection = QSqlDatabase::database(UniqueConnectionName, false);
166+
// pool.localData().DBConnection->setHostName(hostName);
167+
// pool.localData().DBConnection->setDatabaseName(databaseName);
168+
// pool.localData().DBConnection->setPort(port);
169+
// pool.localData().DBConnection->setUserName(userName);
170+
// pool.localData().DBConnection->setPassword(password);
171+
// if(!pool.localData().DBConnection->open()) {
172+
// qDebug() << pool.localData().DBConnection->lastError().text();
173+
// }
174+
// }
175+
} else {
176+
//make new Database connection for this thread
177+
QString UniqueID = QUuid::createUuid().toString();
178+
179+
auto DBConnection =new QSqlDatabase(QSqlDatabase::addDatabase(type, UniqueID));
180+
DBConnection->setHostName(hostName);
181+
DBConnection->setDatabaseName(databaseName);
182+
DBConnection->setPort(port);
183+
DBConnection->setUserName(userName);
184+
DBConnection->setPassword(password);
185+
if (!DBConnection->open()) {
186+
qDebug() << DBConnection->lastError().text();
187+
std::cout << "Database not openable \t"
188+
<< databaseName.toStdString()
189+
<< "\n";
190+
}
191+
pool.localData().DatabaseConnections.insert({UniqueID, DBConnection});
192+
pool.localData().trivialNameToUniqueID.insert({databaseName, UniqueID});
193+
}
118194
}
119-
return *pool.localData().db;
195+
// std::cerr << "Name To ID Mapping";
196+
// for ( auto const & foo : pool.localData().trivialNameToUniqueID) {
197+
// std::cerr << "Name " << foo.first.toStdString()
198+
// << "\tID " << foo.second.toStdString()
199+
// << "\n";
200+
// }
201+
auto UniqueName = pool.localData().trivialNameToUniqueID.at(databaseName);
202+
return *pool.localData().DatabaseConnections.at(UniqueName);
203+
//
120204
}
121205
};
122206

CPPWebFramework/cwf/variant.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
#ifndef VARIANT_H
99
#define VARIANT_H
10-
10+
// clazy:excludeall=const-signal-or-slot
1111
#include <QObject>
1212
#include <QVariant>
1313
#include "cppwebframework_global.h"

0 commit comments

Comments
 (0)