1 // CalendarDataStorage.cpp - CalendarDataStorage class
3 // (c) 2016-2017 Xestia Software Development.
5 // This file is part of Xestia Calendar.
7 // Xestia Calendar is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by the
9 // Free Software Foundation, version 3 of the license.
11 // Xestia Calendar is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with Xestia Calendar. If not, see <http://www.gnu.org/licenses/>
19 #include "CalendarDataStorage.h"
21 #define CDS_RANDOMPOW 24
25 static int callback(void *NotUsed, int argc, char **argv, char **azColName){
29 CalendarDataStorage::CalendarDataStorage(){
31 // Initialise the SQLite database.
33 sqlite3_open_v2(":memory:", &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_FULLMUTEX, nullptr);
38 CalendarDataStorage::~CalendarDataStorage(){
40 // Destory the SQLite database.
46 void CalendarDataStorage::SetupTables(){
48 // Setup the tables for the Calendar Data Storage.
50 char *setupTablesErrMsg = nullptr;
53 // Setup the accounts table.
55 const char *accountsTableString;
56 accountsTableString = "CREATE TABLE accounts(id INTEGER PRIMARY KEY AUTOINCREMENT"
57 ", preferencesid INTEGER"
61 resultCode = sqlite3_exec(db, accountsTableString, callback, nullptr, &setupTablesErrMsg);
64 DataStorageInitOK = false;
68 // Setup the calendars table.
70 const char *calendarTableString;
71 calendarTableString = "CREATE TABLE calendars(id INTEGER PRIMARY KEY AUTOINCREMENT"
79 resultCode = sqlite3_exec(db, calendarTableString, callback, nullptr, &setupTablesErrMsg);
82 DataStorageInitOK = false;
86 // Setup the calendar entries table.
88 const char *calendarentriesTableString;
89 calendarentriesTableString = "CREATE TABLE calendarentries(id INTEGER PRIMARY KEY AUTOINCREMENT"
90 ", calendarid INTEGER"
92 ", entrydescription TEXT"
93 ", entrystartyear INTEGER"
94 ", entrystartmonth INTEGER"
95 ", entrystartday INTEGER"
96 ", entrystarthour INTEGER"
97 ", entrystartminute INTEGER"
98 ", entrystartsecond INTEGER"
99 ", entryendyear INTEGER"
100 ", entryendmonth INTEGER"
101 ", entryendday INTEGER"
102 ", entryendhour INTEGER"
103 ", entryendminute INTEGER"
104 ", entryendsecond INTEGER"
105 ", entrydurationweek INTEGER"
106 ", entrydurationday INTEGER"
107 ", entrydurationhour INTEGER"
108 ", entrydurationminute INTEGER"
109 ", entrydurationsecond INTEGER"
113 resultCode = sqlite3_exec(db, calendarentriesTableString, callback, nullptr, &setupTablesErrMsg);
115 if (resultCode != 0){
116 DataStorageInitOK = false;
120 // Setup the checksums table.
122 const char *checksumsTableString;
123 checksumsTableString = "CREATE TABLE checksums(checksumname TEXT PRIMARY KEY "
124 ", checksumvalue TEXT);";
126 resultCode = sqlite3_exec(db, checksumsTableString, callback, nullptr, &setupTablesErrMsg);
128 if (resultCode != 0){
129 DataStorageInitOK = false;
133 // Setup internal checksums.
135 CDSChecksumResult addChecksum = AddChecksum("internal_updatedata", "");
137 if (addChecksum != CDSCHECKSUM_OK){
138 DataStorageInitOK = false;
142 DataStorageInitOK = true;
146 bool CalendarDataStorage::DidInitOK()
149 return DataStorageInitOK;
153 CDSAccountResult CalendarDataStorage::AddAccount(string accountName, int accountPreferencesID)
156 CDSAccountResult addResult = CDSACCOUNT_UNITTESTFAIL;
159 sqlite3_stmt *statementHandle;
160 sqlite3_stmt *findHandle;
162 if (accountName == ""){
163 return CDSACCOUNT_NONAME;
166 // Check if the account name already exsits.
168 resultCode = sqlite3_prepare_v2(db, "SELECT name FROM accounts WHERE name=(?1);", -1, &findHandle, nullptr);
170 if (resultCode != 0){
171 return CDSACCOUNT_FAILED;
174 resultCode = sqlite3_bind_text(findHandle, 1, accountName.c_str(), -1, SQLITE_STATIC);
176 if (resultCode != 0){
177 return CDSACCOUNT_FAILED;
180 resultCode = sqlite3_step(findHandle);
182 if (resultCode != SQLITE_DONE){
183 return CDSACCOUNT_FAILED;
184 } else if (resultCode == SQLITE_ROW){
185 return CDSACCOUNT_FAILED;
188 resultCode = sqlite3_prepare_v2(db, "INSERT INTO accounts (name, preferencesid) VALUES(?1, ?2);", -1, &statementHandle, nullptr);
190 if (resultCode != 0){
191 return CDSACCOUNT_FAILED;
194 resultCode = sqlite3_bind_text(statementHandle, 1, accountName.c_str(), -1, SQLITE_STATIC);
196 if (resultCode != 0){
197 return CDSACCOUNT_FAILED;
200 resultCode = sqlite3_bind_int(statementHandle, 2, accountPreferencesID);
202 if (resultCode != 0){
203 return CDSACCOUNT_FAILED;
206 resultCode = sqlite3_step(statementHandle);
208 if (resultCode != SQLITE_DONE){
209 return CDSACCOUNT_FAILED;
212 // Update the checksum.
214 UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
216 addResult = CDSACCOUNT_OK;
222 CDSAccountList CalendarDataStorage::GetAccountList()
225 CDSAccountList accountList;
227 // Check if calendar exists first.
231 sqlite3_stmt *statementHandle;
233 resultCode = sqlite3_prepare_v2(db, "SELECT id, name, preferencesid from accounts;", -1, &statementHandle, nullptr);
235 if (resultCode != 0){
236 accountList.getAccountListResult = CDSACCOUNT_FAILED;
240 if (resultCode != 0){
241 accountList.getAccountListResult = CDSACCOUNT_FAILED;
245 resultCode = sqlite3_step(statementHandle);
247 if (resultCode == SQLITE_ROW){
249 } else if (resultCode == SQLITE_DONE) {
250 accountList.getAccountListResult = CDSACCOUNT_NOACCOUNT;
253 accountList.getAccountListResult = CDSACCOUNT_FAILED;
257 while (resultCode == SQLITE_ROW){
259 CDSGetAccountInfo accountInfo;
261 accountInfo.accountID = sqlite3_column_int(statementHandle, 0);
262 accountInfo.accountPreferencesID = sqlite3_column_int(statementHandle, 2);
264 stringstream calendarStream;
266 calendarStream << sqlite3_column_text(statementHandle, 1);
268 accountInfo.accountName = calendarStream.str();
269 accountInfo.accountInfoResult = CDSACCOUNT_OK;
271 accountList.accountList.push_back(accountInfo);
273 resultCode = sqlite3_step(statementHandle);
277 accountList.getAccountListResult = CDSACCOUNT_OK;
283 CDSGetAccountInfo CalendarDataStorage::GetAccount(string accountName)
286 CDSGetAccountInfo accountInfo;
287 accountInfo.accountInfoResult = CDSACCOUNT_UNITTESTFAIL;
290 sqlite3_stmt *statementHandle;
292 resultCode = sqlite3_prepare_v2(db, "SELECT name, id, preferencesid FROM accounts WHERE name=(?1);", -1, &statementHandle, nullptr);
294 if (resultCode != 0){
295 accountInfo.accountInfoResult = CDSACCOUNT_FAILED;
299 resultCode = sqlite3_bind_text(statementHandle, 1, accountName.c_str(), -1, SQLITE_STATIC);
301 if (resultCode != 0){
302 accountInfo.accountInfoResult = CDSACCOUNT_FAILED;
306 resultCode = sqlite3_step(statementHandle);
308 if (resultCode == SQLITE_DONE){
310 accountInfo.accountInfoResult = CDSACCOUNT_NOACCOUNT;
313 } else if (resultCode == SQLITE_ROW){
315 // Get the result data.
317 stringstream accountNameStream;
319 accountNameStream << sqlite3_column_text(statementHandle, 0);
321 accountInfo.accountName = accountNameStream.str();
322 accountInfo.accountID = sqlite3_column_int(statementHandle, 1);
323 accountInfo.accountPreferencesID = sqlite3_column_int(statementHandle, 2);
325 accountInfo.accountInfoResult = CDSACCOUNT_OK;
330 accountInfo.accountInfoResult = CDSACCOUNT_FAILED;
339 CDSAccountResult CalendarDataStorage::UpdateAccount(int accountID, string accountName)
342 CDSAccountResult updateResult = CDSACCOUNT_UNITTESTFAIL;
345 sqlite3_stmt *findHandle;
346 sqlite3_stmt *existingHandle;
347 sqlite3_stmt *statementHandle;
349 if (accountName == ""){
350 return CDSACCOUNT_NONAME;
353 // Check if account exists first.
355 resultCode = sqlite3_prepare_v2(db, "SELECT id from accounts WHERE id=(?1);", -1, &findHandle, nullptr);
357 if (resultCode != 0){
358 return CDSACCOUNT_FAILED;
361 resultCode = sqlite3_bind_int(findHandle, 1, accountID);
363 if (resultCode != 0){
364 return CDSACCOUNT_FAILED;
367 resultCode = sqlite3_step(findHandle);
369 if (resultCode == SQLITE_ROW){
371 } else if (resultCode == SQLITE_DONE) {
372 return CDSACCOUNT_NOACCOUNT;
374 return CDSACCOUNT_FAILED;
377 // Check if account with the name given already exists before renaming.
379 resultCode = sqlite3_prepare_v2(db, "SELECT name from accounts WHERE name=(?1);", -1, &existingHandle, nullptr);
381 if (resultCode != 0){
382 return CDSACCOUNT_FAILED;
385 resultCode = sqlite3_bind_text(existingHandle, 1, accountName.c_str(), -1, SQLITE_STATIC);
387 if (resultCode != 0){
388 return CDSACCOUNT_FAILED;
391 resultCode = sqlite3_step(existingHandle);
393 if (resultCode == SQLITE_ROW){
394 return CDSACCOUNT_FAILED;
395 } else if (resultCode == SQLITE_DONE) {
398 return CDSACCOUNT_FAILED;
401 // Update the account.
403 resultCode = sqlite3_prepare_v2(db, "UPDATE accounts SET name=(?1) WHERE id=(?2);", -1, &statementHandle, nullptr);
405 if (resultCode != 0){
406 return CDSACCOUNT_FAILED;
409 resultCode = sqlite3_bind_text(statementHandle, 1, accountName.c_str(), -1, SQLITE_STATIC);
411 if (resultCode != 0){
412 return CDSACCOUNT_FAILED;
415 resultCode = sqlite3_bind_int(statementHandle, 2, accountID);
417 if (resultCode != 0){
418 return CDSACCOUNT_FAILED;
421 resultCode = sqlite3_step(statementHandle);
423 if (resultCode != SQLITE_DONE){
424 return CDSACCOUNT_FAILED;
427 // Update the checksum.
429 UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
431 updateResult = CDSACCOUNT_OK;
437 CDSAccountResult CalendarDataStorage::DeleteAccount(int accountID)
440 CDSAccountResult deleteResult = CDSACCOUNT_UNITTESTFAIL;
442 // Check if account exists first.
444 sqlite3_stmt *findHandle;
445 sqlite3_stmt *statementHandle;
446 sqlite3_stmt *calendarHandle;
449 resultCode = sqlite3_prepare_v2(db, "SELECT id from accounts WHERE id=(?1);", -1, &findHandle, nullptr);
451 if (resultCode != 0){
452 return CDSACCOUNT_FAILED;
455 resultCode = sqlite3_bind_int(findHandle, 1, accountID);
457 if (resultCode != 0){
458 return CDSACCOUNT_FAILED;
461 resultCode = sqlite3_step(findHandle);
463 if (resultCode == SQLITE_ROW){
465 } else if (resultCode == SQLITE_DONE) {
466 return CDSACCOUNT_NOACCOUNT;
471 // Delete the account.
473 resultCode = sqlite3_prepare_v2(db, "DELETE FROM accounts WHERE id=(?1);", -1, &statementHandle, nullptr);
475 if (resultCode != 0){
476 return CDSACCOUNT_FAILED;
479 resultCode = sqlite3_bind_int(statementHandle, 1, accountID);
481 if (resultCode != 0){
482 return CDSACCOUNT_FAILED;
485 resultCode = sqlite3_step(statementHandle);
487 if (resultCode == SQLITE_DONE){
488 //deleteResult = CDSACCOUNT_OK;
490 return CDSACCOUNT_FAILED;
493 // Get the calendar IDs and delete each calendar (and associated entries).
495 resultCode = sqlite3_prepare_v2(db, "SELECT id FROM calendars WHERE accountid=(?1);", -1, &calendarHandle, nullptr);
497 if (resultCode != 0){
498 return CDSACCOUNT_FAILED;
501 resultCode = sqlite3_bind_int(calendarHandle, 1, accountID);
503 if (resultCode != 0){
504 return CDSACCOUNT_FAILED;
507 resultCode = sqlite3_step(calendarHandle);
509 if (resultCode == SQLITE_DONE || resultCode == SQLITE_ROW){
510 deleteResult = CDSACCOUNT_OK;
512 return CDSACCOUNT_FAILED;
515 while (resultCode == SQLITE_ROW){
517 int calendarDeleteID = sqlite3_column_int(calendarHandle, 0);
519 DeleteCalendar(calendarDeleteID);
521 resultCode = sqlite3_step(calendarHandle);
525 // Update the checksum.
527 UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
529 deleteResult = CDSACCOUNT_OK;
536 CDSCalendarResult CalendarDataStorage::AddCalendar(int accountID, string calendarName, string calendarID, Colour calendarColour, string calendarDescription)
539 CDSCalendarResult addResult = CDSCALENDAR_UNITTESTFAIL;
542 sqlite3_stmt *findHandle;
543 sqlite3_stmt *statementHandle;
545 // Check if the account exists first.
547 resultCode = sqlite3_prepare_v2(db, "SELECT id FROM accounts WHERE id=(?1);", -1, &findHandle, nullptr);
549 if (resultCode != 0){
550 return CDSCALENDAR_FAILED;
553 resultCode = sqlite3_bind_int(findHandle, 1, accountID);
555 if (resultCode != 0){
556 return CDSCALENDAR_FAILED;
559 resultCode = sqlite3_step(findHandle);
561 if (resultCode != SQLITE_DONE){
562 return CDSCALENDAR_NOACCOUNT;
565 resultCode = sqlite3_prepare_v2(db, "INSERT INTO calendars (name, calendarid, accountid, colour, description) VALUES(?1, ?2, ?3, ?4, ?5);", -1, &statementHandle, nullptr);
567 if (resultCode != 0){
568 return CDSCALENDAR_FAILED;
571 resultCode = sqlite3_bind_text(statementHandle, 1, calendarName.c_str(), -1, SQLITE_STATIC);
573 if (resultCode != 0){
574 return CDSCALENDAR_FAILED;
577 resultCode = sqlite3_bind_text(statementHandle, 2, calendarID.c_str(), -1, SQLITE_STATIC);
579 if (resultCode != 0){
580 return CDSCALENDAR_FAILED;
583 resultCode = sqlite3_bind_int(statementHandle, 3, accountID);
585 if (resultCode != 0){
586 return CDSCALENDAR_FAILED;
589 resultCode = sqlite3_bind_text(statementHandle, 4, ((string)calendarColour).c_str(), -1, SQLITE_STATIC);
591 if (resultCode != 0){
592 return CDSCALENDAR_FAILED;
595 resultCode = sqlite3_bind_text(statementHandle, 5, calendarDescription.c_str(), -1, SQLITE_STATIC);
597 if (resultCode != 0){
598 return CDSCALENDAR_FAILED;
601 resultCode = sqlite3_step(statementHandle);
603 if (resultCode != SQLITE_DONE){
604 return CDSCALENDAR_FAILED;
607 // Update the checksum.
609 UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
611 addResult = CDSCALENDAR_OK;
617 CDSGetCalendarInfo CalendarDataStorage::GetCalendar(std::string accountName, std::string calendarTextID)
620 CDSGetAccountInfo accountResult;
621 CDSGetCalendarInfo calendarResult;
622 sqlite3_stmt *statementHandle;
625 // Check if the account exists.
627 accountResult = GetAccount(accountName);
629 switch (accountResult.accountInfoResult){
632 calendarResult.accountName = accountResult.accountName;
633 calendarResult.accountInfoResult = CDSACCOUNT_OK;
635 case CDSACCOUNT_FAILED:
636 calendarResult.accountInfoResult = CDSACCOUNT_FAILED;
637 calendarResult.calendarInfoResult = CDSCALENDAR_FAILED;
638 return calendarResult;
639 case CDSACCOUNT_NOACCOUNT:
640 calendarResult.accountInfoResult = CDSACCOUNT_NOACCOUNT;
641 calendarResult.calendarInfoResult = CDSCALENDAR_NOCALENDAR;
642 return calendarResult;
646 // Check if the calendar exists.
648 resultCode = sqlite3_prepare_v2(db, "SELECT id, accountid, name, calendarid, colour, description FROM calendars WHERE accountid=(?1) AND calendarid=(?2);", -1, &statementHandle, nullptr);
650 if (resultCode != 0){
651 calendarResult.calendarInfoResult = CDSCALENDAR_FAILED;
652 return calendarResult;
655 resultCode = sqlite3_bind_int(statementHandle, 1, accountResult.accountID);
657 if (resultCode != 0){
658 calendarResult.calendarInfoResult = CDSCALENDAR_FAILED;
659 return calendarResult;
662 resultCode = sqlite3_bind_text(statementHandle, 2, calendarTextID.c_str(), -1, SQLITE_STATIC);
664 if (resultCode != 0){
665 calendarResult.calendarInfoResult = CDSCALENDAR_FAILED;
666 return calendarResult;
669 resultCode = sqlite3_step(statementHandle);
671 if (resultCode == SQLITE_DONE){
673 calendarResult.calendarInfoResult = CDSCALENDAR_NOCALENDAR;
674 return calendarResult;
676 } else if (resultCode == SQLITE_ROW){
678 // Get the calendar name.
680 stringstream calendarStream;
682 calendarStream << sqlite3_column_text(statementHandle, 2);
683 calendarResult.calendarName = calendarStream.str();
685 calendarStream.str("");
686 calendarStream << sqlite3_column_text(statementHandle, 3);
687 calendarResult.calendarTextID = calendarStream.str();
689 calendarStream.str("");
690 calendarStream << sqlite3_column_text(statementHandle, 4);
691 calendarResult.calendarColour = calendarStream.str();
693 calendarStream.str("");
694 calendarStream << sqlite3_column_text(statementHandle, 5);
695 calendarResult.calendarDescription = calendarStream.str();
697 calendarResult.calendarID = sqlite3_column_int(statementHandle, 0);
698 calendarResult.accountID = sqlite3_column_int(statementHandle, 1);
699 calendarResult.calendarInfoResult = CDSCALENDAR_OK;
700 return calendarResult;
704 calendarResult.calendarInfoResult = CDSCALENDAR_FAILED;
705 return calendarResult;
709 return calendarResult;
713 CDSGetCalendarInfo CalendarDataStorage::GetCalendar(int calendarID)
716 CDSGetCalendarInfo calendarResult;
717 sqlite3_stmt *statementHandle;
718 sqlite3_stmt *accountHandle;
721 // Check if the calendar exists.
723 resultCode = sqlite3_prepare_v2(db, "SELECT id, accountid, name, calendarid, colour, description FROM calendars WHERE id=(?1);", -1, &statementHandle, nullptr);
725 if (resultCode != 0){
726 calendarResult.calendarInfoResult = CDSCALENDAR_FAILED;
727 return calendarResult;
730 resultCode = sqlite3_bind_int(statementHandle, 1, calendarID);
732 if (resultCode != 0){
733 calendarResult.calendarInfoResult = CDSCALENDAR_FAILED;
734 return calendarResult;
737 resultCode = sqlite3_step(statementHandle);
739 if (resultCode == SQLITE_DONE){
741 calendarResult.calendarInfoResult = CDSCALENDAR_NOCALENDAR;
742 return calendarResult;
744 } else if (resultCode == SQLITE_ROW){
746 // Get the calendar data.
748 stringstream calendarStream;
750 calendarStream << sqlite3_column_text(statementHandle, 2);
751 calendarResult.calendarName = calendarStream.str();
753 calendarStream.str("");
754 calendarStream << sqlite3_column_text(statementHandle, 3);
755 calendarResult.calendarTextID = calendarStream.str();
757 calendarStream.str("");
758 calendarStream << sqlite3_column_text(statementHandle, 4);
759 calendarResult.calendarColour = calendarStream.str();
761 calendarStream.str("");
762 calendarStream << sqlite3_column_text(statementHandle, 5);
763 calendarResult.calendarDescription = calendarStream.str();
765 calendarResult.calendarID = sqlite3_column_int(statementHandle, 0);
766 calendarResult.accountID = sqlite3_column_int(statementHandle, 1);
768 // Get the account name.
770 resultCode = sqlite3_prepare_v2(db, "SELECT name FROM accounts WHERE id=(?1);", -1, &accountHandle, nullptr);
772 if (resultCode != 0){
773 calendarResult.calendarInfoResult = CDSCALENDAR_FAILED;
774 return calendarResult;
777 resultCode = sqlite3_bind_int(accountHandle, 1, calendarResult.accountID);
779 if (resultCode != 0){
780 calendarResult.calendarInfoResult = CDSCALENDAR_FAILED;
781 return calendarResult;
784 resultCode = sqlite3_step(accountHandle);
786 if (resultCode == SQLITE_ROW){
788 stringstream accountStream;
789 accountStream << sqlite3_column_text(accountHandle, 0);
790 calendarResult.accountName = accountStream.str();
791 calendarResult.accountInfoResult = CDSACCOUNT_OK;
795 calendarResult.accountInfoResult = CDSACCOUNT_FAILED;
799 calendarResult.calendarInfoResult = CDSCALENDAR_OK;
800 return calendarResult;
804 calendarResult.calendarInfoResult = CDSCALENDAR_FAILED;
805 return calendarResult;
809 return calendarResult;
814 CDSCalendarResult CalendarDataStorage::UpdateCalendar(int calendarID, string calendarName, Colour calendarColour, std::string calendarDescription)
817 CDSCalendarResult updateResult = CDSCALENDAR_UNITTESTFAIL;
820 sqlite3_stmt *findHandle;
821 sqlite3_stmt *statementHandle;
823 // Check if calendar exists first.
825 resultCode = sqlite3_prepare_v2(db, "SELECT id from calendars WHERE id=(?1);", -1, &findHandle, nullptr);
827 if (resultCode != 0){
828 return CDSCALENDAR_FAILED;
831 resultCode = sqlite3_bind_int(findHandle, 1, calendarID);
833 if (resultCode != 0){
834 return CDSCALENDAR_FAILED;
837 resultCode = sqlite3_step(findHandle);
839 if (resultCode == SQLITE_ROW){
841 } else if (resultCode == SQLITE_DONE) {
842 return CDSCALENDAR_NOCALENDAR;
844 return CDSCALENDAR_FAILED;
847 // Update the account.
849 resultCode = sqlite3_prepare_v2(db, "UPDATE calendars SET name=(?1), colour=(?2), description=(?3) WHERE id=(?4);", -1, &statementHandle, nullptr);
851 if (resultCode != 0){
852 return CDSCALENDAR_FAILED;
855 resultCode = sqlite3_bind_text(statementHandle, 1, calendarName.c_str(), -1, SQLITE_STATIC);
857 if (resultCode != 0){
858 return CDSCALENDAR_FAILED;
861 resultCode = sqlite3_bind_text(statementHandle, 2, string(calendarColour).c_str(), -1, SQLITE_STATIC);
863 if (resultCode != 0){
864 return CDSCALENDAR_FAILED;
867 resultCode = sqlite3_bind_text(statementHandle, 3, calendarDescription.c_str(), -1, SQLITE_STATIC);
869 if (resultCode != 0){
870 return CDSCALENDAR_FAILED;
873 resultCode = sqlite3_bind_int(statementHandle, 4, calendarID);
875 if (resultCode != 0){
876 return CDSCALENDAR_FAILED;
879 resultCode = sqlite3_step(statementHandle);
881 if (resultCode != SQLITE_DONE){
882 return CDSCALENDAR_FAILED;
885 // Update the checksum.
887 UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
889 updateResult = CDSCALENDAR_OK;
895 CDSCalendarResult CalendarDataStorage::DeleteCalendar(int calendarID)
898 CDSCalendarResult deleteResult = CDSCALENDAR_UNITTESTFAIL;
900 // Check if account exists first.
902 sqlite3_stmt *findHandle;
903 sqlite3_stmt *statementHandle;
904 sqlite3_stmt *entriesHandle;
907 resultCode = sqlite3_prepare_v2(db, "SELECT id from calendars WHERE id=(?1);", -1, &findHandle, nullptr);
909 if (resultCode != 0){
910 return CDSCALENDAR_FAILED;
913 resultCode = sqlite3_bind_int(findHandle, 1, calendarID);
915 if (resultCode != 0){
916 return CDSCALENDAR_FAILED;
919 resultCode = sqlite3_step(findHandle);
921 if (resultCode == SQLITE_ROW){
923 } else if (resultCode == SQLITE_DONE) {
924 return CDSCALENDAR_NOCALENDAR;
926 return CDSCALENDAR_FAILED;
929 // Delete the calendar.
931 resultCode = sqlite3_prepare_v2(db, "DELETE FROM calendars WHERE id=(?1);", -1, &statementHandle, nullptr);
933 if (resultCode != 0){
934 return CDSCALENDAR_FAILED;
937 resultCode = sqlite3_bind_int(statementHandle, 1, calendarID);
939 if (resultCode != 0){
940 return CDSCALENDAR_FAILED;
943 resultCode = sqlite3_step(statementHandle);
945 if (resultCode != SQLITE_DONE){
946 return CDSCALENDAR_FAILED;
949 // Delete the calendar entries.
951 resultCode = sqlite3_prepare_v2(db, "DELETE FROM calendarentries WHERE calendarid=(?1);", -1, &entriesHandle, nullptr);
953 if (resultCode != 0){
954 return CDSCALENDAR_FAILED;
957 resultCode = sqlite3_bind_int(entriesHandle, 1, calendarID);
959 if (resultCode != 0){
960 return CDSCALENDAR_FAILED;
963 resultCode = sqlite3_step(entriesHandle);
965 if (resultCode == SQLITE_DONE){
966 deleteResult = CDSCALENDAR_OK;
968 return CDSCALENDAR_FAILED;
971 // Update the checksum.
973 UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
979 CDSAddEntryResult CalendarDataStorage::AddEvent(int calendarID, std::string filename)
982 CDSAddEntryResult addResult;
983 addResult.addEventResult = CDSENTRY_UNITTESTFAIL;
985 // Load the event file.
987 CalendarEventObject eventData;
988 CalendarObjectLoadResult eventLoadResult = eventData.LoadFile(filename);
990 // Check the result of the event file load.
992 switch (eventLoadResult){
994 case CALENDAROBJECTLOAD_OK:
996 case CALENDAROBJECTLOAD_MISSING:
997 addResult.addEventResult = CDSENTRY_MISSINGFILE;
999 case CALENDAROBJECTLOAD_INVALIDFORMAT:
1000 addResult.addEventResult = CDSENTRY_INVALIDFILE;
1002 case CALENDAROBJECTLOAD_CANNOTOPEN:
1003 addResult.addEventResult = CDSENTRY_CANNOTOPENFILE;
1008 // Check if calendar exists first.
1012 sqlite3_stmt *findHandle;
1013 sqlite3_stmt *statementHandle;
1015 resultCode = sqlite3_prepare_v2(db, "SELECT id from calendars WHERE id=(?1);", -1, &findHandle, nullptr);
1017 if (resultCode != 0){
1018 addResult.addEventResult = CDSENTRY_FAILED;
1022 resultCode = sqlite3_bind_int(findHandle, 1, calendarID);
1024 if (resultCode != 0){
1025 addResult.addEventResult = CDSENTRY_FAILED;
1029 resultCode = sqlite3_step(findHandle);
1031 if (resultCode == SQLITE_ROW){
1033 } else if (resultCode == SQLITE_DONE) {
1034 addResult.addEventResult = CDSENTRY_NOCALENDAR;
1037 addResult.addEventResult = CDSENTRY_FAILED;
1041 // Get the required values from the event object.
1043 int eventStartYear = 0;
1044 int eventStartMonth = 0;
1045 int eventStartDay = 0;
1046 int eventStartHour = 0;
1047 int eventStartMinute = 0;
1048 int eventStartSecond = 0;
1049 int eventStartDuration = 0;
1050 std::string eventString = "";
1054 if (eventData.dateTimeStartData.size() < 16){
1056 addResult.addEventResult = CDSENTRY_INVALIDFILE;
1061 eventString = eventData.dateTimeStartData.substr(0,4);
1063 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1065 eventStartYear = atoi(eventString.c_str());
1069 addResult.addEventResult = CDSENTRY_INVALIDFILE;
1074 eventString = eventData.dateTimeStartData.substr(4,2);
1076 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1078 eventStartMonth = atoi(eventString.c_str());
1082 addResult.addEventResult = CDSENTRY_INVALIDFILE;
1087 eventString = eventData.dateTimeStartData.substr(6,2);
1089 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1091 eventStartDay = atoi(eventString.c_str());
1095 addResult.addEventResult = CDSENTRY_INVALIDFILE;
1100 eventString = eventData.dateTimeStartData.substr(9,2);
1102 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1104 eventStartHour = atoi(eventString.c_str());
1108 addResult.addEventResult = CDSENTRY_INVALIDFILE;
1113 eventString = eventData.dateTimeStartData.substr(11,2);
1115 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1117 eventStartMinute = atoi(eventString.c_str());
1121 addResult.addEventResult = CDSENTRY_INVALIDFILE;
1126 eventString = eventData.dateTimeStartData.substr(13,2);
1128 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1130 eventStartSecond = atoi(eventString.c_str());
1134 addResult.addEventResult = CDSENTRY_INVALIDFILE;
1139 //eventYear = eventStartDate.substr(0, 4);
1143 int eventEndYear = 0;
1144 int eventEndMonth = 0;
1145 int eventEndDay = 0;
1146 int eventEndHour = 0;
1147 int eventEndMinute = 0;
1148 int eventEndSecond = 0;
1149 int eventEndDuration = 0;
1151 if (eventData.dateTimeEndData != ""){
1153 if (eventData.dateTimeEndData.size() < 16){
1155 addResult.addEventResult = CDSENTRY_INVALIDFILE;
1160 eventString = eventData.dateTimeEndData.substr(0,4);
1162 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1164 eventEndYear = atoi(eventString.c_str());
1168 addResult.addEventResult = CDSENTRY_INVALIDFILE;
1173 eventString = eventData.dateTimeEndData.substr(4,2);
1175 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1177 eventEndMonth = atoi(eventString.c_str());
1181 addResult.addEventResult = CDSENTRY_INVALIDFILE;
1186 eventString = eventData.dateTimeEndData.substr(6,2);
1188 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1190 eventEndDay = atoi(eventString.c_str());
1194 addResult.addEventResult = CDSENTRY_INVALIDFILE;
1199 eventString = eventData.dateTimeEndData.substr(9,2);
1201 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1203 eventEndHour = atoi(eventString.c_str());
1207 addResult.addEventResult = CDSENTRY_INVALIDFILE;
1212 eventString = eventData.dateTimeEndData.substr(11,2);
1214 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1216 eventEndMinute = atoi(eventString.c_str());
1220 addResult.addEventResult = CDSENTRY_INVALIDFILE;
1225 eventString = eventData.dateTimeEndData.substr(13,2);
1227 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1229 eventEndSecond = atoi(eventString.c_str());
1233 addResult.addEventResult = CDSENTRY_INVALIDFILE;
1240 eventString = eventData.durationData;
1242 // Process the duration data.
1244 int eventDurationWeeks = 0;
1245 int eventDurationDays = 0;
1246 int eventDurationHours = 0;
1247 int eventDurationMinutes = 0;
1248 int eventDurationSeconds = 0;
1250 // Get the duration (if DTEND hasn't been specified).
1252 if (eventData.durationData.size() > 0){
1254 bool FoundP = false;
1255 bool FoundW = false;
1256 bool DateTimeMode = false;
1258 std::string::iterator eventDataChar = eventData.durationData.begin();
1259 std::string currentValue = "";
1261 if (*eventDataChar != 'P'){
1263 eventDataChar = eventData.durationData.end();
1267 for(eventDataChar; eventDataChar != eventData.durationData.end(); eventDataChar++){
1269 // Check if value is a digit.
1271 if (isdigit(*eventDataChar)){
1273 currentValue += *eventDataChar;
1277 // Check that the value matches one of the letters.
1279 if (*eventDataChar == 'W' && DateTimeMode == false){
1281 eventDurationWeeks = atoi(currentValue.c_str());
1283 } else if (*eventDataChar == 'D' && DateTimeMode == false){
1285 eventDurationDays = atoi(currentValue.c_str());
1287 } else if (*eventDataChar == 'T' && DateTimeMode == false){
1289 DateTimeMode = true;
1291 } else if (*eventDataChar == 'H'){
1293 eventDurationHours = atoi(currentValue.c_str());
1295 } else if (*eventDataChar == 'M'){
1297 eventDurationMinutes = atoi(currentValue.c_str());
1299 } else if (*eventDataChar == 'S'){
1301 eventDurationSeconds = atoi(currentValue.c_str());
1305 // Reset the current value.
1315 // Add the calendar entry.
1317 std::string sqlParameter = "INSERT INTO calendarentries (calendarid, entryname, entrydescription,"
1318 " entrystartyear, entrystartmonth, entrystartday, entrystarthour, entrystartminute, entrystartsecond,"
1319 " entryendyear, entryendmonth, entryendday, entryendhour, entryendminute, entryendsecond, "
1320 " entrydurationweek, entrydurationday, entrydurationhour, entrydurationminute, entrydurationsecond, "
1322 " VALUES ((?1), (?2), (?3), (?4), (?5), (?6), (?7), (?8), (?9), (?10), "
1323 " (?11), (?12), (?13), (?14), (?15), (?16), (?17), (?18), (?19), (?20), (?21))";
1325 resultCode = sqlite3_prepare_v2(db, sqlParameter.c_str(), -1, &statementHandle, nullptr);
1327 resultCode = sqlite3_bind_int(statementHandle, 1, calendarID);
1329 if (resultCode != 0){
1330 addResult.addEventResult = CDSENTRY_FAILED;
1334 // Process Entry Name.
1336 resultCode = sqlite3_bind_text(statementHandle, 2, eventData.summaryData.c_str(), -1, SQLITE_STATIC);
1338 if (resultCode != 0){
1339 addResult.addEventResult = CDSENTRY_FAILED;
1343 // Process Entry Description.
1345 string eventDescription;
1348 eventDescription = eventData.descriptionList.at(0);
1351 catch (out_of_range &err){
1352 eventDescription = "";
1355 resultCode = sqlite3_bind_text(statementHandle, 3, eventDescription.c_str(), -1, SQLITE_STATIC);
1357 if (resultCode != 0){
1358 addResult.addEventResult = CDSENTRY_FAILED;
1362 // Process Entry Start Date information.
1364 resultCode = sqlite3_bind_int(statementHandle, 4, eventStartYear);
1366 if (resultCode != 0){
1367 addResult.addEventResult = CDSENTRY_FAILED;
1371 resultCode = sqlite3_bind_int(statementHandle, 5, eventStartMonth);
1373 if (resultCode != 0){
1374 addResult.addEventResult = CDSENTRY_FAILED;
1378 resultCode = sqlite3_bind_int(statementHandle, 6, eventStartDay);
1380 if (resultCode != 0){
1381 addResult.addEventResult = CDSENTRY_FAILED;
1385 resultCode = sqlite3_bind_int(statementHandle, 7, eventStartHour);
1387 if (resultCode != 0){
1388 addResult.addEventResult = CDSENTRY_FAILED;
1392 resultCode = sqlite3_bind_int(statementHandle, 8, eventStartMinute);
1394 if (resultCode != 0){
1395 addResult.addEventResult = CDSENTRY_FAILED;
1399 resultCode = sqlite3_bind_int(statementHandle, 9, eventStartSecond);
1401 if (resultCode != 0){
1402 addResult.addEventResult = CDSENTRY_FAILED;
1406 // Process Entry Start End information.
1408 resultCode = sqlite3_bind_int(statementHandle, 10, eventEndYear);
1410 if (resultCode != 0){
1411 addResult.addEventResult = CDSENTRY_FAILED;
1415 resultCode = sqlite3_bind_int(statementHandle, 11, eventEndMonth);
1417 if (resultCode != 0){
1418 addResult.addEventResult = CDSENTRY_FAILED;
1422 resultCode = sqlite3_bind_int(statementHandle, 12, eventEndDay);
1424 if (resultCode != 0){
1425 addResult.addEventResult = CDSENTRY_FAILED;
1429 resultCode = sqlite3_bind_int(statementHandle, 13, eventEndHour);
1431 if (resultCode != 0){
1432 addResult.addEventResult = CDSENTRY_FAILED;
1436 resultCode = sqlite3_bind_int(statementHandle, 14, eventEndMinute);
1438 if (resultCode != 0){
1439 addResult.addEventResult = CDSENTRY_FAILED;
1443 resultCode = sqlite3_bind_int(statementHandle, 15, eventEndSecond);
1445 if (resultCode != 0){
1446 addResult.addEventResult = CDSENTRY_FAILED;
1450 resultCode = sqlite3_bind_int(statementHandle, 16, eventDurationWeeks);
1452 if (resultCode != 0){
1453 addResult.addEventResult = CDSENTRY_FAILED;
1457 resultCode = sqlite3_bind_int(statementHandle, 17, eventDurationDays);
1459 if (resultCode != 0){
1460 addResult.addEventResult = CDSENTRY_FAILED;
1464 resultCode = sqlite3_bind_int(statementHandle, 18, eventDurationHours);
1466 if (resultCode != 0){
1467 addResult.addEventResult = CDSENTRY_FAILED;
1471 resultCode = sqlite3_bind_int(statementHandle, 19, eventDurationMinutes);
1473 if (resultCode != 0){
1474 addResult.addEventResult = CDSENTRY_FAILED;
1478 resultCode = sqlite3_bind_int(statementHandle, 20, eventDurationSeconds);
1480 if (resultCode != 0){
1481 addResult.addEventResult = CDSENTRY_FAILED;
1485 resultCode = sqlite3_bind_text(statementHandle, 21, filename.c_str(), -1, SQLITE_STATIC);
1487 if (resultCode != 0){
1488 addResult.addEventResult = CDSENTRY_FAILED;
1492 resultCode = sqlite3_step(statementHandle);
1494 if (resultCode != SQLITE_DONE){
1495 addResult.addEventResult = CDSENTRY_FAILED;
1499 addResult.calendarEntryID = sqlite3_last_insert_rowid(db);
1500 addResult.addEventResult = CDSENTRY_OK;
1502 // Update the checksum.
1504 UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
1510 CDSEditEntryResult CalendarDataStorage::UpdateEvent(int eventID, std::string filename)
1513 CDSEditEntryResult editResult;
1514 editResult.editEventResult = CDSENTRY_UNITTESTFAIL;
1516 // Load the event file.
1518 CalendarEventObject eventData;
1519 CalendarObjectLoadResult eventLoadResult = eventData.LoadFile(filename);
1521 // Check the result of the event file load.
1523 switch (eventLoadResult){
1525 case CALENDAROBJECTLOAD_OK:
1527 case CALENDAROBJECTLOAD_MISSING:
1528 editResult.editEventResult = CDSENTRY_MISSINGFILE;
1530 case CALENDAROBJECTLOAD_INVALIDFORMAT:
1531 editResult.editEventResult = CDSENTRY_INVALIDFILE;
1533 case CALENDAROBJECTLOAD_CANNOTOPEN:
1534 editResult.editEventResult = CDSENTRY_CANNOTOPENFILE;
1539 // Check if event exists first.
1543 sqlite3_stmt *findHandle;
1544 sqlite3_stmt *statementHandle;
1546 resultCode = sqlite3_prepare_v2(db, "SELECT id from calendarentries WHERE id=(?1);", -1, &findHandle, nullptr);
1548 if (resultCode != 0){
1549 editResult.editEventResult = CDSENTRY_FAILED;
1553 resultCode = sqlite3_bind_int(findHandle, 1, eventID);
1555 if (resultCode != 0){
1556 editResult.editEventResult = CDSENTRY_FAILED;
1560 resultCode = sqlite3_step(findHandle);
1562 if (resultCode == SQLITE_ROW){
1564 } else if (resultCode == SQLITE_DONE) {
1565 editResult.editEventResult = CDSENTRY_NOENTRY;
1568 editResult.editEventResult = CDSENTRY_FAILED;
1572 // Get the required values from the event object.
1574 int eventStartYear = 0;
1575 int eventStartMonth = 0;
1576 int eventStartDay = 0;
1577 int eventStartHour = 0;
1578 int eventStartMinute = 0;
1579 int eventStartSecond = 0;
1580 int eventStartDuration = 0;
1581 std::string eventString = "";
1585 if (eventData.dateTimeStartData.size() < 16){
1587 editResult.editEventResult = CDSENTRY_INVALIDFILE;
1592 eventString = eventData.dateTimeStartData.substr(0,4);
1594 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1596 eventStartYear = atoi(eventString.c_str());
1600 editResult.editEventResult = CDSENTRY_INVALIDFILE;
1605 eventString = eventData.dateTimeStartData.substr(4,2);
1607 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1609 eventStartMonth = atoi(eventString.c_str());
1613 editResult.editEventResult = CDSENTRY_INVALIDFILE;
1618 eventString = eventData.dateTimeStartData.substr(6,2);
1620 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1622 eventStartDay = atoi(eventString.c_str());
1626 editResult.editEventResult = CDSENTRY_INVALIDFILE;
1631 eventString = eventData.dateTimeStartData.substr(9,2);
1633 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1635 eventStartHour = atoi(eventString.c_str());
1639 editResult.editEventResult = CDSENTRY_INVALIDFILE;
1644 eventString = eventData.dateTimeStartData.substr(11,2);
1646 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1648 eventStartMinute = atoi(eventString.c_str());
1652 editResult.editEventResult = CDSENTRY_INVALIDFILE;
1657 eventString = eventData.dateTimeStartData.substr(13,2);
1659 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1661 eventStartSecond = atoi(eventString.c_str());
1665 editResult.editEventResult = CDSENTRY_INVALIDFILE;
1670 //eventYear = eventStartDate.substr(0, 4);
1674 int eventEndYear = 0;
1675 int eventEndMonth = 0;
1676 int eventEndDay = 0;
1677 int eventEndHour = 0;
1678 int eventEndMinute = 0;
1679 int eventEndSecond = 0;
1680 int eventEndDuration = 0;
1682 if (eventData.dateTimeEndData != ""){
1684 if (eventData.dateTimeEndData.size() < 16){
1686 editResult.editEventResult = CDSENTRY_INVALIDFILE;
1691 eventString = eventData.dateTimeEndData.substr(0,4);
1693 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1695 eventEndYear = atoi(eventString.c_str());
1699 editResult.editEventResult = CDSENTRY_INVALIDFILE;
1704 eventString = eventData.dateTimeEndData.substr(4,2);
1706 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1708 eventEndMonth = atoi(eventString.c_str());
1712 editResult.editEventResult = CDSENTRY_INVALIDFILE;
1717 eventString = eventData.dateTimeEndData.substr(6,2);
1719 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1721 eventEndDay = atoi(eventString.c_str());
1725 editResult.editEventResult = CDSENTRY_INVALIDFILE;
1730 eventString = eventData.dateTimeEndData.substr(9,2);
1732 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1734 eventEndHour = atoi(eventString.c_str());
1738 editResult.editEventResult = CDSENTRY_INVALIDFILE;
1743 eventString = eventData.dateTimeEndData.substr(11,2);
1745 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1747 eventEndMinute = atoi(eventString.c_str());
1751 editResult.editEventResult = CDSENTRY_INVALIDFILE;
1756 eventString = eventData.dateTimeEndData.substr(13,2);
1758 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1760 eventEndSecond = atoi(eventString.c_str());
1764 editResult.editEventResult = CDSENTRY_INVALIDFILE;
1771 eventString = eventData.durationData;
1773 // Process the duration data.
1775 int eventDurationWeeks = 0;
1776 int eventDurationDays = 0;
1777 int eventDurationHours = 0;
1778 int eventDurationMinutes = 0;
1779 int eventDurationSeconds = 0;
1781 // Get the duration (if DTEND hasn't been specified).
1783 if (eventData.durationData.size() > 0){
1785 bool FoundP = false;
1786 bool FoundW = false;
1787 bool DateTimeMode = false;
1789 std::string::iterator eventDataChar = eventData.durationData.begin();
1790 std::string currentValue = "";
1792 if (*eventDataChar != 'P'){
1794 eventDataChar = eventData.durationData.end();
1798 for(eventDataChar; eventDataChar != eventData.durationData.end(); eventDataChar++){
1800 // Check if value is a digit.
1802 if (isdigit(*eventDataChar)){
1804 currentValue += *eventDataChar;
1808 // Check that the value matches one of the letters.
1810 if (*eventDataChar == 'W' && DateTimeMode == false){
1812 eventDurationWeeks = atoi(currentValue.c_str());
1814 } else if (*eventDataChar == 'D' && DateTimeMode == false){
1816 eventDurationDays = atoi(currentValue.c_str());
1818 } else if (*eventDataChar == 'T' && DateTimeMode == false){
1820 DateTimeMode = true;
1822 } else if (*eventDataChar == 'H'){
1824 eventDurationHours = atoi(currentValue.c_str());
1826 } else if (*eventDataChar == 'M'){
1828 eventDurationMinutes = atoi(currentValue.c_str());
1830 } else if (*eventDataChar == 'S'){
1832 eventDurationSeconds = atoi(currentValue.c_str());
1836 // Reset the current value.
1846 // Add the calendar entry.
1848 std::string sqlParameter = "UPDATE calendarentries SET entryname=(?2), entrydescription=(?3),"
1849 " entrystartyear=(?4), entrystartmonth=(?5), entrystartday=(?6), entrystarthour=(?7), entrystartminute=(?8), entrystartsecond=(?9),"
1850 " entryendyear=(?10), entryendmonth=(?11), entryendday=(?12), entryendhour=(?13), entryendminute=(?14), entryendsecond=(?15), "
1851 " entrydurationweek=(?16), entrydurationday=(?17), entrydurationhour=(?18), entrydurationminute=(?19), entrydurationsecond=(?20), "
1854 resultCode = sqlite3_prepare_v2(db, sqlParameter.c_str(), -1, &statementHandle, nullptr);
1856 resultCode = sqlite3_bind_int(statementHandle, 1, eventID);
1858 if (resultCode != 0){
1859 editResult.editEventResult = CDSENTRY_FAILED;
1863 // Process Entry Name.
1865 resultCode = sqlite3_bind_text(statementHandle, 2, eventData.summaryData.c_str(), -1, SQLITE_STATIC);
1867 if (resultCode != 0){
1868 editResult.editEventResult = CDSENTRY_FAILED;
1872 // Process Entry Description.
1874 string eventDescription;
1877 eventDescription = eventData.descriptionList.at(0);
1880 catch (out_of_range &err){
1881 eventDescription = "";
1884 resultCode = sqlite3_bind_text(statementHandle, 3, eventDescription.c_str(), -1, SQLITE_STATIC);
1886 if (resultCode != 0){
1887 editResult.editEventResult = CDSENTRY_FAILED;
1891 // Process Entry Start Date information.
1893 resultCode = sqlite3_bind_int(statementHandle, 4, eventStartYear);
1895 if (resultCode != 0){
1896 editResult.editEventResult = CDSENTRY_FAILED;
1900 resultCode = sqlite3_bind_int(statementHandle, 5, eventStartMonth);
1902 if (resultCode != 0){
1903 editResult.editEventResult = CDSENTRY_FAILED;
1907 resultCode = sqlite3_bind_int(statementHandle, 6, eventStartDay);
1909 if (resultCode != 0){
1910 editResult.editEventResult = CDSENTRY_FAILED;
1914 resultCode = sqlite3_bind_int(statementHandle, 7, eventStartHour);
1916 if (resultCode != 0){
1917 editResult.editEventResult = CDSENTRY_FAILED;
1921 resultCode = sqlite3_bind_int(statementHandle, 8, eventStartMinute);
1923 if (resultCode != 0){
1924 editResult.editEventResult = CDSENTRY_FAILED;
1928 resultCode = sqlite3_bind_int(statementHandle, 9, eventStartSecond);
1930 if (resultCode != 0){
1931 editResult.editEventResult = CDSENTRY_FAILED;
1935 // Process Entry Start End information.
1937 resultCode = sqlite3_bind_int(statementHandle, 10, eventEndYear);
1939 if (resultCode != 0){
1940 editResult.editEventResult = CDSENTRY_FAILED;
1944 resultCode = sqlite3_bind_int(statementHandle, 11, eventEndMonth);
1946 if (resultCode != 0){
1947 editResult.editEventResult = CDSENTRY_FAILED;
1951 resultCode = sqlite3_bind_int(statementHandle, 12, eventEndDay);
1953 if (resultCode != 0){
1954 editResult.editEventResult = CDSENTRY_FAILED;
1958 resultCode = sqlite3_bind_int(statementHandle, 13, eventEndHour);
1960 if (resultCode != 0){
1961 editResult.editEventResult = CDSENTRY_FAILED;
1965 resultCode = sqlite3_bind_int(statementHandle, 14, eventEndMinute);
1967 if (resultCode != 0){
1968 editResult.editEventResult = CDSENTRY_FAILED;
1972 resultCode = sqlite3_bind_int(statementHandle, 15, eventEndSecond);
1974 if (resultCode != 0){
1975 editResult.editEventResult = CDSENTRY_FAILED;
1979 resultCode = sqlite3_bind_int(statementHandle, 16, eventDurationWeeks);
1981 if (resultCode != 0){
1982 editResult.editEventResult = CDSENTRY_FAILED;
1986 resultCode = sqlite3_bind_int(statementHandle, 17, eventDurationDays);
1988 if (resultCode != 0){
1989 editResult.editEventResult = CDSENTRY_FAILED;
1993 resultCode = sqlite3_bind_int(statementHandle, 18, eventDurationHours);
1995 if (resultCode != 0){
1996 editResult.editEventResult = CDSENTRY_FAILED;
2000 resultCode = sqlite3_bind_int(statementHandle, 19, eventDurationMinutes);
2002 if (resultCode != 0){
2003 editResult.editEventResult = CDSENTRY_FAILED;
2007 resultCode = sqlite3_bind_int(statementHandle, 20, eventDurationSeconds);
2009 if (resultCode != 0){
2010 editResult.editEventResult = CDSENTRY_FAILED;
2014 resultCode = sqlite3_step(statementHandle);
2016 if (resultCode != SQLITE_DONE){
2017 editResult.editEventResult = CDSENTRY_FAILED;
2021 editResult.calendarEntryID = sqlite3_last_insert_rowid(db);
2022 editResult.editEventResult = CDSENTRY_OK;
2024 // Update the checksum.
2026 UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
2032 CDSGetCalendarEntryInfo CalendarDataStorage::GetEvent(int calendarEntryID)
2035 CDSGetCalendarEntryInfo entryResult;
2037 // Check if the calendar entry exists.
2041 sqlite3_stmt *findHandle;
2042 sqlite3_stmt *statementHandle;
2044 resultCode = sqlite3_prepare_v2(db, "SELECT id FROM calendarentries WHERE id=(?1);", -1, &findHandle, nullptr);
2046 if (resultCode != 0){
2047 entryResult.getEventResult = CDSENTRY_FAILED;
2051 resultCode = sqlite3_bind_int(findHandle, 1, calendarEntryID);
2053 if (resultCode != 0){
2054 entryResult.getEventResult = CDSENTRY_FAILED;
2058 resultCode = sqlite3_step(findHandle);
2060 if (resultCode == SQLITE_ROW){
2062 } else if (resultCode == SQLITE_DONE) {
2063 entryResult.getEventResult = CDSENTRY_NOENTRY;
2066 entryResult.getEventResult = CDSENTRY_FAILED;
2070 // Get the calendar entry data.
2072 std::string sqlParameter = "SELECT entryname, entrydescription,"
2073 " entrystartyear, entrystartmonth, entrystartday, entrystarthour, entrystartminute, entrystartsecond,"
2074 " entryendyear, entryendmonth, entryendday, entryendhour, entryendminute, entryendsecond, "
2075 " entrydurationweek, entrydurationday, entrydurationhour, entrydurationminute, entrydurationsecond, "
2076 " calendarid, id, filename"
2077 " FROM calendarentries WHERE id=(?1)";
2079 resultCode = sqlite3_prepare_v2(db, sqlParameter.c_str(), -1, &statementHandle, nullptr);
2081 if (resultCode != 0){
2082 entryResult.getEventResult = CDSENTRY_FAILED;
2086 resultCode = sqlite3_bind_int(statementHandle, 1, calendarEntryID);
2088 if (resultCode != 0){
2089 entryResult.getEventResult = CDSENTRY_FAILED;
2093 resultCode = sqlite3_step(statementHandle);
2095 if (resultCode == SQLITE_ROW){
2097 // Get the calendar entry name,
2099 stringstream entryStream;
2101 entryStream << sqlite3_column_text(statementHandle, 0);
2102 entryResult.entryName = entryStream.str();
2104 entryStream.str("");
2106 // Get the calendar entry description.
2108 entryStream << sqlite3_column_text(statementHandle, 1);
2109 entryResult.entryDescription = entryStream.str();
2111 entryStream.str("");
2113 // Get the calendar entry filename.
2115 entryStream << sqlite3_column_text(statementHandle, 21);
2116 entryResult.entryFilename = entryStream.str();
2118 entryStream.str("");
2120 entryResult.entryStartYear = sqlite3_column_int(statementHandle, 2);
2121 entryResult.entryStartMonth = sqlite3_column_int(statementHandle, 3);
2122 entryResult.entryStartDay = sqlite3_column_int(statementHandle, 4);
2123 entryResult.entryStartHour = sqlite3_column_int(statementHandle, 5);
2124 entryResult.entryStartMinute = sqlite3_column_int(statementHandle, 6);
2125 entryResult.entryStartSecond = sqlite3_column_int(statementHandle, 7);
2126 entryResult.entryEndYear = sqlite3_column_int(statementHandle, 8);
2127 entryResult.entryEndMonth = sqlite3_column_int(statementHandle, 9);
2128 entryResult.entryEndDay = sqlite3_column_int(statementHandle, 10);
2129 entryResult.entryEndHour = sqlite3_column_int(statementHandle, 11);
2130 entryResult.entryEndMinute = sqlite3_column_int(statementHandle, 12);
2131 entryResult.entryEndSecond = sqlite3_column_int(statementHandle, 13);
2132 entryResult.entryDurationWeeks = sqlite3_column_int(statementHandle, 14);
2133 entryResult.entryDurationDays = sqlite3_column_int(statementHandle, 15);
2134 entryResult.entryDurationHours = sqlite3_column_int(statementHandle, 16);
2135 entryResult.entryDurationMinutes = sqlite3_column_int(statementHandle, 17);
2136 entryResult.entryDurationSeconds = sqlite3_column_int(statementHandle, 18);
2137 entryResult.calendarID = sqlite3_column_int(statementHandle, 19);
2138 entryResult.calendarEntryID = sqlite3_column_int(statementHandle, 20);
2140 } else if (resultCode == SQLITE_DONE) {
2141 entryResult.getEventResult = CDSENTRY_NOCALENDAR;
2144 entryResult.getEventResult = CDSENTRY_FAILED;
2148 entryResult.getEventResult = CDSENTRY_OK;
2154 CDSEntryResult CalendarDataStorage::DeleteEvent(int calendarEntryID)
2157 CDSEntryResult deleteResult = CDSENTRY_UNITTESTFAIL;
2159 // Check if the calendar entry exists.
2163 sqlite3_stmt *findHandle;
2164 sqlite3_stmt *statementHandle;
2166 resultCode = sqlite3_prepare_v2(db, "SELECT id FROM calendarentries WHERE id=(?1);", -1, &findHandle, nullptr);
2168 if (resultCode != 0){
2169 return CDSENTRY_FAILED;
2172 resultCode = sqlite3_bind_int(findHandle, 1, calendarEntryID);
2174 if (resultCode != 0){
2175 return CDSENTRY_FAILED;
2178 resultCode = sqlite3_step(findHandle);
2180 if (resultCode == SQLITE_ROW){
2182 } else if (resultCode == SQLITE_DONE) {
2183 return CDSENTRY_NOENTRY;
2185 return CDSENTRY_FAILED;
2188 // Delete the account.
2190 resultCode = sqlite3_prepare_v2(db, "DELETE FROM calendarentries WHERE id=(?1);", -1, &statementHandle, nullptr);
2192 if (resultCode != 0){
2193 return CDSENTRY_FAILED;
2196 resultCode = sqlite3_bind_int(statementHandle, 1, calendarEntryID);
2198 if (resultCode != 0){
2199 return CDSENTRY_FAILED;
2202 resultCode = sqlite3_step(statementHandle);
2204 if (resultCode == SQLITE_DONE){
2205 deleteResult = CDSENTRY_OK;
2207 return CDSENTRY_FAILED;
2210 // Update the checksum.
2212 UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
2214 return deleteResult;
2218 CDSEntryList CalendarDataStorage::GetEventList(int calendarID){
2220 CDSEntryList entryList;
2221 entryList.getEventListResult = CDSENTRY_UNITTESTFAIL;
2223 // Check if calendar exists first.
2227 sqlite3_stmt *findHandle;
2228 sqlite3_stmt *calendarHandle;
2230 resultCode = sqlite3_prepare_v2(db, "SELECT id from calendars WHERE id=(?1);", -1, &findHandle, nullptr);
2232 if (resultCode != 0){
2233 entryList.getEventListResult = CDSENTRY_FAILED;
2237 resultCode = sqlite3_bind_int(findHandle, 1, calendarID);
2239 if (resultCode != 0){
2240 entryList.getEventListResult = CDSENTRY_FAILED;
2244 resultCode = sqlite3_step(findHandle);
2246 if (resultCode == SQLITE_ROW){
2248 } else if (resultCode == SQLITE_DONE) {
2249 entryList.getEventListResult = CDSENTRY_NOCALENDAR;
2252 entryList.getEventListResult = CDSENTRY_FAILED;
2256 // Get the list of entry IDs.
2258 resultCode = sqlite3_prepare_v2(db, "SELECT id FROM calendarentries WHERE calendarid=(?1);", -1, &calendarHandle, nullptr);
2260 if (resultCode != 0){
2261 entryList.getEventListResult = CDSENTRY_FAILED;
2265 resultCode = sqlite3_bind_int(calendarHandle, 1, calendarID);
2267 if (resultCode != 0){
2268 entryList.getEventListResult = CDSENTRY_FAILED;
2272 resultCode = sqlite3_step(calendarHandle);
2274 if (resultCode == SQLITE_DONE || resultCode == SQLITE_ROW){
2276 entryList.getEventListResult = CDSENTRY_FAILED;
2280 while (resultCode == SQLITE_ROW){
2282 int calendarID = sqlite3_column_int(calendarHandle, 0);
2284 entryList.entryList.push_back(calendarID);
2286 resultCode = sqlite3_step(calendarHandle);
2290 entryList.getEventListResult = CDSENTRY_OK;
2296 CDSEntryList CalendarDataStorage::GetEventListByDate(int calendarYear, int calendarMonth, int calendarDay){
2298 CDSEntryList entryList;
2299 entryList.getEventListResult = CDSENTRY_UNITTESTFAIL;
2301 // Check if calendar exists first.
2305 sqlite3_stmt *calendarHandle;
2307 // Get the list of entry IDs.
2309 resultCode = sqlite3_prepare_v2(db, "SELECT id FROM calendarentries WHERE entrystartyear=(?1) AND entrystartmonth=(?2) AND entrystartday=(?3);", -1, &calendarHandle, nullptr);
2311 if (resultCode != 0){
2312 entryList.getEventListResult = CDSENTRY_FAILED;
2316 resultCode = sqlite3_bind_int(calendarHandle, 1, calendarYear);
2318 if (resultCode != 0){
2319 entryList.getEventListResult = CDSENTRY_FAILED;
2323 resultCode = sqlite3_bind_int(calendarHandle, 2, calendarMonth);
2325 if (resultCode != 0){
2326 entryList.getEventListResult = CDSENTRY_FAILED;
2330 resultCode = sqlite3_bind_int(calendarHandle, 3, calendarDay);
2332 if (resultCode != 0){
2333 entryList.getEventListResult = CDSENTRY_FAILED;
2337 resultCode = sqlite3_step(calendarHandle);
2339 if (resultCode == SQLITE_DONE || resultCode == SQLITE_ROW){
2341 entryList.getEventListResult = CDSENTRY_FAILED;
2345 while (resultCode == SQLITE_ROW){
2347 int calendarID = sqlite3_column_int(calendarHandle, 0);
2349 entryList.entryList.push_back(calendarID);
2351 resultCode = sqlite3_step(calendarHandle);
2355 entryList.getEventListResult = CDSENTRY_OK;
2361 CDSCalendarList CalendarDataStorage::GetCalendarList(int accountID){
2363 CDSCalendarList calendarList;
2364 calendarList.getCalendarListResult = CDSCALENDAR_UNITTESTFAIL;
2366 // Check if calendar exists first.
2370 sqlite3_stmt *findHandle;
2371 sqlite3_stmt *calendarHandle;
2373 resultCode = sqlite3_prepare_v2(db, "SELECT id from accounts WHERE id=(?1);", -1, &findHandle, nullptr);
2375 if (resultCode != 0){
2376 calendarList.getCalendarListResult = CDSCALENDAR_FAILED;
2377 return calendarList;
2380 resultCode = sqlite3_bind_int(findHandle, 1, accountID);
2382 if (resultCode != 0){
2383 calendarList.getCalendarListResult = CDSCALENDAR_FAILED;
2384 return calendarList;
2387 resultCode = sqlite3_step(findHandle);
2389 if (resultCode == SQLITE_ROW){
2391 } else if (resultCode == SQLITE_DONE) {
2392 calendarList.getCalendarListResult = CDSCALENDAR_NOCALENDAR;
2393 return calendarList;
2395 calendarList.getCalendarListResult = CDSCALENDAR_FAILED;
2396 return calendarList;
2399 // Get the list of entry IDs.
2401 resultCode = sqlite3_prepare_v2(db, "SELECT id, calendarid FROM calendars WHERE accountid=(?1);", -1, &calendarHandle, nullptr);
2403 if (resultCode != 0){
2404 calendarList.getCalendarListResult = CDSCALENDAR_FAILED;
2405 return calendarList;
2408 resultCode = sqlite3_bind_int(calendarHandle, 1, accountID);
2410 if (resultCode != 0){
2411 calendarList.getCalendarListResult = CDSCALENDAR_FAILED;
2412 return calendarList;
2415 resultCode = sqlite3_step(calendarHandle);
2417 if (resultCode == SQLITE_DONE || resultCode == SQLITE_ROW){
2419 calendarList.getCalendarListResult = CDSCALENDAR_FAILED;
2420 return calendarList;
2423 while (resultCode == SQLITE_ROW){
2425 int calendarID = sqlite3_column_int(calendarHandle, 0);
2427 stringstream calendarStream;
2429 calendarStream << sqlite3_column_text(calendarHandle, 1);
2431 calendarList.calendarList.push_back(calendarID);
2432 calendarList.calendarListTextID.push_back(calendarStream.str());
2434 calendarStream.str("");
2436 resultCode = sqlite3_step(calendarHandle);
2440 calendarList.getCalendarListResult = CDSCALENDAR_OK;
2442 return calendarList;
2446 CDSChecksumResult CalendarDataStorage::AddChecksum(string checksumName, string checksumValue){
2450 // Check if the checksum already exists.
2452 sqlite3_stmt *findHandle;
2453 sqlite3_stmt *checksumHandle;
2455 resultCode = sqlite3_prepare_v2(db, "SELECT checksumvalue from checksums WHERE checksumname=(?1);", -1, &findHandle, nullptr);
2457 if (resultCode != 0){
2458 return CDSCHECKSUM_FAILED;
2461 resultCode = sqlite3_bind_text(findHandle, 1, checksumName.c_str(), -1, SQLITE_STATIC);
2463 if (resultCode != 0){
2464 return CDSCHECKSUM_FAILED;
2467 resultCode = sqlite3_step(findHandle);
2469 if (resultCode == SQLITE_ROW){
2470 return CDSCHECKSUM_CHECKSUMALREADYEXISTS;
2471 } else if (resultCode == SQLITE_DONE) {
2474 return CDSCHECKSUM_FAILED;
2477 // Add the checksum to the checksum table.
2479 resultCode = sqlite3_prepare_v2(db, "INSERT INTO checksums (checksumname, checksumvalue) VALUES(?1, ?2);", -1, &checksumHandle, nullptr);
2481 if (resultCode != 0){
2482 return CDSCHECKSUM_FAILED;
2485 resultCode = sqlite3_bind_text(checksumHandle, 1, checksumName.c_str(), -1, SQLITE_STATIC);
2487 if (resultCode != 0){
2488 return CDSCHECKSUM_FAILED;
2491 resultCode = sqlite3_bind_text(checksumHandle, 2, checksumValue.c_str(), -1, SQLITE_STATIC);
2493 if (resultCode != 0){
2494 return CDSCHECKSUM_FAILED;
2497 resultCode = sqlite3_step(checksumHandle);
2499 if (resultCode != SQLITE_DONE){
2500 return CDSCHECKSUM_FAILED;
2503 return CDSCHECKSUM_OK;
2507 CDSGetChecksumResult CalendarDataStorage::GetChecksum(string checksumName){
2509 CDSGetChecksumResult getChecksumResult;
2513 // Check if the checksum already exists.
2515 sqlite3_stmt *getHandle;
2517 resultCode = sqlite3_prepare_v2(db, "SELECT checksumvalue from checksums WHERE checksumname=(?1);", -1, &getHandle, nullptr);
2519 if (resultCode != 0){
2520 getChecksumResult.getChecksumResult = CDSCHECKSUM_FAILED;
2521 return getChecksumResult;
2524 resultCode = sqlite3_bind_text(getHandle, 1, checksumName.c_str(), -1, SQLITE_STATIC);
2526 if (resultCode != 0){
2527 getChecksumResult.getChecksumResult = CDSCHECKSUM_FAILED;
2528 return getChecksumResult;
2531 resultCode = sqlite3_step(getHandle);
2533 if (resultCode == SQLITE_ROW){
2534 } else if (resultCode == SQLITE_DONE) {
2535 getChecksumResult.getChecksumResult = CDSCHECKSUM_NOHASH;
2536 return getChecksumResult;
2538 getChecksumResult.getChecksumResult = CDSCHECKSUM_FAILED;
2539 return getChecksumResult;
2542 stringstream checksumStream;
2544 checksumStream << sqlite3_column_text(getHandle, 0);
2546 getChecksumResult.checksumValue = checksumStream.str();
2548 getChecksumResult.getChecksumResult = CDSCHECKSUM_OK;
2550 return getChecksumResult;
2554 CDSChecksumResult CalendarDataStorage::UpdateChecksum(std::string checksumName, std::string checksumValue){
2558 // Check if the checksum already exists.
2560 sqlite3_stmt *getHandle;
2561 sqlite3_stmt *statementHandle;
2563 resultCode = sqlite3_prepare_v2(db, "SELECT checksumvalue from checksums WHERE checksumname=(?1);", -1, &getHandle, nullptr);
2565 if (resultCode != 0){
2566 return CDSCHECKSUM_FAILED;
2569 resultCode = sqlite3_bind_text(getHandle, 1, checksumName.c_str(), -1, SQLITE_STATIC);
2571 if (resultCode != 0){
2572 return CDSCHECKSUM_FAILED;
2575 resultCode = sqlite3_step(getHandle);
2577 if (resultCode == SQLITE_ROW){
2578 } else if (resultCode == SQLITE_DONE) {
2579 return CDSCHECKSUM_NOHASH;
2581 return CDSCHECKSUM_FAILED;
2584 // Update the checksum.
2586 resultCode = sqlite3_prepare_v2(db, "UPDATE checksums SET checksumvalue=(?1) WHERE checksumname=(?2);", -1, &statementHandle, nullptr);
2588 if (resultCode != 0){
2589 return CDSCHECKSUM_FAILED;
2592 resultCode = sqlite3_bind_text(statementHandle, 1, checksumValue.c_str(), -1, SQLITE_STATIC);
2594 if (resultCode != 0){
2595 return CDSCHECKSUM_FAILED;
2598 resultCode = sqlite3_bind_text(statementHandle, 2, checksumName.c_str(), -1, SQLITE_STATIC);
2600 if (resultCode != 0){
2601 return CDSCHECKSUM_FAILED;
2604 resultCode = sqlite3_step(statementHandle);
2606 if (resultCode != SQLITE_DONE){
2607 return CDSCHECKSUM_FAILED;
2610 return CDSCHECKSUM_OK;
2614 CDSCleanupResult CalendarDataStorage::Clear(){
2616 // Remove all data from the tables and reset the sequence numbers.
2619 sqlite3_stmt *statementHandle;
2621 resultCode = sqlite3_prepare_v2(db, "DELETE FROM calendarentries", - 1, &statementHandle, nullptr);
2623 if (resultCode != 0){
2624 cout << "Fail 1" << endl;
2625 return CDSCLEANUP_FAILED;
2628 resultCode = sqlite3_step(statementHandle);
2630 if (resultCode != SQLITE_DONE){
2631 cout << "Fail 2" << endl;
2632 return CDSCLEANUP_FAILED;
2635 resultCode = sqlite3_prepare_v2(db, "DELETE FROM sqlite_sequence WHERE name='calendarentries';", -1, &statementHandle, nullptr);
2637 if (resultCode != 0){
2638 cout << "Fail 3" << endl;
2639 cout << sqlite3_errmsg(db) << endl;
2640 return CDSCLEANUP_FAILED;
2643 resultCode = sqlite3_step(statementHandle);
2645 if (resultCode != SQLITE_DONE){
2646 cout << "Fail 4" << endl;
2647 return CDSCLEANUP_FAILED;
2650 resultCode = sqlite3_prepare_v2(db, "DELETE FROM calendars", -1, &statementHandle, nullptr);
2652 if (resultCode != 0){
2653 cout << "Fail 5" << endl;
2654 return CDSCLEANUP_FAILED;
2657 resultCode = sqlite3_step(statementHandle);
2659 if (resultCode != SQLITE_DONE){
2660 cout << "Fail 6" << endl;
2661 return CDSCLEANUP_FAILED;
2664 resultCode = sqlite3_prepare_v2(db, "DELETE FROM sqlite_sequence WHERE name='calendars';", -1, &statementHandle, nullptr);
2666 if (resultCode != 0){
2667 cout << "Fail 7" << endl;
2668 return CDSCLEANUP_FAILED;
2671 resultCode = sqlite3_step(statementHandle);
2673 if (resultCode != SQLITE_DONE){
2674 cout << "Fail 8" << endl;
2675 return CDSCLEANUP_FAILED;
2678 resultCode = sqlite3_prepare_v2(db, "DELETE FROM accounts", -1, &statementHandle, nullptr);
2680 if (resultCode != 0){
2681 cout << "Fail 9" << endl;
2682 return CDSCLEANUP_FAILED;
2685 resultCode = sqlite3_step(statementHandle);
2687 if (resultCode != SQLITE_DONE){
2688 cout << "Fail 10" << endl;
2689 return CDSCLEANUP_FAILED;
2692 resultCode = sqlite3_prepare_v2(db, "DELETE FROM sqlite_sequence WHERE name='accounts'", -1, &statementHandle, nullptr);
2694 if (resultCode != 0){
2695 cout << "Fail 11" << endl;
2696 return CDSCLEANUP_FAILED;
2699 resultCode = sqlite3_step(statementHandle);
2701 if (resultCode != SQLITE_DONE){
2702 cout << "Fail 12" << endl;
2703 return CDSCLEANUP_FAILED;
2706 // Update the checksum.
2708 UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
2710 return CDSCLEANUP_OK;