OODataBase.cpp
From Giona
(Difference between revisions)
| Line 1: | Line 1: | ||
| + | <pre> | ||
| + | /************************************************************************ | ||
| + | * This class is an abstraction of an Object Oriented Database | ||
| + | * that can manage generic objects and attributes | ||
| + | * ------------------------------------------------------------------- | ||
| + | * Author: Rosario Marino | ||
| + | ************************************************************************/ | ||
| + | |||
| + | #include "OODataBase.h" | ||
| + | |||
| + | /* | ||
| + | * Constructor | ||
| + | */ | ||
| + | OODataBase::OODataBase() | ||
| + | { | ||
| + | Debug::debug("OODataBase::OODataBase()"); | ||
| + | this->db = new SqliteDb(); | ||
| + | this->lastError = NO_ERROR; | ||
| + | } | ||
| + | |||
| + | /* | ||
| + | * Destructor | ||
| + | */ | ||
| + | OODataBase::~OODataBase() | ||
| + | { | ||
| + | Debug::debug("OODataBase::~OODataBase()"); | ||
| + | delete (this->db); | ||
| + | } | ||
| + | |||
| + | /* | ||
| + | * Returns last error code | ||
| + | */ | ||
| + | int OODataBase::getLastError() | ||
| + | { | ||
| + | Debug::debug("int OODataBase::getLastError()"); | ||
| + | return this->lastError; | ||
| + | } | ||
| + | |||
| + | /* | ||
| + | * Sets the name of the database. Returns false in case of error | ||
| + | */ | ||
| + | bool OODataBase::setDatabaseName(STRING databaseName) | ||
| + | { | ||
| + | Debug::debug("bool OODataBase::setDatabaseName(STRING databaseName)"); | ||
| + | if (!this->db->setDataBaseName(databaseName)) | ||
| + | { | ||
| + | STRING errorTxt=EMPTY_STRING; | ||
| + | switch(this->db->getLastError()) | ||
| + | { | ||
| + | case SqliteDb::ERROR_NULL_PARAMETER: | ||
| + | errorTxt="ERROR_NULL_PARAMETER in parameter databaseName"; | ||
| + | break; | ||
| + | case SqliteDb::ERROR_EMPTY_STRING: | ||
| + | errorTxt="ERROR_EMPTY_STRING in parameter databaseName"; | ||
| + | break; | ||
| + | default: | ||
| + | errorTxt="UNDEFINED ERROR"; | ||
| + | } | ||
| + | |||
| + | Debug::debugError("bool OODataBase::setDatabaseName(STRING databaseName)","ERROR_SQLITEDB", errorTxt); | ||
| + | this->lastError=ERROR_SQLITEDB; | ||
| + | return false; | ||
| + | } | ||
| + | return true; | ||
| + | } | ||
| + | |||
| + | /* | ||
| + | * Returns the name of the database | ||
| + | */ | ||
| + | STRING OODataBase::getDatabaseName() | ||
| + | { | ||
| + | Debug::debug("STRING OODataBase::getDatabaseName()"); | ||
| + | return this->db->getDatabaseName(); | ||
| + | } | ||
| + | |||
| + | /* | ||
| + | * Makes the connection to the database | ||
| + | */ | ||
| + | bool OODataBase::connect() | ||
| + | { | ||
| + | Debug::debug("bool OODataBase::connect()"); | ||
| + | if(!this->db->openDataBase()) | ||
| + | { | ||
| + | STRING errorTxt=EMPTY_STRING; | ||
| + | switch(this->db->getLastError()) | ||
| + | { | ||
| + | case SqliteDb::ERROR_CONNECTION_OPEN_YET: | ||
| + | errorTxt="ERROR_CONNECTION_OPEN_YET - the connection is open yet"; | ||
| + | break; | ||
| + | case SqliteDb::ERROR_DATABASE_NAME_NOT_SET: | ||
| + | errorTxt="ERROR_DATABASE_NAME_NOT_SET"; | ||
| + | break; | ||
| + | case SqliteDb::ERROR_SQLITE_ERROR: | ||
| + | errorTxt="ERROR_SQLITE_ERROR - generic sqlite error: try to run with debug error option activated"; | ||
| + | break; | ||
| + | default: | ||
| + | errorTxt="UNDEFINED ERROR"; | ||
| + | } | ||
| + | |||
| + | Debug::debugError("bool OODataBase::connect()","ERROR_SQLITEDB", errorTxt); | ||
| + | this->lastError=ERROR_SQLITEDB; | ||
| + | return false; | ||
| + | } | ||
| + | if(!this->init()) | ||
| + | return false; | ||
| + | return true; | ||
| + | } | ||
| + | |||
| + | /* | ||
| + | * Closes the connection to the database | ||
| + | */ | ||
| + | bool OODataBase::disconnect() | ||
| + | { | ||
| + | Debug::debug("bool OODataBase::disconnect()"); | ||
| + | if(!this->db->closeDataBase()) | ||
| + | { | ||
| + | STRING errorTxt=EMPTY_STRING; | ||
| + | switch(this->db->getLastError()) | ||
| + | { | ||
| + | case SqliteDb::ERROR_CONNECTION_NOT_OPEN: | ||
| + | errorTxt="ERROR_CONNECTION_NOT_OPEN - the connection is not open"; | ||
| + | break; | ||
| + | case SqliteDb::ERROR_SQLITE_ERROR: | ||
| + | errorTxt="ERROR_SQLITE_ERROR - generic sqlite error: try to run with debug error option activated"; | ||
| + | break; | ||
| + | default: | ||
| + | errorTxt="UNDEFINED ERROR"; | ||
| + | } | ||
| + | |||
| + | Debug::debugError("bool OODataBase::connect()","ERROR_SQLITEDB", errorTxt); | ||
| + | this->lastError=ERROR_SQLITEDB; | ||
| + | return false; | ||
| + | } | ||
| + | return true; | ||
| + | } | ||
| + | |||
| + | /* | ||
| + | * Initialization of database: creates tables and relations if | ||
| + | * they dont exist | ||
| + | */ | ||
| + | bool OODataBase::init() | ||
| + | { | ||
| + | Debug::debug("bool OODataBase::init()"); | ||
| + | STRING sql=EMPTY_STRING; | ||
| + | sql = "CREATE TABLE IF NOT EXISTS Object (ObjectName TEXT PRIMARY KEY, ObjectDescription TEXT); \\ | ||
| + | CREATE TABLE IF NOT EXISTS Attribute (AttributeName TEXT , ObjectName TEXT CONSTRAINT fk_object_name REFERENCES Object(ObjectName), AttributeValue TEXT NOT NULL, AttributeDescription TEXT, PRIMARY KEY (AttributeName,ObjectName)); \\ | ||
| + | CREATE UNIQUE INDEX IF NOT EXISTS ObjName ON Object (ObjectName); \\ | ||
| + | CREATE UNIQUE INDEX IF NOT EXISTS AttName ON Attribute (AttributeName)"; | ||
| + | |||
| + | if(!this->db->executeSqlCommand(sql)) | ||
| + | { | ||
| + | STRING errorTxt=EMPTY_STRING; | ||
| + | switch(this->db->getLastError()) | ||
| + | { | ||
| + | case SqliteDb::ERROR_CONNECTION_NOT_OPEN: | ||
| + | errorTxt="ERROR_CONNECTION_NOT_OPEN - the connection is not open"; | ||
| + | break; | ||
| + | case SqliteDb::ERROR_SQLITE_ERROR: | ||
| + | errorTxt="ERROR_SQLITE_ERROR - generic sqlite error: try to run with debug error option activated"; | ||
| + | break; | ||
| + | default: | ||
| + | errorTxt="UNDEFINED ERROR"; | ||
| + | } | ||
| + | |||
| + | Debug::debugError("bool OODataBase::init()","ERROR_SQLITEDB", errorTxt); | ||
| + | this->lastError=ERROR_SQLITEDB; | ||
| + | return false; | ||
| + | } | ||
| + | return true; | ||
| + | } | ||
| + | |||
| + | /* | ||
| + | * Stores the object only if there is not another object with the same | ||
| + | * name stored yet. | ||
| + | * Returns false in case of error | ||
| + | */ | ||
| + | bool OODataBase::storeNewObject(GenericObject* object) | ||
| + | { | ||
| + | Debug::debug("bool OODataBase::storeNewObject(GenericObject* object)"); | ||
| + | if(object==NULL) | ||
| + | { | ||
| + | Debug::debugError("bool OODataBase::storeNewObject(GenericObject* object)","ERROR_NULL_PARAMETER","parameter: object"); | ||
| + | this->lastError = ERROR_NULL_PARAMETER; | ||
| + | return false; | ||
| + | } | ||
| + | |||
| + | STRING name=strdup(object->getName()); | ||
| + | STRING description=strdup(object->getDescription()); | ||
| + | |||
| + | char sql[4096]; | ||
| + | sql[0]='\\0'; | ||
| + | |||
| + | sprintf(sql, "SELECT * FROM Object WHERE ObjectName = '%s'", this->db->apex(name)); | ||
| + | |||
| + | SqliteDBResultSet* rs = this->db->executeQuery(sql); | ||
| + | if(rs==NULL) | ||
| + | { | ||
| + | STRING errorTxt=EMPTY_STRING; | ||
| + | switch(this->db->getLastError()) | ||
| + | { | ||
| + | case SqliteDb::ERROR_CONNECTION_NOT_OPEN: | ||
| + | errorTxt="ERROR_CONNECTION_NOT_OPEN - the connection is not open"; | ||
| + | break; | ||
| + | case SqliteDb::ERROR_SQLITE_ERROR: | ||
| + | errorTxt="ERROR_SQLITE_ERROR - generic sqlite error: try to run with debug error option activated"; | ||
| + | break; | ||
| + | default: | ||
| + | errorTxt="UNDEFINED ERROR"; | ||
| + | } | ||
| + | Debug::debugError("OODataBase::storeNewObject(GenericObject* object)","ERROR_SQLITEDB", errorTxt); | ||
| + | this->lastError=ERROR_SQLITEDB; | ||
| + | return false; | ||
| + | } | ||
| + | if(rs->getNRows()>0) | ||
| + | { | ||
| + | Debug::debugError("OODataBase::storeNewObject(GenericObject* object)","ERROR_OBJECT_EXISTS_YET","You are trying to store an object that is yet stored"); | ||
| + | this->lastError=ERROR_OBJECT_EXISTS_YET; | ||
| + | delete(rs); | ||
| + | return false; | ||
| + | } | ||
| + | delete(rs); | ||
| + | if(!this->db->executeSqlCommand("BEGIN TRANSACTION")) | ||
| + | { | ||
| + | STRING errorTxt=EMPTY_STRING; | ||
| + | switch(this->db->getLastError()) | ||
| + | { | ||
| + | case SqliteDb::ERROR_CONNECTION_NOT_OPEN: | ||
| + | errorTxt="ERROR_CONNECTION_NOT_OPEN - the connection is not open"; | ||
| + | break; | ||
| + | case SqliteDb::ERROR_SQLITE_ERROR: | ||
| + | errorTxt="ERROR_SQLITE_ERROR - generic sqlite error: try to run with debug error option activated"; | ||
| + | break; | ||
| + | default: | ||
| + | errorTxt="UNDEFINED ERROR"; | ||
| + | } | ||
| + | Debug::debugError("OODataBase::storeNewObject(GenericObject* object)","ERROR_SQLITEDB", errorTxt); | ||
| + | this->lastError=ERROR_SQLITEDB; | ||
| + | return false; | ||
| + | } | ||
| + | int attributeNumber = object->getAttributeNumber(); | ||
| + | sql[0]='\\0'; | ||
| + | sprintf(sql, "INSERT INTO Object (ObjectName, ObjectDescription) VALUES ('%s','%s')", | ||
| + | this->db->apex(name), | ||
| + | this->db->apex(description)); | ||
| + | |||
| + | if(!this->db->executeSqlCommand(sql)) | ||
| + | { | ||
| + | STRING errorTxt=EMPTY_STRING; | ||
| + | switch(this->db->getLastError()) | ||
| + | { | ||
| + | case SqliteDb::ERROR_CONNECTION_NOT_OPEN: | ||
| + | errorTxt="ERROR_CONNECTION_NOT_OPEN - the connection is not open"; | ||
| + | break; | ||
| + | case SqliteDb::ERROR_SQLITE_ERROR: | ||
| + | errorTxt="ERROR_SQLITE_ERROR - generic sqlite error: try to run with debug error option activated"; | ||
| + | break; | ||
| + | default: | ||
| + | errorTxt="UNDEFINED ERROR"; | ||
| + | } | ||
| + | Debug::debugError("OODataBase::storeNewObject(GenericObject* object)","ERROR_SQLITEDB", errorTxt); | ||
| + | this->lastError=ERROR_SQLITEDB; | ||
| + | this->db->executeSqlCommand("ROLLBACK TRANSACTION"); | ||
| + | return false; | ||
| + | } | ||
| + | if(attributeNumber>0) | ||
| + | { | ||
| + | STRING* attributes=object->getAttributeNameList(); | ||
| + | for (int i=0; i<attributeNumber; i++) | ||
| + | { | ||
| + | sql[0]='\\0'; | ||
| + | sprintf(sql,"INSERT INTO Attribute (AttributeName, AttributeValue, AttributeDescription, ObjectName) VALUES('%s','%s','%s','%s')", | ||
| + | this->db->apex(attributes[i]), | ||
| + | this->db->apex(object->getAttributeValue(attributes[i])), | ||
| + | this->db->apex(object->getAttributeDescription(attributes[i])), | ||
| + | this->db->apex(object->getName())); | ||
| + | if(!this->db->executeSqlCommand(sql)) | ||
| + | { | ||
| + | STRING errorTxt=EMPTY_STRING; | ||
| + | switch(this->db->getLastError()) | ||
| + | { | ||
| + | case SqliteDb::ERROR_CONNECTION_NOT_OPEN: | ||
| + | errorTxt="ERROR_CONNECTION_NOT_OPEN - the connection is not open"; | ||
| + | break; | ||
| + | case SqliteDb::ERROR_SQLITE_ERROR: | ||
| + | errorTxt="ERROR_SQLITE_ERROR - generic sqlite error: try to run with debug error option activated"; | ||
| + | break; | ||
| + | default: | ||
| + | errorTxt="UNDEFINED ERROR"; | ||
| + | } | ||
| + | Debug::debugError("OODataBase::storeNewObject(GenericObject* object)","ERROR_SQLITEDB", errorTxt); | ||
| + | this->lastError=ERROR_SQLITEDB; | ||
| + | this->db->executeSqlCommand("ROLLBACK TRANSACTION"); | ||
| + | return false; | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | if(!this->db->executeSqlCommand("COMMIT TRANSACTION")) | ||
| + | { | ||
| + | STRING errorTxt=EMPTY_STRING; | ||
| + | switch(this->db->getLastError()) | ||
| + | { | ||
| + | case SqliteDb::ERROR_CONNECTION_NOT_OPEN: | ||
| + | errorTxt="ERROR_CONNECTION_NOT_OPEN - the connection is not open"; | ||
| + | break; | ||
| + | case SqliteDb::ERROR_SQLITE_ERROR: | ||
| + | errorTxt="ERROR_SQLITE_ERROR - generic sqlite error: try to run with debug error option activated"; | ||
| + | break; | ||
| + | default: | ||
| + | errorTxt="UNDEFINED ERROR"; | ||
| + | } | ||
| + | Debug::debugError("OODataBase::storeNewObject(GenericObject* object)","ERROR_SQLITEDB", errorTxt); | ||
| + | this->lastError=ERROR_SQLITEDB; | ||
| + | this->db->executeSqlCommand("ROLLBACK TRANSACTION"); | ||
| + | return false; | ||
| + | } | ||
| + | return true; | ||
| + | } | ||
| + | |||
| + | /* | ||
| + | * Gets the object stored in the Database and returns it | ||
| + | * Returns NULL in case of error or if the object does not exist | ||
| + | */ | ||
| + | GenericObject* OODataBase::getObjectByName(STRING name) | ||
| + | { | ||
| + | Debug::debug("GenericObject* OODataBase::getObjectByName(STRING name)"); | ||
| + | if(name==NULL) | ||
| + | { | ||
| + | Debug::debugError("GenericObject* OODataBase::getObjectByName(STRING name)","ERROR_NULL_PARAMETER","parameter: name"); | ||
| + | this->lastError = ERROR_NULL_PARAMETER; | ||
| + | return NULL; | ||
| + | } | ||
| + | if(strcmp(name,EMPTY_STRING)==0) | ||
| + | { | ||
| + | Debug::debugError("GenericObject* OODataBase::getObjectByName(STRING name)","ERROR_EMPTY_STRING","parameter: name"); | ||
| + | this->lastError = ERROR_EMPTY_STRING; | ||
| + | return NULL; | ||
| + | } | ||
| + | char sql[4096]; | ||
| + | sql[0]='\\0'; | ||
| + | sprintf(sql, "SELECT * FROM Object WHERE ObjectName = '%s'", this->db->apex(name)); | ||
| + | |||
| + | SqliteDBResultSet* rs = this->db->executeQuery(sql); | ||
| + | if(rs==NULL) | ||
| + | { | ||
| + | STRING errorTxt=EMPTY_STRING; | ||
| + | switch(this->db->getLastError()) | ||
| + | { | ||
| + | case SqliteDb::ERROR_CONNECTION_NOT_OPEN: | ||
| + | errorTxt="ERROR_CONNECTION_NOT_OPEN - the connection is not open"; | ||
| + | break; | ||
| + | case SqliteDb::ERROR_SQLITE_ERROR: | ||
| + | errorTxt="ERROR_SQLITE_ERROR - generic sqlite error: try to run with debug error option activated"; | ||
| + | break; | ||
| + | default: | ||
| + | errorTxt="UNDEFINED ERROR"; | ||
| + | } | ||
| + | Debug::debugError("GenericObject* OODataBase::getObjectByName(STRING name)","ERROR_SQLITEDB", errorTxt); | ||
| + | this->lastError=ERROR_SQLITEDB; | ||
| + | return NULL; | ||
| + | } | ||
| + | if(rs->getNRows()==0) | ||
| + | { | ||
| + | Debug::debugError("GenericObject* OODataBase::getObjectByName(STRING name)","ERROR_OBJECT_NOT_FOUND", name); | ||
| + | this->lastError=ERROR_OBJECT_NOT_FOUND; | ||
| + | return NULL; | ||
| + | } | ||
| + | GenericObject* object = new GenericObject(); | ||
| + | object->setName(rs->getFieldValue(0,0)); | ||
| + | object->setDescription(rs->getFieldValue(0,1)); | ||
| + | delete(rs); | ||
| + | sql[0]='\\0'; | ||
| + | sprintf(sql, "SELECT * FROM Attribute WHERE ObjectName = '%s'", this->db->apex(name)); | ||
| + | rs = this->db->executeQuery(sql); | ||
| + | if(rs==NULL) | ||
| + | { | ||
| + | STRING errorTxt=EMPTY_STRING; | ||
| + | switch(this->db->getLastError()) | ||
| + | { | ||
| + | case SqliteDb::ERROR_CONNECTION_NOT_OPEN: | ||
| + | errorTxt="ERROR_CONNECTION_NOT_OPEN - the connection is not open"; | ||
| + | break; | ||
| + | case SqliteDb::ERROR_SQLITE_ERROR: | ||
| + | errorTxt="ERROR_SQLITE_ERROR - generic sqlite error: try to run with debug error option activated"; | ||
| + | break; | ||
| + | default: | ||
| + | errorTxt="UNDEFINED ERROR"; | ||
| + | } | ||
| + | Debug::debugError("GenericObject* OODataBase::getObjectByName(STRING name)","ERROR_SQLITEDB", errorTxt); | ||
| + | this->lastError=ERROR_SQLITEDB; | ||
| + | return NULL; | ||
| + | } | ||
| + | for(int i=0; i<rs->getNRows(); i++) | ||
| + | { | ||
| + | object->setAttributeValue(rs->getFieldValue(i,0),rs->getFieldValue(i,2)); | ||
| + | object->setAttributeDescription(rs->getFieldValue(i,0),rs->getFieldValue(i,3)); | ||
| + | } | ||
| + | delete(rs); | ||
| + | return object; | ||
| + | } | ||
| + | |||
| + | /* | ||
| + | * Returns the list of names of objects stored in the DB | ||
| + | */ | ||
| + | STRING* OODataBase::getObjectNameList() | ||
| + | { | ||
| + | Debug::debug("STRING* OODataBase::getObjectNameList()"); | ||
| + | SqliteDBResultSet* rs = this->db->executeQuery("SELECT * FROM Object"); | ||
| + | if(rs==NULL) | ||
| + | { | ||
| + | STRING errorTxt=EMPTY_STRING; | ||
| + | switch(this->db->getLastError()) | ||
| + | { | ||
| + | case SqliteDb::ERROR_CONNECTION_NOT_OPEN: | ||
| + | errorTxt="ERROR_CONNECTION_NOT_OPEN - the connection is not open"; | ||
| + | break; | ||
| + | case SqliteDb::ERROR_SQLITE_ERROR: | ||
| + | errorTxt="ERROR_SQLITE_ERROR - generic sqlite error: try to run with debug error option activated"; | ||
| + | break; | ||
| + | default: | ||
| + | errorTxt="UNDEFINED ERROR"; | ||
| + | } | ||
| + | Debug::debugError("STRING* OODataBase::getObjectNameList()","ERROR_SQLITEDB", errorTxt); | ||
| + | this->lastError=ERROR_SQLITEDB; | ||
| + | return NULL; | ||
| + | } | ||
| + | |||
| + | STRING* result = new STRING[rs->getNRows()]; | ||
| + | for(int i=0; i<rs->getNRows(); i++) | ||
| + | result[i]=rs->getFieldValue(i,0); | ||
| + | delete(rs); | ||
| + | return result; | ||
| + | } | ||
| + | |||
| + | /* | ||
| + | * Stores the changes made to the object | ||
| + | * Returns false in case of error. | ||
| + | */ | ||
| + | bool OODataBase::updateObject(GenericObject* object) | ||
| + | { | ||
| + | Debug::debug("bool OODataBase::updateObject(GenericObject* object)"); | ||
| + | if(object==NULL) | ||
| + | { | ||
| + | Debug::debugError("bool OODataBase::updateObject(GenericObject* object)","ERROR_NULL_PARAMETER","parameter: object"); | ||
| + | this->lastError = ERROR_NULL_PARAMETER; | ||
| + | return false; | ||
| + | } | ||
| + | |||
| + | STRING name=strdup(object->getName()); | ||
| + | STRING description=strdup(object->getDescription()); | ||
| + | |||
| + | char sql[4096]; | ||
| + | sql[0]='\\0'; | ||
| + | |||
| + | sprintf(sql, "SELECT * FROM Object WHERE ObjectName = '%s'", this->db->apex(name)); | ||
| + | |||
| + | SqliteDBResultSet* rs = this->db->executeQuery(sql); | ||
| + | if(rs==NULL) | ||
| + | { | ||
| + | STRING errorTxt=EMPTY_STRING; | ||
| + | switch(this->db->getLastError()) | ||
| + | { | ||
| + | case SqliteDb::ERROR_CONNECTION_NOT_OPEN: | ||
| + | errorTxt="ERROR_CONNECTION_NOT_OPEN - the connection is not open"; | ||
| + | break; | ||
| + | case SqliteDb::ERROR_SQLITE_ERROR: | ||
| + | errorTxt="ERROR_SQLITE_ERROR - generic sqlite error: try to run with debug error option activated"; | ||
| + | break; | ||
| + | default: | ||
| + | errorTxt="UNDEFINED ERROR"; | ||
| + | } | ||
| + | Debug::debugError("bool OODataBase::updateObject(GenericObject* object)","ERROR_SQLITEDB", errorTxt); | ||
| + | this->lastError=ERROR_SQLITEDB; | ||
| + | return false; | ||
| + | } | ||
| + | if(rs->getNRows()==0) | ||
| + | { | ||
| + | Debug::debugError("bool OODataBase::updateObject(GenericObject* object)","ERROR_OBJECT_NOT_FOUND","You are trying to update an object that is not stored in the database"); | ||
| + | this->lastError=ERROR_OBJECT_NOT_FOUND; | ||
| + | delete(rs); | ||
| + | return false; | ||
| + | } | ||
| + | delete(rs); | ||
| + | if(!this->db->executeSqlCommand("BEGIN TRANSACTION")) | ||
| + | { | ||
| + | STRING errorTxt=EMPTY_STRING; | ||
| + | switch(this->db->getLastError()) | ||
| + | { | ||
| + | case SqliteDb::ERROR_CONNECTION_NOT_OPEN: | ||
| + | errorTxt="ERROR_CONNECTION_NOT_OPEN - the connection is not open"; | ||
| + | break; | ||
| + | case SqliteDb::ERROR_SQLITE_ERROR: | ||
| + | errorTxt="ERROR_SQLITE_ERROR - generic sqlite error: try to run with debug error option activated"; | ||
| + | break; | ||
| + | default: | ||
| + | errorTxt="UNDEFINED ERROR"; | ||
| + | } | ||
| + | Debug::debugError("bool OODataBase::updateObject(GenericObject* object)","ERROR_SQLITEDB", errorTxt); | ||
| + | this->lastError=ERROR_SQLITEDB; | ||
| + | return false; | ||
| + | } | ||
| + | int attributeNumber = object->getAttributeNumber(); | ||
| + | sql[0]='\\0'; | ||
| + | sprintf(sql, "UPDATE Object SET ObjectDescription ='%s' WHERE ObjectName = '%s'", | ||
| + | this->db->apex(description), | ||
| + | this->db->apex(name)); | ||
| + | |||
| + | if(!this->db->executeSqlCommand(sql)) | ||
| + | { | ||
| + | STRING errorTxt=EMPTY_STRING; | ||
| + | switch(this->db->getLastError()) | ||
| + | { | ||
| + | case SqliteDb::ERROR_CONNECTION_NOT_OPEN: | ||
| + | errorTxt="ERROR_CONNECTION_NOT_OPEN - the connection is not open"; | ||
| + | break; | ||
| + | case SqliteDb::ERROR_SQLITE_ERROR: | ||
| + | errorTxt="ERROR_SQLITE_ERROR - generic sqlite error: try to run with debug error option activated"; | ||
| + | break; | ||
| + | default: | ||
| + | errorTxt="UNDEFINED ERROR"; | ||
| + | } | ||
| + | Debug::debugError("bool OODataBase::updateObject(GenericObject* object)","ERROR_SQLITEDB", errorTxt); | ||
| + | this->lastError=ERROR_SQLITEDB; | ||
| + | this->db->executeSqlCommand("ROLLBACK TRANSACTION"); | ||
| + | return false; | ||
| + | } | ||
| + | sql[0]='\\0'; | ||
| + | sprintf(sql, "DELETE FROM Attribute WHERE ObjectName = '%s'", | ||
| + | this->db->apex(name)); | ||
| + | if(!this->db->executeSqlCommand(sql)) | ||
| + | { | ||
| + | STRING errorTxt=EMPTY_STRING; | ||
| + | switch(this->db->getLastError()) | ||
| + | { | ||
| + | case SqliteDb::ERROR_CONNECTION_NOT_OPEN: | ||
| + | errorTxt="ERROR_CONNECTION_NOT_OPEN - the connection is not open"; | ||
| + | break; | ||
| + | case SqliteDb::ERROR_SQLITE_ERROR: | ||
| + | errorTxt="ERROR_SQLITE_ERROR - generic sqlite error: try to run with debug error option activated"; | ||
| + | break; | ||
| + | default: | ||
| + | errorTxt="UNDEFINED ERROR"; | ||
| + | } | ||
| + | Debug::debugError("bool OODataBase::updateObject(GenericObject* object)","ERROR_SQLITEDB", errorTxt); | ||
| + | this->lastError=ERROR_SQLITEDB; | ||
| + | this->db->executeSqlCommand("ROLLBACK TRANSACTION"); | ||
| + | return false; | ||
| + | } | ||
| + | if(attributeNumber>0) | ||
| + | { | ||
| + | STRING* attributes=object->getAttributeNameList(); | ||
| + | for (int i=0; i<attributeNumber; i++) | ||
| + | { | ||
| + | sql[0]='\\0'; | ||
| + | sprintf(sql,"INSERT INTO Attribute (AttributeName, AttributeValue, AttributeDescription, ObjectName) VALUES('%s','%s','%s','%s')", | ||
| + | this->db->apex(attributes[i]), | ||
| + | this->db->apex(object->getAttributeValue(attributes[i])), | ||
| + | this->db->apex(object->getAttributeDescription(attributes[i])), | ||
| + | this->db->apex(object->getName())); | ||
| + | if(!this->db->executeSqlCommand(sql)) | ||
| + | { | ||
| + | STRING errorTxt=EMPTY_STRING; | ||
| + | switch(this->db->getLastError()) | ||
| + | { | ||
| + | case SqliteDb::ERROR_CONNECTION_NOT_OPEN: | ||
| + | errorTxt="ERROR_CONNECTION_NOT_OPEN - the connection is not open"; | ||
| + | break; | ||
| + | case SqliteDb::ERROR_SQLITE_ERROR: | ||
| + | errorTxt="ERROR_SQLITE_ERROR - generic sqlite error: try to run with debug error option activated"; | ||
| + | break; | ||
| + | default: | ||
| + | errorTxt="UNDEFINED ERROR"; | ||
| + | } | ||
| + | Debug::debugError("bool OODataBase::updateObject(GenericObject* object)","ERROR_SQLITEDB", errorTxt); | ||
| + | this->lastError=ERROR_SQLITEDB; | ||
| + | this->db->executeSqlCommand("ROLLBACK TRANSACTION"); | ||
| + | return false; | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | if(!this->db->executeSqlCommand("COMMIT TRANSACTION")) | ||
| + | { | ||
| + | STRING errorTxt=EMPTY_STRING; | ||
| + | switch(this->db->getLastError()) | ||
| + | { | ||
| + | case SqliteDb::ERROR_CONNECTION_NOT_OPEN: | ||
| + | errorTxt="ERROR_CONNECTION_NOT_OPEN - the connection is not open"; | ||
| + | break; | ||
| + | case SqliteDb::ERROR_SQLITE_ERROR: | ||
| + | errorTxt="ERROR_SQLITE_ERROR - generic sqlite error: try to run with debug error option activated"; | ||
| + | break; | ||
| + | default: | ||
| + | errorTxt="UNDEFINED ERROR"; | ||
| + | } | ||
| + | Debug::debugError("bool OODataBase::updateObject(GenericObject* object)","ERROR_SQLITEDB", errorTxt); | ||
| + | this->lastError=ERROR_SQLITEDB; | ||
| + | this->db->executeSqlCommand("ROLLBACK TRANSACTION"); | ||
| + | return false; | ||
| + | } | ||
| + | return true; | ||
| + | } | ||
| + | |||
| + | /* | ||
| + | * Deletes the object from the database | ||
| + | * Returns false in case of error | ||
| + | */ | ||
| + | bool OODataBase::deleteObject(STRING name) | ||
| + | { | ||
| + | Debug::debug("bool OODataBase::deleteObject(STRING name)"); | ||
| + | if(name==NULL) | ||
| + | { | ||
| + | Debug::debugError("bool OODataBase::deleteObject(STRING name)","ERROR_NULL_PARAMETER","parameter: name"); | ||
| + | this->lastError = ERROR_NULL_PARAMETER; | ||
| + | return false; | ||
| + | } | ||
| + | if(strcmp(name,EMPTY_STRING)==0) | ||
| + | { | ||
| + | Debug::debugError("bool OODataBase::deleteObject(STRING name)","ERROR_EMPTY_STRING","parameter: name"); | ||
| + | this->lastError = ERROR_EMPTY_STRING; | ||
| + | return false; | ||
| + | } | ||
| + | if(!this->db->executeSqlCommand("BEGIN TRANSACTION")) | ||
| + | { | ||
| + | STRING errorTxt=EMPTY_STRING; | ||
| + | switch(this->db->getLastError()) | ||
| + | { | ||
| + | case SqliteDb::ERROR_CONNECTION_NOT_OPEN: | ||
| + | errorTxt="ERROR_CONNECTION_NOT_OPEN - the connection is not open"; | ||
| + | break; | ||
| + | case SqliteDb::ERROR_SQLITE_ERROR: | ||
| + | errorTxt="ERROR_SQLITE_ERROR - generic sqlite error: try to run with debug error option activated"; | ||
| + | break; | ||
| + | default: | ||
| + | errorTxt="UNDEFINED ERROR"; | ||
| + | } | ||
| + | Debug::debugError("bool OODataBase::deleteObject(STRING name)","ERROR_SQLITEDB", errorTxt); | ||
| + | this->lastError=ERROR_SQLITEDB; | ||
| + | return false; | ||
| + | } | ||
| + | char sql[4096]; | ||
| + | sql[0]='\\0'; | ||
| + | sprintf(sql, "DELETE FROM Attribute WHERE ObjectName = '%s'", | ||
| + | this->db->apex(name)); | ||
| + | if(!this->db->executeSqlCommand(sql)) | ||
| + | { | ||
| + | STRING errorTxt=EMPTY_STRING; | ||
| + | switch(this->db->getLastError()) | ||
| + | { | ||
| + | case SqliteDb::ERROR_CONNECTION_NOT_OPEN: | ||
| + | errorTxt="ERROR_CONNECTION_NOT_OPEN - the connection is not open"; | ||
| + | break; | ||
| + | case SqliteDb::ERROR_SQLITE_ERROR: | ||
| + | errorTxt="ERROR_SQLITE_ERROR - generic sqlite error: try to run with debug error option activated"; | ||
| + | break; | ||
| + | default: | ||
| + | errorTxt="UNDEFINED ERROR"; | ||
| + | } | ||
| + | Debug::debugError("bool OODataBase::deleteObject(STRING name)","ERROR_SQLITEDB", errorTxt); | ||
| + | this->lastError=ERROR_SQLITEDB; | ||
| + | this->db->executeSqlCommand("ROLLBACK TRANSACTION"); | ||
| + | return false; | ||
| + | } | ||
| + | sql[0]='\\0'; | ||
| + | sprintf(sql, "DELETE FROM Object WHERE ObjectName = '%s'", | ||
| + | this->db->apex(name)); | ||
| + | if(!this->db->executeSqlCommand(sql)) | ||
| + | { | ||
| + | STRING errorTxt=EMPTY_STRING; | ||
| + | switch(this->db->getLastError()) | ||
| + | { | ||
| + | case SqliteDb::ERROR_CONNECTION_NOT_OPEN: | ||
| + | errorTxt="ERROR_CONNECTION_NOT_OPEN - the connection is not open"; | ||
| + | break; | ||
| + | case SqliteDb::ERROR_SQLITE_ERROR: | ||
| + | errorTxt="ERROR_SQLITE_ERROR - generic sqlite error: try to run with debug error option activated"; | ||
| + | break; | ||
| + | default: | ||
| + | errorTxt="UNDEFINED ERROR"; | ||
| + | } | ||
| + | Debug::debugError("bool OODataBase::deleteObject(STRING name)","ERROR_SQLITEDB", errorTxt); | ||
| + | this->lastError=ERROR_SQLITEDB; | ||
| + | this->db->executeSqlCommand("ROLLBACK TRANSACTION"); | ||
| + | return false; | ||
| + | } | ||
| + | if(!this->db->executeSqlCommand("COMMIT TRANSACTION")) | ||
| + | { | ||
| + | STRING errorTxt=EMPTY_STRING; | ||
| + | switch(this->db->getLastError()) | ||
| + | { | ||
| + | case SqliteDb::ERROR_CONNECTION_NOT_OPEN: | ||
| + | errorTxt="ERROR_CONNECTION_NOT_OPEN - the connection is not open"; | ||
| + | break; | ||
| + | case SqliteDb::ERROR_SQLITE_ERROR: | ||
| + | errorTxt="ERROR_SQLITE_ERROR - generic sqlite error: try to run with debug error option activated"; | ||
| + | break; | ||
| + | default: | ||
| + | errorTxt="UNDEFINED ERROR"; | ||
| + | } | ||
| + | Debug::debugError("bool OODataBase::deleteObject(STRING name)","ERROR_SQLITEDB", errorTxt); | ||
| + | this->lastError=ERROR_SQLITEDB; | ||
| + | this->db->executeSqlCommand("ROLLBACK TRANSACTION"); | ||
| + | return false; | ||
| + | } | ||
| + | return true; | ||
| + | } | ||
| + | </pre> | ||
[[Il database Object Oriented Generico|BACK]] | [[Il database Object Oriented Generico|BACK]] | ||
[[Category:Codice Sorgente]] | [[Category:Codice Sorgente]] | ||
Current revision as of 10:44, 18 December 2007
/************************************************************************
* This class is an abstraction of an Object Oriented Database
* that can manage generic objects and attributes
* -------------------------------------------------------------------
* Author: Rosario Marino
************************************************************************/
#include "OODataBase.h"
/*
* Constructor
*/
OODataBase::OODataBase()
{
Debug::debug("OODataBase::OODataBase()");
this->db = new SqliteDb();
this->lastError = NO_ERROR;
}
/*
* Destructor
*/
OODataBase::~OODataBase()
{
Debug::debug("OODataBase::~OODataBase()");
delete (this->db);
}
/*
* Returns last error code
*/
int OODataBase::getLastError()
{
Debug::debug("int OODataBase::getLastError()");
return this->lastError;
}
/*
* Sets the name of the database. Returns false in case of error
*/
bool OODataBase::setDatabaseName(STRING databaseName)
{
Debug::debug("bool OODataBase::setDatabaseName(STRING databaseName)");
if (!this->db->setDataBaseName(databaseName))
{
STRING errorTxt=EMPTY_STRING;
switch(this->db->getLastError())
{
case SqliteDb::ERROR_NULL_PARAMETER:
errorTxt="ERROR_NULL_PARAMETER in parameter databaseName";
break;
case SqliteDb::ERROR_EMPTY_STRING:
errorTxt="ERROR_EMPTY_STRING in parameter databaseName";
break;
default:
errorTxt="UNDEFINED ERROR";
}
Debug::debugError("bool OODataBase::setDatabaseName(STRING databaseName)","ERROR_SQLITEDB", errorTxt);
this->lastError=ERROR_SQLITEDB;
return false;
}
return true;
}
/*
* Returns the name of the database
*/
STRING OODataBase::getDatabaseName()
{
Debug::debug("STRING OODataBase::getDatabaseName()");
return this->db->getDatabaseName();
}
/*
* Makes the connection to the database
*/
bool OODataBase::connect()
{
Debug::debug("bool OODataBase::connect()");
if(!this->db->openDataBase())
{
STRING errorTxt=EMPTY_STRING;
switch(this->db->getLastError())
{
case SqliteDb::ERROR_CONNECTION_OPEN_YET:
errorTxt="ERROR_CONNECTION_OPEN_YET - the connection is open yet";
break;
case SqliteDb::ERROR_DATABASE_NAME_NOT_SET:
errorTxt="ERROR_DATABASE_NAME_NOT_SET";
break;
case SqliteDb::ERROR_SQLITE_ERROR:
errorTxt="ERROR_SQLITE_ERROR - generic sqlite error: try to run with debug error option activated";
break;
default:
errorTxt="UNDEFINED ERROR";
}
Debug::debugError("bool OODataBase::connect()","ERROR_SQLITEDB", errorTxt);
this->lastError=ERROR_SQLITEDB;
return false;
}
if(!this->init())
return false;
return true;
}
/*
* Closes the connection to the database
*/
bool OODataBase::disconnect()
{
Debug::debug("bool OODataBase::disconnect()");
if(!this->db->closeDataBase())
{
STRING errorTxt=EMPTY_STRING;
switch(this->db->getLastError())
{
case SqliteDb::ERROR_CONNECTION_NOT_OPEN:
errorTxt="ERROR_CONNECTION_NOT_OPEN - the connection is not open";
break;
case SqliteDb::ERROR_SQLITE_ERROR:
errorTxt="ERROR_SQLITE_ERROR - generic sqlite error: try to run with debug error option activated";
break;
default:
errorTxt="UNDEFINED ERROR";
}
Debug::debugError("bool OODataBase::connect()","ERROR_SQLITEDB", errorTxt);
this->lastError=ERROR_SQLITEDB;
return false;
}
return true;
}
/*
* Initialization of database: creates tables and relations if
* they dont exist
*/
bool OODataBase::init()
{
Debug::debug("bool OODataBase::init()");
STRING sql=EMPTY_STRING;
sql = "CREATE TABLE IF NOT EXISTS Object (ObjectName TEXT PRIMARY KEY, ObjectDescription TEXT); \\
CREATE TABLE IF NOT EXISTS Attribute (AttributeName TEXT , ObjectName TEXT CONSTRAINT fk_object_name REFERENCES Object(ObjectName), AttributeValue TEXT NOT NULL, AttributeDescription TEXT, PRIMARY KEY (AttributeName,ObjectName)); \\
CREATE UNIQUE INDEX IF NOT EXISTS ObjName ON Object (ObjectName); \\
CREATE UNIQUE INDEX IF NOT EXISTS AttName ON Attribute (AttributeName)";
if(!this->db->executeSqlCommand(sql))
{
STRING errorTxt=EMPTY_STRING;
switch(this->db->getLastError())
{
case SqliteDb::ERROR_CONNECTION_NOT_OPEN:
errorTxt="ERROR_CONNECTION_NOT_OPEN - the connection is not open";
break;
case SqliteDb::ERROR_SQLITE_ERROR:
errorTxt="ERROR_SQLITE_ERROR - generic sqlite error: try to run with debug error option activated";
break;
default:
errorTxt="UNDEFINED ERROR";
}
Debug::debugError("bool OODataBase::init()","ERROR_SQLITEDB", errorTxt);
this->lastError=ERROR_SQLITEDB;
return false;
}
return true;
}
/*
* Stores the object only if there is not another object with the same
* name stored yet.
* Returns false in case of error
*/
bool OODataBase::storeNewObject(GenericObject* object)
{
Debug::debug("bool OODataBase::storeNewObject(GenericObject* object)");
if(object==NULL)
{
Debug::debugError("bool OODataBase::storeNewObject(GenericObject* object)","ERROR_NULL_PARAMETER","parameter: object");
this->lastError = ERROR_NULL_PARAMETER;
return false;
}
STRING name=strdup(object->getName());
STRING description=strdup(object->getDescription());
char sql[4096];
sql[0]='\\0';
sprintf(sql, "SELECT * FROM Object WHERE ObjectName = '%s'", this->db->apex(name));
SqliteDBResultSet* rs = this->db->executeQuery(sql);
if(rs==NULL)
{
STRING errorTxt=EMPTY_STRING;
switch(this->db->getLastError())
{
case SqliteDb::ERROR_CONNECTION_NOT_OPEN:
errorTxt="ERROR_CONNECTION_NOT_OPEN - the connection is not open";
break;
case SqliteDb::ERROR_SQLITE_ERROR:
errorTxt="ERROR_SQLITE_ERROR - generic sqlite error: try to run with debug error option activated";
break;
default:
errorTxt="UNDEFINED ERROR";
}
Debug::debugError("OODataBase::storeNewObject(GenericObject* object)","ERROR_SQLITEDB", errorTxt);
this->lastError=ERROR_SQLITEDB;
return false;
}
if(rs->getNRows()>0)
{
Debug::debugError("OODataBase::storeNewObject(GenericObject* object)","ERROR_OBJECT_EXISTS_YET","You are trying to store an object that is yet stored");
this->lastError=ERROR_OBJECT_EXISTS_YET;
delete(rs);
return false;
}
delete(rs);
if(!this->db->executeSqlCommand("BEGIN TRANSACTION"))
{
STRING errorTxt=EMPTY_STRING;
switch(this->db->getLastError())
{
case SqliteDb::ERROR_CONNECTION_NOT_OPEN:
errorTxt="ERROR_CONNECTION_NOT_OPEN - the connection is not open";
break;
case SqliteDb::ERROR_SQLITE_ERROR:
errorTxt="ERROR_SQLITE_ERROR - generic sqlite error: try to run with debug error option activated";
break;
default:
errorTxt="UNDEFINED ERROR";
}
Debug::debugError("OODataBase::storeNewObject(GenericObject* object)","ERROR_SQLITEDB", errorTxt);
this->lastError=ERROR_SQLITEDB;
return false;
}
int attributeNumber = object->getAttributeNumber();
sql[0]='\\0';
sprintf(sql, "INSERT INTO Object (ObjectName, ObjectDescription) VALUES ('%s','%s')",
this->db->apex(name),
this->db->apex(description));
if(!this->db->executeSqlCommand(sql))
{
STRING errorTxt=EMPTY_STRING;
switch(this->db->getLastError())
{
case SqliteDb::ERROR_CONNECTION_NOT_OPEN:
errorTxt="ERROR_CONNECTION_NOT_OPEN - the connection is not open";
break;
case SqliteDb::ERROR_SQLITE_ERROR:
errorTxt="ERROR_SQLITE_ERROR - generic sqlite error: try to run with debug error option activated";
break;
default:
errorTxt="UNDEFINED ERROR";
}
Debug::debugError("OODataBase::storeNewObject(GenericObject* object)","ERROR_SQLITEDB", errorTxt);
this->lastError=ERROR_SQLITEDB;
this->db->executeSqlCommand("ROLLBACK TRANSACTION");
return false;
}
if(attributeNumber>0)
{
STRING* attributes=object->getAttributeNameList();
for (int i=0; i<attributeNumber; i++)
{
sql[0]='\\0';
sprintf(sql,"INSERT INTO Attribute (AttributeName, AttributeValue, AttributeDescription, ObjectName) VALUES('%s','%s','%s','%s')",
this->db->apex(attributes[i]),
this->db->apex(object->getAttributeValue(attributes[i])),
this->db->apex(object->getAttributeDescription(attributes[i])),
this->db->apex(object->getName()));
if(!this->db->executeSqlCommand(sql))
{
STRING errorTxt=EMPTY_STRING;
switch(this->db->getLastError())
{
case SqliteDb::ERROR_CONNECTION_NOT_OPEN:
errorTxt="ERROR_CONNECTION_NOT_OPEN - the connection is not open";
break;
case SqliteDb::ERROR_SQLITE_ERROR:
errorTxt="ERROR_SQLITE_ERROR - generic sqlite error: try to run with debug error option activated";
break;
default:
errorTxt="UNDEFINED ERROR";
}
Debug::debugError("OODataBase::storeNewObject(GenericObject* object)","ERROR_SQLITEDB", errorTxt);
this->lastError=ERROR_SQLITEDB;
this->db->executeSqlCommand("ROLLBACK TRANSACTION");
return false;
}
}
}
if(!this->db->executeSqlCommand("COMMIT TRANSACTION"))
{
STRING errorTxt=EMPTY_STRING;
switch(this->db->getLastError())
{
case SqliteDb::ERROR_CONNECTION_NOT_OPEN:
errorTxt="ERROR_CONNECTION_NOT_OPEN - the connection is not open";
break;
case SqliteDb::ERROR_SQLITE_ERROR:
errorTxt="ERROR_SQLITE_ERROR - generic sqlite error: try to run with debug error option activated";
break;
default:
errorTxt="UNDEFINED ERROR";
}
Debug::debugError("OODataBase::storeNewObject(GenericObject* object)","ERROR_SQLITEDB", errorTxt);
this->lastError=ERROR_SQLITEDB;
this->db->executeSqlCommand("ROLLBACK TRANSACTION");
return false;
}
return true;
}
/*
* Gets the object stored in the Database and returns it
* Returns NULL in case of error or if the object does not exist
*/
GenericObject* OODataBase::getObjectByName(STRING name)
{
Debug::debug("GenericObject* OODataBase::getObjectByName(STRING name)");
if(name==NULL)
{
Debug::debugError("GenericObject* OODataBase::getObjectByName(STRING name)","ERROR_NULL_PARAMETER","parameter: name");
this->lastError = ERROR_NULL_PARAMETER;
return NULL;
}
if(strcmp(name,EMPTY_STRING)==0)
{
Debug::debugError("GenericObject* OODataBase::getObjectByName(STRING name)","ERROR_EMPTY_STRING","parameter: name");
this->lastError = ERROR_EMPTY_STRING;
return NULL;
}
char sql[4096];
sql[0]='\\0';
sprintf(sql, "SELECT * FROM Object WHERE ObjectName = '%s'", this->db->apex(name));
SqliteDBResultSet* rs = this->db->executeQuery(sql);
if(rs==NULL)
{
STRING errorTxt=EMPTY_STRING;
switch(this->db->getLastError())
{
case SqliteDb::ERROR_CONNECTION_NOT_OPEN:
errorTxt="ERROR_CONNECTION_NOT_OPEN - the connection is not open";
break;
case SqliteDb::ERROR_SQLITE_ERROR:
errorTxt="ERROR_SQLITE_ERROR - generic sqlite error: try to run with debug error option activated";
break;
default:
errorTxt="UNDEFINED ERROR";
}
Debug::debugError("GenericObject* OODataBase::getObjectByName(STRING name)","ERROR_SQLITEDB", errorTxt);
this->lastError=ERROR_SQLITEDB;
return NULL;
}
if(rs->getNRows()==0)
{
Debug::debugError("GenericObject* OODataBase::getObjectByName(STRING name)","ERROR_OBJECT_NOT_FOUND", name);
this->lastError=ERROR_OBJECT_NOT_FOUND;
return NULL;
}
GenericObject* object = new GenericObject();
object->setName(rs->getFieldValue(0,0));
object->setDescription(rs->getFieldValue(0,1));
delete(rs);
sql[0]='\\0';
sprintf(sql, "SELECT * FROM Attribute WHERE ObjectName = '%s'", this->db->apex(name));
rs = this->db->executeQuery(sql);
if(rs==NULL)
{
STRING errorTxt=EMPTY_STRING;
switch(this->db->getLastError())
{
case SqliteDb::ERROR_CONNECTION_NOT_OPEN:
errorTxt="ERROR_CONNECTION_NOT_OPEN - the connection is not open";
break;
case SqliteDb::ERROR_SQLITE_ERROR:
errorTxt="ERROR_SQLITE_ERROR - generic sqlite error: try to run with debug error option activated";
break;
default:
errorTxt="UNDEFINED ERROR";
}
Debug::debugError("GenericObject* OODataBase::getObjectByName(STRING name)","ERROR_SQLITEDB", errorTxt);
this->lastError=ERROR_SQLITEDB;
return NULL;
}
for(int i=0; i<rs->getNRows(); i++)
{
object->setAttributeValue(rs->getFieldValue(i,0),rs->getFieldValue(i,2));
object->setAttributeDescription(rs->getFieldValue(i,0),rs->getFieldValue(i,3));
}
delete(rs);
return object;
}
/*
* Returns the list of names of objects stored in the DB
*/
STRING* OODataBase::getObjectNameList()
{
Debug::debug("STRING* OODataBase::getObjectNameList()");
SqliteDBResultSet* rs = this->db->executeQuery("SELECT * FROM Object");
if(rs==NULL)
{
STRING errorTxt=EMPTY_STRING;
switch(this->db->getLastError())
{
case SqliteDb::ERROR_CONNECTION_NOT_OPEN:
errorTxt="ERROR_CONNECTION_NOT_OPEN - the connection is not open";
break;
case SqliteDb::ERROR_SQLITE_ERROR:
errorTxt="ERROR_SQLITE_ERROR - generic sqlite error: try to run with debug error option activated";
break;
default:
errorTxt="UNDEFINED ERROR";
}
Debug::debugError("STRING* OODataBase::getObjectNameList()","ERROR_SQLITEDB", errorTxt);
this->lastError=ERROR_SQLITEDB;
return NULL;
}
STRING* result = new STRING[rs->getNRows()];
for(int i=0; i<rs->getNRows(); i++)
result[i]=rs->getFieldValue(i,0);
delete(rs);
return result;
}
/*
* Stores the changes made to the object
* Returns false in case of error.
*/
bool OODataBase::updateObject(GenericObject* object)
{
Debug::debug("bool OODataBase::updateObject(GenericObject* object)");
if(object==NULL)
{
Debug::debugError("bool OODataBase::updateObject(GenericObject* object)","ERROR_NULL_PARAMETER","parameter: object");
this->lastError = ERROR_NULL_PARAMETER;
return false;
}
STRING name=strdup(object->getName());
STRING description=strdup(object->getDescription());
char sql[4096];
sql[0]='\\0';
sprintf(sql, "SELECT * FROM Object WHERE ObjectName = '%s'", this->db->apex(name));
SqliteDBResultSet* rs = this->db->executeQuery(sql);
if(rs==NULL)
{
STRING errorTxt=EMPTY_STRING;
switch(this->db->getLastError())
{
case SqliteDb::ERROR_CONNECTION_NOT_OPEN:
errorTxt="ERROR_CONNECTION_NOT_OPEN - the connection is not open";
break;
case SqliteDb::ERROR_SQLITE_ERROR:
errorTxt="ERROR_SQLITE_ERROR - generic sqlite error: try to run with debug error option activated";
break;
default:
errorTxt="UNDEFINED ERROR";
}
Debug::debugError("bool OODataBase::updateObject(GenericObject* object)","ERROR_SQLITEDB", errorTxt);
this->lastError=ERROR_SQLITEDB;
return false;
}
if(rs->getNRows()==0)
{
Debug::debugError("bool OODataBase::updateObject(GenericObject* object)","ERROR_OBJECT_NOT_FOUND","You are trying to update an object that is not stored in the database");
this->lastError=ERROR_OBJECT_NOT_FOUND;
delete(rs);
return false;
}
delete(rs);
if(!this->db->executeSqlCommand("BEGIN TRANSACTION"))
{
STRING errorTxt=EMPTY_STRING;
switch(this->db->getLastError())
{
case SqliteDb::ERROR_CONNECTION_NOT_OPEN:
errorTxt="ERROR_CONNECTION_NOT_OPEN - the connection is not open";
break;
case SqliteDb::ERROR_SQLITE_ERROR:
errorTxt="ERROR_SQLITE_ERROR - generic sqlite error: try to run with debug error option activated";
break;
default:
errorTxt="UNDEFINED ERROR";
}
Debug::debugError("bool OODataBase::updateObject(GenericObject* object)","ERROR_SQLITEDB", errorTxt);
this->lastError=ERROR_SQLITEDB;
return false;
}
int attributeNumber = object->getAttributeNumber();
sql[0]='\\0';
sprintf(sql, "UPDATE Object SET ObjectDescription ='%s' WHERE ObjectName = '%s'",
this->db->apex(description),
this->db->apex(name));
if(!this->db->executeSqlCommand(sql))
{
STRING errorTxt=EMPTY_STRING;
switch(this->db->getLastError())
{
case SqliteDb::ERROR_CONNECTION_NOT_OPEN:
errorTxt="ERROR_CONNECTION_NOT_OPEN - the connection is not open";
break;
case SqliteDb::ERROR_SQLITE_ERROR:
errorTxt="ERROR_SQLITE_ERROR - generic sqlite error: try to run with debug error option activated";
break;
default:
errorTxt="UNDEFINED ERROR";
}
Debug::debugError("bool OODataBase::updateObject(GenericObject* object)","ERROR_SQLITEDB", errorTxt);
this->lastError=ERROR_SQLITEDB;
this->db->executeSqlCommand("ROLLBACK TRANSACTION");
return false;
}
sql[0]='\\0';
sprintf(sql, "DELETE FROM Attribute WHERE ObjectName = '%s'",
this->db->apex(name));
if(!this->db->executeSqlCommand(sql))
{
STRING errorTxt=EMPTY_STRING;
switch(this->db->getLastError())
{
case SqliteDb::ERROR_CONNECTION_NOT_OPEN:
errorTxt="ERROR_CONNECTION_NOT_OPEN - the connection is not open";
break;
case SqliteDb::ERROR_SQLITE_ERROR:
errorTxt="ERROR_SQLITE_ERROR - generic sqlite error: try to run with debug error option activated";
break;
default:
errorTxt="UNDEFINED ERROR";
}
Debug::debugError("bool OODataBase::updateObject(GenericObject* object)","ERROR_SQLITEDB", errorTxt);
this->lastError=ERROR_SQLITEDB;
this->db->executeSqlCommand("ROLLBACK TRANSACTION");
return false;
}
if(attributeNumber>0)
{
STRING* attributes=object->getAttributeNameList();
for (int i=0; i<attributeNumber; i++)
{
sql[0]='\\0';
sprintf(sql,"INSERT INTO Attribute (AttributeName, AttributeValue, AttributeDescription, ObjectName) VALUES('%s','%s','%s','%s')",
this->db->apex(attributes[i]),
this->db->apex(object->getAttributeValue(attributes[i])),
this->db->apex(object->getAttributeDescription(attributes[i])),
this->db->apex(object->getName()));
if(!this->db->executeSqlCommand(sql))
{
STRING errorTxt=EMPTY_STRING;
switch(this->db->getLastError())
{
case SqliteDb::ERROR_CONNECTION_NOT_OPEN:
errorTxt="ERROR_CONNECTION_NOT_OPEN - the connection is not open";
break;
case SqliteDb::ERROR_SQLITE_ERROR:
errorTxt="ERROR_SQLITE_ERROR - generic sqlite error: try to run with debug error option activated";
break;
default:
errorTxt="UNDEFINED ERROR";
}
Debug::debugError("bool OODataBase::updateObject(GenericObject* object)","ERROR_SQLITEDB", errorTxt);
this->lastError=ERROR_SQLITEDB;
this->db->executeSqlCommand("ROLLBACK TRANSACTION");
return false;
}
}
}
if(!this->db->executeSqlCommand("COMMIT TRANSACTION"))
{
STRING errorTxt=EMPTY_STRING;
switch(this->db->getLastError())
{
case SqliteDb::ERROR_CONNECTION_NOT_OPEN:
errorTxt="ERROR_CONNECTION_NOT_OPEN - the connection is not open";
break;
case SqliteDb::ERROR_SQLITE_ERROR:
errorTxt="ERROR_SQLITE_ERROR - generic sqlite error: try to run with debug error option activated";
break;
default:
errorTxt="UNDEFINED ERROR";
}
Debug::debugError("bool OODataBase::updateObject(GenericObject* object)","ERROR_SQLITEDB", errorTxt);
this->lastError=ERROR_SQLITEDB;
this->db->executeSqlCommand("ROLLBACK TRANSACTION");
return false;
}
return true;
}
/*
* Deletes the object from the database
* Returns false in case of error
*/
bool OODataBase::deleteObject(STRING name)
{
Debug::debug("bool OODataBase::deleteObject(STRING name)");
if(name==NULL)
{
Debug::debugError("bool OODataBase::deleteObject(STRING name)","ERROR_NULL_PARAMETER","parameter: name");
this->lastError = ERROR_NULL_PARAMETER;
return false;
}
if(strcmp(name,EMPTY_STRING)==0)
{
Debug::debugError("bool OODataBase::deleteObject(STRING name)","ERROR_EMPTY_STRING","parameter: name");
this->lastError = ERROR_EMPTY_STRING;
return false;
}
if(!this->db->executeSqlCommand("BEGIN TRANSACTION"))
{
STRING errorTxt=EMPTY_STRING;
switch(this->db->getLastError())
{
case SqliteDb::ERROR_CONNECTION_NOT_OPEN:
errorTxt="ERROR_CONNECTION_NOT_OPEN - the connection is not open";
break;
case SqliteDb::ERROR_SQLITE_ERROR:
errorTxt="ERROR_SQLITE_ERROR - generic sqlite error: try to run with debug error option activated";
break;
default:
errorTxt="UNDEFINED ERROR";
}
Debug::debugError("bool OODataBase::deleteObject(STRING name)","ERROR_SQLITEDB", errorTxt);
this->lastError=ERROR_SQLITEDB;
return false;
}
char sql[4096];
sql[0]='\\0';
sprintf(sql, "DELETE FROM Attribute WHERE ObjectName = '%s'",
this->db->apex(name));
if(!this->db->executeSqlCommand(sql))
{
STRING errorTxt=EMPTY_STRING;
switch(this->db->getLastError())
{
case SqliteDb::ERROR_CONNECTION_NOT_OPEN:
errorTxt="ERROR_CONNECTION_NOT_OPEN - the connection is not open";
break;
case SqliteDb::ERROR_SQLITE_ERROR:
errorTxt="ERROR_SQLITE_ERROR - generic sqlite error: try to run with debug error option activated";
break;
default:
errorTxt="UNDEFINED ERROR";
}
Debug::debugError("bool OODataBase::deleteObject(STRING name)","ERROR_SQLITEDB", errorTxt);
this->lastError=ERROR_SQLITEDB;
this->db->executeSqlCommand("ROLLBACK TRANSACTION");
return false;
}
sql[0]='\\0';
sprintf(sql, "DELETE FROM Object WHERE ObjectName = '%s'",
this->db->apex(name));
if(!this->db->executeSqlCommand(sql))
{
STRING errorTxt=EMPTY_STRING;
switch(this->db->getLastError())
{
case SqliteDb::ERROR_CONNECTION_NOT_OPEN:
errorTxt="ERROR_CONNECTION_NOT_OPEN - the connection is not open";
break;
case SqliteDb::ERROR_SQLITE_ERROR:
errorTxt="ERROR_SQLITE_ERROR - generic sqlite error: try to run with debug error option activated";
break;
default:
errorTxt="UNDEFINED ERROR";
}
Debug::debugError("bool OODataBase::deleteObject(STRING name)","ERROR_SQLITEDB", errorTxt);
this->lastError=ERROR_SQLITEDB;
this->db->executeSqlCommand("ROLLBACK TRANSACTION");
return false;
}
if(!this->db->executeSqlCommand("COMMIT TRANSACTION"))
{
STRING errorTxt=EMPTY_STRING;
switch(this->db->getLastError())
{
case SqliteDb::ERROR_CONNECTION_NOT_OPEN:
errorTxt="ERROR_CONNECTION_NOT_OPEN - the connection is not open";
break;
case SqliteDb::ERROR_SQLITE_ERROR:
errorTxt="ERROR_SQLITE_ERROR - generic sqlite error: try to run with debug error option activated";
break;
default:
errorTxt="UNDEFINED ERROR";
}
Debug::debugError("bool OODataBase::deleteObject(STRING name)","ERROR_SQLITEDB", errorTxt);
this->lastError=ERROR_SQLITEDB;
this->db->executeSqlCommand("ROLLBACK TRANSACTION");
return false;
}
return true;
}
