MySQLWrapper/MySQLTransaction.cpp

59 lines
865 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()
{
if(!_operated)
{
rollback();
}
}
int Transaction::commit()
{
if(_operated) return -2;
if(conn.exec("commit",nullptr)<0)
{
return -1;
}
else
{
_operated=true;
return conn.getAffectedRows();
}
}
int Transaction::rollback()
{
if(_operated) return -2;
if(conn.exec("rollback",nullptr)<0)
{
return -1;
}
else
{
_operated=true;
return conn.getAffectedRows();
}
}
bool Transaction::isReady() const
{
return _isReady;
}