forked from oracle/node-oracledb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnjsIntLob.cpp
More file actions
622 lines (535 loc) · 20.6 KB
/
njsIntLob.cpp
File metadata and controls
622 lines (535 loc) · 20.6 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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
/* 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
* njsIntLob.cpp
*
* DESCRIPTION
* ILob class implementation.
*
*****************************************************************************/
#include <stdlib.h>
#include <iostream>
using namespace std;
#ifndef NODE_BUFFER_H_
# include <node_buffer.h>
#endif
#include "njsIntLob.h"
// peristent ILob class handle
Nan::Persistent<FunctionTemplate> njsILob::iLobTemplate_s;
//-----------------------------------------------------------------------------
// njsILob::Init()
// Initialization function of ILob class. Maps functions and properties from
// JS to C++.
//-----------------------------------------------------------------------------
void njsILob::Init(Local<Object> target)
{
Nan::HandleScope scope;
Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(New);
tpl->InstanceTemplate()->SetInternalFieldCount(1);
tpl->SetClassName(Nan::New<v8::String>("ILob").ToLocalChecked());
Nan::SetPrototypeMethod(tpl, "close", Close);
Nan::SetPrototypeMethod(tpl, "read", Read);
Nan::SetPrototypeMethod(tpl, "write", Write);
Nan::SetAccessor(tpl->InstanceTemplate(),
Nan::New<v8::String>("chunkSize").ToLocalChecked(),
njsILob::GetChunkSize, njsILob::SetChunkSize);
Nan::SetAccessor(tpl->InstanceTemplate(),
Nan::New<v8::String>("length").ToLocalChecked(),
njsILob::GetLength, njsILob::SetLength);
Nan::SetAccessor(tpl->InstanceTemplate(),
Nan::New<v8::String>("pieceSize").ToLocalChecked(),
njsILob::GetPieceSize, njsILob::SetPieceSize);
Nan::SetAccessor(tpl->InstanceTemplate(),
Nan::New<v8::String>("offset").ToLocalChecked(),
njsILob::GetOffset, njsILob::SetOffset);
Nan::SetAccessor(tpl->InstanceTemplate(),
Nan::New<v8::String>("type").ToLocalChecked(),
njsILob::GetType, njsILob::SetType);
Nan::SetAccessor(tpl->InstanceTemplate(),
Nan::New<v8::String>("autoCloseLob").ToLocalChecked(),
njsILob::GetIsAutoClose, njsILob::SetIsAutoClose);
Nan::SetAccessor(tpl->InstanceTemplate(),
Nan::New<v8::String>("valid").ToLocalChecked(),
njsILob::GetIsValid, njsILob::SetIsValid);
iLobTemplate_s.Reset(tpl);
Nan::Set(target, Nan::New<v8::String>("ILob").ToLocalChecked(),
Nan::GetFunction(tpl).ToLocalChecked());
}
//-----------------------------------------------------------------------------
// njsILob::CreateFromProtoLob()
// Create a new LOB from the njsProtoILob instance.
//-----------------------------------------------------------------------------
Local<Object> njsILob::CreateFromProtoLob(njsProtoILob *protoLob)
{
Nan::EscapableHandleScope scope;
Local<Function> func;
Local<Object> obj;
njsILob *lob;
func = Nan::GetFunction(
Nan::New<FunctionTemplate>(iLobTemplate_s)).ToLocalChecked();
obj = Nan::NewInstance(func).ToLocalChecked();
lob = Nan::ObjectWrap::Unwrap<njsILob>(obj);
lob->dpiLobHandle = protoLob->dpiLobHandle;
protoLob->dpiLobHandle = NULL;
lob->chunkSize = protoLob->chunkSize;
lob->pieceSize = protoLob->chunkSize;
lob->length = protoLob->length;
lob->dataType = protoLob->dataType;
lob->isAutoClose = protoLob->isAutoClose;
lob->activeBaton = NULL;
lob->offset = 1;
return scope.Escape(obj);
}
//-----------------------------------------------------------------------------
// njsILob::GetInstance()
// Return the instance associated with the value, or NULL if there is no
// instance associated with the value. Javascript code provides the stream;
// this extracts the member iLob instance which is used internally.
//-----------------------------------------------------------------------------
njsILob *njsILob::GetInstance(Local<Value> val)
{
Nan::HandleScope scope;
Local<Object> obj = val.As<Object>();
Local <String> key = Nan::New<v8::String>("iLob").ToLocalChecked();
MaybeLocal<Value> mval = Nan::Get(obj, key);
Local<Value> v8Value;
if (!mval.ToLocal(&v8Value))
return NULL;
if (v8Value->IsObject()) {
Local<Object> obj = v8Value.As<Object>();
if (Nan::New(iLobTemplate_s)->HasInstance(obj))
return Nan::ObjectWrap::Unwrap<njsILob>(obj);
}
return NULL;
}
//-----------------------------------------------------------------------------
// njsILob::HasInstance()
// Return boolean indicating if the specified V8 object refers to an ILob
// instance or not.
//-----------------------------------------------------------------------------
bool njsILob::HasInstance(Local<Value> val)
{
njsILob *lob = GetInstance(val);
return (lob) ? true : false;
}
//-----------------------------------------------------------------------------
// njsILob::ClearDPILobHandle()
// Clear the DPI LOB handle. This is done for the IN side of an IN/OUT bind
// once the LOB has been successfully cloned.
//-----------------------------------------------------------------------------
bool njsILob::ClearDPILobHandle(njsBaton *baton)
{
if (dpiLob_close(dpiLobHandle) < 0) {
baton->GetDPIError();
return false;
}
if (dpiLob_release(dpiLobHandle) < 0) {
baton->GetDPIError();
return false;
}
dpiLobHandle = NULL;
return true;
}
//-----------------------------------------------------------------------------
// njsILob::New()
// Create new object accesible from JS. This is always called from within
// njsILob::CreateFromBaton() and never from any external JS.
//-----------------------------------------------------------------------------
NAN_METHOD(njsILob::New)
{
njsILob *iLob = new njsILob();
iLob->Wrap(info.Holder());
info.GetReturnValue().Set(info.Holder());
}
//-----------------------------------------------------------------------------
// njsILob::GetChunkSize()
// Get accessor of "chunkSize" property.
//-----------------------------------------------------------------------------
NAN_GETTER(njsILob::GetChunkSize)
{
njsILob *lob = (njsILob*) ValidateGetter(info);
if (lob)
info.GetReturnValue().Set(lob->chunkSize);
}
//-----------------------------------------------------------------------------
// njsILob::SetChunkSize()
// Set accessor of "chunkSize" property.
//-----------------------------------------------------------------------------
NAN_SETTER(njsILob::SetChunkSize)
{
PropertyIsReadOnly("chunkSize");
}
//-----------------------------------------------------------------------------
// njsILob::GetLength()
// Get accessor of "length" property.
//-----------------------------------------------------------------------------
NAN_GETTER(njsILob::GetLength)
{
njsILob *lob = (njsILob*) ValidateGetter(info);
if (lob)
info.GetReturnValue().Set( (double) lob->length);
}
//-----------------------------------------------------------------------------
// njsILob::SetLength()
// Set accessor of "length" property.
//-----------------------------------------------------------------------------
NAN_SETTER(njsILob::SetLength)
{
PropertyIsReadOnly("length");
}
//-----------------------------------------------------------------------------
// njsILob::GetPieceSize()
// Get accessor of "pieceSize" property.
//-----------------------------------------------------------------------------
NAN_GETTER(njsILob::GetPieceSize)
{
njsILob *lob = (njsILob*) ValidateGetter(info);
if (lob)
info.GetReturnValue().Set(lob->pieceSize);
}
//-----------------------------------------------------------------------------
// njsILob::SetPieceSize()
// Set accessor of "pieceSize" property.
//-----------------------------------------------------------------------------
NAN_SETTER(njsILob::SetPieceSize)
{
njsILob *lob = (njsILob*) ValidateSetter(info);
if (!lob)
return;
if (lob->bufferPtr) {
delete [] lob->bufferPtr;
lob->bufferPtr = NULL;
}
if (!lob->SetPropUnsignedInt(value, &lob->pieceSize, "pieceSize"))
return;
}
//-----------------------------------------------------------------------------
// njsILob::GetOffset()
// Get accessor of "offset" property.
//-----------------------------------------------------------------------------
NAN_GETTER(njsILob::GetOffset)
{
njsILob *lob = (njsILob*) ValidateGetter(info);
if (lob)
info.GetReturnValue().Set((uint32_t) lob->offset);
}
//-----------------------------------------------------------------------------
// njsILob::SetOffset()
// Set accessor of "offset" property.
//-----------------------------------------------------------------------------
NAN_SETTER(njsILob::SetOffset)
{
njsILob *lob = (njsILob*) ValidateSetter(info);
if (!lob)
return;
uint32_t offset;
if (lob->SetPropUnsignedInt(value, &offset, "offset"))
lob->offset = offset;
}
//-----------------------------------------------------------------------------
// njsILob::GetType()
// Get accessor of "type" property.
//-----------------------------------------------------------------------------
NAN_GETTER(njsILob::GetType)
{
njsILob *lob = (njsILob*) ValidateGetter(info);
if (lob)
info.GetReturnValue().Set(lob->dataType);
}
//-----------------------------------------------------------------------------
// njsILob::SetType()
// Set accessor of "type" property.
//-----------------------------------------------------------------------------
NAN_SETTER(njsILob::SetType)
{
PropertyIsReadOnly("type");
}
//-----------------------------------------------------------------------------
// njsILob::GetIsAutoClose()
// Get accessor of "autoCloseLob" property.
//-----------------------------------------------------------------------------
NAN_GETTER(njsILob::GetIsAutoClose)
{
njsILob *lob = (njsILob*) ValidateGetter(info);
if (lob)
info.GetReturnValue().Set(lob->isAutoClose);
}
//-----------------------------------------------------------------------------
// njsILob::SetIsAutoClose()
// Set accessor of "autoCloseLob" property.
//-----------------------------------------------------------------------------
NAN_SETTER(njsILob::SetIsAutoClose)
{
PropertyIsReadOnly("autoCloseLob");
}
//-----------------------------------------------------------------------------
// njsILob::GetIsValid()
// Get accessor of "valid" property.
//-----------------------------------------------------------------------------
NAN_GETTER(njsILob::GetIsValid)
{
njsILob *lob;
lob = Nan::ObjectWrap::Unwrap<njsILob>(info.Holder());
if (!lob) {
std::string errMsg = njsMessages::Get(errInvalidJSObject);
Nan::ThrowError(errMsg.c_str());
return;
}
info.GetReturnValue().Set(lob->IsValid());
}
//-----------------------------------------------------------------------------
// njsILob::SetIsValid()
// Set accessor of "valid" property.
//-----------------------------------------------------------------------------
NAN_SETTER(njsILob::SetIsValid)
{
PropertyIsReadOnly("valid");
}
//-----------------------------------------------------------------------------
// njsILob::Read()
// Read some data from the LOB.
//
// PARAMETERS
// - JS callback which will receive (error, data)
//-----------------------------------------------------------------------------
NAN_METHOD(njsILob::Read)
{
njsBaton *baton;
njsILob *lob;
lob = (njsILob*) ValidateArgs(info, 1, 1);
if (!lob)
return;
baton = lob->CreateBaton(info);
if (!baton)
return;
if (lob->activeBaton) {
baton->error = njsMessages::Get(errBusyLob);
} else if (baton->error.empty()) {
lob->activeBaton = baton;
if (lob->dataType == NJS_DATATYPE_BLOB) {
baton->bufferSize = lob->pieceSize;
} else if (dpiLob_getBufferSize(lob->dpiLobHandle, lob->pieceSize,
&baton->bufferSize) < 0) {
baton->GetDPIError();
}
if (baton->error.empty()) {
if (!lob->bufferPtr)
lob->bufferPtr = new char[baton->bufferSize];
baton->bufferPtr = lob->bufferPtr;
baton->SetDPILobHandle(lob->dpiLobHandle);
baton->lobAmount = lob->pieceSize;
baton->lobOffset = lob->offset;
}
}
baton->QueueWork("Read", Async_Read, Async_AfterRead, 2);
}
//-----------------------------------------------------------------------------
// njsILob::Async_Read()
// Worker function for njsILob::Read() method.
//-----------------------------------------------------------------------------
void njsILob::Async_Read(njsBaton *baton)
{
if (dpiLob_readBytes(baton->dpiLobHandle, baton->lobOffset,
baton->lobAmount, baton->bufferPtr, &baton->bufferSize) < 0)
baton->GetDPIError();
// if an error occurs or the end of the LOB has been reached, and the LOB
// is marked as one that should be automatically closed, close and release
// it, ignoring any further errors that occur during the attempt to close
njsILob *lob = (njsILob*) baton->callingObj;
if (lob->isAutoClose && (!baton->bufferSize || !baton->error.empty())) {
dpiLob_close(lob->dpiLobHandle);
dpiLob_release(lob->dpiLobHandle);
lob->dpiLobHandle = NULL;
}
}
//-----------------------------------------------------------------------------
// njsILob::Close()
// Close the LOB. The reference to the DPI handle is transferred to the baton
// so that it will be cleared automatically upon success and so that the LOB
// is marked as invalid immediately.
//
// PARAMETERS
// - JS callback which will receive (error)
//-----------------------------------------------------------------------------
NAN_METHOD(njsILob::Close)
{
njsBaton *baton;
njsILob *lob;
lob = (njsILob*) ValidateArgs(info, 1, 1);
if (!lob)
return;
baton = lob->CreateBaton(info);
if (!baton)
return;
if (lob->activeBaton) {
baton->error = njsMessages::Get(errBusyLob);
} else {
baton->dpiLobHandle = lob->dpiLobHandle;
lob->dpiLobHandle = NULL;
}
baton->QueueWork("Close", Async_Close, NULL, 1);
}
//-----------------------------------------------------------------------------
// njsILob::Async_Close()
// Worker function for njsILob::Close() method. If the attempt to close
// fails, the reference to the DPI handle is transferred back from the baton to
// the LOB.
//-----------------------------------------------------------------------------
void njsILob::Async_Close(njsBaton *baton)
{
if (dpiLob_close(baton->dpiLobHandle) < 0) {
njsILob *lob = (njsILob*) baton->callingObj;
lob->dpiLobHandle = baton->dpiLobHandle;
baton->dpiLobHandle = NULL;
baton->GetDPIError();
}
}
//-----------------------------------------------------------------------------
// njsILob::Async_AfterRead()
// Returns result to JS by invoking JS callback.
//-----------------------------------------------------------------------------
void njsILob::Async_AfterRead(njsBaton *baton, Local<Value> argv[])
{
Nan::EscapableHandleScope scope;
njsILob *lob = (njsILob*) baton->callingObj;
if (!baton->bufferSize) {
argv[1] = scope.Escape(Nan::Null());
} else if (lob->dataType == NJS_DATATYPE_CLOB) {
Local<String> strValue = Nan::New<String>(baton->bufferPtr,
(int) baton->bufferSize).ToLocalChecked();
lob->offset += strValue.As<String>()->Length();
argv[1] = scope.Escape(strValue);
} else {
Local<Value> bufferValue = Nan::CopyBuffer(baton->bufferPtr,
(uint32_t) baton->bufferSize).ToLocalChecked();
lob->offset += baton->bufferSize;
argv[1] = scope.Escape(bufferValue);
}
}
//-----------------------------------------------------------------------------
// njsILob::Write()
// Write some data to the LOB.
//
// PARAMETERS
// - JS callback which will receive (error)
//-----------------------------------------------------------------------------
NAN_METHOD(njsILob::Write)
{
Local<Object> jsBuffer;
njsBaton *baton;
njsILob *lob;
lob = (njsILob*) ValidateArgs(info, 2, 2);
if (!lob)
return;
if (!lob->GetObjectArg(info, 0, jsBuffer))
return;
baton = lob->CreateBaton(info);
if (!baton)
return;
if (lob->activeBaton) {
baton->error = njsMessages::Get(errBusyLob);
} else if (baton->error.empty()) {
baton->jsBuffer.Reset(jsBuffer);
baton->bufferPtr = Buffer::Data(jsBuffer);
baton->bufferSize = Buffer::Length(jsBuffer);
if (jsBuffer->IsString()) {
baton->lobAmount += jsBuffer.As<String>()->Length();
} else {
baton->lobAmount += baton->bufferSize;
}
baton->lobOffset = lob->offset;
lob->activeBaton = baton;
baton->SetDPILobHandle(lob->dpiLobHandle);
}
baton->QueueWork("Write", Async_Write, Async_AfterWrite, 1);
}
//-----------------------------------------------------------------------------
// njsILob::Async_Write()
// Worker function for njsILob::Write() method.
//-----------------------------------------------------------------------------
void njsILob::Async_Write(njsBaton *baton)
{
if (dpiLob_writeBytes(baton->dpiLobHandle, baton->lobOffset,
baton->bufferPtr, baton->bufferSize) < 0) {
baton->GetDPIError();
// if an error occurs and the LOB is marked as one that should be
// automatically closed, close and release it, ignoring any further
// errors that occur during the attempt to close
njsILob *lob = (njsILob*) baton->callingObj;
if (lob->isAutoClose) {
dpiLob_close(lob->dpiLobHandle);
dpiLob_release(lob->dpiLobHandle);
lob->dpiLobHandle = NULL;
}
}
}
//-----------------------------------------------------------------------------
// njsILob::Async_AfterWrite()
// Sets the offset after writing.
//-----------------------------------------------------------------------------
void njsILob::Async_AfterWrite(njsBaton *baton, Local<Value> argv[])
{
njsILob *lob = (njsILob*) baton->callingObj;
lob->offset += baton->lobAmount;
}
//-----------------------------------------------------------------------------
// njsProtoILob::PopulateFromDPI()
// Populate the proto internal LOB from DPI.
//-----------------------------------------------------------------------------
bool njsProtoILob::PopulateFromDPI(njsBaton *baton, dpiLob *dpiLobHandle,
bool addRef)
{
if (addRef && dpiLob_addRef(dpiLobHandle) < 0) {
baton->GetDPIError();
return false;
}
this->dpiLobHandle = dpiLobHandle;
if (dpiLob_getChunkSize(dpiLobHandle, &this->chunkSize) < 0) {
baton->GetDPIError();
return false;
}
if (dpiLob_getSize(dpiLobHandle, &this->length) < 0) {
baton->GetDPIError();
return false;
}
return true;
}