1 #include "CalendarDataStorage.h"
3 #define CDS_RANDOMPOW 24
9 static int callback(void *NotUsed, int argc, char **argv, char **azColName){
13 CalendarDataStorage::CalendarDataStorage(){
15 // Initialise the SQLite database.
17 sqlite3_open_v2(":memory:", &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_FULLMUTEX, nullptr);
22 CalendarDataStorage::~CalendarDataStorage(){
24 // Destory the SQLite database.
30 void CalendarDataStorage::SetupTables(){
32 // Setup the tables for the Calendar Data Storage.
34 char *setupTablesErrMsg = nullptr;
37 // Setup the accounts table.
39 const char *accountsTableString;
40 accountsTableString = "CREATE TABLE accounts(id INTEGER PRIMARY KEY "
44 resultCode = sqlite3_exec(db, accountsTableString, callback, nullptr, &setupTablesErrMsg);
47 DataStorageInitOK = false;
51 // Setup the calendars table.
53 const char *calendarTableString;
54 calendarTableString = "CREATE TABLE calendars(id INTEGER PRIMARY KEY "
61 resultCode = sqlite3_exec(db, calendarTableString, callback, nullptr, &setupTablesErrMsg);
64 DataStorageInitOK = false;
68 // Setup the calendar entries table.
70 const char *calendarentriesTableString;
71 calendarentriesTableString = "CREATE TABLE calendarentries(id INTEGER PRIMARY KEY "
72 ", calendarid INTEGER"
74 ", entrydescription TEXT"
75 ", entrystartyear INTEGER"
76 ", entrystartmonth INTEGER"
77 ", entrystartday INTEGER"
78 ", entrystarthour INTEGER"
79 ", entrystartminute INTEGER"
80 ", entrystartsecond INTEGER"
81 ", entryendyear INTEGER"
82 ", entryendmonth INTEGER"
83 ", entryendday INTEGER"
84 ", entryendhour INTEGER"
85 ", entryendminute INTEGER"
86 ", entryendsecond INTEGER"
87 ", entrydurationweek INTEGER"
88 ", entrydurationday INTEGER"
89 ", entrydurationhour INTEGER"
90 ", entrydurationminute INTEGER"
91 ", entrydurationsecond INTEGER"
94 resultCode = sqlite3_exec(db, calendarentriesTableString, callback, nullptr, &setupTablesErrMsg);
97 DataStorageInitOK = false;
101 // Setup the checksums table.
103 const char *checksumsTableString;
104 checksumsTableString = "CREATE TABLE checksums(checksumname TEXT PRIMARY KEY "
105 ", checksumvalue TEXT);";
107 resultCode = sqlite3_exec(db, checksumsTableString, callback, nullptr, &setupTablesErrMsg);
109 if (resultCode != 0){
110 DataStorageInitOK = false;
114 // Setup internal checksums.
116 CDSChecksumResult addChecksum = AddChecksum("internal_updatedata", "");
118 if (addChecksum != CDSCHECKSUM_OK){
119 DataStorageInitOK = false;
123 DataStorageInitOK = true;
127 bool CalendarDataStorage::DidInitOK()
130 return DataStorageInitOK;
134 CDSAccountResult CalendarDataStorage::AddAccount(string accountName)
137 CDSAccountResult addResult = CDSACCOUNT_UNITTESTFAIL;
140 sqlite3_stmt *statementHandle;
142 resultCode = sqlite3_prepare_v2(db, "INSERT INTO accounts (name) VALUES(?1);", -1, &statementHandle, nullptr);
144 if (resultCode != 0){
145 return CDSACCOUNT_FAILED;
148 resultCode = sqlite3_bind_text(statementHandle, 1, accountName.c_str(), -1, SQLITE_STATIC);
150 if (resultCode != 0){
151 return CDSACCOUNT_FAILED;
154 resultCode = sqlite3_step(statementHandle);
156 if (resultCode != SQLITE_DONE){
157 return CDSACCOUNT_FAILED;
160 // Update the checksum.
162 UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
164 addResult = CDSACCOUNT_OK;
170 CDSAccountList CalendarDataStorage::GetAccountList()
173 CDSAccountList accountList;
175 // Check if calendar exists first.
179 sqlite3_stmt *statementHandle;
181 resultCode = sqlite3_prepare_v2(db, "SELECT id, name from accounts;", -1, &statementHandle, nullptr);
183 if (resultCode != 0){
184 accountList.getAccountListResult = CDSACCOUNT_FAILED;
188 if (resultCode != 0){
189 accountList.getAccountListResult = CDSACCOUNT_FAILED;
193 resultCode = sqlite3_step(statementHandle);
195 if (resultCode == SQLITE_ROW){
197 } else if (resultCode == SQLITE_DONE) {
198 accountList.getAccountListResult = CDSACCOUNT_NOACCOUNT;
201 accountList.getAccountListResult = CDSACCOUNT_FAILED;
205 while (resultCode == SQLITE_ROW){
207 CDSGetAccountInfo accountInfo;
209 accountInfo.accountID = sqlite3_column_int(statementHandle, 0);
211 stringstream calendarStream;
213 calendarStream << sqlite3_column_text(statementHandle, 1);
215 accountInfo.accountName = calendarStream.str();
216 accountInfo.accountInfoResult = CDSACCOUNT_OK;
218 accountList.accountList.push_back(accountInfo);
220 resultCode = sqlite3_step(statementHandle);
224 accountList.getAccountListResult = CDSACCOUNT_OK;
230 CDSGetAccountInfo CalendarDataStorage::GetAccount(string accountName)
233 CDSGetAccountInfo accountInfo;
234 accountInfo.accountInfoResult = CDSACCOUNT_UNITTESTFAIL;
237 sqlite3_stmt *statementHandle;
239 resultCode = sqlite3_prepare_v2(db, "SELECT name, id FROM accounts WHERE name=(?1);", -1, &statementHandle, nullptr);
241 if (resultCode != 0){
242 accountInfo.accountInfoResult = CDSACCOUNT_FAILED;
246 resultCode = sqlite3_bind_text(statementHandle, 1, accountName.c_str(), -1, SQLITE_STATIC);
248 if (resultCode != 0){
249 accountInfo.accountInfoResult = CDSACCOUNT_FAILED;
253 resultCode = sqlite3_step(statementHandle);
255 if (resultCode == SQLITE_DONE){
257 accountInfo.accountInfoResult = CDSACCOUNT_NOACCOUNT;
260 } else if (resultCode == SQLITE_ROW){
262 // Get the result data.
264 stringstream accountNameStream;
266 accountNameStream << sqlite3_column_text(statementHandle, 0);
268 accountInfo.accountName = accountNameStream.str();
269 accountInfo.accountID = sqlite3_column_int(statementHandle, 1);
271 accountInfo.accountInfoResult = CDSACCOUNT_OK;
276 accountInfo.accountInfoResult = CDSACCOUNT_FAILED;
285 CDSAccountResult CalendarDataStorage::UpdateAccount(int accountID, string accountName)
288 CDSAccountResult updateResult = CDSACCOUNT_UNITTESTFAIL;
291 sqlite3_stmt *findHandle;
292 sqlite3_stmt *statementHandle;
294 // Check if account exists first.
296 resultCode = sqlite3_prepare_v2(db, "SELECT id from accounts WHERE id=(?1);", -1, &findHandle, nullptr);
298 if (resultCode != 0){
299 return CDSACCOUNT_FAILED;
302 resultCode = sqlite3_bind_int(findHandle, 1, accountID);
304 if (resultCode != 0){
305 return CDSACCOUNT_FAILED;
308 resultCode = sqlite3_step(findHandle);
310 if (resultCode == SQLITE_ROW){
312 } else if (resultCode == SQLITE_DONE) {
313 return CDSACCOUNT_NOACCOUNT;
315 return CDSACCOUNT_FAILED;
318 // Update the account.
320 resultCode = sqlite3_prepare_v2(db, "UPDATE accounts SET name=(?1) WHERE id=(?2);", -1, &statementHandle, nullptr);
322 if (resultCode != 0){
323 return CDSACCOUNT_FAILED;
326 resultCode = sqlite3_bind_text(statementHandle, 1, accountName.c_str(), -1, SQLITE_STATIC);
328 if (resultCode != 0){
329 return CDSACCOUNT_FAILED;
332 resultCode = sqlite3_bind_int(statementHandle, 2, accountID);
334 if (resultCode != 0){
335 return CDSACCOUNT_FAILED;
338 resultCode = sqlite3_step(statementHandle);
340 if (resultCode != SQLITE_DONE){
341 return CDSACCOUNT_FAILED;
344 // Update the checksum.
346 UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
348 updateResult = CDSACCOUNT_OK;
354 CDSAccountResult CalendarDataStorage::DeleteAccount(int accountID)
357 CDSAccountResult deleteResult = CDSACCOUNT_UNITTESTFAIL;
359 // Check if account exists first.
361 sqlite3_stmt *findHandle;
362 sqlite3_stmt *statementHandle;
363 sqlite3_stmt *calendarHandle;
366 resultCode = sqlite3_prepare_v2(db, "SELECT id from accounts WHERE id=(?1);", -1, &findHandle, nullptr);
368 if (resultCode != 0){
369 return CDSACCOUNT_FAILED;
372 resultCode = sqlite3_bind_int(findHandle, 1, accountID);
374 if (resultCode != 0){
375 return CDSACCOUNT_FAILED;
378 resultCode = sqlite3_step(findHandle);
380 if (resultCode == SQLITE_ROW){
382 } else if (resultCode == SQLITE_DONE) {
383 return CDSACCOUNT_NOACCOUNT;
385 return CDSACCOUNT_FAILED;
388 // Delete the account.
390 resultCode = sqlite3_prepare_v2(db, "DELETE FROM accounts WHERE id=(?1);", -1, &statementHandle, nullptr);
392 if (resultCode != 0){
393 return CDSACCOUNT_FAILED;
396 resultCode = sqlite3_bind_int(statementHandle, 1, accountID);
398 if (resultCode != 0){
399 return CDSACCOUNT_FAILED;
402 resultCode = sqlite3_step(statementHandle);
404 if (resultCode == SQLITE_DONE){
405 //deleteResult = CDSACCOUNT_OK;
407 return CDSACCOUNT_FAILED;
410 // Get the calendar IDs and delete each calendar (and associated entries).
412 resultCode = sqlite3_prepare_v2(db, "SELECT id FROM calendars WHERE accountid=(?1);", -1, &calendarHandle, nullptr);
414 if (resultCode != 0){
415 return CDSACCOUNT_FAILED;
418 resultCode = sqlite3_bind_int(calendarHandle, 1, accountID);
420 if (resultCode != 0){
421 return CDSACCOUNT_FAILED;
424 resultCode = sqlite3_step(calendarHandle);
426 if (resultCode == SQLITE_DONE || resultCode == SQLITE_ROW){
427 deleteResult = CDSACCOUNT_OK;
429 return CDSACCOUNT_FAILED;
432 while (resultCode == SQLITE_ROW){
434 int calendarDeleteID = sqlite3_column_int(calendarHandle, 0);
436 DeleteCalendar(calendarDeleteID);
438 resultCode = sqlite3_step(calendarHandle);
442 // Update the checksum.
444 UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
446 deleteResult = CDSACCOUNT_OK;
453 CDSCalendarResult CalendarDataStorage::AddCalendar(int accountID, string calendarName, string calendarID, Colour calendarColour)
456 CDSCalendarResult addResult = CDSCALENDAR_UNITTESTFAIL;
459 sqlite3_stmt *statementHandle;
461 resultCode = sqlite3_prepare_v2(db, "INSERT INTO calendars (name, calendarid, accountid, colour) VALUES(?1, ?2, ?3, ?4);", -1, &statementHandle, nullptr);
463 if (resultCode != 0){
464 return CDSCALENDAR_FAILED;
467 resultCode = sqlite3_bind_text(statementHandle, 1, calendarName.c_str(), -1, SQLITE_STATIC);
469 if (resultCode != 0){
470 return CDSCALENDAR_FAILED;
473 resultCode = sqlite3_bind_text(statementHandle, 2, calendarID.c_str(), -1, SQLITE_STATIC);
475 if (resultCode != 0){
476 return CDSCALENDAR_FAILED;
479 resultCode = sqlite3_bind_int(statementHandle, 3, accountID);
481 if (resultCode != 0){
482 return CDSCALENDAR_FAILED;
485 resultCode = sqlite3_bind_text(statementHandle, 4, ((string)calendarColour).c_str(), -1, SQLITE_STATIC);
487 if (resultCode != 0){
488 return CDSCALENDAR_FAILED;
491 resultCode = sqlite3_step(statementHandle);
493 if (resultCode != SQLITE_DONE){
494 return CDSCALENDAR_FAILED;
497 // Update the checksum.
499 UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
501 addResult = CDSCALENDAR_OK;
507 CDSGetCalendarInfo CalendarDataStorage::GetCalendar(std::string accountName, std::string calendarTextID)
510 CDSGetAccountInfo accountResult;
511 CDSGetCalendarInfo calendarResult;
512 sqlite3_stmt *statementHandle;
515 // Check if the account exists.
517 accountResult = GetAccount(accountName);
519 switch (accountResult.accountInfoResult){
522 calendarResult.accountName = accountResult.accountName;
523 calendarResult.accountInfoResult = CDSACCOUNT_OK;
525 case CDSACCOUNT_FAILED:
526 calendarResult.accountInfoResult = CDSACCOUNT_FAILED;
527 calendarResult.calendarInfoResult = CDSCALENDAR_FAILED;
528 return calendarResult;
529 case CDSACCOUNT_NOACCOUNT:
530 calendarResult.accountInfoResult = CDSACCOUNT_NOACCOUNT;
531 calendarResult.calendarInfoResult = CDSCALENDAR_NOCALENDAR;
532 return calendarResult;
536 // Check if the calendar exists.
538 resultCode = sqlite3_prepare_v2(db, "SELECT id, accountid, name, calendarid, colour FROM calendars WHERE accountid=(?1) AND calendarid=(?2);", -1, &statementHandle, nullptr);
540 if (resultCode != 0){
541 calendarResult.calendarInfoResult = CDSCALENDAR_FAILED;
542 return calendarResult;
545 resultCode = sqlite3_bind_int(statementHandle, 1, accountResult.accountID);
547 if (resultCode != 0){
548 calendarResult.calendarInfoResult = CDSCALENDAR_FAILED;
549 return calendarResult;
552 resultCode = sqlite3_bind_text(statementHandle, 2, calendarTextID.c_str(), -1, SQLITE_STATIC);
554 if (resultCode != 0){
555 calendarResult.calendarInfoResult = CDSCALENDAR_FAILED;
556 return calendarResult;
559 resultCode = sqlite3_step(statementHandle);
561 if (resultCode == SQLITE_DONE){
563 calendarResult.calendarInfoResult = CDSCALENDAR_NOCALENDAR;
564 return calendarResult;
566 } else if (resultCode == SQLITE_ROW){
568 // Get the calendar name.
570 stringstream calendarStream;
572 calendarStream << sqlite3_column_text(statementHandle, 2);
573 calendarResult.calendarName = calendarStream.str();
575 calendarStream.str("");
576 calendarStream << sqlite3_column_text(statementHandle, 3);
577 calendarResult.calendarTextID = calendarStream.str();
579 calendarStream.str("");
580 calendarStream << sqlite3_column_text(statementHandle, 4);
581 calendarResult.calendarColour = calendarStream.str();
583 calendarResult.calendarID = sqlite3_column_int(statementHandle, 0);
584 calendarResult.accountID = sqlite3_column_int(statementHandle, 1);
585 calendarResult.calendarInfoResult = CDSCALENDAR_OK;
586 return calendarResult;
590 calendarResult.calendarInfoResult = CDSCALENDAR_FAILED;
591 return calendarResult;
595 return calendarResult;
599 CDSGetCalendarInfo CalendarDataStorage::GetCalendar(int calendarID)
602 CDSGetCalendarInfo calendarResult;
603 sqlite3_stmt *statementHandle;
606 // Check if the calendar exists.
608 resultCode = sqlite3_prepare_v2(db, "SELECT id, accountid, name, calendarid, colour FROM calendars WHERE id=(?1);", -1, &statementHandle, nullptr);
610 if (resultCode != 0){
611 calendarResult.calendarInfoResult = CDSCALENDAR_FAILED;
612 return calendarResult;
615 resultCode = sqlite3_bind_int(statementHandle, 1, calendarID);
617 if (resultCode != 0){
618 calendarResult.calendarInfoResult = CDSCALENDAR_FAILED;
619 return calendarResult;
622 resultCode = sqlite3_step(statementHandle);
624 if (resultCode == SQLITE_DONE){
626 calendarResult.calendarInfoResult = CDSCALENDAR_NOCALENDAR;
627 return calendarResult;
629 } else if (resultCode == SQLITE_ROW){
631 // Get the calendar data.
633 stringstream calendarStream;
635 calendarStream << sqlite3_column_text(statementHandle, 2);
636 calendarResult.calendarName = calendarStream.str();
638 calendarStream.str("");
639 calendarStream << sqlite3_column_text(statementHandle, 3);
640 calendarResult.calendarTextID = calendarStream.str();
642 calendarStream.str("");
643 calendarStream << sqlite3_column_text(statementHandle, 4);
644 calendarResult.calendarColour = calendarStream.str();
646 calendarResult.calendarID = sqlite3_column_int(statementHandle, 0);
647 calendarResult.accountID = sqlite3_column_int(statementHandle, 1);
648 calendarResult.calendarInfoResult = CDSCALENDAR_OK;
649 return calendarResult;
653 calendarResult.calendarInfoResult = CDSCALENDAR_FAILED;
654 return calendarResult;
658 return calendarResult;
663 CDSCalendarResult CalendarDataStorage::UpdateCalendar(int calendarID, string calendarName)
666 CDSCalendarResult updateResult = CDSCALENDAR_UNITTESTFAIL;
669 sqlite3_stmt *findHandle;
670 sqlite3_stmt *statementHandle;
672 // Check if calendar exists first.
674 resultCode = sqlite3_prepare_v2(db, "SELECT id from calendars WHERE id=(?1);", -1, &findHandle, nullptr);
676 if (resultCode != 0){
677 return CDSCALENDAR_FAILED;
680 resultCode = sqlite3_bind_int(findHandle, 1, calendarID);
682 if (resultCode != 0){
683 return CDSCALENDAR_FAILED;
686 resultCode = sqlite3_step(findHandle);
688 if (resultCode == SQLITE_ROW){
690 } else if (resultCode == SQLITE_DONE) {
691 return CDSCALENDAR_NOCALENDAR;
693 return CDSCALENDAR_FAILED;
696 // Update the account.
698 resultCode = sqlite3_prepare_v2(db, "UPDATE calendars SET name=(?1) WHERE id=(?2);", -1, &statementHandle, nullptr);
700 if (resultCode != 0){
701 return CDSCALENDAR_FAILED;
704 resultCode = sqlite3_bind_text(statementHandle, 1, calendarName.c_str(), -1, SQLITE_STATIC);
706 if (resultCode != 0){
707 return CDSCALENDAR_FAILED;
710 resultCode = sqlite3_bind_int(statementHandle, 2, calendarID);
712 if (resultCode != 0){
713 return CDSCALENDAR_FAILED;
716 resultCode = sqlite3_step(statementHandle);
718 if (resultCode != SQLITE_DONE){
719 return CDSCALENDAR_FAILED;
722 // Update the checksum.
724 UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
726 updateResult = CDSCALENDAR_OK;
732 CDSCalendarResult CalendarDataStorage::DeleteCalendar(int calendarID)
735 CDSCalendarResult deleteResult = CDSCALENDAR_UNITTESTFAIL;
737 // Check if account exists first.
739 sqlite3_stmt *findHandle;
740 sqlite3_stmt *statementHandle;
741 sqlite3_stmt *entriesHandle;
744 resultCode = sqlite3_prepare_v2(db, "SELECT id from calendars WHERE id=(?1);", -1, &findHandle, nullptr);
746 if (resultCode != 0){
747 return CDSCALENDAR_FAILED;
750 resultCode = sqlite3_bind_int(findHandle, 1, calendarID);
752 if (resultCode != 0){
753 return CDSCALENDAR_FAILED;
756 resultCode = sqlite3_step(findHandle);
758 if (resultCode == SQLITE_ROW){
760 } else if (resultCode == SQLITE_DONE) {
761 return CDSCALENDAR_NOCALENDAR;
763 return CDSCALENDAR_FAILED;
766 // Delete the calendar.
768 resultCode = sqlite3_prepare_v2(db, "DELETE FROM calendars WHERE id=(?1);", -1, &statementHandle, nullptr);
770 if (resultCode != 0){
771 return CDSCALENDAR_FAILED;
774 resultCode = sqlite3_bind_int(statementHandle, 1, calendarID);
776 if (resultCode != 0){
777 return CDSCALENDAR_FAILED;
780 resultCode = sqlite3_step(statementHandle);
782 if (resultCode != SQLITE_DONE){
783 return CDSCALENDAR_FAILED;
786 // Delete the calendar entries.
788 resultCode = sqlite3_prepare_v2(db, "DELETE FROM calendarentries WHERE calendarid=(?1);", -1, &entriesHandle, nullptr);
790 if (resultCode != 0){
791 return CDSCALENDAR_FAILED;
794 resultCode = sqlite3_bind_int(entriesHandle, 1, calendarID);
796 if (resultCode != 0){
797 return CDSCALENDAR_FAILED;
800 resultCode = sqlite3_step(entriesHandle);
802 if (resultCode == SQLITE_DONE){
803 deleteResult = CDSCALENDAR_OK;
805 return CDSCALENDAR_FAILED;
808 // Update the checksum.
810 UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
816 CDSAddEntryResult CalendarDataStorage::AddEvent(int calendarID, std::string filename)
819 CDSAddEntryResult addResult;
820 addResult.addEventResult = CDSENTRY_UNITTESTFAIL;
822 // Load the event file.
824 CalendarEventObject eventData;
825 CalendarObjectLoadResult eventLoadResult = eventData.LoadFile(filename);
827 // Check the result of the event file load.
829 switch (eventLoadResult){
831 case CALENDAROBJECTLOAD_OK:
833 case CALENDAROBJECTLOAD_MISSING:
834 addResult.addEventResult = CDSENTRY_MISSINGFILE;
836 case CALENDAROBJECTLOAD_INVALIDFORMAT:
837 addResult.addEventResult = CDSENTRY_INVALIDFILE;
839 case CALENDAROBJECTLOAD_CANNOTOPEN:
840 addResult.addEventResult = CDSENTRY_CANNOTOPENFILE;
845 // Check if calendar exists first.
849 sqlite3_stmt *findHandle;
850 sqlite3_stmt *statementHandle;
852 resultCode = sqlite3_prepare_v2(db, "SELECT id from calendars WHERE id=(?1);", -1, &findHandle, nullptr);
854 if (resultCode != 0){
855 addResult.addEventResult = CDSENTRY_FAILED;
859 resultCode = sqlite3_bind_int(findHandle, 1, calendarID);
861 if (resultCode != 0){
862 addResult.addEventResult = CDSENTRY_FAILED;
866 resultCode = sqlite3_step(findHandle);
868 if (resultCode == SQLITE_ROW){
870 } else if (resultCode == SQLITE_DONE) {
871 addResult.addEventResult = CDSENTRY_NOCALENDAR;
874 addResult.addEventResult = CDSENTRY_FAILED;
878 // Get the required values from the event object.
880 int eventStartYear = 0;
881 int eventStartMonth = 0;
882 int eventStartDay = 0;
883 int eventStartHour = 0;
884 int eventStartMinute = 0;
885 int eventStartSecond = 0;
886 int eventStartDuration = 0;
887 std::string eventString = "";
891 if (eventData.DateTimeStartData.size() < 16){
893 addResult.addEventResult = CDSENTRY_INVALIDFILE;
898 eventString = eventData.DateTimeStartData.substr(0,4);
900 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
902 eventStartYear = atoi(eventString.c_str());
906 addResult.addEventResult = CDSENTRY_INVALIDFILE;
911 eventString = eventData.DateTimeStartData.substr(4,2);
913 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
915 eventStartMonth = atoi(eventString.c_str());
919 addResult.addEventResult = CDSENTRY_INVALIDFILE;
924 eventString = eventData.DateTimeStartData.substr(6,2);
926 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
928 eventStartDay = atoi(eventString.c_str());
932 addResult.addEventResult = CDSENTRY_INVALIDFILE;
937 eventString = eventData.DateTimeStartData.substr(9,2);
939 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
941 eventStartHour = atoi(eventString.c_str());
945 addResult.addEventResult = CDSENTRY_INVALIDFILE;
950 eventString = eventData.DateTimeStartData.substr(11,2);
952 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
954 eventStartMinute = atoi(eventString.c_str());
958 addResult.addEventResult = CDSENTRY_INVALIDFILE;
963 eventString = eventData.DateTimeStartData.substr(13,2);
965 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
967 eventStartSecond = atoi(eventString.c_str());
971 addResult.addEventResult = CDSENTRY_INVALIDFILE;
976 //eventYear = eventStartDate.substr(0, 4);
980 int eventEndYear = 0;
981 int eventEndMonth = 0;
983 int eventEndHour = 0;
984 int eventEndMinute = 0;
985 int eventEndSecond = 0;
986 int eventEndDuration = 0;
988 if (eventData.DateTimeEndData != ""){
990 if (eventData.DateTimeEndData.size() < 16){
992 addResult.addEventResult = CDSENTRY_INVALIDFILE;
997 eventString = eventData.DateTimeEndData.substr(0,4);
999 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1001 eventEndYear = atoi(eventString.c_str());
1005 addResult.addEventResult = CDSENTRY_INVALIDFILE;
1010 eventString = eventData.DateTimeEndData.substr(4,2);
1012 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1014 eventEndMonth = atoi(eventString.c_str());
1018 addResult.addEventResult = CDSENTRY_INVALIDFILE;
1023 eventString = eventData.DateTimeEndData.substr(6,2);
1025 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1027 eventEndDay = atoi(eventString.c_str());
1031 addResult.addEventResult = CDSENTRY_INVALIDFILE;
1036 eventString = eventData.DateTimeEndData.substr(9,2);
1038 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1040 eventEndHour = atoi(eventString.c_str());
1044 addResult.addEventResult = CDSENTRY_INVALIDFILE;
1049 eventString = eventData.DateTimeEndData.substr(11,2);
1051 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1053 eventEndMinute = atoi(eventString.c_str());
1057 addResult.addEventResult = CDSENTRY_INVALIDFILE;
1062 eventString = eventData.DateTimeEndData.substr(13,2);
1064 if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
1066 eventEndSecond = atoi(eventString.c_str());
1070 addResult.addEventResult = CDSENTRY_INVALIDFILE;
1077 eventString = eventData.DurationData;
1079 // Process the duration data.
1081 int eventDurationWeeks = 0;
1082 int eventDurationDays = 0;
1083 int eventDurationHours = 0;
1084 int eventDurationMinutes = 0;
1085 int eventDurationSeconds = 0;
1087 // Get the duration (if DTEND hasn't been specified).
1089 if (eventData.DurationData.size() > 0){
1091 bool FoundP = false;
1092 bool FoundW = false;
1093 bool DateTimeMode = false;
1095 std::string::iterator eventDataChar = eventData.DurationData.begin();
1096 std::string currentValue = "";
1098 if (*eventDataChar != 'P'){
1100 eventDataChar = eventData.DurationData.end();
1104 for(eventDataChar; eventDataChar != eventData.DurationData.end(); eventDataChar++){
1106 // Check if value is a digit.
1108 if (isdigit(*eventDataChar)){
1110 currentValue += *eventDataChar;
1114 // Check that the value matches one of the letters.
1116 if (*eventDataChar == 'W' && DateTimeMode == false){
1118 eventDurationWeeks = atoi(currentValue.c_str());
1120 } else if (*eventDataChar == 'D' && DateTimeMode == false){
1122 eventDurationDays = atoi(currentValue.c_str());
1124 } else if (*eventDataChar == 'T' && DateTimeMode == false){
1126 DateTimeMode = true;
1128 } else if (*eventDataChar == 'H'){
1130 eventDurationHours = atoi(currentValue.c_str());
1132 } else if (*eventDataChar == 'M'){
1134 eventDurationMinutes = atoi(currentValue.c_str());
1136 } else if (*eventDataChar == 'S'){
1138 eventDurationSeconds = atoi(currentValue.c_str());
1142 // Reset the current value.
1152 // Add the calendar entry.
1154 std::string sqlParameter = "INSERT INTO calendarentries (calendarid, entryname, entrydescription,"
1155 " entrystartyear, entrystartmonth, entrystartday, entrystarthour, entrystartminute, entrystartsecond,"
1156 " entryendyear, entryendmonth, entryendday, entryendhour, entryendminute, entryendsecond, "
1157 " entrydurationweek, entrydurationday, entrydurationhour, entrydurationminute, entrydurationsecond)"
1158 " VALUES ((?1), (?2), (?3), (?4), (?5), (?6), (?7), (?8), (?9), (?10), "
1159 " (?11), (?12), (?13), (?14), (?15), (?16), (?17), (?18), (?19), (?20))";
1161 resultCode = sqlite3_prepare_v2(db, sqlParameter.c_str(), -1, &statementHandle, nullptr);
1163 resultCode = sqlite3_bind_int(statementHandle, 1, calendarID);
1165 if (resultCode != 0){
1166 addResult.addEventResult = CDSENTRY_FAILED;
1170 // Process Entry Name.
1172 resultCode = sqlite3_bind_text(statementHandle, 2, eventData.SummaryData.c_str(), -1, SQLITE_STATIC);
1174 if (resultCode != 0){
1175 addResult.addEventResult = CDSENTRY_FAILED;
1179 // Process Entry Description.
1181 string eventDescription;
1184 eventDescription = eventData.DescriptionList.at(0);
1187 catch (out_of_range &err){
1188 eventDescription = "";
1191 resultCode = sqlite3_bind_text(statementHandle, 3, eventDescription.c_str(), -1, SQLITE_STATIC);
1193 if (resultCode != 0){
1194 addResult.addEventResult = CDSENTRY_FAILED;
1198 // Process Entry Start Date information.
1200 resultCode = sqlite3_bind_int(statementHandle, 4, eventStartYear);
1202 if (resultCode != 0){
1203 addResult.addEventResult = CDSENTRY_FAILED;
1207 resultCode = sqlite3_bind_int(statementHandle, 5, eventStartMonth);
1209 if (resultCode != 0){
1210 addResult.addEventResult = CDSENTRY_FAILED;
1214 resultCode = sqlite3_bind_int(statementHandle, 6, eventStartDay);
1216 if (resultCode != 0){
1217 addResult.addEventResult = CDSENTRY_FAILED;
1221 resultCode = sqlite3_bind_int(statementHandle, 7, eventStartHour);
1223 if (resultCode != 0){
1224 addResult.addEventResult = CDSENTRY_FAILED;
1228 resultCode = sqlite3_bind_int(statementHandle, 8, eventStartMinute);
1230 if (resultCode != 0){
1231 addResult.addEventResult = CDSENTRY_FAILED;
1235 resultCode = sqlite3_bind_int(statementHandle, 9, eventStartSecond);
1237 if (resultCode != 0){
1238 addResult.addEventResult = CDSENTRY_FAILED;
1242 // Process Entry Start End information.
1244 resultCode = sqlite3_bind_int(statementHandle, 10, eventEndYear);
1246 if (resultCode != 0){
1247 addResult.addEventResult = CDSENTRY_FAILED;
1251 resultCode = sqlite3_bind_int(statementHandle, 11, eventEndMonth);
1253 if (resultCode != 0){
1254 addResult.addEventResult = CDSENTRY_FAILED;
1258 resultCode = sqlite3_bind_int(statementHandle, 12, eventEndDay);
1260 if (resultCode != 0){
1261 addResult.addEventResult = CDSENTRY_FAILED;
1265 resultCode = sqlite3_bind_int(statementHandle, 13, eventEndHour);
1267 if (resultCode != 0){
1268 addResult.addEventResult = CDSENTRY_FAILED;
1272 resultCode = sqlite3_bind_int(statementHandle, 14, eventEndMinute);
1274 if (resultCode != 0){
1275 addResult.addEventResult = CDSENTRY_FAILED;
1279 resultCode = sqlite3_bind_int(statementHandle, 15, eventEndSecond);
1281 if (resultCode != 0){
1282 addResult.addEventResult = CDSENTRY_FAILED;
1286 resultCode = sqlite3_bind_int(statementHandle, 16, eventDurationWeeks);
1288 if (resultCode != 0){
1289 addResult.addEventResult = CDSENTRY_FAILED;
1293 resultCode = sqlite3_bind_int(statementHandle, 17, eventDurationDays);
1295 if (resultCode != 0){
1296 addResult.addEventResult = CDSENTRY_FAILED;
1300 resultCode = sqlite3_bind_int(statementHandle, 18, eventDurationHours);
1302 if (resultCode != 0){
1303 addResult.addEventResult = CDSENTRY_FAILED;
1307 resultCode = sqlite3_bind_int(statementHandle, 19, eventDurationMinutes);
1309 if (resultCode != 0){
1310 addResult.addEventResult = CDSENTRY_FAILED;
1314 resultCode = sqlite3_bind_int(statementHandle, 20, eventDurationSeconds);
1316 if (resultCode != 0){
1317 addResult.addEventResult = CDSENTRY_FAILED;
1321 resultCode = sqlite3_step(statementHandle);
1323 if (resultCode != SQLITE_DONE){
1324 addResult.addEventResult = CDSENTRY_FAILED;
1328 addResult.calendarEntryID = sqlite3_last_insert_rowid(db);
1329 addResult.addEventResult = CDSENTRY_OK;
1331 // Update the checksum.
1333 UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
1339 CDSGetCalendarEntryInfo CalendarDataStorage::GetEvent(int calendarEntryID)
1342 CDSGetCalendarEntryInfo entryResult;
1344 // Check if the calendar entry exists.
1348 sqlite3_stmt *findHandle;
1349 sqlite3_stmt *statementHandle;
1351 resultCode = sqlite3_prepare_v2(db, "SELECT id FROM calendarentries WHERE id=(?1);", -1, &findHandle, nullptr);
1353 if (resultCode != 0){
1354 entryResult.getEventResult = CDSENTRY_FAILED;
1358 resultCode = sqlite3_bind_int(findHandle, 1, calendarEntryID);
1360 if (resultCode != 0){
1361 entryResult.getEventResult = CDSENTRY_FAILED;
1365 resultCode = sqlite3_step(findHandle);
1367 if (resultCode == SQLITE_ROW){
1369 } else if (resultCode == SQLITE_DONE) {
1370 entryResult.getEventResult = CDSENTRY_NOENTRY;
1373 entryResult.getEventResult = CDSENTRY_FAILED;
1377 // Get the calendar entry data.
1379 std::string sqlParameter = "SELECT entryname, entrydescription,"
1380 " entrystartyear, entrystartmonth, entrystartday, entrystarthour, entrystartminute, entrystartsecond,"
1381 " entryendyear, entryendmonth, entryendday, entryendhour, entryendminute, entryendsecond, "
1382 " entrydurationweek, entrydurationday, entrydurationhour, entrydurationminute, entrydurationsecond, "
1384 " FROM calendarentries WHERE id=(?1)";
1386 resultCode = sqlite3_prepare_v2(db, sqlParameter.c_str(), -1, &statementHandle, nullptr);
1388 if (resultCode != 0){
1389 entryResult.getEventResult = CDSENTRY_FAILED;
1393 resultCode = sqlite3_bind_int(statementHandle, 1, calendarEntryID);
1395 if (resultCode != 0){
1396 entryResult.getEventResult = CDSENTRY_FAILED;
1400 resultCode = sqlite3_step(statementHandle);
1402 if (resultCode == SQLITE_ROW){
1404 // Get the calendar entry name,
1406 stringstream entryStream;
1408 entryStream << sqlite3_column_text(statementHandle, 0);
1409 entryResult.entryName = entryStream.str();
1411 entryStream.str("");
1413 // Get the calendar entry description.
1415 entryStream << sqlite3_column_text(statementHandle, 1);
1416 entryResult.entryDescription = entryStream.str();
1418 entryStream.str("");
1420 entryResult.entryStartYear = sqlite3_column_int(statementHandle, 2);
1421 entryResult.entryStartMonth = sqlite3_column_int(statementHandle, 3);
1422 entryResult.entryStartDay = sqlite3_column_int(statementHandle, 4);
1423 entryResult.entryStartHour = sqlite3_column_int(statementHandle, 5);
1424 entryResult.entryStartMinute = sqlite3_column_int(statementHandle, 6);
1425 entryResult.entryStartSecond = sqlite3_column_int(statementHandle, 7);
1426 entryResult.entryEndYear = sqlite3_column_int(statementHandle, 8);
1427 entryResult.entryEndMonth = sqlite3_column_int(statementHandle, 9);
1428 entryResult.entryEndDay = sqlite3_column_int(statementHandle, 10);
1429 entryResult.entryEndHour = sqlite3_column_int(statementHandle, 11);
1430 entryResult.entryEndMinute = sqlite3_column_int(statementHandle, 12);
1431 entryResult.entryEndSecond = sqlite3_column_int(statementHandle, 13);
1432 entryResult.entryDurationWeeks = sqlite3_column_int(statementHandle, 14);
1433 entryResult.entryDurationDays = sqlite3_column_int(statementHandle, 15);
1434 entryResult.entryDurationHours = sqlite3_column_int(statementHandle, 16);
1435 entryResult.entryDurationMinutes = sqlite3_column_int(statementHandle, 17);
1436 entryResult.entryDurationSeconds = sqlite3_column_int(statementHandle, 18);
1437 entryResult.calendarID = sqlite3_column_int(statementHandle, 19);
1438 entryResult.calendarEntryID = sqlite3_column_int(statementHandle, 20);
1440 } else if (resultCode == SQLITE_DONE) {
1441 entryResult.getEventResult = CDSENTRY_NOCALENDAR;
1444 entryResult.getEventResult = CDSENTRY_FAILED;
1448 entryResult.getEventResult = CDSENTRY_OK;
1454 CDSEntryResult CalendarDataStorage::DeleteEvent(int calendarEntryID)
1457 CDSEntryResult deleteResult = CDSENTRY_UNITTESTFAIL;
1459 // Check if the calendar entry exists.
1463 sqlite3_stmt *findHandle;
1464 sqlite3_stmt *statementHandle;
1466 resultCode = sqlite3_prepare_v2(db, "SELECT id FROM calendarentries WHERE id=(?1);", -1, &findHandle, nullptr);
1468 if (resultCode != 0){
1469 return CDSENTRY_FAILED;
1472 resultCode = sqlite3_bind_int(findHandle, 1, calendarEntryID);
1474 if (resultCode != 0){
1475 return CDSENTRY_FAILED;
1478 resultCode = sqlite3_step(findHandle);
1480 if (resultCode == SQLITE_ROW){
1482 } else if (resultCode == SQLITE_DONE) {
1483 return CDSENTRY_NOENTRY;
1485 return CDSENTRY_FAILED;
1488 // Delete the account.
1490 resultCode = sqlite3_prepare_v2(db, "DELETE FROM calendarentries WHERE id=(?1);", -1, &statementHandle, nullptr);
1492 if (resultCode != 0){
1493 return CDSENTRY_FAILED;
1496 resultCode = sqlite3_bind_int(statementHandle, 1, calendarEntryID);
1498 if (resultCode != 0){
1499 return CDSENTRY_FAILED;
1502 resultCode = sqlite3_step(statementHandle);
1504 if (resultCode == SQLITE_DONE){
1505 deleteResult = CDSENTRY_OK;
1507 return CDSENTRY_FAILED;
1510 // Update the checksum.
1512 UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
1514 return deleteResult;
1518 CDSEntryList CalendarDataStorage::GetEventList(int calendarID){
1520 CDSEntryList entryList;
1521 entryList.getEventListResult = CDSENTRY_UNITTESTFAIL;
1523 // Check if calendar exists first.
1527 sqlite3_stmt *findHandle;
1528 sqlite3_stmt *calendarHandle;
1530 resultCode = sqlite3_prepare_v2(db, "SELECT id from calendars WHERE id=(?1);", -1, &findHandle, nullptr);
1532 if (resultCode != 0){
1533 entryList.getEventListResult = CDSENTRY_FAILED;
1537 resultCode = sqlite3_bind_int(findHandle, 1, calendarID);
1539 if (resultCode != 0){
1540 entryList.getEventListResult = CDSENTRY_FAILED;
1544 resultCode = sqlite3_step(findHandle);
1546 if (resultCode == SQLITE_ROW){
1548 } else if (resultCode == SQLITE_DONE) {
1549 entryList.getEventListResult = CDSENTRY_NOCALENDAR;
1552 entryList.getEventListResult = CDSENTRY_FAILED;
1556 // Get the list of entry IDs.
1558 resultCode = sqlite3_prepare_v2(db, "SELECT id FROM calendarentries WHERE calendarid=(?1);", -1, &calendarHandle, nullptr);
1560 if (resultCode != 0){
1561 entryList.getEventListResult = CDSENTRY_FAILED;
1565 resultCode = sqlite3_bind_int(calendarHandle, 1, calendarID);
1567 if (resultCode != 0){
1568 entryList.getEventListResult = CDSENTRY_FAILED;
1572 resultCode = sqlite3_step(calendarHandle);
1574 if (resultCode == SQLITE_DONE || resultCode == SQLITE_ROW){
1576 entryList.getEventListResult = CDSENTRY_FAILED;
1580 while (resultCode == SQLITE_ROW){
1582 int calendarID = sqlite3_column_int(calendarHandle, 0);
1584 entryList.entryList.push_back(calendarID);
1586 resultCode = sqlite3_step(calendarHandle);
1590 entryList.getEventListResult = CDSENTRY_OK;
1596 CDSEntryList CalendarDataStorage::GetEventListByDate(int calendarYear, int calendarMonth, int calendarDay){
1598 CDSEntryList entryList;
1599 entryList.getEventListResult = CDSENTRY_UNITTESTFAIL;
1601 // Check if calendar exists first.
1605 sqlite3_stmt *calendarHandle;
1607 // Get the list of entry IDs.
1609 resultCode = sqlite3_prepare_v2(db, "SELECT id FROM calendarentries WHERE entrystartyear=(?1) AND entrystartmonth=(?2) AND entrystartday=(?3);", -1, &calendarHandle, nullptr);
1611 if (resultCode != 0){
1612 entryList.getEventListResult = CDSENTRY_FAILED;
1616 resultCode = sqlite3_bind_int(calendarHandle, 1, calendarYear);
1618 if (resultCode != 0){
1619 entryList.getEventListResult = CDSENTRY_FAILED;
1623 resultCode = sqlite3_bind_int(calendarHandle, 2, calendarMonth);
1625 if (resultCode != 0){
1626 entryList.getEventListResult = CDSENTRY_FAILED;
1630 resultCode = sqlite3_bind_int(calendarHandle, 3, calendarDay);
1632 if (resultCode != 0){
1633 entryList.getEventListResult = CDSENTRY_FAILED;
1637 resultCode = sqlite3_step(calendarHandle);
1639 if (resultCode == SQLITE_DONE || resultCode == SQLITE_ROW){
1641 entryList.getEventListResult = CDSENTRY_FAILED;
1645 while (resultCode == SQLITE_ROW){
1647 int calendarID = sqlite3_column_int(calendarHandle, 0);
1649 entryList.entryList.push_back(calendarID);
1651 resultCode = sqlite3_step(calendarHandle);
1655 entryList.getEventListResult = CDSENTRY_OK;
1661 CDSCalendarList CalendarDataStorage::GetCalendarList(int accountID){
1663 CDSCalendarList calendarList;
1664 calendarList.getCalendarListResult = CDSCALENDAR_UNITTESTFAIL;
1666 // Check if calendar exists first.
1670 sqlite3_stmt *findHandle;
1671 sqlite3_stmt *calendarHandle;
1673 resultCode = sqlite3_prepare_v2(db, "SELECT id from accounts WHERE id=(?1);", -1, &findHandle, nullptr);
1675 if (resultCode != 0){
1676 calendarList.getCalendarListResult = CDSCALENDAR_FAILED;
1677 return calendarList;
1680 resultCode = sqlite3_bind_int(findHandle, 1, accountID);
1682 if (resultCode != 0){
1683 calendarList.getCalendarListResult = CDSCALENDAR_FAILED;
1684 return calendarList;
1687 resultCode = sqlite3_step(findHandle);
1689 if (resultCode == SQLITE_ROW){
1691 } else if (resultCode == SQLITE_DONE) {
1692 calendarList.getCalendarListResult = CDSCALENDAR_NOCALENDAR;
1693 return calendarList;
1695 calendarList.getCalendarListResult = CDSCALENDAR_FAILED;
1696 return calendarList;
1699 // Get the list of entry IDs.
1701 resultCode = sqlite3_prepare_v2(db, "SELECT id, calendarid FROM calendars WHERE accountid=(?1);", -1, &calendarHandle, nullptr);
1703 if (resultCode != 0){
1704 calendarList.getCalendarListResult = CDSCALENDAR_FAILED;
1705 return calendarList;
1708 resultCode = sqlite3_bind_int(calendarHandle, 1, accountID);
1710 if (resultCode != 0){
1711 calendarList.getCalendarListResult = CDSCALENDAR_FAILED;
1712 return calendarList;
1715 resultCode = sqlite3_step(calendarHandle);
1717 if (resultCode == SQLITE_DONE || resultCode == SQLITE_ROW){
1719 calendarList.getCalendarListResult = CDSCALENDAR_FAILED;
1720 return calendarList;
1723 while (resultCode == SQLITE_ROW){
1725 int calendarID = sqlite3_column_int(calendarHandle, 0);
1727 stringstream calendarStream;
1729 calendarStream << sqlite3_column_text(calendarHandle, 1);
1731 calendarList.calendarList.push_back(calendarID);
1732 calendarList.calendarListTextID.push_back(calendarStream.str());
1734 calendarStream.str("");
1736 resultCode = sqlite3_step(calendarHandle);
1740 calendarList.getCalendarListResult = CDSCALENDAR_OK;
1742 return calendarList;
1746 CDSChecksumResult CalendarDataStorage::AddChecksum(string checksumName, string checksumValue){
1750 // Check if the checksum already exists.
1752 sqlite3_stmt *findHandle;
1753 sqlite3_stmt *checksumHandle;
1755 resultCode = sqlite3_prepare_v2(db, "SELECT checksumvalue from checksums WHERE checksumname=(?1);", -1, &findHandle, nullptr);
1757 if (resultCode != 0){
1758 return CDSCHECKSUM_FAILED;
1761 resultCode = sqlite3_bind_text(findHandle, 1, checksumName.c_str(), -1, SQLITE_STATIC);
1763 if (resultCode != 0){
1764 return CDSCHECKSUM_FAILED;
1767 resultCode = sqlite3_step(findHandle);
1769 if (resultCode == SQLITE_ROW){
1770 return CDSCHECKSUM_CHECKSUMALREADYEXISTS;
1771 } else if (resultCode == SQLITE_DONE) {
1774 return CDSCHECKSUM_FAILED;
1777 // Add the checksum to the checksum table.
1779 resultCode = sqlite3_prepare_v2(db, "INSERT INTO checksums (checksumname, checksumvalue) VALUES(?1, ?2);", -1, &checksumHandle, nullptr);
1781 if (resultCode != 0){
1782 return CDSCHECKSUM_FAILED;
1785 resultCode = sqlite3_bind_text(checksumHandle, 1, checksumName.c_str(), -1, SQLITE_STATIC);
1787 if (resultCode != 0){
1788 return CDSCHECKSUM_FAILED;
1791 resultCode = sqlite3_bind_text(checksumHandle, 2, checksumValue.c_str(), -1, SQLITE_STATIC);
1793 if (resultCode != 0){
1794 return CDSCHECKSUM_FAILED;
1797 resultCode = sqlite3_step(checksumHandle);
1799 if (resultCode != SQLITE_DONE){
1800 return CDSCHECKSUM_FAILED;
1803 return CDSCHECKSUM_OK;
1807 CDSGetChecksumResult CalendarDataStorage::GetChecksum(string checksumName){
1809 CDSGetChecksumResult getChecksumResult;
1813 // Check if the checksum already exists.
1815 sqlite3_stmt *getHandle;
1817 resultCode = sqlite3_prepare_v2(db, "SELECT checksumvalue from checksums WHERE checksumname=(?1);", -1, &getHandle, nullptr);
1819 if (resultCode != 0){
1820 getChecksumResult.getChecksumResult = CDSCHECKSUM_FAILED;
1821 return getChecksumResult;
1824 resultCode = sqlite3_bind_text(getHandle, 1, checksumName.c_str(), -1, SQLITE_STATIC);
1826 if (resultCode != 0){
1827 getChecksumResult.getChecksumResult = CDSCHECKSUM_FAILED;
1828 return getChecksumResult;
1831 resultCode = sqlite3_step(getHandle);
1833 if (resultCode == SQLITE_ROW){
1834 } else if (resultCode == SQLITE_DONE) {
1835 getChecksumResult.getChecksumResult = CDSCHECKSUM_NOHASH;
1836 return getChecksumResult;
1838 getChecksumResult.getChecksumResult = CDSCHECKSUM_FAILED;
1839 return getChecksumResult;
1842 stringstream checksumStream;
1844 checksumStream << sqlite3_column_text(getHandle, 0);
1846 getChecksumResult.checksumValue = checksumStream.str();
1848 getChecksumResult.getChecksumResult = CDSCHECKSUM_OK;
1850 return getChecksumResult;
1854 CDSChecksumResult CalendarDataStorage::UpdateChecksum(std::string checksumName, std::string checksumValue){
1858 // Check if the checksum already exists.
1860 sqlite3_stmt *getHandle;
1861 sqlite3_stmt *statementHandle;
1863 resultCode = sqlite3_prepare_v2(db, "SELECT checksumvalue from checksums WHERE checksumname=(?1);", -1, &getHandle, nullptr);
1865 if (resultCode != 0){
1866 return CDSCHECKSUM_FAILED;
1869 resultCode = sqlite3_bind_text(getHandle, 1, checksumName.c_str(), -1, SQLITE_STATIC);
1871 if (resultCode != 0){
1872 return CDSCHECKSUM_FAILED;
1875 resultCode = sqlite3_step(getHandle);
1877 if (resultCode == SQLITE_ROW){
1878 } else if (resultCode == SQLITE_DONE) {
1879 return CDSCHECKSUM_NOHASH;
1881 return CDSCHECKSUM_FAILED;
1884 // Update the checksum.
1886 resultCode = sqlite3_prepare_v2(db, "UPDATE checksums SET checksumvalue=(?1) WHERE checksumname=(?2);", -1, &statementHandle, nullptr);
1888 if (resultCode != 0){
1889 return CDSCHECKSUM_FAILED;
1892 resultCode = sqlite3_bind_text(statementHandle, 1, checksumValue.c_str(), -1, SQLITE_STATIC);
1894 if (resultCode != 0){
1895 return CDSCHECKSUM_FAILED;
1898 resultCode = sqlite3_bind_text(statementHandle, 2, checksumName.c_str(), -1, SQLITE_STATIC);
1900 if (resultCode != 0){
1901 return CDSCHECKSUM_FAILED;
1904 resultCode = sqlite3_step(statementHandle);
1906 if (resultCode != SQLITE_DONE){
1907 return CDSCHECKSUM_FAILED;
1910 return CDSCHECKSUM_OK;