Files
SQLiteCpp/src/SQLiteC++/Database.cpp
Sebastien Rombauts 9658ee7e16 Added a small private method Statement::check(ret)
- Throws a SQLite::Exception with the SQLite error message
2012-04-01 20:53:26 +02:00

72 lines
2.0 KiB
C++

/**
* @file Database.cpp
* @brief Management of a SQLite Database Connection.
*
* Copyright (c) 2012 Sebastien Rombauts (sebastien dot rombauts at gmail dot com)
*
* Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
* or copy at http://opensource.org/licenses/MIT)
*/
#include "Database.h"
#include "Statement.h"
#include <iostream>
namespace SQLite
{
// Open the provided database UTF-8 filename.
Database::Database(const char* apFilename, const int aFlags /*= SQLITE_OPEN_READONLY*/) : // throw(SQLite::Exception)
mpSQLite(NULL),
mFilename(apFilename)
{
int ret = sqlite3_open_v2(apFilename, &mpSQLite, aFlags, NULL);
if (SQLITE_OK != ret)
{
std::string strerr = sqlite3_errmsg(mpSQLite);
sqlite3_close(mpSQLite);
throw SQLite::Exception(strerr);
}
}
// Close the SQLite database connection.
Database::~Database(void) throw() // nothrow
{
// check for undestroyed statements
std::vector<Statement*>::iterator iStatement;
for (iStatement = mStatementList.begin();
iStatement != mStatementList.end();
iStatement++)
{
// TODO (*iStatement)->Finalize(); ?
std::cout << "Unregistered statement: " << (*iStatement)->getQuery().c_str() << " !\n";
}
int ret = sqlite3_close(mpSQLite);
if (SQLITE_OK != ret)
{
// Never throw an exception in a destructor
//std::cout << sqlite3_errmsg(mpSQLite);
}
}
// Register a Statement object (a SQLite query)
void Database::registerStatement(Statement& aStatement) // throw(SQLite::Exception)
{
mStatementList.push_back(&aStatement);
}
// Unregister a Statement object
void Database::unregisterStatement(Statement& aStatement) // throw(SQLite::Exception)
{
std::vector<Statement*>::iterator iStatement;
iStatement = std::find(mStatementList.begin(), mStatementList.end(), &aStatement);
if (mStatementList.end() != iStatement)
{
mStatementList.erase(iStatement);
}
}
}; // namespace SQLite