/home/martin/workspace/OpenStreetNav/src/sqlite/Database.cpp
Go to the documentation of this file.
00001 /*
00002  * Database.cpp
00003  *
00004  *  Created on: Nov 2, 2011
00005  *      Author: martin
00006  */
00007 
00008 #include "Database.h"
00009 #include "SqliteException.h"
00010 #include "InvalidUseException.h"
00011 #include <boost/filesystem.hpp>
00012 
00013 #include "Statement.h"
00014 
00015 namespace sqlite
00016 {
00017 
00018 Database::Database(const std::string& filename)
00019     : conn(NULL)
00020 {
00021     is_nw = !boost::filesystem::exists(filename);
00022     throw_sqlite_status(sqlite3_open(filename.c_str(), &conn), conn);
00023 }
00024 
00025 Database::~Database()
00026 {
00027     force_close();
00028 }
00029 
00030 Database& Database::operator =(Database && other)
00031 {
00032     force_close();
00033     conn = other.conn;
00034     other.conn = NULL;
00035     stmts = other.stmts;
00036     other.stmts.clear();
00037     return *this;
00038 }
00039 
00040 Database::Database(Database && other)
00041     : conn(NULL)
00042 {
00043     *this = std::move(other);
00044 }
00045 
00046 void Database::close()
00047 {
00048     if (stmts.size() > 0)
00049         throw InvalidUseException("Trying to close connection with non-finalized statements");
00050     if (conn == NULL)
00051         return;
00052     sqlite3_close(conn);
00053     conn = NULL;
00054 }
00055 
00056 void Database::force_close() throw()
00057 {
00058     while (stmts.size() > 0)
00059         (*stmts.begin())->finalize();
00060     close();
00061 }
00062 
00063 void Database::register_statement(Statement& st)
00064 {
00065     if (stmts.count(&st))
00066         return;
00067     stmts.insert(&st);
00068 }
00069 
00070 void Database::unregister_statement(Statement& st)
00071 {
00072     stmts.erase(&st);
00073 }
00074 
00075 int Database::unfinalized()
00076 {
00077     return stmts.size();
00078 }
00079 
00080 bool Database::is_new()
00081 {
00082     return is_nw;
00083 }
00084 
00085 sqlite3* const& Database::cobj()
00086 {
00087     return conn;
00088 }
00089 
00090 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines