-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathnjsConnection.c
2745 lines (2337 loc) · 101 KB
/
njsConnection.c
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, 2024, Oracle and/or its affiliates.
//-----------------------------------------------------------------------------
//
// This software is dual-licensed to you under the Universal Permissive License
// (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl and Apache License
// 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose
// either license.
//
// If you elect to accept the software under the Apache License, Version 2.0,
// the following applies:
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://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.
//
// NAME
// njsConnection.c
//
// DESCRIPTION
// Connection class implementation.
//
//-----------------------------------------------------------------------------
#include "njsModule.h"
// class methods
NJS_NAPI_METHOD_DECL_ASYNC(njsConnection_breakExecution);
NJS_NAPI_METHOD_DECL_ASYNC(njsConnection_changePassword);
NJS_NAPI_METHOD_DECL_ASYNC(njsConnection_close);
NJS_NAPI_METHOD_DECL_ASYNC(njsConnection_commit);
NJS_NAPI_METHOD_DECL_ASYNC(njsConnection_connect);
NJS_NAPI_METHOD_DECL_ASYNC(njsConnection_createLob);
NJS_NAPI_METHOD_DECL_ASYNC(njsConnection_execute);
NJS_NAPI_METHOD_DECL_SYNC(njsConnection_getCallTimeout);
NJS_NAPI_METHOD_DECL_SYNC(njsConnection_getCurrentSchema);
NJS_NAPI_METHOD_DECL_SYNC(njsConnection_getDbName);
NJS_NAPI_METHOD_DECL_SYNC(njsConnection_getDbDomain);
NJS_NAPI_METHOD_DECL_ASYNC(njsConnection_getDbObjectClass);
NJS_NAPI_METHOD_DECL_SYNC(njsConnection_getExternalName);
NJS_NAPI_METHOD_DECL_SYNC(njsConnection_getInstanceName);
NJS_NAPI_METHOD_DECL_SYNC(njsConnection_getInternalName);
NJS_NAPI_METHOD_DECL_SYNC(njsConnection_getMaxOpenCursors);
NJS_NAPI_METHOD_DECL_SYNC(njsConnection_getOracleServerVersion);
NJS_NAPI_METHOD_DECL_SYNC(njsConnection_getOracleServerVersionString);
NJS_NAPI_METHOD_DECL_ASYNC(njsConnection_getQueue);
NJS_NAPI_METHOD_DECL_SYNC(njsConnection_getServiceName);
NJS_NAPI_METHOD_DECL_SYNC(njsConnection_getSodaDatabase);
NJS_NAPI_METHOD_DECL_ASYNC(njsConnection_getStatementInfo);
NJS_NAPI_METHOD_DECL_SYNC(njsConnection_getStmtCacheSize);
NJS_NAPI_METHOD_DECL_SYNC(njsConnection_getTag);
NJS_NAPI_METHOD_DECL_SYNC(njsConnection_getTransactionInProgress);
NJS_NAPI_METHOD_DECL_SYNC(njsConnection_getWarning);
NJS_NAPI_METHOD_DECL_SYNC(njsConnection_isHealthy);
NJS_NAPI_METHOD_DECL_ASYNC(njsConnection_ping);
NJS_NAPI_METHOD_DECL_ASYNC(njsConnection_rollback);
NJS_NAPI_METHOD_DECL_SYNC(njsConnection_setAction);
NJS_NAPI_METHOD_DECL_SYNC(njsConnection_setCallTimeout);
NJS_NAPI_METHOD_DECL_SYNC(njsConnection_setClientId);
NJS_NAPI_METHOD_DECL_SYNC(njsConnection_setClientInfo);
NJS_NAPI_METHOD_DECL_SYNC(njsConnection_setCurrentSchema);
NJS_NAPI_METHOD_DECL_SYNC(njsConnection_setDbOp);
NJS_NAPI_METHOD_DECL_SYNC(njsConnection_setECID);
NJS_NAPI_METHOD_DECL_SYNC(njsConnection_setExternalName);
NJS_NAPI_METHOD_DECL_SYNC(njsConnection_setInternalName);
NJS_NAPI_METHOD_DECL_SYNC(njsConnection_setModule);
NJS_NAPI_METHOD_DECL_SYNC(njsConnection_setTag);
NJS_NAPI_METHOD_DECL_ASYNC(njsConnection_shutdown);
NJS_NAPI_METHOD_DECL_ASYNC(njsConnection_startup);
NJS_NAPI_METHOD_DECL_ASYNC(njsConnection_subscribe);
NJS_NAPI_METHOD_DECL_ASYNC(njsConnection_tpcBegin);
NJS_NAPI_METHOD_DECL_ASYNC(njsConnection_tpcCommit);
NJS_NAPI_METHOD_DECL_ASYNC(njsConnection_tpcEnd);
NJS_NAPI_METHOD_DECL_ASYNC(njsConnection_tpcForget);
NJS_NAPI_METHOD_DECL_ASYNC(njsConnection_tpcPrepare);
NJS_NAPI_METHOD_DECL_ASYNC(njsConnection_tpcRollback);
NJS_NAPI_METHOD_DECL_ASYNC(njsConnection_unsubscribe);
// asynchronous methods
static NJS_ASYNC_METHOD(njsConnection_breakExecutionAsync);
static NJS_ASYNC_METHOD(njsConnection_changePasswordAsync);
static NJS_ASYNC_METHOD(njsConnection_closeAsync);
static NJS_ASYNC_METHOD(njsConnection_commitAsync);
static NJS_ASYNC_METHOD(njsConnection_connectAsync);
static NJS_ASYNC_METHOD(njsConnection_createLobAsync);
static NJS_ASYNC_METHOD(njsConnection_executeAsync);
static NJS_ASYNC_METHOD(njsConnection_executeManyAsync);
static NJS_ASYNC_METHOD(njsConnection_getDbObjectClassAsync);
static NJS_ASYNC_METHOD(njsConnection_getQueueAsync);
static NJS_ASYNC_METHOD(njsConnection_getStatementInfoAsync);
static NJS_ASYNC_METHOD(njsConnection_pingAsync);
static NJS_ASYNC_METHOD(njsConnection_rollbackAsync);
static NJS_ASYNC_METHOD(njsConnection_shutdownAsync);
static NJS_ASYNC_METHOD(njsConnection_startupAsync);
static NJS_ASYNC_METHOD(njsConnection_subscribeAsync);
static NJS_ASYNC_METHOD(njsConnection_tpcBeginAsync);
static NJS_ASYNC_METHOD(njsConnection_tpcCommitAsync);
static NJS_ASYNC_METHOD(njsConnection_tpcEndAsync);
static NJS_ASYNC_METHOD(njsConnection_tpcForgetAsync);
static NJS_ASYNC_METHOD(njsConnection_tpcPrepareAsync);
static NJS_ASYNC_METHOD(njsConnection_tpcRollbackAsync);
static NJS_ASYNC_METHOD(njsConnection_unsubscribeAsync);
// post asynchronous methods
static NJS_ASYNC_POST_METHOD(njsConnection_connectPostAsync);
static NJS_ASYNC_POST_METHOD(njsConnection_createLobPostAsync);
static NJS_ASYNC_POST_METHOD(njsConnection_executePostAsync);
static NJS_ASYNC_POST_METHOD(njsConnection_executeManyPostAsync);
static NJS_ASYNC_POST_METHOD(njsConnection_getDbObjectClassPostAsync);
static NJS_ASYNC_POST_METHOD(njsConnection_getQueuePostAsync);
static NJS_ASYNC_POST_METHOD(njsConnection_getStatementInfoPostAsync);
static NJS_ASYNC_POST_METHOD(njsConnection_tpcPreparePostAsync);
static NJS_ASYNC_POST_METHOD(njsConnection_subscribePostAsync);
// finalize
static NJS_NAPI_FINALIZE(njsConnection_finalize);
// properties defined by the class
static const napi_property_descriptor njsClassProperties[] = {
{ "breakExecution", NULL, njsConnection_breakExecution, NULL, NULL, NULL,
napi_default, NULL },
{ "changePassword", NULL, njsConnection_changePassword, NULL, NULL,
NULL, napi_default, NULL },
{ "close", NULL, njsConnection_close, NULL, NULL, NULL,
napi_default, NULL },
{ "commit", NULL, njsConnection_commit, NULL, NULL, NULL,
napi_default, NULL },
{ "connect", NULL, njsConnection_connect, NULL, NULL, NULL, napi_default,
NULL },
{ "createLob", NULL, njsConnection_createLob, NULL, NULL, NULL,
napi_default, NULL },
{ "execute", NULL, njsConnection_execute, NULL, NULL, NULL,
napi_default, NULL },
{ "getCallTimeout", NULL, njsConnection_getCallTimeout, NULL, NULL, NULL,
napi_default, NULL },
{ "getCurrentSchema", NULL, njsConnection_getCurrentSchema, NULL, NULL,
NULL, napi_default, NULL },
{ "getDbName", NULL, njsConnection_getDbName, NULL, NULL, NULL,
napi_default, NULL },
{ "getDbDomain", NULL, njsConnection_getDbDomain, NULL, NULL, NULL,
napi_default, NULL },
{ "getDbObjectClass", NULL, njsConnection_getDbObjectClass, NULL, NULL,
NULL, napi_default, NULL },
{ "getExternalName", NULL, njsConnection_getExternalName, NULL, NULL, NULL,
napi_default, NULL },
{ "getInstanceName", NULL, njsConnection_getInstanceName, NULL, NULL, NULL,
napi_default, NULL },
{ "getInternalName", NULL, njsConnection_getInternalName, NULL, NULL, NULL,
napi_default, NULL },
{ "getMaxOpenCursors", NULL, njsConnection_getMaxOpenCursors, NULL, NULL,
NULL, napi_default, NULL },
{ "getOracleServerVersion", NULL, njsConnection_getOracleServerVersion,
NULL, NULL, NULL, napi_default, NULL },
{ "getOracleServerVersionString", NULL,
njsConnection_getOracleServerVersionString, NULL, NULL, NULL,
napi_default, NULL },
{ "getQueue", NULL, njsConnection_getQueue, NULL, NULL, NULL,
napi_default, NULL },
{ "getServiceName", NULL, njsConnection_getServiceName, NULL, NULL, NULL,
napi_default, NULL },
{ "getSodaDatabase", NULL, njsConnection_getSodaDatabase, NULL, NULL,
NULL, napi_default, NULL },
{ "getStatementInfo", NULL, njsConnection_getStatementInfo, NULL,
NULL, NULL, napi_default, NULL },
{ "getStmtCacheSize", NULL, njsConnection_getStmtCacheSize, NULL, NULL,
NULL, napi_default, NULL },
{ "getTag", NULL, njsConnection_getTag, NULL, NULL, NULL, napi_default,
NULL },
{ "getTransactionInProgress", NULL, njsConnection_getTransactionInProgress,
NULL, NULL, NULL, napi_default, NULL },
{ "getWarning", NULL, njsConnection_getWarning, NULL, NULL, NULL,
napi_default, NULL },
{ "isHealthy", NULL, njsConnection_isHealthy, NULL, NULL, NULL,
napi_default, NULL },
{ "ping", NULL, njsConnection_ping, NULL, NULL, NULL, napi_default,
NULL },
{ "rollback", NULL, njsConnection_rollback, NULL, NULL, NULL,
napi_default, NULL },
{ "setAction", NULL, njsConnection_setAction, NULL, NULL, NULL,
napi_default, NULL },
{ "setCallTimeout", NULL, njsConnection_setCallTimeout, NULL, NULL, NULL,
napi_default, NULL },
{ "setClientId", NULL, njsConnection_setClientId, NULL, NULL, NULL,
napi_default, NULL },
{ "setClientInfo", NULL, njsConnection_setClientInfo, NULL, NULL, NULL,
napi_default, NULL },
{ "setCurrentSchema", NULL, njsConnection_setCurrentSchema, NULL, NULL,
NULL, napi_default, NULL },
{ "setDbOp", NULL, njsConnection_setDbOp, NULL, NULL, NULL, napi_default,
NULL },
{ "setECID", NULL, njsConnection_setECID, NULL, NULL, NULL, napi_default,
NULL },
{ "setExternalName", NULL, njsConnection_setExternalName, NULL, NULL, NULL,
napi_default, NULL },
{ "setInternalName", NULL, njsConnection_setInternalName, NULL, NULL, NULL,
napi_default, NULL },
{ "setModule", NULL, njsConnection_setModule, NULL, NULL, NULL,
napi_default, NULL },
{ "setTag", NULL, njsConnection_setTag, NULL, NULL, NULL, napi_default,
NULL },
{ "shutdown", NULL, njsConnection_shutdown, NULL, NULL, NULL,
napi_default, NULL },
{ "startup", NULL, njsConnection_startup, NULL, NULL, NULL,
napi_default, NULL },
{ "subscribe", NULL, njsConnection_subscribe, NULL, NULL, NULL,
napi_default, NULL },
{ "tpcBegin", NULL, njsConnection_tpcBegin, NULL, NULL, NULL,
napi_default, NULL },
{ "tpcCommit", NULL, njsConnection_tpcCommit, NULL, NULL, NULL,
napi_default, NULL },
{ "tpcEnd", NULL, njsConnection_tpcEnd, NULL, NULL, NULL,
napi_default, NULL },
{ "tpcForget", NULL, njsConnection_tpcForget, NULL, NULL, NULL,
napi_default, NULL },
{ "tpcPrepare", NULL, njsConnection_tpcPrepare, NULL, NULL, NULL,
napi_default, NULL },
{ "tpcRollback", NULL, njsConnection_tpcRollback, NULL, NULL, NULL,
napi_default, NULL },
{ "unsubscribe", NULL, njsConnection_unsubscribe, NULL, NULL, NULL,
napi_default, NULL },
{ NULL, NULL, NULL, NULL, NULL, NULL, napi_default, NULL }
};
// class definition
const njsClassDef njsClassDefConnection = {
"ConnectionImpl", sizeof(njsConnection), njsConnection_finalize,
njsClassProperties, false
};
// other methods used internally
static bool njsConnection_getBatchErrors(njsBaton *baton, napi_env env,
napi_value *batchErrors);
static bool njsConnection_getExecuteManyOutBinds(njsBaton *baton, napi_env env,
uint32_t numOutBinds, napi_value *outBinds);
static bool njsConnection_getExecuteOutBinds(njsBaton *baton,
napi_env env, napi_value *outBinds);
static bool njsConnection_getImplicitResults(njsBaton *baton,
napi_env env, napi_value *implicitResults);
static bool njsConnection_getOutBinds(njsBaton *baton, napi_env env,
uint32_t numOutBinds, uint32_t pos, napi_value *outBinds);
static bool njsConnection_getRowCounts(njsBaton *baton, napi_env env,
napi_value *rowCounts);
static bool njsConnection_prepareAndBind(njsConnection *conn, njsBaton *baton);
static bool njsConnection_processBinds(njsBaton *baton, napi_env env,
napi_value binds);
static bool njsConnection_processImplicitResults(njsBaton *baton);
static bool njsConnection_setTextAttribute(napi_env env, void *instance,
njsModuleGlobals *globals, napi_value value,
int (*setter)(dpiConn*, const char *, uint32_t));
//-----------------------------------------------------------------------------
// njsConnection_breakExecution()
// Break (interrupt) the currently executing operation.
//
// PARAMETERS - NONE
//-----------------------------------------------------------------------------
NJS_NAPI_METHOD_IMPL_ASYNC(njsConnection_breakExecution, 0, NULL)
{
return njsBaton_queueWork(baton, env, "Break",
njsConnection_breakExecutionAsync, NULL, returnValue);
}
//-----------------------------------------------------------------------------
// njsConnection_breakExecutionAsync()
// Worker function for njsConnection_breakExecution().
//-----------------------------------------------------------------------------
static bool njsConnection_breakExecutionAsync(njsBaton *baton)
{
njsConnection *conn = (njsConnection*) baton->callingInstance;
if (dpiConn_breakExecution(conn->handle) < 0)
return njsBaton_setErrorDPI(baton);
return true;
}
//-----------------------------------------------------------------------------
// njsConnection_changePassword()
// Change the password on the connection
//
// PARAMETERS
// - user which will have its password changed
// - original password
// - new password
//-----------------------------------------------------------------------------
NJS_NAPI_METHOD_IMPL_ASYNC(njsConnection_changePassword, 3, NULL)
{
if (!njsUtils_copyStringFromJS(env, args[0], &baton->user,
&baton->userLength))
return false;
if (!njsUtils_copyStringFromJS(env, args[1], &baton->password,
&baton->passwordLength))
return false;
if (!njsUtils_copyStringFromJS(env, args[2], &baton->newPassword,
&baton->newPasswordLength))
return false;
return njsBaton_queueWork(baton, env, "ChangePassword",
njsConnection_changePasswordAsync, NULL, returnValue);
}
//-----------------------------------------------------------------------------
// njsConnection_changePasswordAsync()
// Worker function for njsConnection_changePassword().
//-----------------------------------------------------------------------------
static bool njsConnection_changePasswordAsync(njsBaton *baton)
{
njsConnection *conn = (njsConnection*) baton->callingInstance;
if (dpiConn_changePassword(conn->handle, baton->user,
(uint32_t) baton->userLength, baton->password,
(uint32_t) baton->passwordLength, baton->newPassword,
(uint32_t) baton->newPasswordLength) < 0)
return njsBaton_setErrorDPI(baton);
return true;
}
//-----------------------------------------------------------------------------
// njsConnection_close()
// Releases the connection from use by JS. This releases the connection back
// to the pool or closes the standalone connection so further use is not
// possible.
//
// PARAMETERS
// - options object
//-----------------------------------------------------------------------------
NJS_NAPI_METHOD_IMPL_ASYNC(njsConnection_close, 1, NULL)
{
njsConnection *conn = (njsConnection*) baton->callingInstance;
if (!njsUtils_getNamedPropertyBool(env, args[0], "drop",
&baton->dropSession))
return false;
baton->dpiConnHandle = conn->handle;
conn->handle = NULL;
return njsBaton_queueWork(baton, env, "Close", njsConnection_closeAsync,
NULL, returnValue);
}
//-----------------------------------------------------------------------------
// njsConnection_closeAsync()
// Worker function for njsConnection_close().
//-----------------------------------------------------------------------------
static bool njsConnection_closeAsync(njsBaton *baton)
{
njsConnection *conn = (njsConnection*) baton->callingInstance;
uint32_t mode = DPI_MODE_CONN_CLOSE_DEFAULT, tagLength = 0;
const char *tag = NULL;
if (baton->dropSession) {
mode = DPI_MODE_CONN_CLOSE_DROP;
} else if (conn->retag) {
mode = DPI_MODE_CONN_CLOSE_RETAG;
tag = conn->tag;
tagLength = (uint32_t) conn->tagLength;
}
if (dpiConn_close(baton->dpiConnHandle, mode, tag, tagLength) < 0) {
njsBaton_setErrorDPI(baton);
conn->handle = baton->dpiConnHandle;
baton->dpiConnHandle = NULL;
return false;
}
return true;
}
//-----------------------------------------------------------------------------
// njsConnection_commit()
// Commits the active transaction.
//
// PARAMETERS - NONE
//-----------------------------------------------------------------------------
NJS_NAPI_METHOD_IMPL_ASYNC(njsConnection_commit, 0, NULL)
{
return njsBaton_queueWork(baton, env, "Commit", njsConnection_commitAsync,
NULL, returnValue);
}
//-----------------------------------------------------------------------------
// njsConnection_commitAsync()
// Worker function for njsConnection_commit().
//-----------------------------------------------------------------------------
static bool njsConnection_commitAsync(njsBaton *baton)
{
njsConnection *conn = (njsConnection*) baton->callingInstance;
if (dpiConn_commit(conn->handle) < 0)
return njsBaton_setErrorDPI(baton);
return true;
}
//-----------------------------------------------------------------------------
// njsConnection_connect()
// Create a standalone connection to the database.
//
// PARAMETERS
// - options
//-----------------------------------------------------------------------------
NJS_NAPI_METHOD_IMPL_ASYNC(njsConnection_connect, 1, &njsClassDefConnection)
{
if (!njsBaton_commonConnectProcessArgs(baton, env, args))
return false;
if (!njsUtils_getNamedPropertyString(env, args[0], "newPassword",
&baton->newPassword, &baton->newPasswordLength))
return false;
if (!njsUtils_getNamedPropertyUnsignedInt(env, args[0], "privilege",
&baton->privilege))
return false;
if (!njsUtils_getNamedPropertyShardingKey(env, args[0], "shardingKey",
&baton->numShardingKeyColumns, &baton->shardingKeyColumns))
return false;
if (!njsUtils_getNamedPropertyShardingKey(env, args[0], "superShardingKey",
&baton->numSuperShardingKeyColumns,
&baton->superShardingKeyColumns))
return false;
return njsBaton_queueWork(baton, env, "Connect",
njsConnection_connectAsync, njsConnection_connectPostAsync,
returnValue);
}
//-----------------------------------------------------------------------------
// njsConnection_connectAsync()
// Worker function for Connect() performed on thread. This establishes the
// connection using the information found in the baton.
//-----------------------------------------------------------------------------
static bool njsConnection_connectAsync(njsBaton *baton)
{
dpiCommonCreateParams commonParams;
dpiConnCreateParams params;
dpiAccessToken accessToken;
if (dpiContext_initConnCreateParams(baton->globals->context, ¶ms) < 0)
return njsBaton_setErrorDPI(baton);
if (baton->privilege)
params.authMode = (dpiAuthMode) baton->privilege;
params.externalAuth = baton->externalAuth;
params.connectionClass = baton->connectionClass;
params.connectionClassLength = (uint32_t) baton->connectionClassLength;
params.newPassword = baton->newPassword;
params.newPasswordLength = (uint32_t) baton->newPasswordLength;
if (!njsBaton_initCommonCreateParams(baton, &commonParams))
return false;
// Sharding
params.shardingKeyColumns = baton->shardingKeyColumns;
params.numShardingKeyColumns = baton->numShardingKeyColumns;
params.superShardingKeyColumns = baton->superShardingKeyColumns;
params.numSuperShardingKeyColumns = baton->numSuperShardingKeyColumns;
commonParams.edition = baton->edition;
commonParams.editionLength = (uint32_t) baton->editionLength;
commonParams.stmtCacheSize = baton->stmtCacheSize;
// set token based auth parameters
if (baton->token) {
accessToken.token = baton->token;
accessToken.tokenLength = baton->tokenLength;
accessToken.privateKey = baton->privateKey;
accessToken.privateKeyLength = baton->privateKeyLength;
commonParams.accessToken = &accessToken;
}
if (dpiConn_create(baton->globals->context, baton->user,
(uint32_t) baton->userLength, baton->password,
(uint32_t) baton->passwordLength, baton->connectString,
(uint32_t) baton->connectStringLength, &commonParams, ¶ms,
&baton->dpiConnHandle) < 0)
return njsBaton_setErrorDPI(baton);
// handle warnings if any
dpiContext_getError(baton->globals->context, &baton->warningInfo);
return true;
}
//-----------------------------------------------------------------------------
// njsConnection_connectPostAsync()
// Defines the value returned to JS.
//-----------------------------------------------------------------------------
static bool njsConnection_connectPostAsync(njsBaton *baton, napi_env env,
napi_value *result)
{
njsConnection *conn = (njsConnection*) baton->callingInstance;
// process warnings if any
if (baton->warningInfo.isWarning) {
conn->warningInfo = baton->warningInfo;
}
// transfer the ODPI-C connection handle to the new object
conn->handle = baton->dpiConnHandle;
baton->dpiConnHandle = NULL;
return true;
}
//-----------------------------------------------------------------------------
// njsConnection_createLob()
// Create a new temporary LOB and return it for use by the application.
//
// PARAMETERS
// - LOB type
//-----------------------------------------------------------------------------
NJS_NAPI_METHOD_IMPL_ASYNC(njsConnection_createLob, 1, NULL)
{
napi_value lobType;
NJS_CHECK_NAPI(env, napi_get_named_property(env, args[0], "num", &lobType))
NJS_CHECK_NAPI(env, napi_get_value_uint32(env, lobType, &baton->lobType))
return njsBaton_queueWork(baton, env, "CreateLob",
njsConnection_createLobAsync, njsConnection_createLobPostAsync,
returnValue);
}
//-----------------------------------------------------------------------------
// njsConnection_createLobAsync()
// Worker function for njsConnection_createLob().
//-----------------------------------------------------------------------------
static bool njsConnection_createLobAsync(njsBaton *baton)
{
njsConnection *conn = (njsConnection*) baton->callingInstance;
baton->lob = calloc(1, sizeof(njsLobBuffer));
if (!baton->lob)
return njsBaton_setErrorInsufficientMemory(baton);
if (dpiConn_newTempLob(conn->handle, baton->lobType,
&baton->lob->handle) < 0)
return njsBaton_setErrorDPI(baton);
baton->lob->dataType = baton->lobType;
if (!njsLob_populateBuffer(baton, baton->lob))
return false;
return true;
}
//-----------------------------------------------------------------------------
// njsConnection_createLobPostAsync()
// Defines the value returned to JS.
//-----------------------------------------------------------------------------
static bool njsConnection_createLobPostAsync(njsBaton *baton, napi_env env,
napi_value *result)
{
napi_value connObj;
NJS_CHECK_NAPI(env, napi_get_reference_value(env, baton->jsCallingObjRef,
&connObj))
return njsLob_new(baton->globals, baton->lob, env, connObj, result);
}
//-----------------------------------------------------------------------------
// njsConnection_execute()
// Executes a statement on the connection.
//
// PARAMETERS
// - SQL statement
// - number of iterations
// - array of binds
// - options
// - executeMany flag
//-----------------------------------------------------------------------------
NJS_NAPI_METHOD_IMPL_ASYNC(njsConnection_execute, 5, NULL)
{
bool executeMany;
napi_value temp;
// validate connection and process arguments
if (!njsBaton_setJsValues(baton, env))
return false;
if (!njsUtils_copyStringFromJS(env, args[0], &baton->sql,
&baton->sqlLength))
return false;
NJS_CHECK_NAPI(env, napi_get_value_uint32(env, args[1],
&baton->bindArraySize))
NJS_CHECK_NAPI(env, napi_get_value_bool(env, args[4], &executeMany))
// process options
NJS_CHECK_NAPI(env, napi_create_reference(env, args[3], 1,
&baton->jsExecuteOptionsRef))
if (executeMany) {
if (!njsUtils_getNamedPropertyBool(env, args[3], "batchErrors",
&baton->batchErrors))
return false;
if (!njsUtils_getNamedPropertyBool(env, args[3], "dmlRowCounts",
&baton->dmlRowCounts))
return false;
} else {
NJS_CHECK_NAPI(env, napi_get_named_property(env, args[3],
"fetchArraySize", &temp))
NJS_CHECK_NAPI(env, napi_get_value_uint32(env, temp,
&baton->fetchArraySize))
NJS_CHECK_NAPI(env, napi_get_named_property(env, args[3],
"prefetchRows", &temp))
NJS_CHECK_NAPI(env, napi_get_value_uint32(env, temp,
&baton->prefetchRows))
}
NJS_CHECK_NAPI(env, napi_get_named_property(env, args[3], "autoCommit",
&temp))
NJS_CHECK_NAPI(env, napi_get_value_bool(env, temp, &baton->autoCommit))
NJS_CHECK_NAPI(env, napi_get_named_property(env, args[3],
"keepInStmtCache", &temp))
NJS_CHECK_NAPI(env, napi_get_value_bool(env, temp,
&baton->keepInStmtCache))
// process binds
if (!njsConnection_processBinds(baton, env, args[2]))
return false;
// queue async work
if (executeMany)
return njsBaton_queueWork(baton, env, "ExecuteMany",
njsConnection_executeManyAsync,
njsConnection_executeManyPostAsync, returnValue);
return njsBaton_queueWork(baton, env, "Execute",
njsConnection_executeAsync, njsConnection_executePostAsync,
returnValue);
}
//-----------------------------------------------------------------------------
// njsConnection_executeAsync()
// Worker function for njsConnection_execute().
//-----------------------------------------------------------------------------
static bool njsConnection_executeAsync(njsBaton *baton)
{
njsConnection *conn = (njsConnection*) baton->callingInstance;
dpiExecMode mode;
// prepare statement and perform any binds that are needed
if (!njsConnection_prepareAndBind(conn, baton))
return false;
// set prefetch rows if a value other than the default is specified
if (baton->prefetchRows != DPI_DEFAULT_PREFETCH_ROWS) {
if (dpiStmt_setPrefetchRows(baton->dpiStmtHandle,
baton->prefetchRows) < 0)
return njsBaton_setErrorDPI(baton);
}
// mark statement for removal from the cache, if applicable
if (!baton->keepInStmtCache) {
if (dpiStmt_deleteFromCache(baton->dpiStmtHandle) < 0) {
return njsBaton_setErrorDPI(baton);
}
}
// execute statement
mode = (baton->autoCommit) ? DPI_MODE_EXEC_COMMIT_ON_SUCCESS :
DPI_MODE_EXEC_DEFAULT;
if (dpiStmt_execute(baton->dpiStmtHandle, mode, &baton->numQueryVars) < 0)
return njsBaton_setErrorDPI(baton);
// handle warnings if any
dpiContext_getError(baton->globals->context, &baton->warningInfo);
// for queries, initialize query variables
if (baton->numQueryVars > 0) {
baton->queryVars = calloc(baton->numQueryVars, sizeof(njsVariable));
if (!baton->queryVars)
return njsBaton_setErrorInsufficientMemory(baton);
if (!njsVariable_initForQuery(baton->queryVars, baton->numQueryVars,
baton->dpiStmtHandle, baton))
return false;
// for all other statements, determine the number of rows affected, process
// variables (to manage LOBs, REF cursors, PL/SQL arrays, etc.) and process
// implicit results
} else {
if (dpiStmt_getRowCount(baton->dpiStmtHandle,
&baton->rowsAffected) < 0)
return njsBaton_setErrorDPI(baton);
baton->bufferRowIndex = 0;
if (!njsVariable_process(baton->bindVars, baton->numBindVars, 1,
baton))
return false;
if (!njsConnection_processImplicitResults(baton))
return false;
}
return true;
}
//-----------------------------------------------------------------------------
// njsConnection_executePostAsync()
// Defines the value returned to JS.
//-----------------------------------------------------------------------------
static bool njsConnection_executePostAsync(njsBaton *baton, napi_env env,
napi_value *result)
{
napi_value resultSet, rowsAffected, outBinds, lastRowid, error;
napi_value implicitResults;
uint32_t rowidValueLength;
const char *rowidValue;
dpiRowid *rowid;
// set JavaScript values to simplify creation of returned objects
if (!njsBaton_setJsValues(baton, env))
return false;
// create result object
NJS_CHECK_NAPI(env, napi_create_object(env, result))
// process warnings if any
if (baton->warningInfo.isWarning) {
if (!njsUtils_getError(env, &baton->warningInfo, NULL, &error))
return false;
NJS_CHECK_NAPI(env, napi_set_named_property(env, *result, "warning",
error))
}
// handle queries
if (baton->queryVars) {
// perform any initialization required within JavaScript
if (!njsVariable_initForQueryJS(baton->queryVars, baton->numQueryVars,
env, baton))
return false;
// return result set
if (!njsResultSet_new(baton, env,
(njsConnection*) baton->callingInstance, baton->dpiStmtHandle,
baton->queryVars, baton->numQueryVars, &resultSet))
return false;
baton->dpiStmtHandle = NULL;
baton->queryVars = NULL;
baton->numQueryVars = 0;
NJS_CHECK_NAPI(env, napi_set_named_property(env, *result, "resultSet",
resultSet))
} else {
// store OUT binds, if applicable
outBinds = NULL;
if (!njsConnection_getExecuteOutBinds(baton, env, &outBinds))
return false;
if (outBinds) {
NJS_CHECK_NAPI(env, napi_set_named_property(env, *result,
"outBinds", outBinds))
}
// for DML statements, check to see if the last rowid is available
if (baton->stmtInfo.isDML) {
if (dpiStmt_getLastRowid(baton->dpiStmtHandle, &rowid) < 0)
return njsBaton_setErrorDPI(baton);
if (rowid) {
if (dpiRowid_getStringValue(rowid, &rowidValue,
&rowidValueLength) < 0)
return njsBaton_setErrorDPI(baton);
NJS_CHECK_NAPI(env, napi_create_string_utf8(env, rowidValue,
rowidValueLength, &lastRowid))
NJS_CHECK_NAPI(env, napi_set_named_property(env, *result,
"lastRowid", lastRowid))
}
}
// check for implicit results when executing PL/SQL
if (baton->stmtInfo.isPLSQL) {
implicitResults = NULL;
if (!njsConnection_getImplicitResults(baton, env,
&implicitResults))
return false;
if (implicitResults) {
NJS_CHECK_NAPI(env, napi_set_named_property(env, *result,
"implicitResults", implicitResults))
}
// set rows affected if not executing PL/SQL
} else {
NJS_CHECK_NAPI(env, napi_create_uint32(env,
(uint32_t) baton->rowsAffected, &rowsAffected))
NJS_CHECK_NAPI(env, napi_set_named_property(env, *result,
"rowsAffected", rowsAffected))
}
}
return true;
}
//-----------------------------------------------------------------------------
// njsConnection_executeManyAsync()
// Worker function for njsConnection_executeMany().
//-----------------------------------------------------------------------------
static bool njsConnection_executeManyAsync(njsBaton *baton)
{
njsConnection *conn = (njsConnection*) baton->callingInstance;
uint32_t mode;
// prepare statement and perform any binds that are needed
if (!njsConnection_prepareAndBind(conn, baton))
return false;
// mark statement for removal from the cache, if applicable
if (!baton->keepInStmtCache) {
if (dpiStmt_deleteFromCache(baton->dpiStmtHandle) < 0) {
return njsBaton_setErrorDPI(baton);
}
}
// execute statement
mode = (baton->autoCommit) ? DPI_MODE_EXEC_COMMIT_ON_SUCCESS :
DPI_MODE_EXEC_DEFAULT;
if (baton->batchErrors)
mode |= DPI_MODE_EXEC_BATCH_ERRORS;
if (baton->dmlRowCounts)
mode |= DPI_MODE_EXEC_ARRAY_DML_ROWCOUNTS;
if (dpiStmt_executeMany(baton->dpiStmtHandle, mode,
baton->bindArraySize) < 0)
return njsBaton_setErrorDPI(baton);
dpiContext_getError(baton->globals->context, &baton->warningInfo);
// process any LOBS for out binds, as needed
if (dpiStmt_getRowCount(baton->dpiStmtHandle, &baton->rowsAffected) < 0)
return njsBaton_setErrorDPI(baton);
baton->bufferRowIndex = 0;
if (!njsVariable_process(baton->bindVars, baton->numBindVars,
baton->bindArraySize, baton))
return false;
// get DML row counts if option was enabled
if (baton->dmlRowCounts) {
if (dpiStmt_getRowCounts(baton->dpiStmtHandle, &baton->numRowCounts,
&baton->rowCounts) < 0)
return njsBaton_setErrorDPI(baton);
}
// get batch errors, if option was enabled
if (baton->batchErrors) {
if (dpiStmt_getBatchErrorCount(baton->dpiStmtHandle,
&baton->numBatchErrorInfos) < 0)
return njsBaton_setErrorDPI(baton);
if (baton->numBatchErrorInfos > 0) {
baton->batchErrorInfos = calloc(baton->numBatchErrorInfos,
sizeof(dpiErrorInfo));
if (!baton->batchErrorInfos)
return njsBaton_setErrorInsufficientMemory(baton);
if (dpiStmt_getBatchErrors(baton->dpiStmtHandle,
baton->numBatchErrorInfos, baton->batchErrorInfos) < 0)
return njsBaton_setErrorDPI(baton);
}
}
return true;
}
//-----------------------------------------------------------------------------
// njsConnection_executeManyPostAsync()
// Defines the value returned to JS.
//-----------------------------------------------------------------------------
static bool njsConnection_executeManyPostAsync(njsBaton *baton, napi_env env,
napi_value *result)
{
uint32_t numOutBinds;
napi_value temp, error;
// set JavaScript values to simplify creation of returned objects
if (!njsBaton_setJsValues(baton, env))
return false;
// create object for result
NJS_CHECK_NAPI(env, napi_create_object(env, result))
// process warnings if any
if (baton->warningInfo.isWarning) {
if (!njsUtils_getError(env, &baton->warningInfo, NULL, &error))
return false;
NJS_CHECK_NAPI(env, napi_set_named_property(env, *result, "warning",
error))
}
// get out binds
numOutBinds = njsBaton_getNumOutBinds(baton);
if (numOutBinds > 0) {
if (!njsConnection_getExecuteManyOutBinds(baton, env, numOutBinds,
&temp))
return false;
NJS_CHECK_NAPI(env, napi_set_named_property(env, *result, "outBinds",
temp))
}
// get total number of rows affected
if (!baton->stmtInfo.isPLSQL) {
NJS_CHECK_NAPI(env, napi_create_uint32(env,
(uint32_t) baton->rowsAffected, &temp))
NJS_CHECK_NAPI(env, napi_set_named_property(env, *result,
"rowsAffected", temp))
}
// get DML row counts if option was enabled
if (baton->dmlRowCounts && baton->numRowCounts > 0) {
if (!njsConnection_getRowCounts(baton, env, &temp))
return false;
NJS_CHECK_NAPI(env, napi_set_named_property(env, *result,
"dmlRowCounts", temp))
}
// get batch errors, if option was enabled
if (baton->batchErrors && baton->numBatchErrorInfos > 0) {
if (!njsConnection_getBatchErrors(baton, env, &temp))
return false;
NJS_CHECK_NAPI(env, napi_set_named_property(env, *result,
"batchErrors", temp))
}
return true;
}
//-----------------------------------------------------------------------------
// njsConnection_finalize()
// Invoked when the njsConnection object is garbage collected.
//-----------------------------------------------------------------------------
static void njsConnection_finalize(napi_env env, void *finalizeData,
void *finalizeHint)
{
uint32_t mode = DPI_MODE_CONN_CLOSE_DEFAULT, tagLength = 0;
njsConnection *conn = (njsConnection*) finalizeData;
const char *tag = NULL;
if (conn->handle) {
if (conn->retag) {
mode = DPI_MODE_CONN_CLOSE_RETAG;
tag = conn->tag;
tagLength = (uint32_t) conn->tagLength;
}
dpiConn_close(conn->handle, mode, tag, tagLength);
dpiConn_release(conn->handle);
conn->handle = NULL;
}
NJS_FREE_AND_CLEAR(conn->tag);
free(conn);
}
//-----------------------------------------------------------------------------
// njsConnection_getBatchErrors()
// Get the array of batch errors from the baton.
//-----------------------------------------------------------------------------
static bool njsConnection_getBatchErrors(njsBaton *baton, napi_env env,
napi_value *batchErrors)
{
napi_value temp, error, message;
dpiErrorInfo *info;
uint32_t i;
NJS_CHECK_NAPI(env, napi_create_array_with_length(env,
baton->numBatchErrorInfos, batchErrors))
for (i = 0; i < baton->numBatchErrorInfos; i++) {
info = &baton->batchErrorInfos[i];
// create error from message
NJS_CHECK_NAPI(env, napi_create_string_utf8(env, info->message,
info->messageLength, &message))
NJS_CHECK_NAPI(env, napi_create_error(env, NULL, message, &error))
// add error number
NJS_CHECK_NAPI(env, napi_create_int32(env, info->code, &temp))
NJS_CHECK_NAPI(env, napi_set_named_property(env, error, "errorNum",
temp))
// set offset
NJS_CHECK_NAPI(env, napi_create_uint32(env, info->offset, &temp))
NJS_CHECK_NAPI(env, napi_set_named_property(env, error, "offset",
temp))
// add error to array
NJS_CHECK_NAPI(env, napi_set_element(env, *batchErrors, i, error))
}
return true;
}