Skip to content

Commit 4fd0e28

Browse files
committed
Close the result set on error in addition to if the rows are exhausted
1 parent a828f7c commit 4fd0e28

File tree

2 files changed

+44
-27
lines changed

2 files changed

+44
-27
lines changed

src/njsResultSet.cpp

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -275,76 +275,91 @@ NAN_METHOD(njsResultSet::GetRows)
275275

276276

277277
//-----------------------------------------------------------------------------
278-
// njsResultSet::Async_GetRows()
279-
// Worker function for njsResultSet::GetRowsCommon() method.
278+
// njsResultSet::GetRowsHelper()
279+
// Get rows from the result set and return a boolean indicating if the
280+
// fetch was successful or not.
280281
//-----------------------------------------------------------------------------
281-
void njsResultSet::Async_GetRows(njsBaton *baton)
282+
bool njsResultSet::GetRowsHelper(njsBaton *baton, int *moreRows)
282283
{
283-
njsResultSet *resultSet = (njsResultSet*) baton->callingObj;
284284
uint32_t i, numRowsToFetch;
285285
njsVariable *var;
286-
int moreRows;
287286

288287
// determine how many rows to fetch; use fetchArraySize unless it is less
289288
// than maxRows (no need to waste memory!)
290289
numRowsToFetch = baton->fetchArraySize;
291-
if (resultSet->maxRows > 0 && resultSet->maxRows < numRowsToFetch)
292-
numRowsToFetch = resultSet->maxRows;
290+
if (this->maxRows > 0 && this->maxRows < numRowsToFetch)
291+
numRowsToFetch = this->maxRows;
293292

294293
// create ODPI-C variables and define them, if necessary
295-
for (i = 0; i < resultSet->numQueryVars; i++) {
296-
var = &resultSet->queryVars[i];
294+
for (i = 0; i < this->numQueryVars; i++) {
295+
var = &this->queryVars[i];
297296
if (var->dpiVarHandle && var->maxArraySize >= numRowsToFetch)
298297
continue;
299298
if (var->dpiVarHandle) {
300299
if (dpiVar_release(var->dpiVarHandle) < 0) {
301300
baton->GetDPIError();
302-
return;
301+
return false;
303302
}
304303
var->dpiVarHandle = NULL;
305304
}
306-
if (dpiConn_newVar(resultSet->dpiConnHandle, var->varTypeNum,
305+
if (dpiConn_newVar(this->dpiConnHandle, var->varTypeNum,
307306
var->nativeTypeNum, numRowsToFetch, var->maxSize, 1, 0, NULL,
308307
&var->dpiVarHandle, &var->dpiVarData) < 0) {
309308
baton->GetDPIError();
310-
return;
309+
return false;
311310
}
312311
var->maxArraySize = numRowsToFetch;
313-
if (dpiStmt_define(resultSet->dpiStmtHandle, i + 1,
312+
if (dpiStmt_define(this->dpiStmtHandle, i + 1,
314313
var->dpiVarHandle) < 0) {
315314
baton->GetDPIError();
316-
return;
315+
return false;
317316
}
318317
}
319318

320319
// set fetch array size as requested
321-
if (dpiStmt_setFetchArraySize(resultSet->dpiStmtHandle,
320+
if (dpiStmt_setFetchArraySize(this->dpiStmtHandle,
322321
numRowsToFetch) < 0) {
323322
baton->GetDPIError();
324-
return;
323+
return false;
325324
}
326325

327326
// perform fetch
328-
if (dpiStmt_fetchRows(resultSet->dpiStmtHandle, numRowsToFetch,
329-
&baton->bufferRowIndex, &baton->rowsFetched, &moreRows) < 0) {
327+
if (dpiStmt_fetchRows(this->dpiStmtHandle, numRowsToFetch,
328+
&baton->bufferRowIndex, &baton->rowsFetched, moreRows) < 0) {
330329
baton->GetDPIError();
331-
return;
330+
return false;
332331
}
333332

334333
// result sets that should be auto closed are closed if the result set
335334
// is exhaused or the maximum number of rows has been fetched
336-
if (moreRows && resultSet->maxRows > 0) {
337-
if (baton->rowsFetched == resultSet->maxRows)
338-
moreRows = 0;
339-
else resultSet->maxRows -= baton->rowsFetched;
335+
if (*moreRows && this->maxRows > 0) {
336+
if (baton->rowsFetched == this->maxRows)
337+
*moreRows = 0;
338+
else this->maxRows -= baton->rowsFetched;
340339
}
341-
if (!moreRows && resultSet->autoClose) {
340+
return njsConnection::ProcessVars(baton, this->queryVars,
341+
this->numQueryVars, baton->rowsFetched);
342+
}
343+
344+
345+
//-----------------------------------------------------------------------------
346+
// njsResultSet::Async_GetRows()
347+
// Worker function for njsResultSet::GetRows() method.
348+
//-----------------------------------------------------------------------------
349+
void njsResultSet::Async_GetRows(njsBaton *baton)
350+
{
351+
njsResultSet *resultSet = (njsResultSet*) baton->callingObj;
352+
int moreRows;
353+
bool success;
354+
355+
// result sets that should be auto closed are closed if the result set
356+
// is exhaused or the maximum number of rows has been fetched or an error
357+
// has taken place
358+
success = resultSet->GetRowsHelper(baton, &moreRows);
359+
if ((!success || !moreRows) && resultSet->autoClose) {
342360
dpiStmt_release(resultSet->dpiStmtHandle);
343361
resultSet->dpiStmtHandle = NULL;
344362
}
345-
346-
njsConnection::ProcessVars(baton, resultSet->queryVars,
347-
resultSet->numQueryVars, baton->rowsFetched);
348363
}
349364

350365

src/njsResultSet.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ class njsResultSet: public njsCommon {
8383
extendedMetaData(false), autoClose(false) {}
8484
~njsResultSet();
8585

86+
bool GetRowsHelper(njsBaton *baton, int *moreRows);
87+
8688
static NAN_METHOD(New);
8789

8890
// Get Rows Methods

0 commit comments

Comments
 (0)