Logo
  • Main Page
  • Related Pages
  • Modules
  • Classes
  • Files

mmsdbsqlite.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2005-2007 Stefan Schwarzer, Jens Schneider,             *
00003  *                           Matthias Hardt, Guido Madaus                  *
00004  *                                                                         *
00005  *   Copyright (C) 2007-2008 BerLinux Solutions GbR                        *
00006  *                           Stefan Schwarzer & Guido Madaus               *
00007  *                                                                         *
00008  *   Copyright (C) 2009-2013 BerLinux Solutions GmbH                       *
00009  *                                                                         *
00010  *   Authors:                                                              *
00011  *      Stefan Schwarzer   <stefan.schwarzer@diskohq.org>,                 *
00012  *      Matthias Hardt     <matthias.hardt@diskohq.org>,                   *
00013  *      Jens Schneider     <jens.schneider@diskohq.org>,                   *
00014  *      Guido Madaus       <guido.madaus@diskohq.org>,                     *
00015  *      Patrick Helterhoff <patrick.helterhoff@diskohq.org>,               *
00016  *      René Bählkow       <rene.baehlkow@diskohq.org>                     *
00017  *                                                                         *
00018  *   This library is free software; you can redistribute it and/or         *
00019  *   modify it under the terms of the GNU Lesser General Public            *
00020  *   License version 2.1 as published by the Free Software Foundation.     *
00021  *                                                                         *
00022  *   This library is distributed in the hope that it will be useful,       *
00023  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00024  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00025  *   Lesser General Public License for more details.                       *
00026  *                                                                         *
00027  *   You should have received a copy of the GNU Lesser General Public      *
00028  *   License along with this library; if not, write to the                 *
00029  *   Free Software Foundation, Inc.,                                       *
00030  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
00031  **************************************************************************/
00032 
00033 /**
00034  * @file  mmsdbsqlite.cpp
00035  *
00036  * @brief Source file for sqlite3 database functions.
00037  *
00038  * @author Stefan Schwarzer <stefan.schwarzer@diskohq.org>
00039  * @author Guido Madaus     <guido.madaus@diskohq.org>
00040  * @author Jens Schneider   <pupeider@gmx.de>
00041  *
00042  * @ingroup mmstools
00043  */
00044 #ifdef __ENABLE_SQLITE__
00045 
00046 #include "mmstools/mmsdbsqlite.h"
00047 #include "mmstools/mmslogger.h"
00048 
00049 /**
00050  * @brief ????
00051  *
00052  */
00053 MMSDBSQLite::MMSDBSQLite(DataSource *_datasource) : IMMSDB(_datasource) {
00054     if(!this->datasource)
00055         throw MMSError(0, "Cannot instantiate MMSDBSQLite without datasource");
00056 };
00057 
00058 /**
00059  * @brief ????
00060  *
00061  */
00062 MMSDBSQLite::~MMSDBSQLite() {
00063     this->disconnect();
00064 }
00065 
00066 /**
00067  * @brief ????
00068  *
00069  */
00070 int MMSDBSQLite::getResults(void *rs, int numCols, char **results, char **columnNames)
00071 {
00072     int             i=0;
00073     MMSRecordSet   *myrs;
00074 
00075     myrs = (MMSRecordSet *) rs;
00076     myrs->addRow();
00077     for(i=0; i < numCols; i++)
00078     {
00079         if(results[i] != NULL)
00080             (*myrs)[columnNames[i]]=results[i];
00081     }
00082 
00083     return 0;
00084 }
00085 
00086 /**
00087  * @brief ????
00088  *
00089  */
00090 void MMSDBSQLite::startTransaction() {
00091     int     rc=0;
00092     char    *errmsg=NULL;
00093 
00094     //open transaction
00095     if((rc = sqlite3_exec((sqlite3 *)this->dbhandle, "BEGIN TRANSACTION", NULL, NULL, &errmsg)) != SQLITE_OK)
00096     {
00097         throw MMSError(rc, errmsg);
00098     }
00099 
00100     return;
00101 }
00102 
00103 /**
00104  * @brief ????
00105  *
00106  */
00107 void MMSDBSQLite::commitTransaction() {
00108     int     rc=0;
00109     char    *errmsg=NULL;
00110 
00111     //open transaction
00112     if((rc = sqlite3_exec((sqlite3 *)this->dbhandle, "COMMIT", NULL, NULL, &errmsg)) != SQLITE_OK)
00113     {
00114         throw MMSError(rc, errmsg);
00115     }
00116 
00117     return;
00118 }
00119 
00120 /**
00121  * @brief ????
00122  *
00123  */
00124 void MMSDBSQLite::rollbackTransaction() {
00125     int     rc=0;
00126     char    *errmsg=NULL;
00127 
00128     //open transaction
00129     if((rc = sqlite3_exec((sqlite3 *)this->dbhandle, "ROLLBACK", NULL, NULL, &errmsg)) != SQLITE_OK)
00130     {
00131         throw MMSError(rc, errmsg);
00132     }
00133 
00134     return;
00135 }
00136 
00137 /**
00138  * @brief Opens connection to database.
00139  *
00140  * @param datasource DataSource object which contains required information for database
00141  *
00142  * @return void
00143  */
00144 void MMSDBSQLite::connect() {
00145    int     rc=0;
00146 
00147    // Open database connection
00148    if((rc = sqlite3_open(datasource->getDatabaseName().c_str(), &this->dbhandle)) != SQLITE_OK)
00149    {
00150        this->disconnect();
00151        string err = sqlite3_errmsg(dbhandle);
00152        sqlite3_close(this->dbhandle);
00153        throw MMSError(rc, err);
00154    }
00155 
00156    connected = true;
00157 
00158    return;
00159 }
00160 
00161 /**
00162  * @brief Close connection to database.
00163  *
00164  * @return void
00165  */
00166 void MMSDBSQLite::disconnect() {
00167 
00168     if(connected) {
00169         sqlite3_close((sqlite3 *)dbhandle);
00170         this->connected = false;
00171     }
00172 
00173     this->dbname = "";
00174 
00175     return;
00176 }
00177 
00178 /**
00179  * @brief This function executes given database query and puts the results in MMSRecordSet.
00180  *        This method is used for select statements
00181  *
00182  * @param statement buffer with database query
00183  *
00184  * @return Returns the number of affected rows
00185  */
00186 int MMSDBSQLite::query(string statement, MMSRecordSet *rs) {
00187     int     rc=0;
00188     char    *errmsg=NULL;
00189     string  message;
00190 
00191     rs->reset();
00192 
00193     if(!this->connected) {
00194         message = "Query called but no connection established." + string(" [query was: ") + statement + string("]");
00195         throw MMSError(rc, message);
00196     }
00197 
00198     if((rc = sqlite3_exec((sqlite3 *)dbhandle, statement.c_str(), &(this->getResults), (void *) rs, &errmsg)) != SQLITE_OK)
00199     {
00200         message = string(errmsg) + string(" [query was: ") + statement + string("]");
00201         sqlite3_free(errmsg);
00202         throw MMSError(rc, message);
00203     }
00204 
00205     //rewind
00206     rs->setRecordNum(0);
00207 
00208     return (sqlite3_changes((sqlite3 *)dbhandle));
00209 }
00210 
00211 /**
00212  * @brief This function executes given database query.
00213  *        This method is used for insert, update and delete statements
00214  *
00215  * @param statement buffer with database query
00216  *
00217  * @return Returns the number of affected rows
00218  */
00219 int MMSDBSQLite::query(string statement) {
00220 
00221     int     rc=0;
00222     char    *errmsg=NULL;
00223     string  message;
00224 
00225     if(!this->connected) {
00226         message = "Query called but no connection established." + string(" [query was: ") + statement + string("]");
00227         throw MMSError(rc, message);
00228     }
00229 
00230     if((rc = sqlite3_exec((sqlite3 *)dbhandle, statement.c_str(), NULL, NULL, &errmsg)) != SQLITE_OK)
00231     {
00232         message = string(errmsg) + string(" [query was: ") + statement + string("]");
00233         sqlite3_free(errmsg);
00234         throw MMSError(rc, message);
00235     }
00236 
00237     // return the number of affected rows
00238     return (sqlite3_changes((sqlite3 *)dbhandle));
00239 }
00240 
00241 /**
00242  * @brief Returns the ID of the last inserted record
00243  *
00244  * @return Returns the ID of the last inserted record
00245  */
00246 int MMSDBSQLite::getLastInsertedID() {
00247     return sqlite3_last_insert_rowid((sqlite3 *)this->dbhandle);
00248 }
00249 
00250 #endif /*__ENABLE_SQLITE__*/

Generated by doxygen