MySQLWrapper/MySQLTransaction.cpp

59 lines
835 B
C++

#include "MySQLTransaction.h"
Transaction::Transaction(MySQLConn& MySQLConnection) : conn(MySQLConnection)
{
_isReady=false;
_operated=false;
if(conn.exec("start transaction",nullptr)<0)
{
/// SQL execute error.
}
else
{
_isReady=true;
}
}
Transaction::~Transaction()
{
if(_isReady&&(!_operated))
{
rollback();
}
}
int Transaction::commit()
{
if(_operated) return -2;
if(conn.exec("commit",nullptr)<0)
{
return -1;
}
else
{
_operated=true;
return 0;
}
}
int Transaction::rollback()
{
if(_operated) return -2;
if(conn.exec("rollback",nullptr)<0)
{
return -1;
}
else
{
_operated=true;
return 0;
}
}
bool Transaction::isReady() const
{
return _isReady;
}