MySQLWrapper/MySQLTransaction.cpp

59 lines
835 B
C++
Raw Normal View History

2017-11-23 14:48:10 +08:00
#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()
{
2017-11-26 16:58:30 +08:00
if(_isReady&&(!_operated))
2017-11-23 14:48:10 +08:00
{
rollback();
}
}
int Transaction::commit()
{
if(_operated) return -2;
if(conn.exec("commit",nullptr)<0)
{
return -1;
}
else
{
_operated=true;
2017-11-29 15:29:30 +08:00
return 0;
2017-11-23 14:48:10 +08:00
}
}
int Transaction::rollback()
{
if(_operated) return -2;
if(conn.exec("rollback",nullptr)<0)
{
return -1;
}
else
{
_operated=true;
2017-11-29 15:29:30 +08:00
return 0;
2017-11-23 14:48:10 +08:00
}
}
bool Transaction::isReady() const
{
return _isReady;
}