1313
1414#include " Data.hpp"
1515
16- QVector<ListItemData> Data::getListItems () {
17- QVector<ListItemData> items;
18- QSqlQuery index (db);
19- int rowCount = 0 ;
20- index.prepare (" SELECT COUNT(*) FROM Level" );
16+ qint64 Data::getListRowCount () {
17+ QSqlQuery query (db);
18+ qint64 result = 0 ;
2119
22- if (!index.exec ()) {
23- qDebug () << " Error executing query:" << index.lastError ().text ();
24- return items;
20+ if (query.prepare (" SELECT COUNT(*) FROM Level" )) {
21+ if (query.exec ()) {
22+ // Move to the first (and only) result row
23+ if (query.next ()) {
24+ // Assign the count value to result
25+ result = query.value (0 ).toInt ();
26+ qWarning () << " Number of rows in 'Level' table:" << result;
27+ } else {
28+ qDebug () << " No rows returned from the query" ;
29+ }
30+ } else {
31+ qDebug () << " Error executing query:" << query.lastError ().text ();
32+ }
33+ } else {
34+ qDebug () << " Error preparing query:" << query.lastError ().text ();
2535 }
36+ return result;
37+ }
2638
27- // Move to the first (and only) result row
28- if (index.next ()) {
29- // Retrieve the count value (assuming it's in the first column, index 0)
30- rowCount = index.value (0 ).toInt ();
31-
32- // Now, 'rowCount' contains the count of rows in the 'Level' table
33- qDebug () << " Number of rows in 'Level' table:" << rowCount;
34- } else {
35- // Handle the case where no rows are returned
36- qDebug () << " No rows returned from the query" ;
39+ QVector<ListItemData> Data::getListItems () {
40+ QSqlQuery query (db);
41+ bool status = true ;
42+ QVector<ListItemData> items;
43+ qint64 rowCount = getListRowCount ();
44+
45+ if (!query.prepare (
46+ " SELECT Info.title, Author.value, Info.type, "
47+ " Info.class, Info.release, Info.difficulty, "
48+ " Info.duration, Picture.data FROM Level "
49+ " JOIN Info ON Level.infoID = Info.InfoID "
50+ " JOIN Screens ON Level.LevelID = Screens.levelID "
51+ " JOIN Picture ON Screens.pictureID = Picture.PictureID "
52+ " JOIN AuthorList ON Level.LevelID = AuthorList.levelID "
53+ " JOIN Author ON AuthorList.authorID = Author.AuthorID "
54+ " WHERE Level.LevelID = :id "
55+ " GROUP BY Level.LevelID "
56+ " ORDER BY MIN(Picture.PictureID) ASC" )) {
57+ qDebug () << " Error preparing query:" << query.lastError ().text ();
58+ status = false ;
3759 }
38- for (int i=0 ; i < rowCount; i++) {
39- QSqlQuery query (db);
40- query.prepare (" SELECT Level.*, Info.*, Picture.*, Author.* "
41- " FROM Level "
42- " JOIN Info ON Level.infoID = Info.InfoID "
43- " JOIN Screens ON Level.LevelID = Screens.levelID "
44- " JOIN Picture ON Screens.pictureID = Picture.PictureID "
45- " JOIN AuthorList ON Level.LevelID = AuthorList.levelID "
46- " JOIN Author ON AuthorList.authorID = Author.AuthorID "
47- " WHERE Level.LevelID = :id "
48- " GROUP BY Level.LevelID "
49- " ORDER BY MIN(Picture.PictureID) ASC" );
50- query.bindValue (" :id" , i+1 ); // Set the ID autoincrament starts at 1
5160
52- if (query.exec ()) {
53- while (query.next ()) {
54- items.append (ListItemData (
55- query.value (" Info.title" ).toString (),
56- query.value (" Author.value" ).toString (),
57- query.value (" Info.type" ).toInt (),
58- query.value (" Info.class" ).toInt (),
59- query.value (" Info.release" ).toString (),
60- query.value (" Info.difficulty" ).toInt (),
61- query.value (" Info.duration" ).toInt (),
62- query.value (" Picture.data" ).toByteArray ()));
61+ if (status) {
62+ for (qint64 i = 1 ; i <= rowCount; i++) {
63+ query.bindValue (" :id" , i); // Bind the current LevelID
64+ if (query.exec ()) {
65+ while (query.next ()) {
66+ items.append (ListItemData (
67+ query.value (" Info.title" ).toString (),
68+ query.value (" Author.value" ).toString (),
69+ query.value (" Info.type" ).toInt (),
70+ query.value (" Info.class" ).toInt (),
71+ query.value (" Info.release" ).toString (),
72+ query.value (" Info.difficulty" ).toInt (),
73+ query.value (" Info.duration" ).toInt (),
74+ query.value (" Picture.data" ).toByteArray ()));
75+ }
76+ } else {
77+ qDebug () << " Error executing query for Level ID:" << i
78+ << query.lastError ().text ();
6379 }
64- } else {
65- qDebug () << " Error executing query:" << query.lastError ().text ();
6680 }
6781 }
6882 return items;
6983}
7084
7185InfoData Data::getInfo (const int id) {
72- QVector<QByteArray> imageList;
7386 QSqlQuery query (db);
74- query.prepare (" SELECT Level.body, Picture.data "
75- " FROM Level "
76- " JOIN Screens ON Level.LevelID = Screens.levelID "
77- " JOIN Picture ON Screens.pictureID = Picture.PictureID "
78- " WHERE Level.LevelID = :id" );
87+ bool status = false ;
88+ QVector<QByteArray> imageList;
89+ InfoData result;
90+
91+ status = query.prepare (
92+ " SELECT Level.body, Picture.data "
93+ " FROM Level "
94+ " JOIN Screens ON Level.LevelID = Screens.levelID "
95+ " JOIN Picture ON Screens.pictureID = Picture.PictureID "
96+ " WHERE Level.LevelID = :id" );
7997 query.bindValue (" :id" , id);
8098
81- if (query.exec ()) {
82- if (query.next ()) {
83- QString body = query.value (" body" ).toString ();
84- while (query.next ()) {
85- imageList.push_back (query.value (" Picture.data" ).toByteArray ());
99+ if (status) {
100+ if (query.exec ()) {
101+ if (query.next ()) {
102+ QString body = query.value (" body" ).toString ();
103+ // notice that we jump over the fist image
104+ // the first image is the cover image
105+ while (query.next ()) {
106+ imageList.push_back (
107+ query.value (" Picture.data" ).toByteArray ());
108+ }
109+ result = InfoData (body, imageList);
86110 }
87- return InfoData (body, imageList);
111+ } else {
112+ qDebug () << " Error executing query:" << query.lastError ().text ();
88113 }
89- } else {
90- qDebug () << " Error executing query:" << query.lastError ().text ();
91114 }
92- return InfoData () ;
115+ return result ;
93116}
94117
95118QString Data::getWalkthrough (const int id) {
96119 QSqlQuery query (db);
97- query.prepare (" SELECT Level.walkthrough "
98- " FROM Level "
99- " WHERE Level.LevelID = :id" );
120+ bool status = false ;
121+ QString result = " " ;
122+
123+ status = query.prepare (
124+ " SELECT Level.walkthrough "
125+ " FROM Level "
126+ " WHERE Level.LevelID = :id" );
100127 query.bindValue (" :id" , id);
101- if (query.exec ()) {
102- if (query.next ()) {
103- QString body = query.value (" Level.walkthrough" ).toString ();
104- return body;
128+
129+ if (status) {
130+ if (query.exec ()) {
131+ if (query.next ()) {
132+ result = query.value (" Level.walkthrough" ).toString ();
133+ } else {
134+ qDebug () << " No results found for Level ID:" << id;
135+ }
136+ } else {
137+ qDebug () << " Error executing query:" << query.lastError ().text ();
105138 }
106- } else {
107- qDebug () << " Error executing query:" << query.lastError ().text ();
108139 }
109- return " " ;
140+ return result ;
110141}
111142
112143int Data::getType (const int id) {
113144 QSqlQuery query (db);
114- query.prepare (" SELECT Info.type "
115- " FROM Level "
116- " JOIN Info ON Level.infoID = Info.InfoID "
117- " WHERE Level.LevelID = :id" );
145+ bool status = false ;
146+ int result = 0 ;
147+
148+ status = query.prepare (
149+ " SELECT Info.type "
150+ " FROM Level "
151+ " JOIN Info ON Level.infoID = Info.InfoID "
152+ " WHERE Level.LevelID = :id" );
118153 query.bindValue (" :id" , id);
119- if (query.exec ()) {
120- if (query.next ()) {
121- return query.value (" Info.type" ).toInt ();
154+
155+ if (status) {
156+ if (query.exec ()) {
157+ if (query.next ()) {
158+ result = query.value (" Info.type" ).toInt ();
159+ } else {
160+ qDebug () << " No results found for Level ID:" << id;
161+ }
162+ } else {
163+ qDebug () << " Error executing query:" << query.lastError ().text ();
122164 }
123- } else {
124- qDebug () << " Error executing query:" << query.lastError ().text ();
125165 }
126- return 0 ;
166+ return result ;
127167}
128168
129169ZipData Data::getDownload (const int id) {
130170 QSqlQuery query (db);
131- query.prepare (" SELECT Zip.* "
132- " FROM Level "
133- " JOIN ZipList ON Level.LevelID = ZipList.levelID "
134- " JOIN Zip ON ZipList.zipID = Zip.ZipID "
135- " WHERE Level.LevelID = :id" );
171+ bool status = false ;
172+ ZipData result;
173+
174+ status = query.prepare (
175+ " SELECT Zip.* "
176+ " FROM Level "
177+ " JOIN ZipList ON Level.LevelID = ZipList.levelID "
178+ " JOIN Zip ON ZipList.zipID = Zip.ZipID "
179+ " WHERE Level.LevelID = :id" );
136180 query.bindValue (" :id" , id);
137181
138- if (query.exec ()) {
139- if (query.next ()) {
140- return ZipData (
182+ if (status) {
183+ if (query.exec ()) {
184+ if (query.next ()) {
185+ result = ZipData (
141186 query.value (" Zip.name" ).toString (),
142187 query.value (" Zip.size" ).toFloat (),
143188 query.value (" Zip.md5sum" ).toString (),
144189 query.value (" Zip.url" ).toString (),
145190 query.value (" Zip.version" ).toInt (),
146191 query.value (" Zip.release" ).toString ());
192+ } else {
193+ qDebug () << " No results found for Level ID:" << id;
194+ }
195+ } else {
196+ qDebug () << " Error executing query:" << query.lastError ().text ();
147197 }
148- } else {
149- qDebug () << " Error executing query:" << query.lastError ().text ();
150198 }
151- return ZipData () ;
199+ return result ;
152200}
153201
154202void Data::setDownloadMd5 (const int id, const QString& newMd5sum) {
155- QSqlQuery query (db);
156203 bool status = false ;
204+ QSqlQuery query (db);
205+
157206 status = query.prepare (
158207 " UPDATE Zip "
159208 " SET md5sum = :newMd5sum "
@@ -178,15 +227,18 @@ void Data::setDownloadMd5(const int id, const QString& newMd5sum) {
178227}
179228
180229QVector<FileList> Data::getFileList (const int id) {
181- QVector<FileList> list;
182230 QSqlQuery query (db);
183- if (!query.prepare (" SELECT File.path, File.md5sum "
231+ QVector<FileList> list;
232+
233+ if (!query.prepare (
234+ " SELECT File.path, File.md5sum "
184235 " FROM File "
185236 " JOIN GameFileList ON File.FileID = GameFileList.fileID "
186237 " WHERE GameFileList.gameID = :id" )) {
187238 qDebug () << " Error preparing query:" << query.lastError ().text ();
188239 }
189240 query.bindValue (" :id" , id);
241+
190242 if (query.exec ()) {
191243 while (query.next ()) {
192244 list.append ({
0 commit comments