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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
#include "stdafx.h"
#include "SequentialConnection.h"
namespace sql {
/**
* Connection.
*/
SequentialConnection::SequentialConnection()
: toFinalize(null), currentFetching(null) {}
void SequentialConnection::finalizeAll() {
while (toFinalize) {
finalizeStmt(toFinalize);
toFinalize = toFinalize->toFinalize;
}
}
void SequentialConnection::onFinalize(SequentialStatement *stmt) {
if (currentFetching == stmt || currentFetching == null) {
// Finalize immediately, nothing is going on.
finalizeStmt(stmt);
currentFetching = null;
} else {
// Delay finalization until no fetch is going on.
stmt->toFinalize = toFinalize;
toFinalize = stmt;
}
}
void SequentialConnection::startFetch(SequentialStatement *stmt) {
if (currentFetching != stmt)
clearFetch();
currentFetching = stmt;
}
void SequentialConnection::doneFetch(SequentialStatement *stmt) {
if (currentFetching == stmt)
currentFetching = null;
finalizeAll();
}
void SequentialConnection::clearFetch() {
if (currentFetching) {
currentFetching->fetchAll();
// Should be cleared by 'fetchAll'.
assert(currentFetching == null);
currentFetching = null;
}
}
void SequentialConnection::clearFetch(SequentialStatement *stmt) {
if (currentFetching == stmt)
return;
clearFetch();
}
Bool SequentialConnection::isFetching(SequentialStatement *stmt) {
return currentFetching == stmt;
}
/**
* Statement.
*/
SequentialStatement::SequentialStatement(SequentialConnection *owner)
: Statement(owner), owner(owner), atEnd(true) {}
SequentialStatement::~SequentialStatement() {
finalize();
}
void SequentialStatement::finalize() {
// Invalidate any existing result instances.
invalidateResult();
if (owner) {
owner->onFinalize(this);
deregister(owner);
}
// To avoid multiple finalization, for example when the parent class is closed or finalized before us.
owner = null;
}
Statement::Result SequentialStatement::execute() {
invalidateResult();
disposeResult();
owner->startFetch(this);
try {
if (executeSeq()) {
atEnd = false;
} else {
// No result, reset everything.
disposeResult();
}
return Result(this);
} catch (...) {
// Error during execute, make sure to indicate that we don't have any result.
disposeResult();
throw;
}
}
Maybe<Row> SequentialStatement::nextRow() {
Maybe<Row> out; // null by default
if (buffer) {
if (buffer->any()) {
out = Maybe<Row>(buffer->last());
buffer->pop();
} else {
buffer = null;
atEnd = true;
}
} else if (!atEnd) {
out = nextRowSeq();
if (out.empty())
fetchDone();
}
return out;
}
void SequentialStatement::disposeResult() {
// This may if the statement is explicitly finalized, but result instances linger.
if (!owner)
return;
// Note: 'dispose' may be called just about anytime, so we need to make sure to only call
// the Seq version when it is actually our turn. Otherwise we fail to uphold the Seq
// guarantees.
if (owner->isFetching(this)) {
disposeResultSeq();
owner->doneFetch(this);
}
}
void SequentialStatement::fetchDone() {
// Call dispose to help the client to clean up. We will inhibit future calls as soon as we
// call 'owner->doneFetch'.
disposeResultSeq();
atEnd = true;
owner->doneFetch(this);
}
void SequentialStatement::fetchAll() {
buffer = new (this) Array<Row>();
while (!atEnd) {
Maybe<Row> r = nextRowSeq();
if (r.any())
buffer->push(r.value());
else
fetchDone();
}
buffer->reverse();
}
}
|