1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
#include "stdafx.h"
#include "Transaction.h"
#include "SQL.h"
namespace sql {
static const Transaction::End endDone = Transaction::End(-1); // Sentinel value.
Transaction::Transaction(DBConnection *c) : state(new (c) State(c, endRollback)) {}
Transaction::Transaction(DBConnection *c, End end) : state(new (c) State(c, end)) {}
Transaction::~Transaction() {
state->unref();
}
Transaction::Transaction(const Transaction &other) : state(other.state) {
state->ref();
}
Transaction &Transaction::operator =(const Transaction &other) {
other.state->ref();
state->unref();
state = other.state;
return *this;
}
void Transaction::commit() {
state->commit();
}
void Transaction::rollback() {
state->rollback();
}
/**
* State.
*/
Transaction::State::State(DBConnection *c, End end) : connection(c), parent(c->transaction), end(end), refs(1) {
c->transaction = this;
if (parent == null)
c->beginTransaction();
}
void Transaction::State::finish() {
if (connection == null)
return;
if (connection->transaction == this) {
connection->transaction = parent;
if (parent == null && end != endNothing && end != endDone)
connection->endTransaction(end);
}
connection = null;
end = endDone;
}
void Transaction::State::commit() {
if (end != endDone) {
end = endDone;
connection->endTransaction(endCommit);
}
}
void Transaction::State::rollback() {
if (end != endDone) {
end = endDone;
connection->endTransaction(endRollback);
}
}
}
|