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

src/data/SqlLiteConnection.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/Exception.h>
00018 #include <spl/io/File.h>
00019 #include <spl/Int32.h>
00020 #include <spl/data/SqlLiteCommand.h>
00021 #include <spl/data/SqlLiteConnection.h>
00022 #include <spl/data/SqlLiteTransaction.h>
00023 
00024 #include "src/sqllite/sqlite3.h"
00025 
00026 SqlLiteConnection::SqlLiteConnection(const String& databaseFilename)
00027 : Connection("", databaseFilename, "", ""), m_db(NULL)
00028 {
00029         ChangeDatabase(databaseFilename);
00030 }
00031 
00032 SqlLiteConnection::~SqlLiteConnection()
00033 {
00034         Close();
00035 }
00036 
00038 void SqlLiteConnection::Open()
00039 {
00040         if (NULL == m_db)
00041         {
00042                 int sres = sqlite3_open(m_database.GetChars(), (sqlite3 **)&m_db);
00043                 if (0 != sres)
00044                 {
00045                         throw new SqlException(String("Can't open database: ") + sqlite3_errmsg((sqlite3 *)m_db));
00046                 }
00047         }
00048 }
00049 
00051 void SqlLiteConnection::Close()
00052 {
00053         if (NULL != m_db)
00054         {
00055                 sqlite3_close((sqlite3 *)m_db);
00056                 m_db = NULL;
00057         }
00058 }
00059 
00061 void SqlLiteConnection::ChangeDatabase(const String& databaseFilename)
00062 {
00063         m_database = databaseFilename;
00064         if (NULL != m_db)
00065         {
00066                 Close();
00067                 Open();
00068         }
00069 }
00070 
00071 TransactionPtr SqlLiteConnection::BeginTransaction()
00072 {
00073         return SqlLiteTransactionPtr (new SqlLiteTransaction(m_db));
00074 }
00075 
00076 CommandPtr SqlLiteConnection::CreateCommand()
00077 {
00078         return SqlLiteCommandPtr(new SqlLiteCommand(m_db, ""));
00079 }
00080 
00081 CommandPtr SqlLiteConnection::CreateCommand(const String& cmdText)
00082 {
00083         return SqlLiteCommandPtr(new SqlLiteCommand(m_db, cmdText));
00084 }
00085 
00086 int SqlLiteConnection::ExecuteNonQuery(const String& sql)
00087 {
00088         SqlLiteCommand cmd(m_db, sql);
00089         return cmd.ExecuteNonQuery();
00090 }
00091 
00092 RecordSetPtr SqlLiteConnection::ExecuteQuery(const String& sql)
00093 {
00094         SqlLiteCommand cmd(m_db, sql);
00095         return cmd.ExecuteQuery();
00096 }
00097 
00098 void SqlLiteConnection::RegisterThread()
00099 {
00100 }
00101 
00102 void SqlLiteConnection::RegisterThreadExit()
00103 {
00104 }
00105 
00106 void SqlLiteConnection::AllowOutOfOrderCommandParameters(bool allow)
00107 {
00108 }
00109 
00110 #if defined(DEBUG)
00111 void SqlLiteConnection::CheckMem() const
00112 {
00113         Connection::CheckMem();
00114 }
00115 
00116 void SqlLiteConnection::ValidateMem() const
00117 {
00118         Connection::ValidateMem();
00119 }
00120 #endif
00121