Skip to content

Commit

Permalink
fix misra and opimize list by calling prepare only once in data class
Browse files Browse the repository at this point in the history
  • Loading branch information
noisecode3 committed Jan 1, 2025
1 parent 583fe67 commit 1922327
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 99 deletions.
238 changes: 145 additions & 93 deletions src/Data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,147 +13,196 @@

#include "Data.hpp"

QVector<ListItemData> Data::getListItems() {
QVector<ListItemData> items;
QSqlQuery index(db);
int rowCount = 0;
index.prepare("SELECT COUNT(*) FROM Level");
qint64 Data::getListRowCount() {
QSqlQuery query(db);
qint64 result = 0;

if (!index.exec()) {
qDebug() << "Error executing query:" << index.lastError().text();
return items;
if (query.prepare("SELECT COUNT(*) FROM Level")) {
if (query.exec()) {
// Move to the first (and only) result row
if (query.next()) {
// Assign the count value to result
result = query.value(0).toInt();
qWarning() << "Number of rows in 'Level' table:" << result;
} else {
qDebug() << "No rows returned from the query";
}
} else {
qDebug() << "Error executing query:" << query.lastError().text();
}
} else {
qDebug() << "Error preparing query:" << query.lastError().text();
}
return result;
}

// Move to the first (and only) result row
if (index.next()) {
// Retrieve the count value (assuming it's in the first column, index 0)
rowCount = index.value(0).toInt();

// Now, 'rowCount' contains the count of rows in the 'Level' table
qDebug() << "Number of rows in 'Level' table:" << rowCount;
} else {
// Handle the case where no rows are returned
qDebug() << "No rows returned from the query";
QVector<ListItemData> Data::getListItems() {
QSqlQuery query(db);
bool status = true;
QVector<ListItemData> items;
qint64 rowCount = getListRowCount();

if (!query.prepare(
"SELECT Info.title, Author.value, Info.type, "
"Info.class, Info.release, Info.difficulty, "
"Info.duration, Picture.data FROM Level "
"JOIN Info ON Level.infoID = Info.InfoID "
"JOIN Screens ON Level.LevelID = Screens.levelID "
"JOIN Picture ON Screens.pictureID = Picture.PictureID "
"JOIN AuthorList ON Level.LevelID = AuthorList.levelID "
"JOIN Author ON AuthorList.authorID = Author.AuthorID "
"WHERE Level.LevelID = :id "
"GROUP BY Level.LevelID "
"ORDER BY MIN(Picture.PictureID) ASC")) {
qDebug() << "Error preparing query:" << query.lastError().text();
status = false;
}
for (int i=0; i < rowCount; i++) {
QSqlQuery query(db);
query.prepare("SELECT Level.*, Info.*, Picture.*, Author.* "
"FROM Level "
"JOIN Info ON Level.infoID = Info.InfoID "
"JOIN Screens ON Level.LevelID = Screens.levelID "
"JOIN Picture ON Screens.pictureID = Picture.PictureID "
"JOIN AuthorList ON Level.LevelID = AuthorList.levelID "
"JOIN Author ON AuthorList.authorID = Author.AuthorID "
"WHERE Level.LevelID = :id "
"GROUP BY Level.LevelID "
"ORDER BY MIN(Picture.PictureID) ASC");
query.bindValue(":id", i+1); // Set the ID autoincrament starts at 1

if (query.exec()) {
while (query.next()) {
items.append(ListItemData(
query.value("Info.title").toString(),
query.value("Author.value").toString(),
query.value("Info.type").toInt(),
query.value("Info.class").toInt(),
query.value("Info.release").toString(),
query.value("Info.difficulty").toInt(),
query.value("Info.duration").toInt(),
query.value("Picture.data").toByteArray()));
if (status) {
for (qint64 i = 1; i <= rowCount; i++) {
query.bindValue(":id", i); // Bind the current LevelID
if (query.exec()) {
while (query.next()) {
items.append(ListItemData(
query.value("Info.title").toString(),
query.value("Author.value").toString(),
query.value("Info.type").toInt(),
query.value("Info.class").toInt(),
query.value("Info.release").toString(),
query.value("Info.difficulty").toInt(),
query.value("Info.duration").toInt(),
query.value("Picture.data").toByteArray()));
}
} else {
qDebug() << "Error executing query for Level ID:" << i
<< query.lastError().text();
}
} else {
qDebug() << "Error executing query:" << query.lastError().text();
}
}
return items;
}

InfoData Data::getInfo(const int id) {
QVector<QByteArray> imageList;
QSqlQuery query(db);
query.prepare("SELECT Level.body, Picture.data "
"FROM Level "
"JOIN Screens ON Level.LevelID = Screens.levelID "
"JOIN Picture ON Screens.pictureID = Picture.PictureID "
"WHERE Level.LevelID = :id");
bool status = false;
QVector<QByteArray> imageList;
InfoData result;

status = query.prepare(
"SELECT Level.body, Picture.data "
"FROM Level "
"JOIN Screens ON Level.LevelID = Screens.levelID "
"JOIN Picture ON Screens.pictureID = Picture.PictureID "
"WHERE Level.LevelID = :id");
query.bindValue(":id", id);

if (query.exec()) {
if (query.next()) {
QString body = query.value("body").toString();
while (query.next()) {
imageList.push_back(query.value("Picture.data").toByteArray());
if (status) {
if (query.exec()) {
if (query.next()) {
QString body = query.value("body").toString();
// notice that we jump over the fist image
// the first image is the cover image
while (query.next()) {
imageList.push_back(
query.value("Picture.data").toByteArray());
}
result = InfoData(body, imageList);
}
return InfoData(body, imageList);
} else {
qDebug() << "Error executing query:" << query.lastError().text();
}
} else {
qDebug() << "Error executing query:" << query.lastError().text();
}
return InfoData();
return result;
}

QString Data::getWalkthrough(const int id) {
QSqlQuery query(db);
query.prepare("SELECT Level.walkthrough "
"FROM Level "
"WHERE Level.LevelID = :id");
bool status = false;
QString result = "";

status = query.prepare(
"SELECT Level.walkthrough "
"FROM Level "
"WHERE Level.LevelID = :id");
query.bindValue(":id", id);
if (query.exec()) {
if (query.next()) {
QString body = query.value("Level.walkthrough").toString();
return body;

if (status) {
if (query.exec()) {
if (query.next()) {
result = query.value("Level.walkthrough").toString();
} else {
qDebug() << "No results found for Level ID:" << id;
}
} else {
qDebug() << "Error executing query:" << query.lastError().text();
}
} else {
qDebug() << "Error executing query:" << query.lastError().text();
}
return "";
return result;
}

int Data::getType(const int id) {
QSqlQuery query(db);
query.prepare("SELECT Info.type "
"FROM Level "
"JOIN Info ON Level.infoID = Info.InfoID "
"WHERE Level.LevelID = :id");
bool status = false;
int result = 0;

status = query.prepare(
"SELECT Info.type "
"FROM Level "
"JOIN Info ON Level.infoID = Info.InfoID "
"WHERE Level.LevelID = :id");
query.bindValue(":id", id);
if (query.exec()) {
if (query.next()) {
return query.value("Info.type").toInt();

if (status) {
if (query.exec()) {
if (query.next()) {
result = query.value("Info.type").toInt();
} else {
qDebug() << "No results found for Level ID:" << id;
}
} else {
qDebug() << "Error executing query:" << query.lastError().text();
}
} else {
qDebug() << "Error executing query:" << query.lastError().text();
}
return 0;
return result;
}

ZipData Data::getDownload(const int id) {
QSqlQuery query(db);
query.prepare("SELECT Zip.* "
"FROM Level "
"JOIN ZipList ON Level.LevelID = ZipList.levelID "
"JOIN Zip ON ZipList.zipID = Zip.ZipID "
"WHERE Level.LevelID = :id");
bool status = false;
ZipData result;

status = query.prepare(
"SELECT Zip.* "
"FROM Level "
"JOIN ZipList ON Level.LevelID = ZipList.levelID "
"JOIN Zip ON ZipList.zipID = Zip.ZipID "
"WHERE Level.LevelID = :id");
query.bindValue(":id", id);

if (query.exec()) {
if (query.next()) {
return ZipData(
if (status) {
if (query.exec()) {
if (query.next()) {
result = ZipData(
query.value("Zip.name").toString(),
query.value("Zip.size").toFloat(),
query.value("Zip.md5sum").toString(),
query.value("Zip.url").toString(),
query.value("Zip.version").toInt(),
query.value("Zip.release").toString());
} else {
qDebug() << "No results found for Level ID:" << id;
}
} else {
qDebug() << "Error executing query:" << query.lastError().text();
}
} else {
qDebug() << "Error executing query:" << query.lastError().text();
}
return ZipData();
return result;
}

void Data::setDownloadMd5(const int id, const QString& newMd5sum) {
QSqlQuery query(db);
bool status = false;
QSqlQuery query(db);

status = query.prepare(
"UPDATE Zip "
"SET md5sum = :newMd5sum "
Expand All @@ -178,15 +227,18 @@ void Data::setDownloadMd5(const int id, const QString& newMd5sum) {
}

QVector<FileList> Data::getFileList(const int id) {
QVector<FileList> list;
QSqlQuery query(db);
if (!query.prepare("SELECT File.path, File.md5sum "
QVector<FileList> list;

if (!query.prepare(
"SELECT File.path, File.md5sum "
"FROM File "
"JOIN GameFileList ON File.FileID = GameFileList.fileID "
"WHERE GameFileList.gameID = :id")) {
qDebug() << "Error preparing query:" << query.lastError().text();
}
query.bindValue(":id", id);

if (query.exec()) {
while (query.next()) {
list.append({
Expand Down
13 changes: 7 additions & 6 deletions src/Data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ struct ZipData {
*/
ZipData() {}
ZipData(
QString zipName, float zipSize, QString md5sum, QString url,
int version, const QString& release) :
const QString& zipName, float zipSize, const QString& md5sum,
const QString& url, int version, const QString& release) :
name(zipName), megabyteSize(zipSize), md5sum(md5sum),
url(url), version(version), release(release) {}
QString name;
Expand Down Expand Up @@ -101,9 +101,9 @@ struct ListItemData {
* @param imageData The cover image as a `QByteArray`. Supported formats include JPEG, PNG, and WEBP.
*/
ListItemData(
QString title, QString author, qint64 type, qint64 classInput,
QString releaseDate, qint64 difficulty, qint64 duration,
QByteArray imageData):
const QString& title, const QString& author, qint64 type,
qint64 classInput, const QString& releaseDate, qint64 difficulty,
qint64 duration, QByteArray imageData) :
m_title(title), m_author(author), m_type(type),
m_class(classInput), m_releaseDate(releaseDate),
m_difficulty(difficulty), m_duration(duration) {
Expand Down Expand Up @@ -218,7 +218,7 @@ class Data : public QObject {
status = false;
} else {
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(path + "/tombll.db");
db.setDatabaseName(QString("%1/tombll.db").arg(path));
// db.setConnectOptions("QSQLITE_OPEN_READONLY");
if (db.open() == true) { // flawfinder: ignore
status = true;
Expand All @@ -234,6 +234,7 @@ class Data : public QObject {
db.close();
}

qint64 getListRowCount();
QVector<ListItemData> getListItems();
InfoData getInfo(int id);
QString getWalkthrough(int id);
Expand Down

0 comments on commit 1922327

Please sign in to comment.