SqliteDb.cpp
From Giona
(Difference between revisions)
Line 277: | Line 277: | ||
[[Creazione di applicazioni con SQLite | BACK]] | [[Creazione di applicazioni con SQLite | BACK]] | ||
+ | [[Category:Codice Sorgente]] |
Revision as of 12:33, 15 December 2007
/* * Implementation of a class to manage a sqlite database * Author: Rosario Marino */ #include "SqliteDb.h" /* * Constructor */ SqliteDb::SqliteDb() { Debug::debug("SqliteDb::SqliteDb()"); this->db = NULL; this->connected = false; this->lastError=NO_ERROR; this->dataBaseName = NULL; this->rsTmp = NULL; } /* * Destructor */ SqliteDb::~SqliteDb() { Debug::debug("SqliteDb::~SqliteDb()"); } /* * Returns last error code */ int SqliteDb::getLastError() { Debug::debug("int SqliteDb::getLastError()"); return this->lastError; } /* * Returns true if the database is connected to the source */ bool SqliteDb::isConnected() { Debug::debug("bool SqliteDb::isConnected()"); return this->connected; } /* * Sets the name of the database to connect. Returns false * in case of error. */ bool SqliteDb::setDataBaseName(char* dataBaseName) { Debug::debug("bool SqliteDb::setDataBaseName(char* dataBaseName)"); if(dataBaseName==NULL) { Debug::debug("ERROR: ERROR_NULL_PARAMETER - char* dataBaseName"); this->lastError = ERROR_NULL_PARAMETER; return false; } if(strcmp(dataBaseName,"")==0) { Debug::debug("ERROR: ERROR_EMPTY_STRING - char* dataBaseName"); this->lastError = ERROR_EMPTY_STRING; return false; } this->dataBaseName=dataBaseName; return true; } /* * Open the connection to the data source. * If the data source does not exist, a new one is created. * Returns FALSE in case of error */ bool SqliteDb::openDataBase() { Debug::debug("bool SqliteDb::openDataBase()"); if(this->isConnected()) { Debug::debug("ERROR: ERROR_CONNECTION_OPEN_YET - the database connection is open yet\ you cannot connect to another data source untill you close the previous one"); this->lastError = ERROR_CONNECTION_OPEN_YET; return false; } if(this->dataBaseName==NULL) { Debug::debug("ERROR: ERROR_DATABASE_NAME_NOT_SET. You must set the name of the database before trying to open it.\ Use: bool SqliteDb::setDataBaseName(char* dataBaseName)"); this->lastError = ERROR_DATABASE_NAME_NOT_SET; return false; } int con = sqlite3_open(this->dataBaseName, &this->db); if(con != SQLITE_OK) { char txt[1024]; sprintf(txt, "ERROR: ERROR_SQLITE_ERROR - %s", sqlite3_errmsg(this->db)); Debug::debug(txt); this->lastError = ERROR_SQLITE_ERROR; return false; } this->connected=true; return true; } /* * Close the connection to the data source. * Returns false in case of error */ bool SqliteDb::closeDataBase() { Debug::debug("bool SqliteDb::closeDataBase()"); if(!this->connected) { Debug::debug("ERROR: ERROR_CONNECTION_NOT_OPEN - there is no connection to the data source."); this->lastError=ERROR_CONNECTION_NOT_OPEN; return false; } int con = sqlite3_close(this->db); if(con != SQLITE_OK) { char txt[1024]; sprintf(txt, "ERROR: ERROR_SQLITE_ERROR - %s", sqlite3_errmsg(this->db)); Debug::debug(txt); this->lastError = ERROR_SQLITE_ERROR; return false; } return true; } /* * Execute an SQL command (CREATE TABLE, INSERT; UPDATE; DELETE....) * Returns false in case of error */ bool SqliteDb::executeSqlCommand(char* sqlCommand) { Debug::debug("bool SqliteDb::executeSqlCommand(char* sqlCommand)"); if(!this->connected) { Debug::debug("ERROR: ERROR_CONNECTION_NOT_OPEN - there is no connection to the data source.\ Use bool SqliteDb::openDataBase()"); this->lastError=ERROR_CONNECTION_NOT_OPEN; return false; } if(sqlCommand==NULL) { Debug::debug("ERROR: ERROR_NULL_PARAMETER - char* sqlCommand"); this->lastError = ERROR_NULL_PARAMETER; return false; } if(strcmp(sqlCommand,"")==0) { Debug::debug("ERROR: ERROR_EMPTY_STRING - char* sqlCommand"); this->lastError = ERROR_EMPTY_STRING; return false; } char *zErrMsg = NULL; int ret = sqlite3_exec(this->db, sqlCommand, NULL, NULL, &zErrMsg); if(ret != SQLITE_OK) { char txt[1024]; sprintf(txt, "ERROR: ERROR_SQLITE_ERROR - %s", zErrMsg); if(zErrMsg) sqlite3_free(zErrMsg); Debug::debug(txt); this->lastError = ERROR_SQLITE_ERROR; return false; } if(zErrMsg) sqlite3_free(zErrMsg); return true; } /* * Execute the query on the data source and returns the Result Set * Returns NULL in case of error. */ SqliteDBResultSet* SqliteDb::executeQuery(char* query) { Debug::debug("SqliteDBResultSet* SqliteDb::executeQuery(char* query)"); if(!this->connected) { Debug::debug("ERROR: ERROR_CONNECTION_NOT_OPEN - there is no connection to the data source.\ Use bool SqliteDb::openDataBase()"); this->lastError=ERROR_CONNECTION_NOT_OPEN; return false; } if(query==NULL) { Debug::debug("ERROR: ERROR_NULL_PARAMETER - char* sqlCommand"); this->lastError = ERROR_NULL_PARAMETER; return false; } if(strcmp(query,"")==0) { Debug::debug("ERROR: ERROR_EMPTY_STRING - char* sqlCommand"); this->lastError = ERROR_EMPTY_STRING; return false; } char *zErrMsg = NULL; this->rsTmp = new SqliteDBResultSet(); this->nRows = 0; int ret = sqlite3_exec(this->db, query, select_callback, this, &zErrMsg); if(ret != SQLITE_OK) { char txt[1024]; sprintf(txt, "ERROR: ERROR_SQLITE_ERROR - %s - [%s]", query, zErrMsg); Debug::debug(txt); this->lastError = ERROR_SQLITE_ERROR; return NULL; } SqliteDBResultSet* rs = this->rsTmp; this->rsTmp=NULL; return rs; } /* * Compile a Result Set when a query is executed (Private method) */ bool SqliteDb::compileResultSet(int num_fields, char **p_fields, char **p_col_names) { Debug::debug("bool SqliteDb::compileResultSet(int num_fields, char **p_fields, char **p_col_names)"); if(this->rsTmp->getNColumns()==0) { for (int i=0; i < num_fields; i++) { char* txt=strdup(p_col_names[i]); if(!this->rsTmp->addColumn(txt)) { Debug::debug("ERROR: ERROR_CANT_CREATE_RESULT_SET - error in adding a column name"); this->lastError = ERROR_CANT_CREATE_RESULT_SET; return false; } } } for (int i=0; i < num_fields; i++) { char* txt=strdup(p_fields[i]); if(!rsTmp->addField(txt)) { Debug::debug("ERROR: ERROR_CANT_CREATE_RESULT_SET - error in adding a column name"); this->lastError = ERROR_CANT_CREATE_RESULT_SET; return false; } } return true; } /* * Callback invoked by sqlite3_exec (Private method) */ int SqliteDb::select_callback(void *p_data, int num_fields, char **p_fields, char **p_col_names) { Debug::debug("int SqliteDb::select_callback(void *p_data, int num_fields, char **p_fields, char **p_col_names)"); SqliteDb* dbTmp=(SqliteDb*) p_data; if(!dbTmp->compileResultSet(num_fields, p_fields, p_col_names)) { Debug::debug("ERROR: ERROR_CANT_CREATE_RESULT_SET - error in adding a column name"); return ERROR_CANT_CREATE_RESULT_SET; } return SQLITE_OK; }