Skip to content

Commit 7113735

Browse files
committed
separate A calls for getRow and getRows
1 parent 9fbafce commit 7113735

File tree

2 files changed

+108
-58
lines changed

2 files changed

+108
-58
lines changed

src/njs/src/njsResultSet.cpp

Lines changed: 102 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,28 @@
1818
* This file uses NAN:
1919
*
2020
* Copyright (c) 2015 NAN contributors
21-
*
21+
*
2222
* NAN contributors listed at https://github.com/rvagg/nan#contributors
23-
*
23+
*
2424
* Permission is hereby granted, free of charge, to any person obtaining
2525
* a copy of this software and associated documentation files (the
2626
* "Software"), to deal in the Software without restriction, including
2727
* without limitation the rights to use, copy, modify, merge, publish,
2828
* distribute, sublicense, and/or sell copies of the Software, and to
2929
* permit persons to whom the Software is furnished to do so, subject to
3030
* the following conditions:
31-
*
31+
*
3232
* The above copyright notice and this permission notice shall be
3333
* included in all copies or substantial portions of the Software.
34-
*
34+
*
3535
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
3636
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
3737
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
3838
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
3939
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
4040
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
4141
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
42-
*
42+
*
4343
* NAME
4444
* njsResultSet.cpp
4545
*
@@ -88,13 +88,13 @@ void ResultSet::setResultSet ( dpi::Stmt *stmt, dpi::Env *env,
8888
*/
8989
void ResultSet::Init(Handle<Object> target)
9090
{
91-
NanScope();
91+
NanScope();
9292
Local<FunctionTemplate> temp = NanNew<FunctionTemplate>(New);
9393
temp->InstanceTemplate()->SetInternalFieldCount(1);
9494
temp->SetClassName(NanNew<v8::String>("ResultSet"));
9595

9696
NODE_SET_PROTOTYPE_METHOD(temp, "close", Close);
97-
NODE_SET_PROTOTYPE_METHOD(temp, "getRow", GetRows);
97+
NODE_SET_PROTOTYPE_METHOD(temp, "getRow", GetRow);
9898
NODE_SET_PROTOTYPE_METHOD(temp, "getRows", GetRows);
9999

100100
temp->InstanceTemplate()->SetAccessor(
@@ -145,8 +145,8 @@ NAN_PROPERTY_GETTER(ResultSet::GetMetaData)
145145
NanReturnUndefined();
146146
}
147147
std::string *columnNames = new std::string[njsResultSet->numCols_];
148-
Connection::CopyMetaData ( columnNames, njsResultSet->meta_,
149-
njsResultSet->numCols_ );
148+
Connection::CopyMetaData ( columnNames, njsResultSet->meta_,
149+
njsResultSet->numCols_ );
150150
Handle<Value> meta;
151151
meta = Connection::GetMetaData( columnNames,
152152
njsResultSet->numCols_ );
@@ -173,13 +173,42 @@ NAN_SETTER(ResultSet::SetMetaData)
173173
NJS_SET_EXCEPTION(msg.c_str(), (int) msg.length());
174174
}
175175

176+
/*****************************************************************************/
177+
/*
178+
DESCRIPTION
179+
Get Row method on Result Set class.
180+
181+
PARAMETERS:
182+
Arguments - callback
183+
*/
184+
NAN_METHOD(ResultSet::GetRow)
185+
{
186+
NanScope();
187+
188+
Local<Function> callback;
189+
NJS_GET_CALLBACK ( callback, args );
190+
191+
ResultSet *njsResultSet = ObjectWrap::Unwrap<ResultSet>(args.This());
192+
rsBaton *getRowsBaton = new rsBaton ();
193+
NanAssignPersistent(getRowsBaton->cb, callback );
194+
195+
NJS_CHECK_NUMBER_OF_ARGS ( getRowsBaton->error, args, 1, 1, exitGetRow );
196+
197+
getRowsBaton->numRows = 1;
198+
getRowsBaton->njsRS = njsResultSet;
199+
200+
exitGetRow:
201+
ResultSet::GetRowsCommon(getRowsBaton);
202+
NanReturnUndefined();
203+
}
204+
176205
/*****************************************************************************/
177206
/*
178207
DESCRIPTION
179208
Get Rows method on Result Set class.
180209
181210
PARAMETERS:
182-
Arguments - Callback
211+
Arguments - numRows, callback
183212
*/
184213
NAN_METHOD(ResultSet::GetRows)
185214
{
@@ -189,55 +218,69 @@ NAN_METHOD(ResultSet::GetRows)
189218
NJS_GET_CALLBACK ( callback, args );
190219

191220
ResultSet *njsResultSet = ObjectWrap::Unwrap<ResultSet>(args.This());
192-
rsBaton *getRowsBaton = new rsBaton ();
221+
rsBaton *getRowsBaton = new rsBaton ();
193222
NanAssignPersistent(getRowsBaton->cb, callback );
194223

195-
NJS_CHECK_NUMBER_OF_ARGS ( getRowsBaton->error, args, 1, 2, exitGetRows );
196-
if(args.Length() == 2)
197-
{
198-
NJS_GET_ARG_V8UINT ( getRowsBaton->numRows, getRowsBaton->error,
199-
args, 0, exitGetRows );
200-
getRowsBaton->fetchMultiple = true;
201-
}
202-
else
203-
{
204-
getRowsBaton->numRows = 1;
205-
}
224+
NJS_CHECK_NUMBER_OF_ARGS ( getRowsBaton->error, args, 2, 2, exitGetRows );
225+
NJS_GET_ARG_V8UINT ( getRowsBaton->numRows, getRowsBaton->error,
226+
args, 0, exitGetRows );
227+
228+
getRowsBaton->fetchMultiple = true;
229+
getRowsBaton->njsRS = njsResultSet;
230+
exitGetRows:
231+
ResultSet::GetRowsCommon(getRowsBaton);
232+
NanReturnUndefined();
233+
}
206234

207-
getRowsBaton->njsRS = njsResultSet;
208-
getRowsBaton->ebaton = new eBaton;
235+
/*****************************************************************************/
236+
/*
237+
DESCRIPTION
238+
Common method for GetRow and GetRows method
209239
210-
if(!njsResultSet->njsconn_->isValid())
240+
PARAMETERS:
241+
Arguments - resultset baton
242+
*/
243+
void ResultSet::GetRowsCommon(rsBaton *getRowsBaton)
244+
{
245+
ResultSet *njsRS;
246+
eBaton *ebaton;
247+
248+
if(!(getRowsBaton->error).empty()) goto exitGetRowsCommon;
249+
250+
if(!getRowsBaton->njsRS->njsconn_->isValid())
211251
{
212252
getRowsBaton->error = NJSMessages::getErrorMsg ( errInvalidConnection );
213-
goto exitGetRows;
253+
goto exitGetRowsCommon;
214254
}
215-
if(njsResultSet->state_ == INVALID)
255+
if(getRowsBaton->njsRS->state_ == INVALID)
216256
{
217257
getRowsBaton->error = NJSMessages::getErrorMsg ( errInvalidResultSet );
218-
goto exitGetRows;
258+
goto exitGetRowsCommon;
219259
}
220-
else if(njsResultSet->state_ == ACTIVE)
260+
else if(getRowsBaton->njsRS->state_ == ACTIVE)
221261
{
222262
getRowsBaton->error = NJSMessages::getErrorMsg ( errBusyResultSet );
223-
goto exitGetRows;
263+
goto exitGetRowsCommon;
224264
}
225265

226-
njsResultSet->state_ = ACTIVE;
227-
getRowsBaton->ebaton->columnNames = new std::string[njsResultSet->numCols_];
228-
getRowsBaton->ebaton->maxRows = getRowsBaton->numRows;
229-
getRowsBaton->ebaton->dpistmt = njsResultSet->dpistmt_;
230-
getRowsBaton->ebaton->dpienv = njsResultSet->dpienv_;
231-
getRowsBaton->ebaton->getRS = true;
232-
getRowsBaton->ebaton->outFormat = njsResultSet->outFormat_;
266+
getRowsBaton->ebaton = new eBaton;
267+
ebaton = getRowsBaton->ebaton;
268+
njsRS = getRowsBaton->njsRS;
233269

234-
exitGetRows:
235-
getRowsBaton->req.data = (void *)getRowsBaton;
270+
njsRS->state_ = ACTIVE;
271+
ebaton->columnNames = new std::string[njsRS->numCols_];
272+
ebaton->maxRows = getRowsBaton->numRows;
273+
ebaton->dpistmt = njsRS->dpistmt_;
274+
ebaton->dpienv = njsRS->dpienv_;
275+
ebaton->getRS = true;
276+
ebaton->outFormat = njsRS->outFormat_;
277+
278+
exitGetRowsCommon:
279+
getRowsBaton->req.data = (void *)getRowsBaton;
236280

237281
uv_queue_work(uv_default_loop(), &getRowsBaton->req, Async_GetRows,
238282
(uv_after_work_cb)Async_AfterGetRows);
239283

240-
NanReturnUndefined();
241284
}
242285

243286
/*****************************************************************************/
@@ -261,16 +304,16 @@ void ResultSet::Async_GetRows(uv_work_t *req)
261304

262305
if(njsRS->rsEmpty_)
263306
{
264-
ebaton->rowsFetched = 0;
307+
ebaton->rowsFetched = 0;
265308
goto exitAsyncGetRows;
266309
}
267310

268311
try
269312
{
270-
Connection::CopyMetaData ( ebaton->columnNames, njsRS->meta_,
313+
Connection::CopyMetaData ( ebaton->columnNames, njsRS->meta_,
271314
njsRS->numCols_ );
272315
ebaton->numCols = njsRS->numCols_;
273-
if( !njsRS->defineBuffers_ ||
316+
if( !njsRS->defineBuffers_ ||
274317
njsRS->fetchRowCount_ < getRowsBaton->numRows )
275318
{
276319
if( njsRS->defineBuffers_ )
@@ -284,7 +327,7 @@ void ResultSet::Async_GetRows(uv_work_t *req)
284327
}
285328
ebaton->defines = njsRS->defineBuffers_;
286329
Connection::DoFetch(ebaton);
287-
330+
288331
if(ebaton->rowsFetched != getRowsBaton->numRows)
289332
njsRS->rsEmpty_ = true;
290333
}
@@ -324,11 +367,11 @@ void ResultSet::Async_AfterGetRows(uv_work_t *req)
324367
getRowsBaton->njsRS->state_ = INACTIVE;
325368
eBaton* ebaton = getRowsBaton->ebaton;
326369
ebaton->outFormat = getRowsBaton->njsRS->outFormat_;
327-
Handle<Value> rowsArray = NanNew<v8::Array>(0),
370+
Handle<Value> rowsArray = NanNew<v8::Array>(0),
328371
rowsArrayValue = NanNew(NanNull());
329372

330373
if(ebaton->rowsFetched)
331-
{
374+
{
332375
rowsArray = Connection::GetRows(ebaton);
333376
if(!(ebaton->error).empty())
334377
{
@@ -338,7 +381,7 @@ void ResultSet::Async_AfterGetRows(uv_work_t *req)
338381
}
339382
rowsArrayValue = Handle<Array>::Cast(rowsArray)->Get(0);
340383
}
341-
argv[1] = (getRowsBaton->fetchMultiple) ? rowsArray : rowsArrayValue;
384+
argv[1] = (getRowsBaton->fetchMultiple) ? rowsArray : rowsArrayValue;
342385
}
343386

344387
exitAsyncAfterGetRows:
@@ -423,9 +466,11 @@ void ResultSet::Async_Close(uv_work_t *req)
423466

424467
Define* defineBuffers = closeBaton-> njsRS-> defineBuffers_;
425468
unsigned int numCols = closeBaton-> njsRS-> numCols_;
426-
427-
ResultSet::clearFetchBuffer(defineBuffers, numCols);
428-
closeBaton-> njsRS-> defineBuffers_ = NULL;
469+
if(defineBuffers)
470+
{
471+
ResultSet::clearFetchBuffer(defineBuffers, numCols);
472+
closeBaton-> njsRS-> defineBuffers_ = NULL;
473+
}
429474
}
430475
catch(dpi::Exception& e)
431476
{
@@ -474,24 +519,24 @@ void ResultSet::Async_AfterClose(uv_work_t *req)
474519
/*****************************************************************************/
475520
/*
476521
DESCRIPTION
477-
Free FetchBuffers
522+
Free FetchBuffers
478523
479524
PARAMETERS:
480-
Fetch Buffer, numCols
525+
Fetch Buffer, numCols
481526
*/
482527
void ResultSet::clearFetchBuffer( Define* defineBuffers, unsigned int numCols)
483528
{
484529
for( unsigned int i=0; i<numCols; i++ )
485-
{
530+
{
486531
if ( defineBuffers[i].dttmarr )
487-
{
488-
defineBuffers[i].dttmarr->release ();
532+
{
533+
defineBuffers[i].dttmarr->release ();
489534
defineBuffers[i].extbuf = NULL;
490-
}
535+
}
491536
free(defineBuffers[i].buf);
492537
free(defineBuffers[i].len);
493538
free(defineBuffers[i].ind);
494-
}
539+
}
495540
delete [] defineBuffers;
496541
defineBuffers = NULL;
497542
}

src/njs/src/njsResultSet.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,10 @@ typedef struct rsBaton
8484

8585
~rsBaton()
8686
{
87-
delete ebaton;
87+
if(ebaton)
88+
{
89+
delete ebaton;
90+
}
8891
NanDisposePersistent(cb);
8992
}
9093

@@ -108,9 +111,11 @@ class ResultSet: public ObjectWrap {
108111
static NAN_METHOD(New);
109112

110113
// Get Rows Methods
114+
static NAN_METHOD(GetRow);
111115
static NAN_METHOD(GetRows);
112116
static void Async_GetRows(uv_work_t *req);
113117
static void Async_AfterGetRows(uv_work_t *req);
118+
static void GetRowsCommon(rsBaton*);
114119

115120
// Close Methods
116121
static NAN_METHOD(Close);

0 commit comments

Comments
 (0)