MySQLWrapper/MySQLTransaction.cpp
2017-11-26 16:58:30 +08:00

59 lines
877 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 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;
}