Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
CalendarDataStorage: Added checksum functions
authorSteve Brokenshire <sbrokenshire@xestia.co.uk>
Thu, 29 Dec 2016 19:46:02 +0000 (19:46 +0000)
committerSteve Brokenshire <sbrokenshire@xestia.co.uk>
Thu, 29 Dec 2016 19:46:02 +0000 (19:46 +0000)
Added checksum functions and also update the internal
'internal_updatedata' checksum when accounts, calendars and entries
write operations are made.

source/libraries/CalendarDataStorage/CalendarDataStorage.cpp
source/libraries/CalendarDataStorage/CalendarDataStorage.h

index f516df8..ba82027 100644 (file)
@@ -1,5 +1,7 @@
 #include "CalendarDataStorage.h"
 
+#define CDS_RANDOMPOW 24
+
 #include <iostream>
 
 using namespace std;
@@ -99,8 +101,8 @@ void CalendarDataStorage::SetupTables(){
        // 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);
        
@@ -109,6 +111,15 @@ void CalendarDataStorage::SetupTables(){
                return;
        }
        
+       // Setup internal checksums.
+       
+       CDSChecksumResult addChecksum = AddChecksum("internal_updatedata", "");
+       
+       if (addChecksum != CDSCHECKSUM_OK){
+               DataStorageInitOK = false;
+               return;
+       }
+       
        DataStorageInitOK = true;
        
 }
@@ -146,6 +157,10 @@ CDSAccountResult CalendarDataStorage::AddAccount(string accountName)
                return CDSACCOUNT_FAILED;
        }
        
+       // Update the checksum.
+       
+       UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
+       
        addResult = CDSACCOUNT_OK;
        
        return addResult;
@@ -326,6 +341,10 @@ CDSAccountResult CalendarDataStorage::UpdateAccount(int accountID, string accoun
                return CDSACCOUNT_FAILED;
        }
        
+       // Update the checksum.
+       
+       UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
+       
        updateResult = CDSACCOUNT_OK;
        
        return updateResult;
@@ -420,6 +439,10 @@ CDSAccountResult CalendarDataStorage::DeleteAccount(int accountID)
                
        }
        
+       // Update the checksum.
+       
+       UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
+       
        deleteResult = CDSACCOUNT_OK;
        
        return deleteResult;
@@ -471,6 +494,10 @@ CDSCalendarResult CalendarDataStorage::AddCalendar(int accountID, string calenda
                return CDSCALENDAR_FAILED;
        }
        
+       // Update the checksum.
+       
+       UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
+       
        addResult = CDSCALENDAR_OK;
        
        return addResult;
@@ -569,6 +596,70 @@ CDSGetCalendarInfo CalendarDataStorage::GetCalendar(std::string accountName, std
        
 }
 
+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)
 {
        
@@ -628,6 +719,10 @@ CDSCalendarResult CalendarDataStorage::UpdateCalendar(int calendarID, string cal
                return CDSCALENDAR_FAILED;
        }
        
+       // Update the checksum.
+       
+       UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
+       
        updateResult = CDSCALENDAR_OK;
        
        return updateResult;
@@ -710,6 +805,10 @@ CDSCalendarResult CalendarDataStorage::DeleteCalendar(int calendarID)
                return CDSCALENDAR_FAILED;
        }
        
+       // Update the checksum.
+       
+       UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
+       
        return deleteResult;
        
 }
@@ -1229,6 +1328,10 @@ CDSAddEntryResult CalendarDataStorage::AddEvent(int calendarID, std::string file
        addResult.calendarEntryID = sqlite3_last_insert_rowid(db);
        addResult.addEventResult = CDSENTRY_OK;
        
+       // Update the checksum.
+       
+       UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
+       
        return addResult;
        
 }
@@ -1276,7 +1379,8 @@ CDSGetCalendarEntryInfo CalendarDataStorage::GetEvent(int calendarEntryID)
        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);
