#include "CalendarDataStorage.h"
+#define CDS_RANDOMPOW 24
+
#include <iostream>
using namespace std;
// Setup the checksums table.
const char *checksumsTableString;
- checksumsTableString = "CREATE TABLE checksums(hashname TEXT PRIMARY KEY "
- ", hashvalue TEXT);";
+ checksumsTableString = "CREATE TABLE checksums(checksumname TEXT PRIMARY KEY "
+ ", checksumvalue TEXT);";
resultCode = sqlite3_exec(db, checksumsTableString, callback, nullptr, &setupTablesErrMsg);
return;
}
+ // Setup internal checksums.
+
+ CDSChecksumResult addChecksum = AddChecksum("internal_updatedata", "");
+
+ if (addChecksum != CDSCHECKSUM_OK){
+ DataStorageInitOK = false;
+ return;
+ }
+
DataStorageInitOK = true;
}
return CDSACCOUNT_FAILED;
}
+ // Update the checksum.
+
+ UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
+
addResult = CDSACCOUNT_OK;
return addResult;
return CDSACCOUNT_FAILED;
}
+ // Update the checksum.
+
+ UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
+
updateResult = CDSACCOUNT_OK;
return updateResult;
}
+ // Update the checksum.
+
+ UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
+
deleteResult = CDSACCOUNT_OK;
return deleteResult;
return CDSCALENDAR_FAILED;
}
+ // Update the checksum.
+
+ UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
+
addResult = CDSCALENDAR_OK;
return addResult;
}
+CDSGetCalendarInfo CalendarDataStorage::GetCalendar(int calendarID)
+{
+
+ CDSGetCalendarInfo calendarResult;
+ sqlite3_stmt *statementHandle;
+ int resultCode;
+
+ // Check if the calendar exists.
+
+ resultCode = sqlite3_prepare_v2(db, "SELECT id, accountid, name, calendarid, colour FROM calendars WHERE id=(?1);", -1, &statementHandle, nullptr);
+
+ if (resultCode != 0){
+ calendarResult.calendarInfoResult = CDSCALENDAR_FAILED;
+ return calendarResult;
+ }
+
+ resultCode = sqlite3_bind_int(statementHandle, 1, calendarID);
+
+ if (resultCode != 0){
+ calendarResult.calendarInfoResult = CDSCALENDAR_FAILED;
+ return calendarResult;
+ }
+
+ resultCode = sqlite3_step(statementHandle);
+
+ if (resultCode == SQLITE_DONE){
+
+ calendarResult.calendarInfoResult = CDSCALENDAR_NOCALENDAR;
+ return calendarResult;
+
+ } else if (resultCode == SQLITE_ROW){
+
+ // Get the calendar data.
+
+ stringstream calendarStream;
+
+ calendarStream << sqlite3_column_text(statementHandle, 2);
+ calendarResult.calendarName = calendarStream.str();
+
+ calendarStream.str("");
+ calendarStream << sqlite3_column_text(statementHandle, 3);
+ calendarResult.calendarTextID = calendarStream.str();
+
+ calendarStream.str("");
+ calendarStream << sqlite3_column_text(statementHandle, 4);
+ calendarResult.calendarColour = calendarStream.str();
+
+ calendarResult.calendarID = sqlite3_column_int(statementHandle, 0);
+ calendarResult.accountID = sqlite3_column_int(statementHandle, 1);
+ calendarResult.calendarInfoResult = CDSCALENDAR_OK;
+ return calendarResult;
+
+ } else {
+
+ calendarResult.calendarInfoResult = CDSCALENDAR_FAILED;
+ return calendarResult;
+
+ }
+
+ return calendarResult;
+
+}
+
+
CDSCalendarResult CalendarDataStorage::UpdateCalendar(int calendarID, string calendarName)
{
return CDSCALENDAR_FAILED;
}
+ // Update the checksum.
+
+ UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
+
updateResult = CDSCALENDAR_OK;
return updateResult;
return CDSCALENDAR_FAILED;
}
+ // Update the checksum.
+
+ UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
+
return deleteResult;
}
addResult.calendarEntryID = sqlite3_last_insert_rowid(db);
addResult.addEventResult = CDSENTRY_OK;
+ // Update the checksum.
+
+ UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
+
return addResult;
}
std::string sqlParameter = "SELECT entryname, entrydescription,"
" entrystartyear, entrystartmonth, entrystartday, entrystarthour, entrystartminute, entrystartsecond,"
" entryendyear, entryendmonth, entryendday, entryendhour, entryendminute, entryendsecond, "
- " entrydurationweek, entrydurationday, entrydurationhour, entrydurationminute, entrydurationsecond "
+ " entrydurationweek, entrydurationday, entrydurationhour, entrydurationminute, entrydurationsecond, "
+ " calendarid"
" FROM calendarentries WHERE id=(?1)";
resultCode = sqlite3_prepare_v2(db, sqlParameter.c_str(), -1, &statementHandle, nullptr);
entryResult.entryDurationHours = sqlite3_column_int(statementHandle, 16);
entryResult.entryDurationMinutes = sqlite3_column_int(statementHandle, 17);
entryResult.entryDurationSeconds = sqlite3_column_int(statementHandle, 18);
+ entryResult.calendarID = sqlite3_column_int(statementHandle, 19);
} else if (resultCode == SQLITE_DONE) {
entryResult.getEventResult = CDSENTRY_NOCALENDAR;
return CDSENTRY_FAILED;
}
+ // Update the checksum.
+
+ UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
+
return deleteResult;
}
}
+CDSEntryList CalendarDataStorage::GetEventListByDate(int calendarYear, int calendarMonth, int calendarDay){
+
+ CDSEntryList entryList;
+ entryList.getEventListResult = CDSENTRY_UNITTESTFAIL;
+
+ // Check if calendar exists first.
+
+ int resultCode;
+
+ sqlite3_stmt *calendarHandle;
+
+ // Get the list of entry IDs.
+
+ resultCode = sqlite3_prepare_v2(db, "SELECT id FROM calendarentries WHERE entrystartyear=(?1) AND entrystartmonth=(?2) AND entrystartday=(?3);", -1, &calendarHandle, nullptr);
+
+ if (resultCode != 0){
+ entryList.getEventListResult = CDSENTRY_FAILED;
+ return entryList;
+ }
+
+ resultCode = sqlite3_bind_int(calendarHandle, 1, calendarYear);
+
+ if (resultCode != 0){
+ entryList.getEventListResult = CDSENTRY_FAILED;
+ return entryList;
+ }
+
+ resultCode = sqlite3_bind_int(calendarHandle, 2, calendarMonth);
+
+ if (resultCode != 0){
+ entryList.getEventListResult = CDSENTRY_FAILED;
+ return entryList;
+ }
+
+ resultCode = sqlite3_bind_int(calendarHandle, 3, calendarDay);
+
+ if (resultCode != 0){
+ entryList.getEventListResult = CDSENTRY_FAILED;
+ return entryList;
+ }
+
+ resultCode = sqlite3_step(calendarHandle);
+
+ if (resultCode == SQLITE_DONE || resultCode == SQLITE_ROW){
+ } else {
+ entryList.getEventListResult = CDSENTRY_FAILED;
+ return entryList;
+ }
+
+ while (resultCode == SQLITE_ROW){
+
+ int calendarID = sqlite3_column_int(calendarHandle, 0);
+
+ entryList.entryList.push_back(calendarID);
+
+ resultCode = sqlite3_step(calendarHandle);
+
+ }
+
+ entryList.getEventListResult = CDSENTRY_OK;
+
+ return entryList;
+
+}
+
CDSCalendarList CalendarDataStorage::GetCalendarList(int accountID){
CDSCalendarList calendarList;
return calendarList;
+}
+
+CDSChecksumResult CalendarDataStorage::AddChecksum(string checksumName, string checksumValue){
+
+ int resultCode;
+
+ // Check if the checksum already exists.
+
+ sqlite3_stmt *findHandle;
+ sqlite3_stmt *checksumHandle;
+
+ resultCode = sqlite3_prepare_v2(db, "SELECT checksumvalue from checksums WHERE checksumname=(?1);", -1, &findHandle, nullptr);
+
+ if (resultCode != 0){
+ return CDSCHECKSUM_FAILED;
+ }
+
+ resultCode = sqlite3_bind_text(findHandle, 1, checksumName.c_str(), -1, SQLITE_STATIC);
+
+ if (resultCode != 0){
+ return CDSCHECKSUM_FAILED;
+ }
+
+ resultCode = sqlite3_step(findHandle);
+
+ if (resultCode == SQLITE_ROW){
+ return CDSCHECKSUM_CHECKSUMALREADYEXISTS;
+ } else if (resultCode == SQLITE_DONE) {
+
+ } else {
+ return CDSCHECKSUM_FAILED;
+ }
+
+ // Add the checksum to the checksum table.
+
+ resultCode = sqlite3_prepare_v2(db, "INSERT INTO checksums (checksumname, checksumvalue) VALUES(?1, ?2);", -1, &checksumHandle, nullptr);
+
+ if (resultCode != 0){
+ return CDSCHECKSUM_FAILED;
+ }
+
+ resultCode = sqlite3_bind_text(checksumHandle, 1, checksumName.c_str(), -1, SQLITE_STATIC);
+
+ if (resultCode != 0){
+ return CDSCHECKSUM_FAILED;
+ }
+
+ resultCode = sqlite3_bind_text(checksumHandle, 2, checksumValue.c_str(), -1, SQLITE_STATIC);
+
+ if (resultCode != 0){
+ return CDSCHECKSUM_FAILED;
+ }
+
+ resultCode = sqlite3_step(checksumHandle);
+
+ if (resultCode != SQLITE_DONE){
+ return CDSCHECKSUM_FAILED;
+ }
+
+ return CDSCHECKSUM_OK;
+
+}
+
+CDSGetChecksumResult CalendarDataStorage::GetChecksum(string checksumName){
+
+ CDSGetChecksumResult getChecksumResult;
+
+ int resultCode;
+
+ // Check if the checksum already exists.
+
+ sqlite3_stmt *getHandle;
+
+ resultCode = sqlite3_prepare_v2(db, "SELECT checksumvalue from checksums WHERE checksumname=(?1);", -1, &getHandle, nullptr);
+
+ if (resultCode != 0){
+ getChecksumResult.getChecksumResult = CDSCHECKSUM_FAILED;
+ return getChecksumResult;
+ }
+
+ resultCode = sqlite3_bind_text(getHandle, 1, checksumName.c_str(), -1, SQLITE_STATIC);
+
+ if (resultCode != 0){
+ getChecksumResult.getChecksumResult = CDSCHECKSUM_FAILED;
+ return getChecksumResult;
+ }
+
+ resultCode = sqlite3_step(getHandle);
+
+ if (resultCode == SQLITE_ROW){
+ } else if (resultCode == SQLITE_DONE) {
+ getChecksumResult.getChecksumResult = CDSCHECKSUM_NOHASH;
+ return getChecksumResult;
+ } else {
+ getChecksumResult.getChecksumResult = CDSCHECKSUM_FAILED;
+ return getChecksumResult;
+ }
+
+ stringstream checksumStream;
+
+ checksumStream << sqlite3_column_text(getHandle, 0);
+
+ getChecksumResult.checksumValue = checksumStream.str();
+
+ getChecksumResult.getChecksumResult = CDSCHECKSUM_OK;
+
+ return getChecksumResult;
+
+}
+
+CDSChecksumResult CalendarDataStorage::UpdateChecksum(std::string checksumName, std::string checksumValue){
+
+ int resultCode;
+
+ // Check if the checksum already exists.
+
+ sqlite3_stmt *getHandle;
+ sqlite3_stmt *statementHandle;
+
+ resultCode = sqlite3_prepare_v2(db, "SELECT checksumvalue from checksums WHERE checksumname=(?1);", -1, &getHandle, nullptr);
+
+ if (resultCode != 0){
+ return CDSCHECKSUM_FAILED;
+ }
+
+ resultCode = sqlite3_bind_text(getHandle, 1, checksumName.c_str(), -1, SQLITE_STATIC);
+
+ if (resultCode != 0){
+ return CDSCHECKSUM_FAILED;
+ }
+
+ resultCode = sqlite3_step(getHandle);
+
+ if (resultCode == SQLITE_ROW){
+ } else if (resultCode == SQLITE_DONE) {
+ return CDSCHECKSUM_NOHASH;
+ } else {
+ return CDSCHECKSUM_FAILED;
+ }
+
+ // Update the checksum.
+
+ resultCode = sqlite3_prepare_v2(db, "UPDATE checksums SET checksumvalue=(?1) WHERE checksumname=(?2);", -1, &statementHandle, nullptr);
+
+ if (resultCode != 0){
+ return CDSCHECKSUM_FAILED;
+ }
+
+ resultCode = sqlite3_bind_text(statementHandle, 1, checksumValue.c_str(), -1, SQLITE_STATIC);
+
+ if (resultCode != 0){
+ return CDSCHECKSUM_FAILED;
+ }
+
+ resultCode = sqlite3_bind_text(statementHandle, 2, checksumName.c_str(), -1, SQLITE_STATIC);
+
+ if (resultCode != 0){
+ return CDSCHECKSUM_FAILED;
+ }
+
+ resultCode = sqlite3_step(statementHandle);
+
+ if (resultCode != SQLITE_DONE){
+ return CDSCHECKSUM_FAILED;
+ }
+
+ return CDSCHECKSUM_OK;
+
}
\ No newline at end of file