19 m_DB(
"Ranks.sqlite", SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE),
20 m_IsInitialized(false),
46 m_DB.exec(
"CREATE TABLE IF NOT EXISTS Rank (RankID INTEGER PRIMARY KEY, Name, MsgPrefix, MsgSuffix, MsgNameColorCode)");
47 m_DB.exec(
"CREATE TABLE IF NOT EXISTS PlayerRank (PlayerUUID, PlayerName, RankID INTEGER)");
48 m_DB.exec(
"CREATE TABLE IF NOT EXISTS PermGroup (PermGroupID INTEGER PRIMARY KEY, Name)");
49 m_DB.exec(
"CREATE TABLE IF NOT EXISTS RankPermGroup (RankID INTEGER, PermGroupID INTEGER)");
50 m_DB.exec(
"CREATE TABLE IF NOT EXISTS PermissionItem (PermGroupID INTEGER, Permission)");
51 m_DB.exec(
"CREATE TABLE IF NOT EXISTS RestrictionItem (PermGroupID INTEGER, Permission)");
52 m_DB.exec(
"CREATE TABLE IF NOT EXISTS DefaultRank (RankID INTEGER)");
61 LOGINFO(
"Creating default ranks...");
63 LOGINFO(
"Default ranks created.");
69 SQLite::Statement stmt(
m_DB,
70 "SELECT Rank.Name FROM Rank "
71 "LEFT JOIN DefaultRank ON Rank.RankID = DefaultRank.RankID"
73 if (stmt.executeStep())
78 catch (
const SQLite::Exception & ex)
80 LOGWARNING(
"%s: Cannot load default rank: %s", __FUNCTION__, ex.what());
102 SQLite::Statement stmt(
m_DB,
"SELECT Rank.Name FROM Rank LEFT JOIN PlayerRank ON Rank.RankID = PlayerRank.RankID WHERE PlayerRank.PlayerUUID = ?");
105 if (!stmt.executeStep())
110 return stmt.getColumn(0).getText();
112 catch (
const SQLite::Exception & ex)
114 LOGWARNING(
"%s: Cannot get player rank name: %s", __FUNCTION__, ex.what());
131 SQLite::Statement stmt(
m_DB,
"SELECT PlayerName FROM PlayerRank WHERE PlayerUUID = ?");
134 if (stmt.executeStep())
136 return stmt.getColumn(0).getText();
139 catch (SQLite::Exception & ex)
141 LOGWARNING(
"%s: Cannot get player name: %s", __FUNCTION__, ex.what());
159 SQLite::Statement stmt(
m_DB,
160 "SELECT PermGroup.Name FROM PermGroup "
161 "LEFT JOIN RankPermGroup ON PermGroup.PermGroupID = RankPermGroup.PermGroupID "
162 "LEFT JOIN PlayerRank ON PlayerRank.RankID = RankPermGroup.RankID "
163 "WHERE PlayerRank.PlayerUUID = ?"
168 while (stmt.executeStep())
170 res.push_back(stmt.getColumn(0).getText());
173 catch (
const SQLite::Exception & ex)
175 LOGWARNING(
"%s: Cannot get player groups: %s", __FUNCTION__, ex.what());
220 SQLite::Statement stmt(
m_DB,
221 "SELECT PermGroup.Name FROM PermGroup "
222 "LEFT JOIN RankPermGroup ON RankPermGroup.PermGroupID = PermGroup.PermGroupID "
223 "LEFT JOIN Rank ON Rank.RankID = RankPermGroup.RankID "
224 "WHERE Rank.Name = ?"
226 stmt.bind(1, a_RankName);
227 while (stmt.executeStep())
229 res.push_back(stmt.getColumn(0).getText());
232 catch (
const SQLite::Exception & ex)
234 LOGWARNING(
"%s: Failed to get rank groups from DB: %s", __FUNCTION__, ex.what());
251 SQLite::Statement stmt(
m_DB,
252 "SELECT PermissionItem.Permission FROM PermissionItem "
253 "LEFT JOIN PermGroup ON PermGroup.PermGroupID = PermissionItem.PermGroupID "
254 "WHERE PermGroup.Name = ?"
256 stmt.bind(1, a_GroupName);
257 while (stmt.executeStep())
259 res.push_back(stmt.getColumn(0).getText());
262 catch (
const SQLite::Exception & ex)
264 LOGWARNING(
"%s: Failed to get group permissions from DB: %s", __FUNCTION__, ex.what());
281 SQLite::Statement stmt(
m_DB,
282 "SELECT RestrictionItem.Permission FROM RestrictionItem "
283 "LEFT JOIN PermGroup ON PermGroup.PermGroupID = RestrictionItem.PermGroupID "
284 "WHERE PermGroup.Name = ?"
286 stmt.bind(1, a_GroupName);
287 while (stmt.executeStep())
289 res.push_back(stmt.getColumn(0).getText());
292 catch (
const SQLite::Exception & ex)
294 LOGWARNING(
"%s: Failed to get group restrictions from DB: %s", __FUNCTION__, ex.what());
311 SQLite::Statement stmt(
m_DB,
312 "SELECT PermissionItem.Permission FROM PermissionItem "
313 "LEFT JOIN RankPermGroup ON RankPermGroup.PermGroupID = PermissionItem.PermGroupID "
314 "LEFT JOIN Rank ON Rank.RankID = RankPermGroup.RankID "
315 "WHERE Rank.Name = ?"
317 stmt.bind(1, a_RankName);
318 while (stmt.executeStep())
320 res.push_back(stmt.getColumn(0).getText());
323 catch (
const SQLite::Exception & ex)
325 LOGWARNING(
"%s: Failed to get rank permissions from DB: %s", __FUNCTION__, ex.what());
342 SQLite::Statement stmt(
m_DB,
343 "SELECT RestrictionItem.Permission FROM RestrictionItem "
344 "LEFT JOIN RankPermGroup ON RankPermGroup.PermGroupID = RestrictionItem.PermGroupID "
345 "LEFT JOIN Rank ON Rank.RankID = RankPermGroup.RankID "
346 "WHERE Rank.Name = ?"
348 stmt.bind(1, a_RankName);
349 while (stmt.executeStep())
351 res.push_back(stmt.getColumn(0).getText());
354 catch (
const SQLite::Exception & ex)
356 LOGWARNING(
"%s: Failed to get rank restrictions from DB: %s", __FUNCTION__, ex.what());
371 std::vector<cUUID> res;
374 SQLite::Statement stmt(
m_DB,
"SELECT PlayerUUID FROM PlayerRank ORDER BY PlayerName COLLATE NOCASE");
375 while (stmt.executeStep())
377 if (!tempUUID.
FromString(stmt.getColumn(0).getText()))
382 res.push_back(tempUUID);
385 catch (
const SQLite::Exception & ex)
387 LOGWARNING(
"%s: Failed to get players from DB: %s", __FUNCTION__, ex.what());
404 SQLite::Statement stmt(
m_DB,
"SELECT Name FROM Rank");
405 while (stmt.executeStep())
407 res.push_back(stmt.getColumn(0).getText());
410 catch (
const SQLite::Exception & ex)
412 LOGWARNING(
"%s: Failed to get ranks from DB: %s", __FUNCTION__, ex.what());
429 SQLite::Statement stmt(
m_DB,
"SELECT Name FROM PermGroup");
430 while (stmt.executeStep())
432 res.push_back(stmt.getColumn(0).getText());
435 catch (
const SQLite::Exception & ex)
437 LOGWARNING(
"%s: Failed to get groups from DB: %s", __FUNCTION__, ex.what());
454 SQLite::Statement stmt(
m_DB,
"SELECT DISTINCT(Permission) FROM PermissionItem");
455 while (stmt.executeStep())
457 res.push_back(stmt.getColumn(0).getText());
460 catch (
const SQLite::Exception & ex)
462 LOGWARNING(
"%s: Failed to get permissions from DB: %s", __FUNCTION__, ex.what());
479 SQLite::Statement stmt(
m_DB,
"SELECT DISTINCT(Permission) FROM RestrictionItem");
480 while (stmt.executeStep())
482 res.push_back(stmt.getColumn(0).getText());
485 catch (
const SQLite::Exception & ex)
487 LOGWARNING(
"%s: Failed to get restrictions from DB: %s", __FUNCTION__, ex.what());
500 for (
auto & restriction: Restrictions)
502 Permissions.push_back(restriction);
512 const cUUID & a_PlayerUUID,
524 a_MsgNameColorCode.clear();
527 return GetRankVisuals(Rank, a_MsgPrefix, a_MsgSuffix, a_MsgNameColorCode);
538 const AString & a_MsgNameColorCode
548 SQLite::Statement stmt(
m_DB,
"SELECT COUNT(*) FROM Rank WHERE Name = ?");
549 stmt.bind(1, a_RankName);
550 if (stmt.executeStep())
552 if (stmt.getColumn(0).getInt() > 0)
561 SQLite::Statement stmt(
m_DB,
"INSERT INTO Rank (Name, MsgPrefix, MsgSuffix, MsgNameColorCode) VALUES (?, ?, ?, ?)");
562 stmt.bind(1, a_RankName);
563 stmt.bind(2, a_MsgPrefix);
564 stmt.bind(3, a_MsgSuffix);
565 stmt.bind(4, a_MsgNameColorCode);
566 if (stmt.exec() <= 0)
568 LOGWARNING(
"%s: Failed to add a new rank \"%s\".", __FUNCTION__, a_RankName.c_str());
572 catch (
const SQLite::Exception & ex)
574 LOGWARNING(
"%s: Failed to add a new rank \"%s\": %s", __FUNCTION__, a_RankName.c_str(), ex.what());
591 SQLite::Statement stmt(
m_DB,
"SELECT COUNT(*) FROM PermGroup WHERE Name = ?");
592 stmt.bind(1, a_GroupName);
593 if (stmt.executeStep())
595 if (stmt.getColumn(0).getInt() > 0)
604 SQLite::Statement stmt(
m_DB,
"INSERT INTO PermGroup (Name) VALUES (?)");
605 stmt.bind(1, a_GroupName);
606 if (stmt.exec() <= 0)
608 LOGWARNING(
"%s: Failed to add a new group \"%s\".", __FUNCTION__, a_GroupName.c_str());
612 catch (
const SQLite::Exception & ex)
614 LOGWARNING(
"%s: Failed to add a new group \"%s\": %s", __FUNCTION__, a_GroupName.c_str(), ex.what());
629 for (AStringVector::const_iterator itr = a_GroupNames.begin(), end = a_GroupNames.end(); itr != end; ++itr)
633 SQLite::Statement stmt(
m_DB,
"SELECT COUNT(*) FROM PermGroup WHERE Name = ?");
635 if (stmt.executeStep())
637 if (stmt.getColumn(0).getInt() > 0)
646 SQLite::Statement stmt(
m_DB,
"INSERT INTO PermGroup (Name) VALUES (?)");
648 if (stmt.exec() <= 0)
650 LOGWARNING(
"%s: Failed to add a new group \"%s\".", __FUNCTION__, itr->c_str());
655 catch (
const SQLite::Exception & ex)
657 LOGWARNING(
"%s: Failed to add new groups: %s", __FUNCTION__, ex.what());
675 SQLite::Statement stmt(
m_DB,
"SELECT PermGroupID FROM PermGroup WHERE Name = ?");
676 stmt.bind(1, a_GroupName);
677 if (!stmt.executeStep())
679 LOGWARNING(
"%s: No such group (%s), aborting.", __FUNCTION__, a_GroupName.c_str());
682 GroupID = stmt.getColumn(0);
688 SQLite::Statement stmt(
m_DB,
"SELECT RankID FROM Rank WHERE Name = ?");
689 stmt.bind(1, a_RankName);
690 if (!stmt.executeStep())
692 LOGWARNING(
"%s: No such rank (%s), aborting.", __FUNCTION__, a_RankName.c_str());
695 RankID = stmt.getColumn(0);
700 SQLite::Statement stmt(
m_DB,
"SELECT COUNT(*) FROM RankPermGroup WHERE RankID = ? AND PermGroupID = ?");
701 stmt.bind(1, RankID);
702 stmt.bind(2, GroupID);
703 if (!stmt.executeStep())
705 LOGWARNING(
"%s: Failed to check binding between rank %s and group %s, aborting.", __FUNCTION__, a_RankName.c_str(), a_GroupName.c_str());
708 if (stmt.getColumn(0).getInt() > 0)
710 LOGD(
"%s: Group %s already present in rank %s, skipping and returning success.",
711 __FUNCTION__, a_GroupName.c_str(), a_RankName.c_str()
719 SQLite::Statement stmt(
m_DB,
"INSERT INTO RankPermGroup (RankID, PermGroupID) VALUES (?, ?)");
720 stmt.bind(1, RankID);
721 stmt.bind(2, GroupID);
722 if (stmt.exec() <= 0)
724 LOGWARNING(
"%s: Failed to add group %s to rank %s, aborting.", __FUNCTION__, a_GroupName.c_str(), a_RankName.c_str());
732 catch (
const SQLite::Exception & ex)
734 LOGWARNING(
"%s: Failed to add group %s to rank %s: %s", __FUNCTION__, a_GroupName.c_str(), a_RankName.c_str(), ex.what());
753 SQLite::Statement stmt(
m_DB,
"SELECT PermGroupID FROM PermGroup WHERE Name = ?");
754 stmt.bind(1, a_GroupName);
755 if (!stmt.executeStep())
757 LOGWARNING(
"%s: No such group (%s), aborting.", __FUNCTION__, a_GroupName.c_str());
760 GroupID = stmt.getColumn(0).getInt();
765 SQLite::Statement stmt(
m_DB,
"SELECT COUNT(*) FROM PermissionItem WHERE PermGroupID = ? AND Permission = ?");
766 stmt.bind(1, GroupID);
767 stmt.bind(2, a_Permission);
768 if (!stmt.executeStep())
770 LOGWARNING(
"%s: Failed to check binding between permission %s and group %s, aborting.", __FUNCTION__, a_Permission.c_str(), a_GroupName.c_str());
773 if (stmt.getColumn(0).getInt() > 0)
775 LOGD(
"%s: Permission %s is already present in group %s, skipping and returning success.",
776 __FUNCTION__, a_Permission.c_str(), a_GroupName.c_str()
784 SQLite::Statement stmt(
m_DB,
"INSERT INTO PermissionItem (Permission, PermGroupID) VALUES (?, ?)");
785 stmt.bind(1, a_Permission);
786 stmt.bind(2, GroupID);
787 if (stmt.exec() <= 0)
789 LOGWARNING(
"%s: Failed to add permission %s to group %s, aborting.", __FUNCTION__, a_Permission.c_str(), a_GroupName.c_str());
797 catch (
const SQLite::Exception & ex)
799 LOGWARNING(
"%s: Failed to add permission %s to group %s: %s",
800 __FUNCTION__, a_Permission.c_str(), a_GroupName.c_str(), ex.what()
820 SQLite::Statement stmt(
m_DB,
"SELECT PermGroupID FROM PermGroup WHERE Name = ?");
821 stmt.bind(1, a_GroupName);
822 if (!stmt.executeStep())
824 LOGWARNING(
"%s: No such group (%s), aborting.", __FUNCTION__, a_GroupName.c_str());
827 GroupID = stmt.getColumn(0).getInt();
832 SQLite::Statement stmt(
m_DB,
"SELECT COUNT(*) FROM RestrictionItem WHERE PermGroupID = ? AND Permission = ?");
833 stmt.bind(1, GroupID);
834 stmt.bind(2, a_Restriction);
835 if (!stmt.executeStep())
837 LOGWARNING(
"%s: Failed to check binding between restriction %s and group %s, aborting.", __FUNCTION__, a_Restriction.c_str(), a_GroupName.c_str());
840 if (stmt.getColumn(0).getInt() > 0)
842 LOGD(
"%s: Restriction %s is already present in group %s, skipping and returning success.",
843 __FUNCTION__, a_Restriction.c_str(), a_GroupName.c_str()
851 SQLite::Statement stmt(
m_DB,
"INSERT INTO RestrictionItem (Permission, PermGroupID) VALUES (?, ?)");
852 stmt.bind(1, a_Restriction);
853 stmt.bind(2, GroupID);
854 if (stmt.exec() <= 0)
856 LOGWARNING(
"%s: Failed to add restriction %s to group %s, aborting.", __FUNCTION__, a_Restriction.c_str(), a_GroupName.c_str());
864 catch (
const SQLite::Exception & ex)
866 LOGWARNING(
"%s: Failed to add restriction %s to group %s: %s",
867 __FUNCTION__, a_Restriction.c_str(), a_GroupName.c_str(), ex.what()
887 SQLite::Statement stmt(
m_DB,
"SELECT PermGroupID FROM PermGroup WHERE Name = ?");
888 stmt.bind(1, a_GroupName);
889 if (!stmt.executeStep())
891 LOGWARNING(
"%s: No such group (%s), aborting.", __FUNCTION__, a_GroupName.c_str());
894 GroupID = stmt.getColumn(0).getInt();
897 for (AStringVector::const_iterator itr = a_Permissions.begin(), end = a_Permissions.end(); itr != end; ++itr)
901 SQLite::Statement stmt(
m_DB,
"SELECT COUNT(*) FROM PermissionItem WHERE PermGroupID = ? AND Permission = ?");
902 stmt.bind(1, GroupID);
904 if (!stmt.executeStep())
906 LOGWARNING(
"%s: Failed to check binding between permission %s and group %s, aborting.", __FUNCTION__, itr->c_str(), a_GroupName.c_str());
909 if (stmt.getColumn(0).getInt() > 0)
911 LOGD(
"%s: Permission %s is already present in group %s, skipping and returning success.",
912 __FUNCTION__, itr->c_str(), a_GroupName.c_str()
920 SQLite::Statement stmt(
m_DB,
"INSERT INTO PermissionItem (Permission, PermGroupID) VALUES (?, ?)");
922 stmt.bind(2, GroupID);
923 if (stmt.exec() <= 0)
925 LOGWARNING(
"%s: Failed to add permission %s to group %s, skipping.", __FUNCTION__, itr->c_str(), a_GroupName.c_str());
934 catch (
const SQLite::Exception & ex)
936 LOGWARNING(
"%s: Failed to add permissions to group %s: %s",
937 __FUNCTION__, a_GroupName.c_str(), ex.what()
957 SQLite::Statement stmt(
m_DB,
"SELECT PermGroupID FROM PermGroup WHERE Name = ?");
958 stmt.bind(1, a_GroupName);
959 if (!stmt.executeStep())
961 LOGWARNING(
"%s: No such group (%s), aborting.", __FUNCTION__, a_GroupName.c_str());
964 GroupID = stmt.getColumn(0).getInt();
967 for (
auto itr = a_Restrictions.cbegin(), end = a_Restrictions.cend(); itr != end; ++itr)
971 SQLite::Statement stmt(
m_DB,
"SELECT COUNT(*) FROM RestrictionItem WHERE PermGroupID = ? AND Permission = ?");
972 stmt.bind(1, GroupID);
974 if (!stmt.executeStep())
976 LOGWARNING(
"%s: Failed to check binding between restriction %s and group %s, aborting.", __FUNCTION__, itr->c_str(), a_GroupName.c_str());
979 if (stmt.getColumn(0).getInt() > 0)
981 LOGD(
"%s: Restriction %s is already present in group %s, skipping and returning success.",
982 __FUNCTION__, itr->c_str(), a_GroupName.c_str()
990 SQLite::Statement stmt(
m_DB,
"INSERT INTO RestrictionItem (Permission, PermGroupID) VALUES (?, ?)");
992 stmt.bind(2, GroupID);
993 if (stmt.exec() <= 0)
995 LOGWARNING(
"%s: Failed to add restriction %s to group %s, skipping.", __FUNCTION__, itr->c_str(), a_GroupName.c_str());
1004 catch (
const SQLite::Exception & ex)
1006 LOGWARNING(
"%s: Failed to add restrictions to group %s: %s",
1007 __FUNCTION__, a_GroupName.c_str(), ex.what()
1025 LOGWARNING(
"%s: Cannot remove rank %s, it is the default rank and the replacement rank doesn't exist.", __FUNCTION__, a_RankName.c_str());
1035 SQLite::Statement stmt(
m_DB,
"SELECT RankID FROM Rank WHERE Name = ?");
1036 stmt.bind(1, a_RankName);
1037 if (!stmt.executeStep())
1039 LOGINFO(
"%s: Rank %s was not found. Skipping.", __FUNCTION__, a_RankName.c_str());
1042 RemoveRankID = stmt.getColumn(0).getInt();
1046 int ReplacementRankID = -1;
1048 SQLite::Statement stmt(
m_DB,
"SELECT RankID FROM Rank WHERE Name = ?");
1049 stmt.bind(1, a_ReplacementRankName);
1050 if (stmt.executeStep())
1052 ReplacementRankID = stmt.getColumn(0).getInt();
1058 SQLite::Statement stmt(
m_DB,
"DELETE FROM RankPermGroup WHERE RankID = ?");
1059 stmt.bind(1, RemoveRankID);
1064 if (ReplacementRankID == -1)
1067 SQLite::Statement stmt(
m_DB,
"DELETE FROM PlayerRank WHERE RankID = ?");
1068 stmt.bind(1, RemoveRankID);
1074 SQLite::Statement stmt(
m_DB,
"UPDATE PlayerRank SET RankID = ? WHERE RankID = ?");
1075 stmt.bind(1, ReplacementRankID);
1076 stmt.bind(2, RemoveRankID);
1082 SQLite::Statement stmt(
m_DB,
"DELETE FROM Rank WHERE RankID = ?");
1083 stmt.bind(1, RemoveRankID);
1093 catch (
const SQLite::Exception & ex)
1095 LOGWARNING(
"%s: Failed to remove rank from DB: %s", __FUNCTION__, ex.what());
1113 SQLite::Statement stmt(
m_DB,
"SELECT PermGroupID FROM PermGroup WHERE Name = ?");
1114 stmt.bind(1, a_GroupName);
1115 if (!stmt.executeStep())
1117 LOGINFO(
"%s: Group %s was not found, skipping.", __FUNCTION__, a_GroupName.c_str());
1120 GroupID = stmt.getColumn(0).getInt();
1125 SQLite::Statement stmt(
m_DB,
"DELETE FROM PermissionItem WHERE PermGroupID = ?");
1126 stmt.bind(1, GroupID);
1132 SQLite::Statement stmt(
m_DB,
"DELETE FROM RankPermGroup WHERE PermGroupID = ?");
1133 stmt.bind(1, GroupID);
1139 SQLite::Statement stmt(
m_DB,
"DELETE FROM PermGroup WHERE PermGroupID = ?");
1140 stmt.bind(1, GroupID);
1144 catch (
const SQLite::Exception & ex)
1146 LOGWARNING(
"%s: Failed to remove group %s from DB: %s", __FUNCTION__, a_GroupName.c_str(), ex.what());
1162 int GroupID, RankID;
1164 SQLite::Statement stmt(
m_DB,
1165 "SELECT PermGroup.PermGroupID, Rank.RankID FROM PermGroup "
1166 "LEFT JOIN RankPermGroup ON RankPermGroup.PermGroupID = PermGroup.PermGroupID "
1167 "LEFT JOIN Rank ON Rank.RankID = RankPermGroup.RankID "
1168 "WHERE PermGroup.Name = ? AND Rank.Name = ?"
1170 stmt.bind(1, a_GroupName);
1171 stmt.bind(2, a_RankName);
1172 if (!stmt.executeStep())
1174 LOGINFO(
"%s: Group %s was not found in rank %s, skipping.", __FUNCTION__, a_GroupName.c_str(), a_RankName.c_str());
1177 GroupID = stmt.getColumn(0).getInt();
1178 RankID = stmt.getColumn(1).getInt();
1183 SQLite::Statement stmt(
m_DB,
"DELETE FROM RankPermGroup WHERE PermGroupID = ?");
1184 stmt.bind(1, GroupID);
1190 SQLite::Statement stmt(
m_DB,
"DELETE FROM RankPermGroup WHERE PermGroupID = ? AND RankID = ?");
1191 stmt.bind(1, GroupID);
1192 stmt.bind(1, RankID);
1196 catch (
const SQLite::Exception & ex)
1198 LOGWARNING(
"%s: Failed to remove group %s from rank %s in the DB: %s", __FUNCTION__, a_GroupName.c_str(), a_RankName.c_str(), ex.what());
1216 SQLite::Statement stmt(
m_DB,
"SELECT PermGroupID FROM PermGroup WHERE Name = ?");
1217 stmt.bind(1, a_GroupName);
1218 if (!stmt.executeStep())
1220 LOGINFO(
"%s: Group %s was not found, skipping.", __FUNCTION__, a_GroupName.c_str());
1223 GroupID = stmt.getColumn(0).getInt();
1228 SQLite::Statement stmt(
m_DB,
"DELETE FROM PermissionItem WHERE PermGroupID = ? AND Permission = ?");
1229 stmt.bind(1, GroupID);
1230 stmt.bind(2, a_Permission);
1234 catch (
const SQLite::Exception & ex)
1236 LOGWARNING(
"%s: Failed to remove permission %s from group %s in DB: %s",
1237 __FUNCTION__, a_Permission.c_str(), a_GroupName.c_str(), ex.what()
1256 SQLite::Statement stmt(
m_DB,
"SELECT PermGroupID FROM PermGroup WHERE Name = ?");
1257 stmt.bind(1, a_GroupName);
1258 if (!stmt.executeStep())
1260 LOGINFO(
"%s: Group %s was not found, skipping.", __FUNCTION__, a_GroupName.c_str());
1263 GroupID = stmt.getColumn(0).getInt();
1268 SQLite::Statement stmt(
m_DB,
"DELETE FROM RestrictionItem WHERE PermGroupID = ? AND Permission = ?");
1269 stmt.bind(1, GroupID);
1270 stmt.bind(2, a_Restriction);
1274 catch (
const SQLite::Exception & ex)
1276 LOGWARNING(
"%s: Failed to remove restriction %s from group %s in DB: %s",
1277 __FUNCTION__, a_Restriction.c_str(), a_GroupName.c_str(), ex.what()
1295 SQLite::Statement stmt(
m_DB,
"SELECT RankID FROM Rank WHERE Name = ?");
1296 stmt.bind(1, a_NewName);
1297 if (stmt.executeStep())
1299 LOGINFO(
"%s: Rank %s is already present, cannot rename %s", __FUNCTION__, a_NewName.c_str(), a_OldName.c_str());
1306 SQLite::Statement stmt(
m_DB,
"UPDATE Rank SET Name = ? WHERE Name = ?");
1307 stmt.bind(1, a_NewName);
1308 stmt.bind(2, a_OldName);
1309 if (stmt.exec() <= 0)
1311 LOGINFO(
"%s: There is no rank %s, cannot rename to %s.", __FUNCTION__, a_OldName.c_str(), a_NewName.c_str());
1324 catch (
const SQLite::Exception & ex)
1326 LOGWARNING(
"%s: Failed to rename rank %s to %s in DB: %s",
1327 __FUNCTION__, a_OldName.c_str(), a_NewName.c_str(), ex.what());
1345 SQLite::Statement stmt(
m_DB,
"SELECT PermGroupID FROM PermGroup WHERE Name = ?");
1346 stmt.bind(1, a_NewName);
1347 if (stmt.executeStep())
1349 LOGD(
"%s: Group %s is already present, cannot rename %s", __FUNCTION__, a_NewName.c_str(), a_OldName.c_str());
1357 SQLite::Statement stmt(
m_DB,
"UPDATE PermGroup SET Name = ? WHERE Name = ?");
1358 stmt.bind(1, a_NewName);
1359 stmt.bind(2, a_OldName);
1360 res = (stmt.exec() > 0);
1365 catch (
const SQLite::Exception & ex)
1367 LOGWARNING(
"%s: Failed to rename group %s to %s in DB: %s",
1368 __FUNCTION__, a_OldName.c_str(), a_NewName.c_str(), ex.what());
1389 SQLite::Statement stmt(
m_DB,
"SELECT RankID FROM Rank WHERE Name = ?");
1390 stmt.bind(1, a_RankName);
1391 if (!stmt.executeStep())
1393 LOGWARNING(
"%s: There is no rank %s, aborting.", __FUNCTION__, a_RankName.c_str());
1396 RankID = stmt.getColumn(0).getInt();
1401 SQLite::Statement stmt(
m_DB,
"UPDATE PlayerRank SET RankID = ?, PlayerName = ? WHERE PlayerUUID = ?");
1402 stmt.bind(1, RankID);
1403 stmt.bind(2, a_PlayerName);
1404 stmt.bind(3, StrUUID);
1405 if (stmt.exec() > 0)
1413 SQLite::Statement stmt(
m_DB,
"INSERT INTO PlayerRank (RankID, PlayerUUID, PlayerName) VALUES (?, ?, ?)");
1414 stmt.bind(1, RankID);
1415 stmt.bind(2, StrUUID);
1416 stmt.bind(3, a_PlayerName);
1417 if (stmt.exec() > 0)
1423 LOGWARNING(
"%s: Failed to set player UUID %s to rank %s.",
1424 __FUNCTION__, StrUUID.c_str(), a_RankName.c_str()
1427 catch (
const SQLite::Exception & ex)
1429 LOGWARNING(
"%s: Failed to set player UUID %s to rank %s: %s",
1430 __FUNCTION__, StrUUID.c_str(), a_RankName.c_str(), ex.what()
1448 SQLite::Statement stmt(
m_DB,
"DELETE FROM PlayerRank WHERE PlayerUUID = ?");
1449 stmt.bind(1, StrUUID);
1452 catch (
const SQLite::Exception & ex)
1454 LOGWARNING(
"%s: Failed to remove rank from player UUID %s: %s",
1455 __FUNCTION__, StrUUID.c_str(), ex.what()
1468 const AString & a_MsgNameColorCode
1476 SQLite::Statement stmt(
m_DB,
"UPDATE Rank SET MsgPrefix = ?, MsgSuffix = ?, MsgNameColorCode = ? WHERE Name = ?");
1477 stmt.bind(1, a_MsgPrefix);
1478 stmt.bind(2, a_MsgSuffix);
1479 stmt.bind(3, a_MsgNameColorCode);
1480 stmt.bind(4, a_RankName);
1481 if (stmt.exec() < 1)
1483 LOGINFO(
"%s: Rank %s not found, visuals not set.", __FUNCTION__, a_RankName.c_str());
1486 catch (
const SQLite::Exception & ex)
1488 LOGWARNING(
"%s: Failed to get ranks from DB: %s", __FUNCTION__, ex.what());
1508 SQLite::Statement stmt(
m_DB,
"SELECT MsgPrefix, MsgSuffix, MsgNameColorCode FROM Rank WHERE Name = ?");
1509 stmt.bind(1, a_RankName);
1510 if (!stmt.executeStep())
1515 a_MsgPrefix = stmt.getColumn(0).getText();
1516 a_MsgSuffix = stmt.getColumn(1).getText();
1517 a_MsgNameColorCode = stmt.getColumn(2).getText();
1520 catch (
const SQLite::Exception & ex)
1522 LOGWARNING(
"%s: Failed to get ranks from DB: %s", __FUNCTION__, ex.what());
1538 SQLite::Statement stmt(
m_DB,
"SELECT * FROM Rank WHERE Name = ?");
1539 stmt.bind(1, a_RankName);
1540 if (stmt.executeStep())
1546 catch (
const SQLite::Exception & ex)
1548 LOGWARNING(
"%s: Failed to query DB for rank %s: %s", __FUNCTION__, a_RankName.c_str(), ex.what());
1564 SQLite::Statement stmt(
m_DB,
"SELECT * FROM PermGroup WHERE Name = ?");
1565 stmt.bind(1, a_GroupName);
1566 if (stmt.executeStep())
1572 catch (
const SQLite::Exception & ex)
1574 LOGWARNING(
"%s: Failed to query DB for group %s: %s", __FUNCTION__, a_GroupName.c_str(), ex.what());
1592 SQLite::Statement stmt(
m_DB,
"SELECT * FROM PlayerRank WHERE PlayerUUID = ?");
1593 stmt.bind(1, StrUUID);
1594 if (stmt.executeStep())
1600 catch (
const SQLite::Exception & ex)
1602 LOGWARNING(
"%s: Failed to query DB for player UUID %s: %s", __FUNCTION__, StrUUID.c_str(), ex.what());
1618 SQLite::Statement stmt(
m_DB,
1619 "SELECT * FROM Rank "
1620 "LEFT JOIN RankPermGroup ON Rank.RankID = RankPermGroup.RankID "
1621 "LEFT JOIN PermGroup ON PermGroup.PermGroupID = RankPermGroup.PermGroupID "
1622 "WHERE Rank.Name = ? AND PermGroup.Name = ?"
1624 stmt.bind(1, a_RankName);
1625 stmt.bind(2, a_GroupName);
1626 if (stmt.executeStep())
1632 catch (
const SQLite::Exception & ex)
1634 LOGWARNING(
"%s: Failed to query DB: %s", __FUNCTION__, ex.what());
1650 SQLite::Statement stmt(
m_DB,
1651 "SELECT * FROM PermissionItem "
1652 "LEFT JOIN PermGroup ON PermGroup.PermGroupID = PermissionItem.PermGroupID "
1653 "WHERE PermissionItem.Permission = ? AND PermGroup.Name = ?"
1655 stmt.bind(1, a_Permission);
1656 stmt.bind(2, a_GroupName);
1657 if (stmt.executeStep())
1663 catch (
const SQLite::Exception & ex)
1665 LOGWARNING(
"%s: Failed to query DB: %s", __FUNCTION__, ex.what());
1681 SQLite::Statement stmt(
m_DB,
1682 "SELECT * FROM RestrictionItem "
1683 "LEFT JOIN PermGroup ON PermGroup.PermGroupID = RestrictionItem.PermGroupID "
1684 "WHERE RestrictionItem.Permission = ? AND PermGroup.Name = ?"
1686 stmt.bind(1, a_Restriction);
1687 stmt.bind(2, a_GroupName);
1688 if (stmt.executeStep())
1694 catch (
const SQLite::Exception & ex)
1696 LOGWARNING(
"%s: Failed to query DB: %s", __FUNCTION__, ex.what());
1712 SQLite::Statement stmt(
m_DB,
"UPDATE PlayerRank SET PlayerName = ? WHERE PlayerUUID = ?");
1713 stmt.bind(1, a_PlayerName);
1717 catch (
const SQLite::Exception & ex)
1719 LOGWARNING(
"%s: Failed to update DB: %s", __FUNCTION__, ex.what());
1737 SQLite::Statement stmt(
m_DB,
"SELECT RankID FROM Rank WHERE Name = ?");
1738 stmt.bind(1, a_RankName);
1739 if (!stmt.executeStep())
1741 LOGINFO(
"%s: Cannot set rank %s as the default, it does not exist.", __FUNCTION__, a_RankName.c_str());
1748 SQLite::Statement stmt(
m_DB,
"UPDATE DefaultRank SET RankID = ?");
1749 stmt.bind(1, RankID);
1750 if (stmt.exec() < 1)
1753 SQLite::Statement stmt2(
m_DB,
"INSERT INTO DefaultRank (RankID) VALUES (?)");
1754 stmt2.bind(1, RankID);
1755 if (stmt2.exec() < 1)
1757 LOGINFO(
"%s: Cannot update the default rank in the DB to %s.", __FUNCTION__, a_RankName.c_str());
1767 catch (
const SQLite::Exception & ex)
1769 LOGWARNING(
"%s: Failed to update DB: %s", __FUNCTION__, ex.what());
1785 SQLite::Statement stmt(
m_DB,
"DELETE FROM PlayerRank");
1788 catch (SQLite::Exception & ex)
1790 LOGWARNING(
"%s: Failed to remove / clear all players: %s", __FUNCTION__, ex.what());
1807 SQLite::Statement stmt(
m_DB,
"UPDATE PlayerRank SET PlayerName = ? WHERE PlayerUUID = ?");
1808 stmt.bind(1, a_NewPlayerName);
1809 stmt.bind(2, StrUUID);
1810 if (stmt.exec() > 0)
1816 catch (
const SQLite::Exception & ex)
1818 LOGWARNING(
"%s: Failed to update player name from UUID %s: %s", __FUNCTION__, StrUUID.c_str(), ex.what());
1847 SQLite::Statement stmt(
m_DB,
"SELECT COUNT(*) FROM " + a_TableName);
1848 return (stmt.executeStep() && (stmt.getColumn(0).getInt() == 0));
1850 catch (
const SQLite::Exception & ex)
1852 LOGWARNING(
"%s: Failed to query DB: %s", __FUNCTION__, ex.what());
1867 AddRank(
"Default",
"",
"",
"");
1869 AddRank(
"Operator",
"",
"",
"");
1904 SQLite::Statement stmt(
m_DB,
Printf(
"PRAGMA table_info(%s)", a_TableName));
1905 while (stmt.executeStep())
1907 int NumColumns = stmt.getColumnCount();
1908 for (
int i = 0; i < NumColumns; i++)
1910 auto column = stmt.getColumn(i);
1911 if (strcmp(column.getName(),
"name") == 0)
1922 catch (
const SQLite::Exception & ex)
1924 LOGWARNING(
"%s: Failed to query DB: %s", __FUNCTION__, ex.what());
1944 m_DB.exec(
Printf(
"ALTER TABLE %s ADD COLUMN %s %s", a_TableName, a_ColumnName, a_ColumnType));
1946 catch (
const SQLite::Exception & exc)
1948 LOGWARNING(
"%s: Failed to query DB: %s", __FUNCTION__, exc.what());