@@ -1330,6 +1434,7 @@ CDSGetCalendarEntryInfo CalendarDataStorage::GetEvent(int calendarEntryID)
                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;
@@ -1401,6 +1506,10 @@ CDSEntryResult CalendarDataStorage::DeleteEvent(int calendarEntryID)
                return CDSENTRY_FAILED;
        }
        
+       // Update the checksum.
+       
+       UpdateChecksum("internal_updatedata", to_string(GenerateRandomNumber(CDS_RANDOMPOW)));
+       
        return deleteResult;    
        
 }
@@ -1483,6 +1592,71 @@ CDSEntryList CalendarDataStorage::GetEventList(int calendarID){
        
 }
 
+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;
@@ -1566,4 +1740,172 @@ CDSCalendarList CalendarDataStorage::GetCalendarList(int accountID){
        
        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
index 7387194..4b41b90 100644 (file)
@@ -1,3 +1,6 @@
+#ifndef __LIBRARIES_CALENDARDATASTORAGE_H__
+#define __LIBRARIES_CALENDARDATASTORAGE_H__
+
 #include <string>
 #include <iostream>
 #include <sstream>
@@ -8,9 +11,7 @@
 
 #include "../../objects/calendarevent/CalendarEvent.h"
 #include "../../common/colour.h"
-
-#ifndef __LIBRARIES_CALENDARDATASTORAGE_H__
-#define __LIBRARIES_CALENDARDATASTORAGE_H__
+#include "../../common/random.h"
 
 /*enum CDSAddEntryResult{
        CDSADDENTRY_UNITTESTFAIL = -1,
@@ -110,6 +111,20 @@ struct CDSAccountList{
        CDSAccountResult getAccountListResult = CDSACCOUNT_NOACTION;
 };
 
+enum CDSChecksumResult{
+       CDSCHECKSUM_UNITTESTFAIL = -1,
+       CDSCHECKSUM_OK,
+       CDSCHECKSUM_FAILED,
+       CDSCHECKSUM_NOHASH,
+       CDSCHECKSUM_CHECKSUMALREADYEXISTS,
+       CDSCHECKSUM_NORESULT
+};
+
+struct CDSGetChecksumResult{
+       CDSChecksumResult getChecksumResult = CDSCHECKSUM_NORESULT;
+       std::string checksumValue = "";
+};
+
 class CalendarDataStorage{
 
        private:
@@ -133,6 +148,7 @@ class CalendarDataStorage{
        
                CDSCalendarResult AddCalendar(int accountID, std::string calendarName, std::string calendarID, Colour calendarColour);
                CDSGetCalendarInfo GetCalendar(std::string accountName, std::string calendarTextID);
+               CDSGetCalendarInfo GetCalendar(int calendarID);
                CDSCalendarList GetCalendarList(int accountID);
                CDSCalendarResult UpdateCalendar(int calendarID, std::string calendarName);
                CDSCalendarResult DeleteCalendar(int calendarID);
@@ -142,7 +158,14 @@ class CalendarDataStorage{
                CDSAddEntryResult AddEvent(int calendarID, std::string filename);
                CDSGetCalendarEntryInfo GetEvent(int calendarEntryID);
                CDSEntryList GetEventList(int calendarID);
+               CDSEntryList GetEventListByDate(int calendarYear, int calendarMonth, int calendarDay);
                CDSEntryResult DeleteEvent(int calendarEntryID);
+               
+               // Checksum functions.
+               
+               CDSChecksumResult AddChecksum(std::string checksumName, std::string checksumValue);             
+               CDSGetChecksumResult GetChecksum(std::string checksumName);
+               CDSChecksumResult UpdateChecksum(std::string checksumName, std::string checksumValue);
         
 };
 
Xestia Software Development
Yn Maystri
© 2006 - 2019 Xestia Software Development
Software

Xestia Address Book
Xestia Calendar
Development

Xestia Gelforn
Everything else

About
News
Privacy Policy