Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
tests: Added more tests for CalendarDataStorage
[xestiacalendar/.git] / source / libraries / CalendarDataStorage / CalendarDataStorage.cpp
index c18a85b..63c8a3e 100644 (file)
@@ -20,8 +20,6 @@
 
 #define CDS_RANDOMPOW 24
 
-#include <iostream>
-
 using namespace std;
 
 static int callback(void *NotUsed, int argc, char **argv, char **azColName){
@@ -159,6 +157,33 @@ CDSAccountResult CalendarDataStorage::AddAccount(string accountName, int account
        int resultCode;
        
        sqlite3_stmt *statementHandle;
+       sqlite3_stmt *findHandle;
+       
+       if (accountName == ""){
+               return CDSACCOUNT_NONAME;
+       }
+       
+       // Check if the account name already exsits.
+
+       resultCode = sqlite3_prepare_v2(db, "SELECT name FROM accounts WHERE name=(?1);", -1, &findHandle, nullptr);
+       
+       if (resultCode != 0){
+               return CDSACCOUNT_FAILED;
+       }
+       
+       resultCode = sqlite3_bind_text(findHandle, 1, accountName.c_str(), -1, SQLITE_STATIC);
+       
+       if (resultCode != 0){
+               return CDSACCOUNT_FAILED;
+       }
+       
+       resultCode = sqlite3_step(findHandle);
+       
+       if (resultCode != SQLITE_DONE){
+               return CDSACCOUNT_FAILED;
+       } else if (resultCode == SQLITE_ROW){
+               return CDSACCOUNT_FAILED;
+       }
        
        resultCode = sqlite3_prepare_v2(db, "INSERT INTO accounts (name, preferencesid) VALUES(?1, ?2);", -1, &statementHandle, nullptr);
        
@@ -318,8 +343,13 @@ CDSAccountResult CalendarDataStorage::UpdateAccount(int accountID, string accoun
        int resultCode;
        
        sqlite3_stmt *findHandle;
+       sqlite3_stmt *existingHandle;
        sqlite3_stmt *statementHandle;
        
+       if (accountName == ""){
+               return CDSACCOUNT_NONAME;
+       }
+       
        // Check if account exists first.
        
        resultCode = sqlite3_prepare_v2(db, "SELECT id from accounts WHERE id=(?1);", -1, &findHandle, nullptr);
@@ -344,6 +374,30 @@ CDSAccountResult CalendarDataStorage::UpdateAccount(int accountID, string accoun
                return CDSACCOUNT_FAILED;
        }
        
+       // Check if account with the name given already exists before renaming.
+       
+       resultCode = sqlite3_prepare_v2(db, "SELECT name from accounts WHERE name=(?1);", -1, &existingHandle, nullptr);
+       
+       if (resultCode != 0){
+               return CDSACCOUNT_FAILED;
+       }
+       
+       resultCode = sqlite3_bind_text(existingHandle, 1, accountName.c_str(), -1, SQLITE_STATIC);
+       
+       if (resultCode != 0){
+               return CDSACCOUNT_FAILED;
+       }
+       
+       resultCode = sqlite3_step(existingHandle);
+       
+       if (resultCode == SQLITE_ROW){
+               return CDSACCOUNT_FAILED;
+       } else if (resultCode == SQLITE_DONE) {
+       
+       } else {
+               return CDSACCOUNT_FAILED;
+       }
+       
        // Update the account.
        
        resultCode = sqlite3_prepare_v2(db, "UPDATE accounts SET name=(?1) WHERE id=(?2);", -1, &statementHandle, nullptr);
@@ -411,7 +465,7 @@ CDSAccountResult CalendarDataStorage::DeleteAccount(int accountID)
        } else if (resultCode == SQLITE_DONE) {
                return CDSACCOUNT_NOACCOUNT;
        } else {
-               return CDSACCOUNT_FAILED;
+
        }
        
        // Delete the account.
@@ -484,9 +538,30 @@ CDSCalendarResult CalendarDataStorage::AddCalendar(int accountID, string calenda
 
        CDSCalendarResult addResult = CDSCALENDAR_UNITTESTFAIL;
        int resultCode;
-       
+
+       sqlite3_stmt *findHandle;       
        sqlite3_stmt *statementHandle;
        
+       // Check if the account exists first.
+       
+       resultCode = sqlite3_prepare_v2(db, "SELECT id FROM accounts WHERE id=(?1);", -1, &findHandle, nullptr);
+       
+       if (resultCode != 0){
+               return CDSCALENDAR_FAILED;
+       }       
+
+       resultCode = sqlite3_bind_int(findHandle, 1, accountID);
+
+       if (resultCode != 0){
+               return CDSCALENDAR_FAILED;
+       }
+       
+       resultCode = sqlite3_step(findHandle);
+       
+       if (resultCode != SQLITE_DONE){
+               return CDSCALENDAR_NOACCOUNT;
+       }       
+       
        resultCode = sqlite3_prepare_v2(db, "INSERT INTO calendars (name, calendarid, accountid, colour, description) VALUES(?1, ?2, ?3, ?4, ?5);", -1, &statementHandle, nullptr);
        
        if (resultCode != 0){
@@ -976,14 +1051,14 @@ CDSAddEntryResult CalendarDataStorage::AddEvent(int calendarID, std::string file
        
        // Start Date.
        
-       if (eventData.DateTimeStartData.size() < 16){
+       if (eventData.dateTimeStartData.size() < 16){
                
                addResult.addEventResult = CDSENTRY_INVALIDFILE;
                return addResult;
                
        }
        
-       eventString = eventData.DateTimeStartData.substr(0,4);
+       eventString = eventData.dateTimeStartData.substr(0,4);
        
        if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
                
@@ -996,7 +1071,7 @@ CDSAddEntryResult CalendarDataStorage::AddEvent(int calendarID, std::string file
                
        }
 
-       eventString = eventData.DateTimeStartData.substr(4,2);
+       eventString = eventData.dateTimeStartData.substr(4,2);
        
        if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
                
@@ -1009,7 +1084,7 @@ CDSAddEntryResult CalendarDataStorage::AddEvent(int calendarID, std::string file
                
        }
        
-       eventString = eventData.DateTimeStartData.substr(6,2);
+       eventString = eventData.dateTimeStartData.substr(6,2);
        
        if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
                
@@ -1022,7 +1097,7 @@ CDSAddEntryResult CalendarDataStorage::AddEvent(int calendarID, std::string file
                
        }
 
-       eventString = eventData.DateTimeStartData.substr(9,2);
+       eventString = eventData.dateTimeStartData.substr(9,2);
        
        if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
                
@@ -1035,7 +1110,7 @@ CDSAddEntryResult CalendarDataStorage::AddEvent(int calendarID, std::string file
                
        }
        
-       eventString = eventData.DateTimeStartData.substr(11,2);
+       eventString = eventData.dateTimeStartData.substr(11,2);
        
        if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
                
@@ -1048,7 +1123,7 @@ CDSAddEntryResult CalendarDataStorage::AddEvent(int calendarID, std::string file
                
        }
        
-       eventString = eventData.DateTimeStartData.substr(13,2);
+       eventString = eventData.dateTimeStartData.substr(13,2);
        
        if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
                
@@ -1073,16 +1148,16 @@ CDSAddEntryResult CalendarDataStorage::AddEvent(int calendarID, std::string file
        int eventEndSecond = 0;
        int eventEndDuration = 0;
        
-       if (eventData.DateTimeEndData != ""){
+       if (eventData.dateTimeEndData != ""){
        
-               if (eventData.DateTimeEndData.size() < 16){
+               if (eventData.dateTimeEndData.size() < 16){
                
                        addResult.addEventResult = CDSENTRY_INVALIDFILE;
                        return addResult;
                
                }
        
-               eventString = eventData.DateTimeEndData.substr(0,4);
+               eventString = eventData.dateTimeEndData.substr(0,4);
        
                if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
                
@@ -1095,7 +1170,7 @@ CDSAddEntryResult CalendarDataStorage::AddEvent(int calendarID, std::string file
                
                }
 
-               eventString = eventData.DateTimeEndData.substr(4,2);
+               eventString = eventData.dateTimeEndData.substr(4,2);
        
                if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
                
@@ -1108,7 +1183,7 @@ CDSAddEntryResult CalendarDataStorage::AddEvent(int calendarID, std::string file
                
                }
        
-               eventString = eventData.DateTimeEndData.substr(6,2);
+               eventString = eventData.dateTimeEndData.substr(6,2);
        
                if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
                
@@ -1121,7 +1196,7 @@ CDSAddEntryResult CalendarDataStorage::AddEvent(int calendarID, std::string file
                
                }
 
-               eventString = eventData.DateTimeEndData.substr(9,2);
+               eventString = eventData.dateTimeEndData.substr(9,2);
        
                if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
                
@@ -1134,7 +1209,7 @@ CDSAddEntryResult CalendarDataStorage::AddEvent(int calendarID, std::string file
                
                }
        
-               eventString = eventData.DateTimeEndData.substr(11,2);
+               eventString = eventData.dateTimeEndData.substr(11,2);
        
                if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
                
@@ -1147,7 +1222,7 @@ CDSAddEntryResult CalendarDataStorage::AddEvent(int calendarID, std::string file
                
                }
        
-               eventString = eventData.DateTimeEndData.substr(13,2);
+               eventString = eventData.dateTimeEndData.substr(13,2);
        
                if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
                
@@ -1162,7 +1237,7 @@ CDSAddEntryResult CalendarDataStorage::AddEvent(int calendarID, std::string file
                
        }
 
-       eventString = eventData.DurationData;
+       eventString = eventData.durationData;
        
        // Process the duration data.
        
@@ -1174,22 +1249,22 @@ CDSAddEntryResult CalendarDataStorage::AddEvent(int calendarID, std::string file
        
        // Get the duration (if DTEND hasn't been specified).
        
-       if (eventData.DurationData.size() > 0){
+       if (eventData.durationData.size() > 0){
                
                bool FoundP = false;
                bool FoundW = false;
                bool DateTimeMode = false;
                
-               std::string::iterator eventDataChar = eventData.DurationData.begin();
+               std::string::iterator eventDataChar = eventData.durationData.begin();
                std::string currentValue = "";
                
                if (*eventDataChar != 'P'){
                        
-                       eventDataChar = eventData.DurationData.end();
+                       eventDataChar = eventData.durationData.end();
                        
                }
                
-               for(eventDataChar; eventDataChar != eventData.DurationData.end(); eventDataChar++){
+               for(eventDataChar; eventDataChar != eventData.durationData.end(); eventDataChar++){
                        
                        // Check if value is a digit.
                        
@@ -1258,7 +1333,7 @@ CDSAddEntryResult CalendarDataStorage::AddEvent(int calendarID, std::string file
        
        // Process Entry Name.
        
-       resultCode = sqlite3_bind_text(statementHandle, 2, eventData.SummaryData.c_str(), -1, SQLITE_STATIC);
+       resultCode = sqlite3_bind_text(statementHandle, 2, eventData.summaryData.c_str(), -1, SQLITE_STATIC);
        
        if (resultCode != 0){
                addResult.addEventResult = CDSENTRY_FAILED;
@@ -1270,7 +1345,7 @@ CDSAddEntryResult CalendarDataStorage::AddEvent(int calendarID, std::string file
        string eventDescription;
        
        try {
-               eventDescription = eventData.DescriptionList.at(0);
+               eventDescription = eventData.descriptionList.at(0);
        }
        
        catch (out_of_range &err){
@@ -1507,14 +1582,14 @@ CDSEditEntryResult CalendarDataStorage::UpdateEvent(int eventID, std::string fil
        
        // Start Date.
        
-       if (eventData.DateTimeStartData.size() < 16){
+       if (eventData.dateTimeStartData.size() < 16){
                
                editResult.editEventResult = CDSENTRY_INVALIDFILE;
                return editResult;
                
        }
        
-       eventString = eventData.DateTimeStartData.substr(0,4);
+       eventString = eventData.dateTimeStartData.substr(0,4);
        
        if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
                
@@ -1527,7 +1602,7 @@ CDSEditEntryResult CalendarDataStorage::UpdateEvent(int eventID, std::string fil
                
        }
 
-       eventString = eventData.DateTimeStartData.substr(4,2);
+       eventString = eventData.dateTimeStartData.substr(4,2);
        
        if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
                
@@ -1540,7 +1615,7 @@ CDSEditEntryResult CalendarDataStorage::UpdateEvent(int eventID, std::string fil
                
        }
        
-       eventString = eventData.DateTimeStartData.substr(6,2);
+       eventString = eventData.dateTimeStartData.substr(6,2);
        
        if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
                
@@ -1553,7 +1628,7 @@ CDSEditEntryResult CalendarDataStorage::UpdateEvent(int eventID, std::string fil
                
        }
 
-       eventString = eventData.DateTimeStartData.substr(9,2);
+       eventString = eventData.dateTimeStartData.substr(9,2);
        
        if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
                
@@ -1566,7 +1641,7 @@ CDSEditEntryResult CalendarDataStorage::UpdateEvent(int eventID, std::string fil
                
        }
        
-       eventString = eventData.DateTimeStartData.substr(11,2);
+       eventString = eventData.dateTimeStartData.substr(11,2);
        
        if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
                
@@ -1579,7 +1654,7 @@ CDSEditEntryResult CalendarDataStorage::UpdateEvent(int eventID, std::string fil
                
        }
        
-       eventString = eventData.DateTimeStartData.substr(13,2);
+       eventString = eventData.dateTimeStartData.substr(13,2);
        
        if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
                
@@ -1604,16 +1679,16 @@ CDSEditEntryResult CalendarDataStorage::UpdateEvent(int eventID, std::string fil
        int eventEndSecond = 0;
        int eventEndDuration = 0;
        
-       if (eventData.DateTimeEndData != ""){
+       if (eventData.dateTimeEndData != ""){
        
-               if (eventData.DateTimeEndData.size() < 16){
+               if (eventData.dateTimeEndData.size() < 16){
                
                        editResult.editEventResult = CDSENTRY_INVALIDFILE;
                        return editResult;
                
                }
        
-               eventString = eventData.DateTimeEndData.substr(0,4);
+               eventString = eventData.dateTimeEndData.substr(0,4);
        
                if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
                
@@ -1626,7 +1701,7 @@ CDSEditEntryResult CalendarDataStorage::UpdateEvent(int eventID, std::string fil
                
                }
 
-               eventString = eventData.DateTimeEndData.substr(4,2);
+               eventString = eventData.dateTimeEndData.substr(4,2);
        
                if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
                
@@ -1639,7 +1714,7 @@ CDSEditEntryResult CalendarDataStorage::UpdateEvent(int eventID, std::string fil
                
                }
        
-               eventString = eventData.DateTimeEndData.substr(6,2);
+               eventString = eventData.dateTimeEndData.substr(6,2);
        
                if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
                
@@ -1652,7 +1727,7 @@ CDSEditEntryResult CalendarDataStorage::UpdateEvent(int eventID, std::string fil
                
                }
 
-               eventString = eventData.DateTimeEndData.substr(9,2);
+               eventString = eventData.dateTimeEndData.substr(9,2);
        
                if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
                
@@ -1665,7 +1740,7 @@ CDSEditEntryResult CalendarDataStorage::UpdateEvent(int eventID, std::string fil
                
                }
        
-               eventString = eventData.DateTimeEndData.substr(11,2);
+               eventString = eventData.dateTimeEndData.substr(11,2);
        
                if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
                
@@ -1678,7 +1753,7 @@ CDSEditEntryResult CalendarDataStorage::UpdateEvent(int eventID, std::string fil
                
                }
        
-               eventString = eventData.DateTimeEndData.substr(13,2);
+               eventString = eventData.dateTimeEndData.substr(13,2);
        
                if (all_of(eventString.begin(), eventString.end(), ::isdigit)){
                
@@ -1693,7 +1768,7 @@ CDSEditEntryResult CalendarDataStorage::UpdateEvent(int eventID, std::string fil
                
        }
 
-       eventString = eventData.DurationData;
+       eventString = eventData.durationData;
        
        // Process the duration data.
        
@@ -1705,22 +1780,22 @@ CDSEditEntryResult CalendarDataStorage::UpdateEvent(int eventID, std::string fil
        
        // Get the duration (if DTEND hasn't been specified).
        
-       if (eventData.DurationData.size() > 0){
+       if (eventData.durationData.size() > 0){
                
                bool FoundP = false;
                bool FoundW = false;
                bool DateTimeMode = false;
                
-               std::string::iterator eventDataChar = eventData.DurationData.begin();
+               std::string::iterator eventDataChar = eventData.durationData.begin();
                std::string currentValue = "";
                
                if (*eventDataChar != 'P'){
                        
-                       eventDataChar = eventData.DurationData.end();
+                       eventDataChar = eventData.durationData.end();
                        
                }
                
-               for(eventDataChar; eventDataChar != eventData.DurationData.end(); eventDataChar++){
+               for(eventDataChar; eventDataChar != eventData.durationData.end(); eventDataChar++){
                        
                        // Check if value is a digit.
                        
@@ -1787,7 +1862,7 @@ CDSEditEntryResult CalendarDataStorage::UpdateEvent(int eventID, std::string fil
        
        // Process Entry Name.
        
-       resultCode = sqlite3_bind_text(statementHandle, 2, eventData.SummaryData.c_str(), -1, SQLITE_STATIC);
+       resultCode = sqlite3_bind_text(statementHandle, 2, eventData.summaryData.c_str(), -1, SQLITE_STATIC);
        
        if (resultCode != 0){
                editResult.editEventResult = CDSENTRY_FAILED;
@@ -1799,7 +1874,7 @@ CDSEditEntryResult CalendarDataStorage::UpdateEvent(int eventID, std::string fil
        string eventDescription;
        
        try {
-               eventDescription = eventData.DescriptionList.at(0);
+               eventDescription = eventData.descriptionList.at(0);
        }
        
        catch (out_of_range &err){
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