- Fixed a compiler warning in Indexer

- Cleaned up ScopedTransaction
This commit is contained in:
casey 2016-07-04 15:28:14 -07:00
parent 5916bc11b2
commit d918c4e028
3 changed files with 23 additions and 67 deletions

View File

@ -39,71 +39,42 @@
using namespace musik::core::db; using namespace musik::core::db;
ScopedTransaction::ScopedTransaction(Connection &connection)
////////////////////////////////////////// : canceled(false) {
///\brief this->connection = &connection;
///Constructor
///
///\param connection
///Connection to run transaction on
//////////////////////////////////////////
ScopedTransaction::ScopedTransaction(Connection &connection) : canceled(false){
this->connection = &connection;
this->Begin(); this->Begin();
} }
ScopedTransaction::~ScopedTransaction() {
//////////////////////////////////////////
///\brief
///Destructor will end the transaction if it's the last nested transaction
//////////////////////////////////////////
ScopedTransaction::~ScopedTransaction(){
this->End(); this->End();
} }
void ScopedTransaction::Cancel() {
////////////////////////////////////////// this->canceled = true;
///\brief
///If canceled, this all statements in the transaction scope will be canceled
//////////////////////////////////////////
void ScopedTransaction::Cancel(){
this->canceled = true;
} }
void ScopedTransaction::CommitAndRestart() {
//////////////////////////////////////////
///\brief
///Sometimes it's a good option to be able to commit a transaction and restart it all over.
//////////////////////////////////////////
void ScopedTransaction::CommitAndRestart(){
this->End(); this->End();
this->Begin(); this->Begin();
} }
//////////////////////////////////////////
///\brief
///Runs the acctual BEGIN TRANSACTION on the database
//////////////////////////////////////////
void ScopedTransaction::Begin(){ void ScopedTransaction::Begin(){
if(this->connection->transactionCounter==0){ if (this->connection->transactionCounter == 0) {
this->connection->Execute("BEGIN TRANSACTION"); this->connection->Execute("BEGIN TRANSACTION");
} }
++this->connection->transactionCounter; ++this->connection->transactionCounter;
} }
////////////////////////////////////////// void ScopedTransaction::End() {
///\brief
///Runs the COMMIT or ROLLBACK on the database
//////////////////////////////////////////
void ScopedTransaction::End(){
--this->connection->transactionCounter; --this->connection->transactionCounter;
if(this->connection->transactionCounter==0){
if(this->canceled){ if (this->connection->transactionCounter == 0) {
if (this->canceled) {
this->connection->Execute("ROLLBACK TRANSACTION"); this->connection->Execute("ROLLBACK TRANSACTION");
}else{ }
else {
this->connection->Execute("COMMIT TRANSACTION"); this->connection->Execute("COMMIT TRANSACTION");
} }
} }
} }

View File

@ -39,39 +39,24 @@
#include <boost/utility.hpp> #include <boost/utility.hpp>
#include <boost/shared_ptr.hpp> #include <boost/shared_ptr.hpp>
namespace musik{ namespace core{ namespace db{ namespace musik { namespace core { namespace db {
// Forward declare
class Connection; class Connection;
////////////////////////////////////////// class ScopedTransaction : boost::noncopyable {
///\brief
///ScopedTransaction is used to make transactions easier
///
///Usage like this:
///\code
///{
/// musik::code::db::ScopedTransaction transactionScope(db)
/// //everything in this scope is included in the transation
///}
///\endcode
///
///\remarks
///Nested transations are partially supported. First scope is the one deciding when transaction is commited.
//////////////////////////////////////////
class ScopedTransaction : boost::noncopyable{
public: public:
ScopedTransaction(Connection &connection); ScopedTransaction(Connection &connection);
~ScopedTransaction(); ~ScopedTransaction();
void Cancel(); void Cancel();
void CommitAndRestart(); void CommitAndRestart();
private: private:
Connection *connection;
bool canceled;
inline void Begin(); inline void Begin();
inline void End(); inline void End();
Connection *connection;
bool canceled;
}; };
} } } } } }

View File

@ -382,7 +382,7 @@ void Indexer::SyncDirectory(
catch(...) { catch(...) {
} }
#undef WAIT_FOR_ACTIVE() #undef WAIT_FOR_ACTIVE
} }
void Indexer::ThreadLoop() { void Indexer::ThreadLoop() {