forked from oracle/node-oracledb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnjsResultSet.cpp
More file actions
478 lines (427 loc) · 16.9 KB
/
njsResultSet.cpp
File metadata and controls
478 lines (427 loc) · 16.9 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved. */
/******************************************************************************
*
* You may not use the identified files except in compliance with the Apache
* License, Version 2.0 (the "License.")
*
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This file uses NAN:
*
* Copyright (c) 2015 NAN contributors
*
* NAN contributors listed at https://github.com/rvagg/nan#contributors
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* NAME
* njsResultSet.cpp
*
* DESCRIPTION
* ResultSet class implementation.
*
*****************************************************************************/
#include "node.h"
#include <string>
#include "njsResultSet.h"
#include "njsConnection.h"
#include <iostream>
using namespace std;
using namespace node;
using namespace v8;
// peristent ResultSet class handle
Nan::Persistent<FunctionTemplate> njsResultSet::resultSetTemplate_s;
//-----------------------------------------------------------------------------
// njsResultSet::~njsResultSet()
// Destructor.
//-----------------------------------------------------------------------------
njsResultSet::~njsResultSet()
{
jsConnection.Reset();
jsOracledb.Reset();
if (queryVars) {
delete [] queryVars;
queryVars = NULL;
}
if (dpiStmtHandle) {
dpiStmt_release(dpiStmtHandle);
dpiStmtHandle = NULL;
}
}
//-----------------------------------------------------------------------------
// njsResultSet::Init()
// Initialization function of ResultSet class. Maps functions and properties
// from JS to C++.
//-----------------------------------------------------------------------------
void njsResultSet::Init(Local<Object> target)
{
Nan::HandleScope scope;
Local<FunctionTemplate> temp = Nan::New<FunctionTemplate>(New);
temp->InstanceTemplate()->SetInternalFieldCount(1);
temp->SetClassName(Nan::New<v8::String>("ResultSet").ToLocalChecked());
Nan::SetPrototypeMethod(temp, "close", Close);
Nan::SetPrototypeMethod(temp, "getRows", GetRows);
Nan::SetAccessor(temp->InstanceTemplate(),
Nan::New<v8::String>("metaData").ToLocalChecked(),
njsResultSet::GetMetaData, njsResultSet::SetMetaData);
resultSetTemplate_s.Reset(temp);
Nan::Set(target, Nan::New<v8::String>("ResultSet").ToLocalChecked(),
Nan::GetFunction(temp).ToLocalChecked());
}
//-----------------------------------------------------------------------------
// njsResultSet::CreateFromBaton()
// Create a new result set from the baton.
//-----------------------------------------------------------------------------
Local<Object> njsResultSet::CreateFromBaton(njsBaton *baton)
{
Nan::EscapableHandleScope scope;
njsResultSet *resultSet;
Local<Function> func;
Local<Object> obj;
func = Nan::GetFunction(
Nan::New<FunctionTemplate>(resultSetTemplate_s)).ToLocalChecked();
obj = Nan::NewInstance(func).ToLocalChecked();
resultSet = Nan::ObjectWrap::Unwrap<njsResultSet>(obj);
resultSet->dpiStmtHandle = baton->dpiStmtHandle;
baton->dpiStmtHandle = NULL;
resultSet->dpiConnHandle = baton->dpiConnHandle;
resultSet->jsOracledb.Reset(baton->jsOracledb);
resultSet->outFormat = baton->outFormat;
resultSet->numQueryVars = baton->numQueryVars;
resultSet->queryVars = baton->queryVars;
if (!baton->getRS) {
resultSet->autoClose = true;
resultSet->maxRows = baton->maxRows;
}
baton->queryVars = NULL;
resultSet->activeBaton = NULL;
resultSet->jsConnection.Reset(baton->jsCallingObj);
resultSet->extendedMetaData = baton->extendedMetaData;
return scope.Escape(obj);
}
//-----------------------------------------------------------------------------
// njsResultSet::CreateFromRefCursor()
// Create a new result set from the baton (
//-----------------------------------------------------------------------------
bool njsResultSet::CreateFromRefCursor(njsBaton *baton, dpiStmt *dpiStmtHandle,
njsVariable *queryVars, uint32_t numQueryVars, Local<Value> &value)
{
Nan::EscapableHandleScope scope;
njsResultSet *resultSet;
Local<Function> func;
Local<Object> obj;
func = Nan::GetFunction(
Nan::New<FunctionTemplate>(resultSetTemplate_s)).ToLocalChecked();
obj = Nan::NewInstance(func).ToLocalChecked();
resultSet = Nan::ObjectWrap::Unwrap<njsResultSet>(obj);
if (dpiStmt_addRef(dpiStmtHandle) < 0)
return false;
resultSet->dpiStmtHandle = dpiStmtHandle;
resultSet->dpiConnHandle = baton->dpiConnHandle;
resultSet->jsOracledb.Reset(baton->jsOracledb);
resultSet->jsConnection.Reset(baton->jsCallingObj);
resultSet->outFormat = baton->outFormat;
resultSet->extendedMetaData = baton->extendedMetaData;
resultSet->activeBaton = NULL;
resultSet->queryVars = queryVars;
resultSet->numQueryVars = numQueryVars;
baton->queryVars = NULL;
value = scope.Escape(obj);
return true;
}
//-----------------------------------------------------------------------------
// njsResultSet::IsValid()
// Returns whether the result set is valid.
//-----------------------------------------------------------------------------
bool njsResultSet::IsValid() const
{
// no DPI statement implies result set is not valid
if (!dpiStmtHandle)
return false;
// otherwise, check to see if the connection is valid
Nan::HandleScope scope;
Local<Object> obj = Nan::New(jsConnection);
njsConnection *connection = Nan::ObjectWrap::Unwrap<njsConnection>(obj);
if (!connection)
return false;
return connection->IsValid();
}
//-----------------------------------------------------------------------------
// njsResultSet::New()
// Create new object accesible from JS. This is always called from within
// njsResultSet::CreateFromBaton() and never from any external JS.
//-----------------------------------------------------------------------------
NAN_METHOD(njsResultSet::New)
{
njsResultSet *resultSet = new njsResultSet();
resultSet->Wrap(info.Holder());
info.GetReturnValue().Set(info.Holder());
}
//-----------------------------------------------------------------------------
// ResulSet::GetMetaData()
// Get accessor of "metaData" property.
//-----------------------------------------------------------------------------
NAN_GETTER(njsResultSet::GetMetaData)
{
njsResultSet *resultSet = (njsResultSet*) ValidateGetter(info);
if (!resultSet)
return;
if (!resultSet->IsValid()) {
info.GetReturnValue().Set(Nan::Undefined());
return;
}
Local<Value> meta = njsConnection::GetMetaData(resultSet->queryVars,
resultSet->numQueryVars, resultSet->extendedMetaData);
info.GetReturnValue().Set(meta);
}
//-----------------------------------------------------------------------------
// njsResultSet::SetMetaData()
// Set accessor of "metaData" property.
//-----------------------------------------------------------------------------
NAN_SETTER(njsResultSet::SetMetaData)
{
PropertyIsReadOnly("metaData");
}
//-----------------------------------------------------------------------------
// njsResultSet::GetRows()
// Get a number of rows from the result set.
//
// PARAMETERS
// - max number of rows to fetch at this time
// - JS callback which will receive (error, row)
//-----------------------------------------------------------------------------
NAN_METHOD(njsResultSet::GetRows)
{
njsResultSet *resultSet;
uint32_t maxRows;
njsBaton *baton;
resultSet = (njsResultSet*) ValidateArgs(info, 2, 2);
if (!resultSet)
return;
if (!resultSet->GetUnsignedIntArg(info, 0, &maxRows))
return;
if (maxRows == 0) {
string errMsg = njsMessages::Get(errInvalidParameterValue, 1);
Nan::ThrowError(errMsg.c_str());
return;
}
baton = resultSet->CreateBaton(info);
if (!baton)
return;
if (resultSet->activeBaton) {
baton->error = njsMessages::Get(errBusyResultSet);
} else if (baton->error.empty()) {
resultSet->activeBaton = baton;
baton->outFormat = resultSet->outFormat;
baton->jsOracledb.Reset(resultSet->jsOracledb);
baton->fetchArraySize = maxRows;
}
baton->QueueWork("GetRows", Async_GetRows, Async_AfterGetRows, 2);
}
//-----------------------------------------------------------------------------
// njsResultSet::GetRowsHelper()
// Get rows from the result set and return a boolean indicating if the
// fetch was successful or not.
//-----------------------------------------------------------------------------
bool njsResultSet::GetRowsHelper(njsBaton *baton, int *moreRows)
{
uint32_t i, numRowsToFetch;
njsVariable *var;
// determine how many rows to fetch; use fetchArraySize unless it is less
// than maxRows (no need to waste memory!)
numRowsToFetch = baton->fetchArraySize;
if (this->maxRows > 0 && this->maxRows < numRowsToFetch)
numRowsToFetch = this->maxRows;
// create ODPI-C variables and define them, if necessary
for (i = 0; i < this->numQueryVars; i++) {
var = &this->queryVars[i];
if (var->dpiVarHandle && var->maxArraySize >= numRowsToFetch)
continue;
if (var->dpiVarHandle) {
if (dpiVar_release(var->dpiVarHandle) < 0) {
baton->GetDPIError();
return false;
}
var->dpiVarHandle = NULL;
}
if (dpiConn_newVar(this->dpiConnHandle, var->varTypeNum,
var->nativeTypeNum, numRowsToFetch, var->maxSize, 1, 0, NULL,
&var->dpiVarHandle, &var->buffer.dpiVarData) < 0) {
baton->GetDPIError();
return false;
}
var->maxArraySize = numRowsToFetch;
if (dpiStmt_define(this->dpiStmtHandle, i + 1,
var->dpiVarHandle) < 0) {
baton->GetDPIError();
return false;
}
}
// set fetch array size as requested
if (dpiStmt_setFetchArraySize(this->dpiStmtHandle,
numRowsToFetch) < 0) {
baton->GetDPIError();
return false;
}
// perform fetch
if (dpiStmt_fetchRows(this->dpiStmtHandle, numRowsToFetch,
&baton->bufferRowIndex, &baton->rowsFetched, moreRows) < 0) {
baton->GetDPIError();
return false;
}
// result sets that should be auto closed are closed if the result set
// is exhaused or the maximum number of rows has been fetched
if (*moreRows && this->maxRows > 0) {
if (baton->rowsFetched == this->maxRows) {
*moreRows = 0;
} else {
this->maxRows -= baton->rowsFetched;
}
}
return njsConnection::ProcessVars(baton, this->queryVars,
this->numQueryVars, baton->rowsFetched);
}
//-----------------------------------------------------------------------------
// njsResultSet::Async_GetRows()
// Worker function for njsResultSet::GetRows() method.
//-----------------------------------------------------------------------------
void njsResultSet::Async_GetRows(njsBaton *baton)
{
njsResultSet *resultSet = (njsResultSet*) baton->callingObj;
int moreRows;
bool success;
// result sets that should be auto closed are closed if the result set
// is exhaused or the maximum number of rows has been fetched or an error
// has taken place
success = resultSet->GetRowsHelper(baton, &moreRows);
if ((!success || !moreRows) && resultSet->autoClose) {
dpiStmt_release(resultSet->dpiStmtHandle);
resultSet->dpiStmtHandle = NULL;
}
}
//-----------------------------------------------------------------------------
// njsResultSet::Async_AfterGetRows()
// Returns result to JS by invoking JS callback.
//-----------------------------------------------------------------------------
void njsResultSet::Async_AfterGetRows(njsBaton *baton, Local<Value> argv[])
{
Nan::EscapableHandleScope scope;
Local<Object> rowAsObj, rows;
Local<Value> val, keyVal;
Local<Array> rowAsArray;
njsResultSet *resultSet;
njsVariable *var;
rows = Nan::New<Array>((int)baton->rowsFetched);
resultSet = (njsResultSet*) baton->callingObj;
for (uint32_t row = 0; row < baton->rowsFetched; row++) {
if (baton->outFormat == NJS_ROWS_ARRAY) {
rowAsArray = Nan::New<Array>((int)resultSet->numQueryVars);
} else {
rowAsObj = Nan::New<Object>();
}
for (uint32_t col = 0; col < resultSet->numQueryVars; col++) {
var = &resultSet->queryVars[col];
if (!njsConnection::GetScalarValueFromVar(baton, var, &var->buffer,
row, val))
return;
if (baton->outFormat == NJS_ROWS_ARRAY) {
Nan::Set(rowAsArray, col, val);
} else {
keyVal = Nan::New<String>(var->name).ToLocalChecked();
Nan::Set(rowAsObj, keyVal, val);
}
}
if (baton->outFormat == NJS_ROWS_ARRAY) {
Nan::Set(rows, row, rowAsArray);
} else {
Nan::Set(rows, row, rowAsObj);
}
}
if (!resultSet->dpiStmtHandle) {
delete [] resultSet->queryVars;
resultSet->queryVars = NULL;
}
argv[1] = scope.Escape(rows);
}
//-----------------------------------------------------------------------------
// njsResultSet::Close()
// Close the result set. The reference to the DPI handle is transferred to
// the baton so that it will be cleared automatically upon success and so that
// the result set is marked as invalid immediately.
//
// PARAMETERS
// - JS callback which will receive (error)
//-----------------------------------------------------------------------------
NAN_METHOD(njsResultSet::Close)
{
njsResultSet *resultSet;
njsBaton *baton;
resultSet = (njsResultSet*) ValidateArgs(info, 1, 1);
if (!resultSet)
return;
baton = resultSet->CreateBaton(info);
if (!baton)
return;
if (resultSet->activeBaton) {
baton->error = njsMessages::Get(errBusyResultSet);
} else {
baton->dpiStmtHandle = resultSet->dpiStmtHandle;
resultSet->dpiStmtHandle = NULL;
}
baton->QueueWork("Close", Async_Close, Async_AfterClose, 1);
}
//-----------------------------------------------------------------------------
// njsResultSet::Async_Close()
// Worker function for njsResultSet::Close() method. If the attempt to close
// the statement fails, the reference to the DPI handle is transferred back
// from the baton to the result set.
//-----------------------------------------------------------------------------
void njsResultSet::Async_Close(njsBaton *baton)
{
if (dpiStmt_close(baton->dpiStmtHandle, NULL, 0) < 0) {
njsResultSet *resultSet = (njsResultSet*) baton->callingObj;
resultSet->dpiStmtHandle = baton->dpiStmtHandle;
baton->dpiStmtHandle = NULL;
baton->GetDPIError();
}
}
//-----------------------------------------------------------------------------
// njsResultSet::Async_AfterClose()
// Finishes close by transferring variables and fetch as types to the baton
// where they will be freed.
//-----------------------------------------------------------------------------
void njsResultSet::Async_AfterClose(njsBaton *baton, Local<Value> argv[])
{
njsResultSet *resultSet = (njsResultSet*) baton->callingObj;
resultSet->jsConnection.Reset();
resultSet->jsOracledb.Reset();
baton->queryVars = resultSet->queryVars;
baton->numQueryVars = resultSet->numQueryVars;
resultSet->queryVars = NULL;
resultSet->numQueryVars = 0;
}