59 lines
865 B
C++
59 lines
865 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(!_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;
|
|
}
|