• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

src/data/SqlLiteTransaction.cpp

00001 /*
00002  *   This file is part of the Standard Portable Library (SPL).
00003  *
00004  *   SPL is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   SPL is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with SPL.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 #include <spl/data/SqlLiteTransaction.h>
00018 #include <spl/String.h>
00019 #include "src/sqllite/sqlite3.h"
00020 
00021 void _ExecuteSqlLite(void *db, const String& sql)
00022 {
00023         char *zErrMsg = NULL;
00024         int rc = sqlite3_exec((sqlite3 *)db, sql.GetChars(), NULL, NULL, &zErrMsg);
00025         if (SQLITE_OK != rc)
00026         {
00027                 SqlException *ex = new SqlException(zErrMsg);
00028                 sqlite3_free(zErrMsg);
00029                 throw ex;
00030         }
00031 }
00032 
00033 SqlLiteTransaction::SqlLiteTransaction(void *db)
00034 : m_db(db)
00035 {
00036         _ExecuteSqlLite((sqlite3 *)m_db, "BEGIN TRANSACTION;");
00037 }
00038 
00039 SqlLiteTransaction::~SqlLiteTransaction()
00040 {
00041         if (NULL != m_db)
00042         {
00043                 Rollback();
00044         }
00045 }
00046 
00047 void SqlLiteTransaction::Commit()
00048 {
00049         _ExecuteSqlLite((sqlite3 *)m_db, "COMMIT TRANSACTION;");
00050         m_db = NULL;
00051 }
00052 
00053 void SqlLiteTransaction::Rollback()
00054 {
00055         _ExecuteSqlLite((sqlite3 *)m_db, "ROLLBACK TRANSACTION;");
00056         m_db = NULL;
00057 }