forked from oracle/node-oracledb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnjsCommon.cpp
More file actions
1057 lines (944 loc) · 36.9 KB
/
njsCommon.cpp
File metadata and controls
1057 lines (944 loc) · 36.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
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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* 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
* njsCommon.cpp
*
* DESCRIPTION
* Implementation of common classes used throughout driver.
*
*****************************************************************************/
#include "njsCommon.h"
#include "njsOracle.h"
#include "njsIntLob.h"
#include "njsSodaDocument.h"
#include "njsSubscription.h"
#include "njsUtils.h"
using namespace node;
using namespace std;
using namespace v8;
//-----------------------------------------------------------------------------
// njsVariableBuffer::~njsVariableBuffer()
// Destructor.
//-----------------------------------------------------------------------------
njsVariableBuffer::~njsVariableBuffer()
{
if (lobs) {
delete [] lobs;
lobs = NULL;
}
dpiVarData = NULL;
}
//-----------------------------------------------------------------------------
// njsVariable::~njsVariable()
// Destructor.
//-----------------------------------------------------------------------------
njsVariable::~njsVariable()
{
if (dpiVarHandle) {
dpiVar_release(dpiVarHandle);
dpiVarHandle = NULL;
}
if (dmlReturningBuffers) {
delete [] dmlReturningBuffers;
dmlReturningBuffers = NULL;
}
if (queryVars) {
delete [] queryVars;
queryVars = NULL;
}
}
//-----------------------------------------------------------------------------
// njsVariable::DataType()
// Return the data type that is being used by the variable. This is an
// enumeration that is publicly available in the oracledb module.
//-----------------------------------------------------------------------------
njsDataType njsVariable::DataType()
{
switch (varTypeNum) {
case DPI_ORACLE_TYPE_VARCHAR:
case DPI_ORACLE_TYPE_NVARCHAR:
case DPI_ORACLE_TYPE_CHAR:
case DPI_ORACLE_TYPE_NCHAR:
case DPI_ORACLE_TYPE_ROWID:
case DPI_ORACLE_TYPE_LONG_VARCHAR:
return NJS_DATATYPE_STR;
case DPI_ORACLE_TYPE_RAW:
case DPI_ORACLE_TYPE_LONG_RAW:
return NJS_DATATYPE_BUFFER;
case DPI_ORACLE_TYPE_NATIVE_FLOAT:
case DPI_ORACLE_TYPE_NATIVE_DOUBLE:
case DPI_ORACLE_TYPE_NATIVE_INT:
case DPI_ORACLE_TYPE_NUMBER:
return NJS_DATATYPE_NUM;
case DPI_ORACLE_TYPE_DATE:
case DPI_ORACLE_TYPE_TIMESTAMP:
case DPI_ORACLE_TYPE_TIMESTAMP_TZ:
case DPI_ORACLE_TYPE_TIMESTAMP_LTZ:
return NJS_DATATYPE_DATE;
case DPI_ORACLE_TYPE_CLOB:
case DPI_ORACLE_TYPE_NCLOB:
return NJS_DATATYPE_CLOB;
case DPI_ORACLE_TYPE_BLOB:
return NJS_DATATYPE_BLOB;
default:
break;
}
return NJS_DATATYPE_UNKNOWN;
}
//-----------------------------------------------------------------------------
// njsVariable::DBType()
// Return the database data type that the variable represents. This is an
// enumeration that is publicly available in the oracledb module.
//-----------------------------------------------------------------------------
njsDBType njsVariable::DBType()
{
switch (dbTypeNum) {
case DPI_ORACLE_TYPE_VARCHAR:
return NJS_DB_TYPE_VARCHAR;
case DPI_ORACLE_TYPE_NVARCHAR:
return NJS_DB_TYPE_NVARCHAR;
case DPI_ORACLE_TYPE_CHAR:
return NJS_DB_TYPE_CHAR;
case DPI_ORACLE_TYPE_NCHAR:
return NJS_DB_TYPE_NCHAR;
case DPI_ORACLE_TYPE_ROWID:
return NJS_DB_TYPE_ROWID;
case DPI_ORACLE_TYPE_RAW:
return NJS_DB_TYPE_RAW;
case DPI_ORACLE_TYPE_NATIVE_FLOAT:
return NJS_DB_TYPE_BINARY_FLOAT;
case DPI_ORACLE_TYPE_NATIVE_DOUBLE:
return NJS_DB_TYPE_BINARY_DOUBLE;
case DPI_ORACLE_TYPE_NATIVE_INT:
case DPI_ORACLE_TYPE_NUMBER:
return NJS_DB_TYPE_NUMBER;
case DPI_ORACLE_TYPE_DATE:
return NJS_DB_TYPE_DATE;
case DPI_ORACLE_TYPE_TIMESTAMP:
return NJS_DB_TYPE_TIMESTAMP;
case DPI_ORACLE_TYPE_TIMESTAMP_TZ:
return NJS_DB_TYPE_TIMESTAMP_TZ;
case DPI_ORACLE_TYPE_TIMESTAMP_LTZ:
return NJS_DB_TYPE_TIMESTAMP_LTZ;
case DPI_ORACLE_TYPE_CLOB:
return NJS_DB_TYPE_CLOB;
case DPI_ORACLE_TYPE_NCLOB:
return NJS_DB_TYPE_NCLOB;
case DPI_ORACLE_TYPE_BLOB:
return NJS_DB_TYPE_BLOB;
case DPI_ORACLE_TYPE_LONG_VARCHAR:
return NJS_DB_TYPE_LONG;
case DPI_ORACLE_TYPE_LONG_RAW:
return NJS_DB_TYPE_LONG_RAW;
default:
break;
}
return NJS_DB_TYPE_UNKNOWN;
}
//-----------------------------------------------------------------------------
// njsBaton::~njsBaton()
// Destructor.
//-----------------------------------------------------------------------------
njsBaton::~njsBaton()
{
jsCallback.Reset();
jsCallingObj.Reset();
jsOracledb.Reset();
jsSubscription.Reset();
jsBuffer.Reset();
ClearAsyncData();
}
//-----------------------------------------------------------------------------
// njsBaton::CheckJSException()
// Check for a JavaScript exception in the TryCatch variable. If one is
// found, acquire the message and store it in the baton error so that it will
// be propagated to the callback, then reset the exception so that it will not
// be raised in JavaScript once the C++ method has completed its work.
//-----------------------------------------------------------------------------
void njsBaton::CheckJSException(Nan::TryCatch *tryCatch)
{
if (tryCatch->HasCaught()) {
Local<String> message = tryCatch->Message()->Get();
Nan::Utf8String v8str(message);
if (v8str.length () > 0) {
error = std::string(*v8str, static_cast<size_t>(v8str.length()));
tryCatch->Reset();
}
}
}
//-----------------------------------------------------------------------------
// njsBaton::ClearAsyncData()
// Clear the baton of everything except for the JavaScript references which
// must be reset in the main thread.
//-----------------------------------------------------------------------------
void njsBaton::ClearAsyncData()
{
if (dpiPoolHandle) {
dpiPool_release(dpiPoolHandle);
dpiPoolHandle = NULL;
}
if (dpiConnHandle) {
dpiConn_release(dpiConnHandle);
dpiConnHandle = NULL;
}
if (dpiStmtHandle) {
dpiStmt_release(dpiStmtHandle);
dpiStmtHandle = NULL;
}
if (dpiLobHandle) {
dpiLob_release(dpiLobHandle);
dpiLobHandle = NULL;
}
if (dpiSubscrHandle) {
dpiSubscr_release(dpiSubscrHandle);
dpiSubscrHandle = NULL;
}
if (sodaCollNames) {
dpiSodaDb_freeCollectionNames(dpiSodaDbHandle, sodaCollNames);
delete sodaCollNames;
sodaCollNames = NULL;
}
if (dpiSodaDbHandle) {
dpiSodaDb_release(dpiSodaDbHandle);
dpiSodaDbHandle = NULL;
}
if (dpiSodaCollHandle) {
dpiSodaColl_release(dpiSodaCollHandle);
dpiSodaCollHandle = NULL;
}
if (dpiSodaDocHandle) {
dpiSodaDoc_release(dpiSodaDocHandle);
dpiSodaDocHandle = NULL;
}
if (dpiSodaDocCursorHandle) {
dpiSodaDocCursor_release(dpiSodaDocCursorHandle);
dpiSodaDocCursorHandle = NULL;
}
if (bindVars) {
delete [] bindVars;
bindVars = NULL;
numBindVars = 0;
}
if (protoILob) {
delete protoILob;
protoILob = NULL;
}
if (queryVars) {
delete [] queryVars;
queryVars = NULL;
numQueryVars = 0;
}
if (fetchInfo) {
delete [] fetchInfo;
fetchInfo = NULL;
numFetchInfo = 0;
}
if (fetchAsStringTypes) {
delete [] fetchAsStringTypes;
fetchAsStringTypes = NULL;
numFetchAsStringTypes = 0;
}
if (fetchAsBufferTypes) {
delete [] fetchAsBufferTypes;
fetchAsBufferTypes = NULL;
numFetchAsBufferTypes = 0;
}
if (batchErrorInfos) {
delete [] batchErrorInfos;
batchErrorInfos = NULL;
numBatchErrorInfos = 0;
}
if (sodaOperOptions) {
if (sodaOperOptions->keys) {
delete sodaOperOptions->keys;
sodaOperOptions->keys = NULL;
}
if (sodaOperOptions->keyLengths) {
delete sodaOperOptions->keyLengths;
sodaOperOptions->keyLengths = NULL;
}
delete sodaOperOptions;
sodaOperOptions = NULL;
}
}
//-----------------------------------------------------------------------------
// njsBaton::GetOracledb()
// Return the Oracledb object stored in the baton as a JS object.
//-----------------------------------------------------------------------------
njsOracledb *njsBaton::GetOracledb()
{
Nan::HandleScope scope;
Local<Object> obj = Nan::New(jsOracledb);
return Nan::ObjectWrap::Unwrap<njsOracledb>(obj);
}
//-----------------------------------------------------------------------------
// njsBaton::AsyncWorkCallback()
// Callback used during asynchronous processing that takes place on a
// separate thread. This simply calls the assigned routine directly, passing
// the baton -- but only if an error has not already taken place. Blocking
// calls should be made here.
//-----------------------------------------------------------------------------
void njsBaton::AsyncWorkCallback(uv_work_t *req)
{
njsBaton *baton = (njsBaton*) req->data;
if (baton->error.empty())
baton->workCallback(baton);
}
//-----------------------------------------------------------------------------
// njsBaton::AsyncAfterWorkCallback()
// Callback used during asynchronous processing that takes place on the main
// thread after the work on the separate thread has been completed. Blocking
// calls should be avoided. The baton is destroyed after the assigned routine
// is called. Exceptions are caught and a fatal exception is raised in such
// cases.
//-----------------------------------------------------------------------------
void njsBaton::AsyncAfterWorkCallback(uv_work_t *req, int status)
{
Nan::HandleScope scope;
njsBaton *baton = (njsBaton*) req->data;
Nan::TryCatch tc;
Local<Value> *callbackArgs = new Local<Value>[baton->numCallbackArgs];
unsigned int i, numCallbackArgs = baton->numCallbackArgs;
Local<Object> errorObj;
// set all parameters but the first as undefined; the first parameter is
// always expected to be the error and should be null
callbackArgs[0] = Nan::Null();
for (i = 1; i < numCallbackArgs; i++)
callbackArgs[i] = Nan::Undefined();
// if no error so far, call the after work callback, if needed
if (baton->error.empty() && baton->afterWorkCallback)
baton->afterWorkCallback(baton, callbackArgs);
// if we have an error, set it as the first parameter
// reset all remaining parameters as undefined
if (!baton->error.empty()) {
callbackArgs[0] = v8::Exception::Error(Nan::New<v8::String>(
baton->error).ToLocalChecked());
if (baton->dpiError) {
errorObj = callbackArgs[0].As<Object>();
Nan::Set(errorObj,
Nan::New<v8::String>("errorNum").ToLocalChecked(),
Nan::New<v8::Number>(baton->errorInfo.code));
Nan::Set(errorObj, Nan::New<v8::String>("offset").ToLocalChecked(),
Nan::New<v8::Number>(baton->errorInfo.offset));
}
for (i = 1; i < numCallbackArgs; i++)
callbackArgs[i] = Nan::Undefined();
}
// if this baton is considered the active baton, clear it
if (baton->callingObj && baton == baton->callingObj->activeBaton)
baton->callingObj->activeBaton = NULL;
// delete the baton before the callback is made so any unnecessary
// ODPI-C handles are released as soon as possible
Local<Function> callback = Nan::New<Function>(baton->jsCallback);
delete baton;
// make JS callback
Nan::AsyncResource *asyncResource =
new Nan::AsyncResource("node-oracledb");
asyncResource->runInAsyncScope(Nan::GetCurrentContext()->Global(),
callback, static_cast<int>(numCallbackArgs), callbackArgs);
delete asyncResource;
// we no longer need the callback args
delete [] callbackArgs;
// raise fatal exception if an exception was caught
if (tc.HasCaught())
Nan::FatalException(tc);
}
//-----------------------------------------------------------------------------
// njsBaton::GetDPIError()
// Gets the error information from DPI and stores it in the baton. It then
// clears all information from the baton. This is done here so that there are
// no possible race conditions when errors take place.
//-----------------------------------------------------------------------------
void njsBaton::GetDPIError(void)
{
dpiContext_getError(njsOracledb::GetDPIContext(), &errorInfo);
if (errorInfo.code == 1406) {
error = njsMessages::Get(errInsufficientBufferForBinds);
} else {
error = std::string(errorInfo.message, errorInfo.messageLength);
dpiError = true;
}
ClearAsyncData();
}
//-----------------------------------------------------------------------------
// njsBaton::SetDPIConnHandle()
// Set the DPI connection handle. This adds a reference to the DPI connection
// which will be released in the destructor.
//-----------------------------------------------------------------------------
void njsBaton::SetDPIConnHandle(dpiConn *handle)
{
if (dpiConn_addRef(handle) < 0) {
GetDPIError();
} else {
dpiConnHandle = handle;
}
}
//-----------------------------------------------------------------------------
// njsBaton::SetDPIPoolHandle()
// Set the DPI pool handle. This adds a reference to the DPI pool
// which will be released in the destructor.
//-----------------------------------------------------------------------------
void njsBaton::SetDPIPoolHandle(dpiPool *handle)
{
if (dpiPool_addRef(handle) < 0) {
GetDPIError();
} else {
dpiPoolHandle = handle;
}
}
//-----------------------------------------------------------------------------
// njsBaton::SetDPIStmtHandle()
// Set the DPI statement handle. This adds a reference to the DPI statement
// which will be released in the destructor.
//-----------------------------------------------------------------------------
void njsBaton::SetDPIStmtHandle(dpiStmt *handle)
{
if (dpiStmt_addRef(handle) < 0) {
GetDPIError();
} else {
dpiStmtHandle = handle;
}
}
//-----------------------------------------------------------------------------
// njsBaton::SetDPILobHandle()
// Set the DPI statement handle. This adds a reference to the DPI LOB which
// will be released in the destructor.
//-----------------------------------------------------------------------------
void njsBaton::SetDPILobHandle(dpiLob *handle)
{
if (dpiLob_addRef(handle) < 0) {
GetDPIError();
} else {
dpiLobHandle = handle;
}
}
//-----------------------------------------------------------------------------
// njsBaton::SetDPISubscrHandle()
// Set the DPI subscription handle. This adds a reference to the DPI
// subscription which will be released in the destructor.
//-----------------------------------------------------------------------------
void njsBaton::SetDPISubscrHandle(dpiSubscr *handle)
{
if (dpiSubscr_addRef(handle) < 0) {
GetDPIError();
} else {
dpiSubscrHandle = handle;
}
}
//-----------------------------------------------------------------------------
// njsBaton::SetDPISodaDbHandle()
// Set the DPI SODA database handle. This adds a reference to the DPI SODA
// database which will be released in the destructor.
//-----------------------------------------------------------------------------
void njsBaton::SetDPISodaDbHandle(dpiSodaDb *handle)
{
if (dpiSodaDb_addRef(handle) < 0) {
GetDPIError();
} else {
dpiSodaDbHandle = handle;
}
}
//-----------------------------------------------------------------------------
// njsBaton::SetDPISodaCollHandle()
// Set the DPI SODA collection handle. This adds a reference to the DPI
// SODA collection which will be released in the destructor.
//-----------------------------------------------------------------------------
void njsBaton::SetDPISodaCollHandle(dpiSodaColl *handle)
{
if (dpiSodaColl_addRef(handle) < 0) {
GetDPIError();
} else {
dpiSodaCollHandle = handle;
}
}
//-----------------------------------------------------------------------------
// njsBaotn::SetDPISodaDocHandle()
// Set the DPI SODA document handle. This adds a reference to the DPI SODA
// document, which will be released in the destructor.
//-----------------------------------------------------------------------------
void njsBaton::SetDPISodaDocHandle(dpiSodaDoc *handle)
{
if (dpiSodaDoc_addRef(handle) < 0) {
GetDPIError();
} else {
dpiSodaDocHandle = handle;
}
}
//-----------------------------------------------------------------------------
// njsBaton::SetDPISodaDocCursorHandle()
// Set the DPI SODA document cursor handle. This adds a reference to the DPI
// SODA document cursor, which will be released in the destructor.
//-----------------------------------------------------------------------------
void njsBaton::SetDPISodaDocCursorHandle(dpiSodaDocCursor *handle)
{
if (dpiSodaDocCursor_addRef(handle) < 0) {
GetDPIError();
} else {
dpiSodaDocCursorHandle = handle;
}
}
//-----------------------------------------------------------------------------
// njsBaton::GetBoolFromJSON()
// Gets a boolean value from the JSON object for the given key, if possible.
// If undefined, leave value alone. Index is the argument index in the caller.
// It is unused in this method as any argument passed is converted to a boolean
// value and no error is raised; the method signature remains the same, though,
// to match the equivalent string and unsigned integer methods.
//-----------------------------------------------------------------------------
bool njsBaton::GetBoolFromJSON(Local<Object> obj, const char *key, int index,
bool *value)
{
Nan::HandleScope scope;
Local<Value> jsValue;
if (!error.empty())
return false;
MaybeLocal<Value> mval = Nan::Get (obj, Nan::New(key).ToLocalChecked());
if(!mval.ToLocal(&jsValue))
return false;
/* Undefined implies value not provided or equivalent */
if (!jsValue->IsUndefined()) {
if (jsValue->IsBoolean()) {
*value = jsValue.As<Boolean>()->Value();
} else {
error = njsMessages::Get(errInvalidPropertyValueInParam, key,
index + 1);
return false;
}
}
return true;
}
//-----------------------------------------------------------------------------
// njsBaton::GetFunctionFromJSON()
// Gets a function from the JSON object for the given key, if possible. If
// undefined, leave value alone and do not set error; otherwise, set error.
// Index is the argument index in the caller.
//-----------------------------------------------------------------------------
bool njsBaton::GetFunctionFromJSON(Local<Object> obj, const char *key,
int index, Local<Function> *value)
{
Nan::EscapableHandleScope scope;
Local<Value> jsValue;
if (!error.empty())
return false;
MaybeLocal<Value> mval = Nan::Get(obj, Nan::New(key).ToLocalChecked());
if (!mval.ToLocal(&jsValue))
return false;
if (jsValue->IsFunction()) {
*value = scope.Escape(jsValue.As<Function>());
return true;
} else if (jsValue->IsUndefined()) {
return true;
}
error = njsMessages::Get(errInvalidPropertyTypeInParam, key, index + 1);
return false;
}
//-----------------------------------------------------------------------------
// njsBaton::GetIntFromJSON()
// Gets a signed integer value from the JSON object for the given key, if
// possible. If undefined, leave value alone and do not set error; otherwise,
// set error. Index is the argument index in the caller.
//-----------------------------------------------------------------------------
bool njsBaton::GetIntFromJSON(Local<Object> obj, const char *key,
int index, int32_t *value)
{
return njsUtils::GetIntFromJSON(obj, key, index, value, error);
}
//-----------------------------------------------------------------------------
// njsBaton::GetPositiveIntFromJSON()
// Gets a positive integer value from the JSON object for the given key, if
// possible. If undefined, leave value alone and do not set error; otherwise,
// set error. Index is the argument index in the caller.
//-----------------------------------------------------------------------------
bool njsBaton::GetPositiveIntFromJSON(Local<Object> obj, const char *key,
int index, uint32_t *value)
{
uint32_t tempValue = *value;
if (!GetUnsignedIntFromJSON(obj, key, index, &tempValue))
return false;
if (tempValue == 0) {
error = njsMessages::Get(errInvalidPropertyValueInParam, key,
index + 1);
return false;
}
*value = tempValue;
return true;
}
//-----------------------------------------------------------------------------
// njsBaton::GetStringFromJSON()
// Gets a string value from the JSON object for the given key, if possible.
// If null or undefined, leave value alone and do not set error; otherwise, set
// error. Index is the argument index in the caller.
//-----------------------------------------------------------------------------
bool njsBaton::GetStringFromJSON(Local<Object> obj, const char *key, int index,
string &value)
{
return njsUtils::GetStringFromJSON(obj, key, index, value, error);
}
//-----------------------------------------------------------------------------
// njsBaton::GetUnsignedIntFromJSON()
// Gets an unsigned integer value from the JSON object for the given key, if
// possible. If undefined, leave value alone and do not set error; otherwise,
// set error. Index is the argument index in the caller.
//-----------------------------------------------------------------------------
bool njsBaton::GetUnsignedIntFromJSON(Local<Object> obj, const char *key,
int index, uint32_t *value)
{
Nan::HandleScope scope;
Local<Value> jsValue;
if (!error.empty())
return false;
MaybeLocal<Value> mval = Nan::Get(obj, Nan::New(key).ToLocalChecked());
if (!mval.ToLocal(&jsValue))
return false;
if (jsValue->IsUint32()) {
*value = Nan::To<uint32_t>(jsValue).FromJust();
return true;
} else if (jsValue->IsUndefined()) {
return true;
} else if (jsValue->IsNumber() || jsValue->IsNull()) {
error = njsMessages::Get(errInvalidPropertyValueInParam, key,
index + 1);
return false;
} else {
error = njsMessages::Get(errInvalidPropertyTypeInParam, key,
index + 1);
return false;
}
}
//-----------------------------------------------------------------------------
// njsBaton::GetSodaDocument()
// Examines the passed object. If it is a buffer object, a new SODA document
// is created; otherwise, it is an existing SODA document object and the
// reference to it is retained on the baton (this is checked in Javascript).
//-----------------------------------------------------------------------------
bool njsBaton::GetSodaDocument(Local<Object> obj, dpiSodaDb *db)
{
if (Buffer::HasInstance(obj)) {
if (dpiSodaDb_createDocument(db, NULL, 0,
Buffer::Data(obj), Buffer::Length(obj), NULL, 0,
DPI_SODA_FLAGS_DEFAULT, &dpiSodaDocHandle) < 0) {
GetDPIError();
return false;
}
} else {
njsSodaDocument *doc = Nan::ObjectWrap::Unwrap<njsSodaDocument>(obj);
SetDPISodaDocHandle(doc->GetDPISodaDocHandle());
}
return true;
}
//-----------------------------------------------------------------------------
// njsBaton::GetNumOutBinds()
// Return the number of in/out and out binds created by the baton.
//-----------------------------------------------------------------------------
uint32_t njsBaton::GetNumOutBinds()
{
uint32_t numOutBinds = 0;
for (uint32_t i = 0; i < numBindVars; i++) {
if (bindVars[i].bindDir != NJS_BIND_IN)
numOutBinds++;
}
return numOutBinds;
}
//-----------------------------------------------------------------------------
// njsBaton::QueueWork()
// Queue work on a separate thread. The baton is passed as context. If an
// error has already taken place, the work is not queued on a separate thread;
// instead, the after work method is called directly. If an error takes place
// while queuing the work, a JS exception is raised.
//-----------------------------------------------------------------------------
void njsBaton::QueueWork(const char *methodName,
void (*workCallback)(njsBaton*),
void (*afterWorkCallback)(njsBaton*, Local<Value>[]),
unsigned int numCallbackArgs)
{
this->methodName = methodName;
this->workCallback = workCallback;
this->afterWorkCallback = afterWorkCallback;
this->numCallbackArgs = numCallbackArgs;
if (uv_queue_work(uv_default_loop(), &req, AsyncWorkCallback,
AsyncAfterWorkCallback)) {
delete this;
string errMsg = njsMessages::Get(errInternalError, "uv_queue_work",
methodName);
Nan::ThrowError(errMsg.c_str());
}
}
//-----------------------------------------------------------------------------
// njsCommon::CreateBaton()
// Creates a baton for use in asynchronous methods. In each of these cases
// the last argument passed in from JS is expected to be a JS callback. NULL is
// returned and an exception raised for JS if this is not the case.
//-----------------------------------------------------------------------------
njsBaton *njsCommon::CreateBaton(Nan::NAN_METHOD_ARGS_TYPE args)
{
Nan::HandleScope scope;
Local<Function> callback;
njsBaton *baton;
if (!args.Length() || !args[(args.Length() - 1)]->IsFunction()) {
string errMsg = njsMessages::Get(errMissingCallback);
Nan::ThrowError(errMsg.c_str());
return NULL;
}
callback = Local<Function>::Cast(args[args.Length() - 1]);
baton = new njsBaton(callback, args.Holder());
if (!IsValid()) {
njsErrorType errNum = GetInvalidErrorType();
baton->error = njsMessages::Get(errNum);
}
return baton;
}
//-----------------------------------------------------------------------------
// njsCommon::GetObjectArg()
// Gets an object from the list of arguments. If the argument is not an
// object, an error is raised and false is returned.
//-----------------------------------------------------------------------------
bool njsCommon::GetObjectArg(Nan::NAN_METHOD_ARGS_TYPE args,
int index, Local<Object> &value)
{
Nan::EscapableHandleScope scope;
if (!args[index]->IsObject()) {
string errMsg = njsMessages::Get(errInvalidParameterType, index + 1);
Nan::ThrowError(errMsg.c_str());
return false;
}
value = scope.Escape(args[index].As<Object>());
return true;
}
//-----------------------------------------------------------------------------
// njsCommon::GetStringArg()
// Gets a string from the list of arguments. If the argument is not a
// string, an error is raised and false is returned.
//-----------------------------------------------------------------------------
bool njsCommon::GetStringArg(Nan::NAN_METHOD_ARGS_TYPE args,
int index, std::string &value)
{
Nan::HandleScope scope;
if (!args[index]->IsString()) {
string errMsg = njsMessages::Get(errInvalidParameterType, index + 1);
Nan::ThrowError(errMsg.c_str());
return false;
}
Nan::Utf8String utf8str(args[index].As<String>());
value = std::string(*utf8str, static_cast<size_t>(utf8str.length()));
return true;
}
//-----------------------------------------------------------------------------
// njsCommon::GetUnsignedIntArg()
// Gets a unsigned integer from the list of arguments. If the argument is not
// a string, an error is raised and false is returned.
//-----------------------------------------------------------------------------
bool njsCommon::GetUnsignedIntArg(Nan::NAN_METHOD_ARGS_TYPE args,
int index, uint32_t *value)
{
if (!args[index]->IsUint32()) {
string errMsg = njsMessages::Get(errInvalidParameterType, index + 1);
Nan::ThrowError(errMsg.c_str());
return false;
}
*value = Nan::To<uint32_t>(args[index]).FromJust();
return true;
}
//-----------------------------------------------------------------------------
// njsCommon::SetPropBool()
// Sets a property to a boolean value. If the value is not a boolean, an
// error is raised and false is returned.
//-----------------------------------------------------------------------------
bool njsCommon::SetPropBool(Local<Value> value, bool *valuePtr,
const char *name)
{
if (!value->IsBoolean()) {
string errMsg = njsMessages::Get(errInvalidPropertyValue, name);
Nan::ThrowError(errMsg.c_str());
return false;
}
*valuePtr = value.As<v8::Boolean>()->Value();
return true;
}
//-----------------------------------------------------------------------------
// njsCommon::SetPropInt()
// Sets a property to an integer value. If the value is not an integer, an
// error is raised and false is returned.
//-----------------------------------------------------------------------------
bool njsCommon::SetPropInt(Local<Value> value, int32_t *valuePtr,
const char *name)
{
if (!value->IsInt32()) {
string errMsg = njsMessages::Get(errInvalidPropertyValue, name);
Nan::ThrowError(errMsg.c_str());
return false;
}
*valuePtr = Nan::To<int32_t>(value).FromJust();
return true;
}
//-----------------------------------------------------------------------------
// njsCommon::SetPropPositiveInt()
// Sets a property to a positive integer value. If the value is not a
// positive integer, an error is raised and false is returned.
//-----------------------------------------------------------------------------
bool njsCommon::SetPropPositiveInt(Local<Value> value, uint32_t *valuePtr,
const char *name)
{
uint32_t tempValue = *valuePtr;
if (!SetPropUnsignedInt(value, &tempValue, name))
return false;
if (tempValue == 0) {
string errMsg = njsMessages::Get(errInvalidPropertyValue, name);
Nan::ThrowError(errMsg.c_str());
return false;
}
*valuePtr = tempValue;
return true;
}
//-----------------------------------------------------------------------------
// njsCommon::SetPropString()
// Sets a property to a string value. If the value is not a string, an error
// is raised and false is returned.
//-----------------------------------------------------------------------------
bool njsCommon::SetPropString(Local<Value> value, std::string *valuePtr,
const char *name)
{
if (!value->IsString()) {
string errMsg = njsMessages::Get(errInvalidPropertyValue, name);
Nan::ThrowError(errMsg.c_str());
return false;
}
Nan::Utf8String utfstr(value.As<String>());
*valuePtr = std::string(*utfstr, static_cast<size_t>(utfstr.length()));
return true;
}
//-----------------------------------------------------------------------------
// njsCommon::SetPropUnsignedInt()
// Sets a property to an unsigned integer value. If the value is not an
// unsigned integer, an error is raised and false is returned.
//-----------------------------------------------------------------------------
bool njsCommon::SetPropUnsignedInt(Local<Value> value, uint32_t *valuePtr,
const char *name)
{
if (!value->IsUint32()) {
string errMsg = njsMessages::Get(errInvalidPropertyValue, name);
Nan::ThrowError(errMsg.c_str());
return false;
}
*valuePtr = Nan::To<uint32_t>(value).FromJust();
return true;
}
//-----------------------------------------------------------------------------
// njsCommon::Validate()
// Validates the pointer is not NULL and that it refers to a valid object.
// If not, an exception is raised in JS.
//-----------------------------------------------------------------------------
bool njsCommon::Validate(njsCommon *obj, bool checkValid)
{
string errMsg;
if (!obj) {
errMsg = njsMessages::Get(errInvalidJSObject);
Nan::ThrowError(errMsg.c_str());
return false;
}
if (checkValid && !obj->IsValid()) {
njsErrorType errNum = obj->GetInvalidErrorType();
errMsg = njsMessages::Get(errNum);
Nan::ThrowError(errMsg.c_str());
return false;
}
return true;
}
//-----------------------------------------------------------------------------
// njsCommon::ValidateGetter()
// Ensures that the JS caller is valid and returns the C++ object. NULL is
// returned and an exception raised in JS if this is not true.
//-----------------------------------------------------------------------------
njsCommon *njsCommon::ValidateGetter(Nan::NAN_GETTER_ARGS_TYPE args)
{
njsCommon *obj;
obj = Nan::ObjectWrap::Unwrap<njsCommon>(args.Holder());
if (!Validate(obj, false)) // no exception for invalid object
return NULL;
return obj;