SqliteDBResultSet.cpp
From Giona
(Difference between revisions)
Line 1: | Line 1: | ||
+ | <pre> | ||
+ | /* | ||
+ | * This class represents a Result set obtained by making a query on a Sqlite Database | ||
+ | * Author: Rosario Marino | ||
+ | */ | ||
+ | #include "SqliteDBResultSet.h" | ||
+ | using namespace std; | ||
+ | /* | ||
+ | * Constructor | ||
+ | */ | ||
+ | SqliteDBResultSet::SqliteDBResultSet() | ||
+ | { | ||
+ | Debug::debug("SqliteDBResultSet::SqliteDBResultSet()"); | ||
+ | this->columns=new GenericList(); | ||
+ | this->data=new GenericList(); | ||
+ | this->lastError=NO_ERROR; | ||
+ | } | ||
+ | |||
+ | /* | ||
+ | * Destructor | ||
+ | */ | ||
+ | SqliteDBResultSet::~SqliteDBResultSet() | ||
+ | { | ||
+ | Debug::debug("SqliteDBResultSet::~SqliteDBResultSet()"); | ||
+ | delete (this->data); | ||
+ | delete (this->columns); | ||
+ | } | ||
+ | |||
+ | /* | ||
+ | * Returns last error code | ||
+ | */ | ||
+ | |||
+ | int SqliteDBResultSet::getLastError() | ||
+ | { | ||
+ | Debug::debug("int SqliteDBResultSet::getLastError()"); | ||
+ | return this->lastError; | ||
+ | } | ||
+ | |||
+ | /* | ||
+ | * Returns the number of columns of the table | ||
+ | */ | ||
+ | int SqliteDBResultSet::getNColumns() | ||
+ | { | ||
+ | Debug::debug("int SqliteDBResultSet::getNColumns()"); | ||
+ | return this->columns->getLength(); | ||
+ | } | ||
+ | |||
+ | /* | ||
+ | * Returns the number of rows of the table | ||
+ | */ | ||
+ | int SqliteDBResultSet::getNRows() | ||
+ | { | ||
+ | Debug::debug("int SqliteDBResultSet::getNRows()"); | ||
+ | if(this->columns->getLength()==0) | ||
+ | return 0; | ||
+ | int nRows=this->data->getLength()/this->columns->getLength(); | ||
+ | return nRows; | ||
+ | } | ||
+ | |||
+ | /* | ||
+ | * Returns the name of a column in the specified position | ||
+ | * Returns NULL in case of error | ||
+ | */ | ||
+ | char* SqliteDBResultSet::getColumnName(int columnNumber) | ||
+ | { | ||
+ | Debug::debug("char* SqliteDBResultSet::getColumnName(int columnNumber)"); | ||
+ | if((columnNumber<0)||(columnNumber>=this->columns->getLength())) | ||
+ | { | ||
+ | Debug::debug("ERROR: ERROR_INDEX_OUT_OF_RANGE"); | ||
+ | this->lastError = ERROR_INDEX_OUT_OF_RANGE; | ||
+ | return NULL; | ||
+ | } | ||
+ | GenericListNode * columnNode= this->columns->getHead(); | ||
+ | for(int i=0; i<columnNumber;i++) | ||
+ | columnNode=columnNode->getNext(); | ||
+ | return (char*)(columnNode->getElem()); | ||
+ | } | ||
+ | |||
+ | /* | ||
+ | * Returns the value of the field in the specified rows and cols | ||
+ | * Returns NULL in case of error | ||
+ | */ | ||
+ | char* SqliteDBResultSet::getFieldValue(int rowNumber, int columnNumber) | ||
+ | { | ||
+ | Debug::debug("char* SqliteDBResultSet::getFieldValue(int rowNumber, int columnNumber)"); | ||
+ | |||
+ | |||
+ | if((columnNumber<0)||(columnNumber>=this->columns->getLength())) | ||
+ | { | ||
+ | Debug::debug("ERROR: ERROR_INDEX_OUT_OF_RANGE - int columnNumber"); | ||
+ | this->lastError = ERROR_INDEX_OUT_OF_RANGE; | ||
+ | return NULL; | ||
+ | } | ||
+ | |||
+ | if((rowNumber<0)||(rowNumber>=this->getNRows())) | ||
+ | { | ||
+ | Debug::debug("ERROR: ERROR_INDEX_OUT_OF_RANGE - int rowNumber"); | ||
+ | this->lastError = ERROR_INDEX_OUT_OF_RANGE; | ||
+ | return NULL; | ||
+ | } | ||
+ | |||
+ | int position=(rowNumber*this->getNColumns())+columnNumber; | ||
+ | GenericListNode * fieldNode= this->data->getHead(); | ||
+ | for(int i=0; i<position;i++) | ||
+ | fieldNode=fieldNode->getNext(); | ||
+ | return (char*)(fieldNode->getElem()); | ||
+ | } | ||
+ | |||
+ | |||
+ | /* | ||
+ | * Adds a column to the table | ||
+ | * WARNING: This method should NEVER BE CALLED | ||
+ | */ | ||
+ | bool SqliteDBResultSet::addColumn(char* columnName) | ||
+ | { | ||
+ | Debug::debug("bool SqliteDBResultSet::addColumn(char* columnName)"); | ||
+ | GenericListNode* node=new GenericListNode(); | ||
+ | node->setElem((genericListElemT)columnName); | ||
+ | node->setNotAnObject(); | ||
+ | return this->columns->addNode(node); | ||
+ | } | ||
+ | |||
+ | /* | ||
+ | * Adds a field in the specified row of the table. | ||
+ | * WARNING: This method should NEVER BE CALLED | ||
+ | */ | ||
+ | bool SqliteDBResultSet::addField(char* field) | ||
+ | { | ||
+ | Debug::debug("bool SqliteDBResultSet::addField(char* field)"); | ||
+ | GenericListNode* node=new GenericListNode(); | ||
+ | node->setElem((genericListElemT)field); | ||
+ | node->setNotAnObject(); | ||
+ | return this->data->addNode(node); | ||
+ | } | ||
+ | |||
+ | </pre> | ||
[[Creazione di applicazioni con SQLite | BACK]] | [[Creazione di applicazioni con SQLite | BACK]] |
Revision as of 12:22, 15 December 2007
/* * This class represents a Result set obtained by making a query on a Sqlite Database * Author: Rosario Marino */ #include "SqliteDBResultSet.h" using namespace std; /* * Constructor */ SqliteDBResultSet::SqliteDBResultSet() { Debug::debug("SqliteDBResultSet::SqliteDBResultSet()"); this->columns=new GenericList(); this->data=new GenericList(); this->lastError=NO_ERROR; } /* * Destructor */ SqliteDBResultSet::~SqliteDBResultSet() { Debug::debug("SqliteDBResultSet::~SqliteDBResultSet()"); delete (this->data); delete (this->columns); } /* * Returns last error code */ int SqliteDBResultSet::getLastError() { Debug::debug("int SqliteDBResultSet::getLastError()"); return this->lastError; } /* * Returns the number of columns of the table */ int SqliteDBResultSet::getNColumns() { Debug::debug("int SqliteDBResultSet::getNColumns()"); return this->columns->getLength(); } /* * Returns the number of rows of the table */ int SqliteDBResultSet::getNRows() { Debug::debug("int SqliteDBResultSet::getNRows()"); if(this->columns->getLength()==0) return 0; int nRows=this->data->getLength()/this->columns->getLength(); return nRows; } /* * Returns the name of a column in the specified position * Returns NULL in case of error */ char* SqliteDBResultSet::getColumnName(int columnNumber) { Debug::debug("char* SqliteDBResultSet::getColumnName(int columnNumber)"); if((columnNumber<0)||(columnNumber>=this->columns->getLength())) { Debug::debug("ERROR: ERROR_INDEX_OUT_OF_RANGE"); this->lastError = ERROR_INDEX_OUT_OF_RANGE; return NULL; } GenericListNode * columnNode= this->columns->getHead(); for(int i=0; i<columnNumber;i++) columnNode=columnNode->getNext(); return (char*)(columnNode->getElem()); } /* * Returns the value of the field in the specified rows and cols * Returns NULL in case of error */ char* SqliteDBResultSet::getFieldValue(int rowNumber, int columnNumber) { Debug::debug("char* SqliteDBResultSet::getFieldValue(int rowNumber, int columnNumber)"); if((columnNumber<0)||(columnNumber>=this->columns->getLength())) { Debug::debug("ERROR: ERROR_INDEX_OUT_OF_RANGE - int columnNumber"); this->lastError = ERROR_INDEX_OUT_OF_RANGE; return NULL; } if((rowNumber<0)||(rowNumber>=this->getNRows())) { Debug::debug("ERROR: ERROR_INDEX_OUT_OF_RANGE - int rowNumber"); this->lastError = ERROR_INDEX_OUT_OF_RANGE; return NULL; } int position=(rowNumber*this->getNColumns())+columnNumber; GenericListNode * fieldNode= this->data->getHead(); for(int i=0; i<position;i++) fieldNode=fieldNode->getNext(); return (char*)(fieldNode->getElem()); } /* * Adds a column to the table * WARNING: This method should NEVER BE CALLED */ bool SqliteDBResultSet::addColumn(char* columnName) { Debug::debug("bool SqliteDBResultSet::addColumn(char* columnName)"); GenericListNode* node=new GenericListNode(); node->setElem((genericListElemT)columnName); node->setNotAnObject(); return this->columns->addNode(node); } /* * Adds a field in the specified row of the table. * WARNING: This method should NEVER BE CALLED */ bool SqliteDBResultSet::addField(char* field) { Debug::debug("bool SqliteDBResultSet::addField(char* field)"); GenericListNode* node=new GenericListNode(); node->setElem((genericListElemT)field); node->setNotAnObject(); return this->data->addNode(node); }