database interface separated from server, multiple concurrent database connections are now possible

This commit is contained in:
Max-Wilhelm Bruker
2012-05-28 16:32:45 +02:00
parent 2b89c353bf
commit b328c1ed4d
21 changed files with 347 additions and 310 deletions

View File

@@ -18,9 +18,10 @@
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <QtSql>
#include <QSqlQuery>
#include <QHostAddress>
#include <QDebug>
#include <QDateTime>
#include "serversocketinterface.h"
#include "servatrice.h"
#include "servatrice_database_interface.h"
@@ -63,6 +64,7 @@ static const int protocolVersion = 14;
ServerSocketInterface::ServerSocketInterface(Servatrice *_server, Servatrice_DatabaseInterface *_databaseInterface, QTcpSocket *_socket, QObject *parent)
: Server_ProtocolHandler(_server, _databaseInterface, parent),
servatrice(_server),
sqlInterface(reinterpret_cast<Servatrice_DatabaseInterface *>(databaseInterface)),
socket(_socket),
messageInProgress(false),
handshakeStarted(false)
@@ -244,18 +246,17 @@ Response::ResponseCode ServerSocketInterface::cmdAddToList(const Command_AddToLi
return Response::RespContextError;
int id1 = userInfo->id();
int id2 = static_cast<Servatrice_DatabaseInterface *>(databaseInterface)->getUserIdInDB(user);
int id2 = sqlInterface->getUserIdInDB(user);
if (id2 < 0)
return Response::RespNameNotFound;
if (id1 == id2)
return Response::RespContextError;
// QMutexLocker locker(&servatrice->dbMutex);
QSqlQuery query;
QSqlQuery query(sqlInterface->getDatabase());
query.prepare("insert into " + servatrice->getDbPrefix() + "_" + list + "list (id_user1, id_user2) values(:id1, :id2)");
query.bindValue(":id1", id1);
query.bindValue(":id2", id2);
if (!servatrice->execSqlQuery(query))
if (!sqlInterface->execSqlQuery(query))
return Response::RespInternalError;
Event_AddToList event;
@@ -285,16 +286,15 @@ Response::ResponseCode ServerSocketInterface::cmdRemoveFromList(const Command_Re
return Response::RespContextError;
int id1 = userInfo->id();
int id2 = static_cast<Servatrice_DatabaseInterface *>(databaseInterface)->getUserIdInDB(user);
int id2 = sqlInterface->getUserIdInDB(user);
if (id2 < 0)
return Response::RespNameNotFound;
// QMutexLocker locker(&servatrice->dbMutex);
QSqlQuery query;
QSqlQuery query(sqlInterface->getDatabase());
query.prepare("delete from " + servatrice->getDbPrefix() + "_" + list + "list where id_user1 = :id1 and id_user2 = :id2");
query.bindValue(":id1", id1);
query.bindValue(":id2", id2);
if (!servatrice->execSqlQuery(query))
if (!sqlInterface->execSqlQuery(query))
return Response::RespInternalError;
Event_RemoveFromList event;
@@ -312,13 +312,12 @@ int ServerSocketInterface::getDeckPathId(int basePathId, QStringList path)
if (path[0].isEmpty())
return 0;
// QMutexLocker locker(&servatrice->dbMutex);
QSqlQuery query;
QSqlQuery query(sqlInterface->getDatabase());
query.prepare("select id from " + servatrice->getDbPrefix() + "_decklist_folders where id_parent = :id_parent and name = :name and user = :user");
query.bindValue(":id_parent", basePathId);
query.bindValue(":name", path.takeFirst());
query.bindValue(":user", QString::fromStdString(userInfo->name()));
if (!servatrice->execSqlQuery(query))
if (!sqlInterface->execSqlQuery(query))
return -1;
if (!query.next())
return -1;
@@ -336,12 +335,11 @@ int ServerSocketInterface::getDeckPathId(const QString &path)
bool ServerSocketInterface::deckListHelper(int folderId, ServerInfo_DeckStorage_Folder *folder)
{
// QMutexLocker locker(&servatrice->dbMutex);
QSqlQuery query;
QSqlQuery query(sqlInterface->getDatabase());
query.prepare("select id, name from " + servatrice->getDbPrefix() + "_decklist_folders where id_parent = :id_parent and user = :user");
query.bindValue(":id_parent", folderId);
query.bindValue(":user", QString::fromStdString(userInfo->name()));
if (!servatrice->execSqlQuery(query))
if (!sqlInterface->execSqlQuery(query))
return false;
while (query.next()) {
@@ -356,7 +354,7 @@ bool ServerSocketInterface::deckListHelper(int folderId, ServerInfo_DeckStorage_
query.prepare("select id, name, upload_time from " + servatrice->getDbPrefix() + "_decklist_files where id_folder = :id_folder and user = :user");
query.bindValue(":id_folder", folderId);
query.bindValue(":user", QString::fromStdString(userInfo->name()));
if (!servatrice->execSqlQuery(query))
if (!sqlInterface->execSqlQuery(query))
return false;
while (query.next()) {
@@ -379,7 +377,7 @@ Response::ResponseCode ServerSocketInterface::cmdDeckList(const Command_DeckList
if (authState != PasswordRight)
return Response::RespFunctionNotAllowed;
servatrice->checkSql();
sqlInterface->checkSql();
Response_DeckList *re = new Response_DeckList;
ServerInfo_DeckStorage_Folder *root = re->mutable_root();
@@ -396,43 +394,40 @@ Response::ResponseCode ServerSocketInterface::cmdDeckNewDir(const Command_DeckNe
if (authState != PasswordRight)
return Response::RespFunctionNotAllowed;
servatrice->checkSql();
sqlInterface->checkSql();
int folderId = getDeckPathId(QString::fromStdString(cmd.path()));
if (folderId == -1)
return Response::RespNameNotFound;
// QMutexLocker locker(&servatrice->dbMutex);
QSqlQuery query;
QSqlQuery query(sqlInterface->getDatabase());
query.prepare("insert into " + servatrice->getDbPrefix() + "_decklist_folders (id_parent, user, name) values(:id_parent, :user, :name)");
query.bindValue(":id_parent", folderId);
query.bindValue(":user", QString::fromStdString(userInfo->name()));
query.bindValue(":name", QString::fromStdString(cmd.dir_name()));
if (!servatrice->execSqlQuery(query))
if (!sqlInterface->execSqlQuery(query))
return Response::RespContextError;
return Response::RespOk;
}
void ServerSocketInterface::deckDelDirHelper(int basePathId)
{
servatrice->checkSql();
// QMutexLocker locker(&servatrice->dbMutex);
QSqlQuery query;
sqlInterface->checkSql();
QSqlQuery query(sqlInterface->getDatabase());
query.prepare("select id from " + servatrice->getDbPrefix() + "_decklist_folders where id_parent = :id_parent");
query.bindValue(":id_parent", basePathId);
servatrice->execSqlQuery(query);
sqlInterface->execSqlQuery(query);
while (query.next())
deckDelDirHelper(query.value(0).toInt());
query.prepare("delete from " + servatrice->getDbPrefix() + "_decklist_files where id_folder = :id_folder");
query.bindValue(":id_folder", basePathId);
servatrice->execSqlQuery(query);
sqlInterface->execSqlQuery(query);
query.prepare("delete from " + servatrice->getDbPrefix() + "_decklist_folders where id = :id");
query.bindValue(":id", basePathId);
servatrice->execSqlQuery(query);
sqlInterface->execSqlQuery(query);
}
Response::ResponseCode ServerSocketInterface::cmdDeckDelDir(const Command_DeckDelDir &cmd, ResponseContainer & /*rc*/)
@@ -440,11 +435,11 @@ Response::ResponseCode ServerSocketInterface::cmdDeckDelDir(const Command_DeckDe
if (authState != PasswordRight)
return Response::RespFunctionNotAllowed;
servatrice->checkSql();
sqlInterface->checkSql();
int basePathId = getDeckPathId(QString::fromStdString(cmd.path()));
if ((basePathId == -1) || (basePathId == 0))
return RespNameNotFound;
return Response::RespNameNotFound;
deckDelDirHelper(basePathId);
return Response::RespOk;
}
@@ -454,21 +449,19 @@ Response::ResponseCode ServerSocketInterface::cmdDeckDel(const Command_DeckDel &
if (authState != PasswordRight)
return Response::RespFunctionNotAllowed;
servatrice->checkSql();
// QMutexLocker locker(&servatrice->dbMutex);
QSqlQuery query;
sqlInterface->checkSql();
QSqlQuery query(sqlInterface->getDatabase());
query.prepare("select id from " + servatrice->getDbPrefix() + "_decklist_files where id = :id and user = :user");
query.bindValue(":id", cmd.deck_id());
query.bindValue(":user", QString::fromStdString(userInfo->name()));
servatrice->execSqlQuery(query);
sqlInterface->execSqlQuery(query);
if (!query.next())
return Response::RespNameNotFound;
query.prepare("delete from " + servatrice->getDbPrefix() + "_decklist_files where id = :id");
query.bindValue(":id", cmd.deck_id());
servatrice->execSqlQuery(query);
sqlInterface->execSqlQuery(query);
return Response::RespOk;
}
@@ -481,7 +474,7 @@ Response::ResponseCode ServerSocketInterface::cmdDeckUpload(const Command_DeckUp
if (!cmd.has_deck_list())
return Response::RespInvalidData;
servatrice->checkSql();
sqlInterface->checkSql();
QString deckStr = QString::fromStdString(cmd.deck_list());
DeckList deck(deckStr);
@@ -495,14 +488,13 @@ Response::ResponseCode ServerSocketInterface::cmdDeckUpload(const Command_DeckUp
if (folderId == -1)
return Response::RespNameNotFound;
// QMutexLocker locker(&servatrice->dbMutex);
QSqlQuery query;
QSqlQuery query(sqlInterface->getDatabase());
query.prepare("insert into " + servatrice->getDbPrefix() + "_decklist_files (id_folder, user, name, upload_time, content) values(:id_folder, :user, :name, NOW(), :content)");
query.bindValue(":id_folder", folderId);
query.bindValue(":user", QString::fromStdString(userInfo->name()));
query.bindValue(":name", deckName);
query.bindValue(":content", deckStr);
servatrice->execSqlQuery(query);
sqlInterface->execSqlQuery(query);
Response_DeckUpload *re = new Response_DeckUpload;
ServerInfo_DeckStorage_TreeItem *fileInfo = re->mutable_new_file();
@@ -511,14 +503,13 @@ Response::ResponseCode ServerSocketInterface::cmdDeckUpload(const Command_DeckUp
fileInfo->mutable_file()->set_creation_time(QDateTime::currentDateTime().toTime_t());
rc.setResponseExtension(re);
} else if (cmd.has_deck_id()) {
// QMutexLocker locker(&servatrice->dbMutex);
QSqlQuery query;
QSqlQuery query(sqlInterface->getDatabase());
query.prepare("update " + servatrice->getDbPrefix() + "_decklist_files set name=:name, upload_time=NOW(), content=:content where id = :id_deck and user = :user");
query.bindValue(":id_deck", cmd.deck_id());
query.bindValue(":user", QString::fromStdString(userInfo->name()));
query.bindValue(":name", deckName);
query.bindValue(":content", deckStr);
servatrice->execSqlQuery(query);
sqlInterface->execSqlQuery(query);
if (query.numRowsAffected() == 0)
return Response::RespNameNotFound;
@@ -542,7 +533,7 @@ Response::ResponseCode ServerSocketInterface::cmdDeckDownload(const Command_Deck
DeckList *deck;
try {
deck = servatrice->getDeckFromDatabase(cmd.deck_id(), QString::fromStdString(userInfo->name()));
deck = sqlInterface->getDeckFromDatabase(cmd.deck_id(), QString::fromStdString(userInfo->name()));
} catch(Response::ResponseCode r) {
return r;
}
@@ -562,11 +553,10 @@ Response::ResponseCode ServerSocketInterface::cmdReplayList(const Command_Replay
Response_ReplayList *re = new Response_ReplayList;
// servatrice->dbMutex.lock();
QSqlQuery query1;
QSqlQuery query1(sqlInterface->getDatabase());
query1.prepare("select a.id_game, a.replay_name, b.room_name, b.time_started, b.time_finished, b.descr, a.do_not_hide from cockatrice_replays_access a left join cockatrice_games b on b.id = a.id_game where a.id_player = :id_player and (a.do_not_hide = 1 or date_add(b.time_started, interval 7 day) > now())");
query1.bindValue(":id_player", userInfo->id());
servatrice->execSqlQuery(query1);
sqlInterface->execSqlQuery(query1);
while (query1.next()) {
ServerInfo_ReplayMatch *matchInfo = re->add_match_list();
@@ -581,25 +571,27 @@ Response::ResponseCode ServerSocketInterface::cmdReplayList(const Command_Replay
const QString replayName = query1.value(1).toString();
matchInfo->set_do_not_hide(query1.value(6).toBool());
QSqlQuery query2;
query2.prepare("select player_name from cockatrice_games_players where id_game = :id_game");
query2.bindValue(":id_game", gameId);
servatrice->execSqlQuery(query2);
while (query2.next())
matchInfo->add_player_names(query2.value(0).toString().toStdString());
QSqlQuery query3;
query3.prepare("select id, duration from " + servatrice->getDbPrefix() + "_replays where id_game = :id_game");
query3.bindValue(":id_game", gameId);
servatrice->execSqlQuery(query3);
while (query3.next()) {
ServerInfo_Replay *replayInfo = matchInfo->add_replay_list();
replayInfo->set_replay_id(query3.value(0).toInt());
replayInfo->set_replay_name(replayName.toStdString());
replayInfo->set_duration(query3.value(1).toInt());
{
QSqlQuery query2(sqlInterface->getDatabase());
query2.prepare("select player_name from cockatrice_games_players where id_game = :id_game");
query2.bindValue(":id_game", gameId);
sqlInterface->execSqlQuery(query2);
while (query2.next())
matchInfo->add_player_names(query2.value(0).toString().toStdString());
}
{
QSqlQuery query3(sqlInterface->getDatabase());
query3.prepare("select id, duration from " + servatrice->getDbPrefix() + "_replays where id_game = :id_game");
query3.bindValue(":id_game", gameId);
sqlInterface->execSqlQuery(query3);
while (query3.next()) {
ServerInfo_Replay *replayInfo = matchInfo->add_replay_list();
replayInfo->set_replay_id(query3.value(0).toInt());
replayInfo->set_replay_name(replayName.toStdString());
replayInfo->set_duration(query3.value(1).toInt());
}
}
}
// servatrice->dbMutex.unlock();
rc.setResponseExtension(re);
return Response::RespOk;
@@ -610,26 +602,26 @@ Response::ResponseCode ServerSocketInterface::cmdReplayDownload(const Command_Re
if (authState != PasswordRight)
return Response::RespFunctionNotAllowed;
// QMutexLocker dbLocker(&servatrice->dbMutex);
{
QSqlQuery query(sqlInterface->getDatabase());
query.prepare("select 1 from " + servatrice->getDbPrefix() + "_replays_access a left join " + servatrice->getDbPrefix() + "_replays b on a.id_game = b.id_game where b.id = :id_replay and a.id_player = :id_player");
query.bindValue(":id_replay", cmd.replay_id());
query.bindValue(":id_player", userInfo->id());
if (!sqlInterface->execSqlQuery(query))
return Response::RespInternalError;
if (!query.next())
return Response::RespAccessDenied;
}
QSqlQuery query1;
query1.prepare("select 1 from " + servatrice->getDbPrefix() + "_replays_access a left join " + servatrice->getDbPrefix() + "_replays b on a.id_game = b.id_game where b.id = :id_replay and a.id_player = :id_player");
query1.bindValue(":id_replay", cmd.replay_id());
query1.bindValue(":id_player", userInfo->id());
if (!servatrice->execSqlQuery(query1))
QSqlQuery query(sqlInterface->getDatabase());
query.prepare("select replay from " + servatrice->getDbPrefix() + "_replays where id = :id_replay");
query.bindValue(":id_replay", cmd.replay_id());
if (!sqlInterface->execSqlQuery(query))
return Response::RespInternalError;
if (!query1.next())
return Response::RespAccessDenied;
QSqlQuery query2;
query2.prepare("select replay from " + servatrice->getDbPrefix() + "_replays where id = :id_replay");
query2.bindValue(":id_replay", cmd.replay_id());
if (!servatrice->execSqlQuery(query2))
return Response::RespInternalError;
if (!query2.next())
if (!query.next())
return Response::RespNameNotFound;
QByteArray data = query2.value(0).toByteArray();
QByteArray data = query.value(0).toByteArray();
Response_ReplayDownload *re = new Response_ReplayDownload;
re->set_replay_data(data.data(), data.size());
@@ -643,15 +635,18 @@ Response::ResponseCode ServerSocketInterface::cmdReplayModifyMatch(const Command
if (authState != PasswordRight)
return Response::RespFunctionNotAllowed;
// QMutexLocker dbLocker(&servatrice->dbMutex);
if (!sqlInterface->checkSql())
return Response::RespInternalError;
QSqlQuery query1;
query1.prepare("update " + servatrice->getDbPrefix() + "_replays_access set do_not_hide=:do_not_hide where id_player = :id_player and id_game = :id_game");
query1.bindValue(":id_player", userInfo->id());
query1.bindValue(":id_game", cmd.game_id());
query1.bindValue(":do_not_hide", cmd.do_not_hide());
QSqlQuery query(sqlInterface->getDatabase());
query.prepare("update " + servatrice->getDbPrefix() + "_replays_access set do_not_hide=:do_not_hide where id_player = :id_player and id_game = :id_game");
query.bindValue(":id_player", userInfo->id());
query.bindValue(":id_game", cmd.game_id());
query.bindValue(":do_not_hide", cmd.do_not_hide());
return servatrice->execSqlQuery(query1) ? Response::RespOk : Response::RespNameNotFound;
if (!sqlInterface->execSqlQuery(query))
return Response::RespInternalError;
return query.numRowsAffected() > 0 ? Response::RespOk : Response::RespNameNotFound;
}
Response::ResponseCode ServerSocketInterface::cmdReplayDeleteMatch(const Command_ReplayDeleteMatch &cmd, ResponseContainer & /*rc*/)
@@ -659,15 +654,17 @@ Response::ResponseCode ServerSocketInterface::cmdReplayDeleteMatch(const Command
if (authState != PasswordRight)
return Response::RespFunctionNotAllowed;
// QMutexLocker dbLocker(&servatrice->dbMutex);
if (!sqlInterface->checkSql())
return Response::RespInternalError;
QSqlQuery query1;
query1.prepare("delete from " + servatrice->getDbPrefix() + "_replays_access where id_player = :id_player and id_game = :id_game");
query1.bindValue(":id_player", userInfo->id());
query1.bindValue(":id_game", cmd.game_id());
QSqlQuery query(sqlInterface->getDatabase());
query.prepare("delete from " + servatrice->getDbPrefix() + "_replays_access where id_player = :id_player and id_game = :id_game");
query.bindValue(":id_player", userInfo->id());
query.bindValue(":id_game", cmd.game_id());
servatrice->execSqlQuery(query1);
return query1.numRowsAffected() > 0 ? Response::RespOk: Response::RespNameNotFound;
if (!sqlInterface->execSqlQuery(query))
return Response::RespInternalError;
return query.numRowsAffected() > 0 ? Response::RespOk : Response::RespNameNotFound;
}
@@ -676,12 +673,14 @@ Response::ResponseCode ServerSocketInterface::cmdReplayDeleteMatch(const Command
Response::ResponseCode ServerSocketInterface::cmdBanFromServer(const Command_BanFromServer &cmd, ResponseContainer & /*rc*/)
{
if (!sqlInterface->checkSql())
return Response::RespInternalError;
QString userName = QString::fromStdString(cmd.user_name());
QString address = QString::fromStdString(cmd.address());
int minutes = cmd.minutes();
// servatrice->dbMutex.lock();
QSqlQuery query;
QSqlQuery query(sqlInterface->getDatabase());
query.prepare("insert into " + servatrice->getDbPrefix() + "_bans (user_name, ip_address, id_admin, time_from, minutes, reason, visible_reason) values(:user_name, :ip_address, :id_admin, NOW(), :minutes, :reason, :visible_reason)");
query.bindValue(":user_name", userName);
query.bindValue(":ip_address", address);
@@ -689,8 +688,7 @@ Response::ResponseCode ServerSocketInterface::cmdBanFromServer(const Command_Ban
query.bindValue(":minutes", minutes);
query.bindValue(":reason", QString::fromStdString(cmd.reason()));
query.bindValue(":visible_reason", QString::fromStdString(cmd.visible_reason()));
servatrice->execSqlQuery(query);
// servatrice->dbMutex.unlock();
sqlInterface->execSqlQuery(query);
QList<ServerSocketInterface *> userList = servatrice->getUsersWithAddressAsList(QHostAddress(address));
ServerSocketInterface *user = static_cast<ServerSocketInterface *>(server->getUsers().value(userName));