-
Notifications
You must be signed in to change notification settings - Fork 133
/
Copy pathstatement.js
1684 lines (1508 loc) · 46 KB
/
statement.js
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 Snowflake Computing Inc. All rights reserved.
*/
const async = require('async');
const uuidv4 = require('uuid/v4');
var Url = require('url');
var QueryString = require('querystring');
var EventEmitter = require('events').EventEmitter;
var Util = require('../util');
var Result = require('./result/result');
var Parameters = require('../parameters');
var RowStream = require('./result/row_stream');
var Errors = require('../errors');
var ErrorCodes = Errors.codes;
var Logger = require('../logger');
var NativeTypes = require('./result/data_types').NativeTypes;
var file_transfer_agent = require('.././file_transfer_agent/file_transfer_agent');
var Bind = require('./bind_uploader');
var states =
{
FETCHING: 'fetching',
COMPLETE: 'complete'
};
var statementTypes =
{
ROW_PRE_EXEC: 'ROW_PRE_EXEC',
ROW_POST_EXEC: 'ROW_POST_EXEC',
FILE_PRE_EXEC: 'FILE_PRE_EXEC',
FILE_POST_EXEC: 'FILE_POST_EXEC'
};
exports.createContext = function (
options, services, connectionConfig)
{
// create a statement context for a pre-exec statement
var context = createContextPreExec(
options, services, connectionConfig);
context.type = statementTypes.FILE_PRE_EXEC;
createStatement(options, context, services, connectionConfig);
// add the result request headers to the context
context.resultRequestHeaders = buildResultRequestHeadersFile();
return context;
};
function createStatement(
statementOptions, context, services, connectionConfig)
{
// call super
BaseStatement.apply(this, arguments);
}
/**
* Check the type of command to execute.
*
* @param {Object} options
* @param {Object} services
* @param {Object} connectionConfig
*
* @returns {Object}
*/
exports.createStatementPreExec = function (
options, services, connectionConfig)
{
Logger.getInstance().debug('--createStatementPreExec');
// create a statement context for a pre-exec statement
var context = createContextPreExec(
options, services, connectionConfig);
if (options.sqlText && (Util.isPutCommand(options.sqlText) || Util.isGetCommand(options.sqlText)))
{
return createFileStatementPreExec(
options, context, services, connectionConfig);
}
var numBinds = countBinding(context.binds)
Logger.getInstance().debug('numBinds = %d', numBinds);
// default threshold value is 100000
var threshold = Parameters.getValue(Parameters.names.CLIENT_STAGE_ARRAY_BINDING_THRESHOLD);
if(connectionConfig.getbindThreshold())
{
threshold = connectionConfig.getbindThreshold();
}
Logger.getInstance().debug('threshold = %d', threshold);
// check array binding,
if(numBinds > threshold)
{
var bindUploaderRequestId = uuidv4();
var bind = new Bind.BindUploader(options, services, connectionConfig, bindUploaderRequestId);
var bindData;
try
{
bindData = bind.Upload(context.binds);
}
catch(e)
{
Logger.getInstance().debug('bind upload error, use normal binding');
return createRowStatementPreExec(
options, context, services, connectionConfig);
}
if(bindData != null)
{
context.bindStage = Bind.GetStageName(bindUploaderRequestId);
Logger.getInstance().debug('context.bindStage = %s', context.bindStage);
return createStage(services, connectionConfig, bindData, options, context);
}
}
else
{
return createRowStatementPreExec(
options, context, services, connectionConfig);
}
};
function createStage(services, connectionConfig, bindData, options, context)
{
Logger.getInstance().debug('createStage');
var createStageOpt = {sqlText:Bind.GetCreateStageStmt(),
complete: function (err, stmt, rows)
{
Logger.getInstance().debug('stream');
Logger.getInstance().debug('err '+err);
if(err)
{
context.bindStage = null;
return createRowStatementPreExec(
options, context, services, connectionConfig);
}
else
{
var stream = stmt.streamRows();
stream.on('data', function (rows)
{
Logger.getInstance().debug('stream on data');
return uploadFiles(services, connectionConfig, bindData, options, context);
});
}
}
}
Logger.getInstance().debug('CREATE_STAGE_STMT = %s', Bind.GetCreateStageStmt());
exports.createStatementPreExec(createStageOpt, services, connectionConfig);
}
function uploadFiles(services, connectionConfig, bindData, options, context, curIndex = 0)
{
Logger.getInstance().debug('uploadFiles %d, %s', curIndex, context.bindStage);
if (curIndex < bindData.files.length) {
Logger.getInstance().debug('Put=' + bindData.puts[curIndex]);
var fileOpt = {
sqlText: bindData.puts[curIndex],
complete: function (err, stmt, rows) {
if (err) {
return createRowStatementPreExec(
options, context, services, connectionConfig);
}
Logger.getInstance().debug('uploadFiles done ');
var stream = stmt.streamRows();
stream.on('data', function (rows) {
Logger.getInstance().debug('stream on data');
});
stream.on('end', function (rows) {
Logger.getInstance().debug('stream on end ');
curIndex++;
if (curIndex < bindData.files.length) {
uploadFiles(services, connectionConfig, bindData, options, context, curIndex);
}
else {
Logger.getInstance().debug('all completed');
cleanTempFiles(bindData);
return createRowStatementPreExec(
options, context, services, connectionConfig);
}
});
}
}
exports.createStatementPreExec(fileOpt, services, connectionConfig);
}
}
function cleanTempFiles(bindData)
{
for(var i=0; i<bindData.files.length; i++)
{
Logger.getInstance().debug('Clean File='+bindData.files[i]);
Bind.CleanFile(bindData.files[i]);
}
}
/**
* Executes a statement and returns a statement object that can be used to fetch
* its result.
*
* @param {Object} statementOptions
* @param {Object} statementContext
* @param {Object} services
* @param {Object} connectionConfig
*
* @returns {Object}
*/
function createRowStatementPreExec (
statementOptions, statementContext, services, connectionConfig)
{
// set the statement type
statementContext.type = statementTypes.ROW_PRE_EXEC;
return new RowStatementPreExec(
statementOptions, statementContext, services, connectionConfig);
};
/**
* Creates a statement object that can be used to fetch the result of a
* previously executed statement.
*
* @param {Object} statementOptions
* @param {Object} services
* @param {Object} connectionConfig
*
* @returns {Object}
*/
exports.createStatementPostExec = function (
statementOptions, services, connectionConfig)
{
// check for missing options
Errors.checkArgumentExists(Util.exists(statementOptions),
ErrorCodes.ERR_CONN_FETCH_RESULT_MISSING_OPTIONS);
// check for invalid options
Errors.checkArgumentValid(Util.isObject(statementOptions),
ErrorCodes.ERR_CONN_FETCH_RESULT_INVALID_OPTIONS);
// check for missing statement id
Errors.checkArgumentExists(Util.exists(statementOptions.statementId),
ErrorCodes.ERR_CONN_FETCH_RESULT_MISSING_STATEMENT_ID);
// check for invalid statement id
Errors.checkArgumentValid(Util.isString(statementOptions.statementId),
ErrorCodes.ERR_CONN_FETCH_RESULT_INVALID_STATEMENT_ID);
// check for invalid complete callback
var complete = statementOptions.complete;
if (Util.exists(complete))
{
Errors.checkArgumentValid(Util.isFunction(complete),
ErrorCodes.ERR_CONN_FETCH_RESULT_INVALID_COMPLETE);
}
// check for invalid streamResult
if (Util.exists(statementOptions.streamResult))
{
Errors.checkArgumentValid(Util.isBoolean(statementOptions.streamResult),
ErrorCodes.ERR_CONN_FETCH_RESULT_INVALID_STREAM_RESULT);
}
// check for invalid fetchAsString
var fetchAsString = statementOptions.fetchAsString;
if (Util.exists(fetchAsString))
{
// check that the value is an array
Errors.checkArgumentValid(Util.isArray(fetchAsString),
ErrorCodes.ERR_CONN_FETCH_RESULT_INVALID_FETCH_AS_STRING);
// check that all the array elements are valid
var invalidValueIndex = NativeTypes.findInvalidValue(fetchAsString);
Errors.checkArgumentValid(invalidValueIndex === -1,
ErrorCodes.ERR_CONN_FETCH_RESULT_INVALID_FETCH_AS_STRING_VALUES,
JSON.stringify(fetchAsString[invalidValueIndex]));
}
// validate non-user-specified arguments
Errors.assertInternal(Util.isObject(services));
Errors.assertInternal(Util.isObject(connectionConfig));
// create a statement context
var statementContext = createStatementContext();
statementContext.statementId = statementOptions.statementId;
statementContext.complete = complete;
statementContext.streamResult = statementOptions.streamResult;
statementContext.fetchAsString = statementOptions.fetchAsString;
statementContext.multiResultIds = statementOptions.multiResultIds;
statementContext.multiCurId = statementOptions.multiCurId;
// set the statement type
statementContext.type = (statementContext.type == statementTypes.ROW_PRE_EXEC) ? statementTypes.ROW_POST_EXEC : statementTypes.FILE_POST_EXEC;
return new StatementPostExec(
statementOptions, statementContext, services, connectionConfig);
};
/**
* Creates a new statement context object.
*
* @returns {Object}
*/
function createStatementContext()
{
return new EventEmitter();
}
/**
* Creates a statement object that can be used to execute a PUT or GET file
* operation.
*
* @param {Object} statementOptions
* @param {Object} statementContext
* @param {Object} services
* @param {Object} connectionConfig
*
* @returns {Object}
*/
function createFileStatementPreExec (
statementOptions, statementContext, services, connectionConfig)
{
// set the statement type
statementContext.type = statementTypes.FILE_PRE_EXEC;
return new FileStatementPreExec(
statementOptions, statementContext, services, connectionConfig);
};
/**
* Creates a statement context object for pre-exec statement.
*
* @param {Object} statementOptions
* @param {Object} services
* @param {Object} connectionConfig
*
* @returns {Object}
*/
function createContextPreExec(
statementOptions, services, connectionConfig)
{
// check for missing options
Errors.checkArgumentExists(Util.exists(statementOptions),
ErrorCodes.ERR_CONN_EXEC_STMT_MISSING_OPTIONS);
// check for invalid options
Errors.checkArgumentValid(Util.isObject(statementOptions),
ErrorCodes.ERR_CONN_EXEC_STMT_INVALID_OPTIONS);
if (!Util.exists(statementOptions.requestId))
{
// check for missing sql text
Errors.checkArgumentExists(Util.exists(statementOptions.sqlText),
ErrorCodes.ERR_CONN_EXEC_STMT_MISSING_SQL_TEXT);
// check for invalid sql text
Errors.checkArgumentValid(Util.isString(statementOptions.sqlText),
ErrorCodes.ERR_CONN_EXEC_STMT_INVALID_SQL_TEXT);
}
// check for invalid complete callback
var complete = statementOptions.complete;
if (Util.exists(complete))
{
Errors.checkArgumentValid(Util.isFunction(complete),
ErrorCodes.ERR_CONN_EXEC_STMT_INVALID_COMPLETE);
}
// check for invalid streamResult
if (Util.exists(statementOptions.streamResult))
{
Errors.checkArgumentValid(Util.isBoolean(statementOptions.streamResult),
ErrorCodes.ERR_CONN_EXEC_STMT_INVALID_STREAM_RESULT);
}
// check for invalid fetchAsString
var fetchAsString = statementOptions.fetchAsString;
if (Util.exists(fetchAsString))
{
// check that the value is an array
Errors.checkArgumentValid(Util.isArray(fetchAsString),
ErrorCodes.ERR_CONN_EXEC_STMT_INVALID_FETCH_AS_STRING);
// check that all the array elements are valid
var invalidValueIndex = NativeTypes.findInvalidValue(fetchAsString);
Errors.checkArgumentValid(invalidValueIndex === -1,
ErrorCodes.ERR_CONN_EXEC_STMT_INVALID_FETCH_AS_STRING_VALUES,
JSON.stringify(fetchAsString[invalidValueIndex]));
}
// check for invalid requestId
if (Util.exists(statementOptions.requestId))
{
Errors.checkArgumentValid(Util.isString(statementOptions.requestId),
ErrorCodes.ERR_CONN_EXEC_STMT_INVALID_REQUEST_ID);
}
// if parameters are specified, make sure the specified value is an object
if (Util.exists(statementOptions.parameters))
{
Errors.checkArgumentValid(Util.isObject(statementOptions.parameters),
ErrorCodes.ERR_CONN_EXEC_STMT_INVALID_PARAMETERS);
}
// if binds are specified
var binds = statementOptions.binds;
if (Util.exists(binds))
{
// make sure the specified value is an array
Errors.checkArgumentValid(Util.isArray(binds),
ErrorCodes.ERR_CONN_EXEC_STMT_INVALID_BINDS);
// make sure everything in the binds array is stringifiable
for (var index = 0, length = binds.length; index < length; index++)
{
Errors.checkArgumentValid(JSON.stringify(binds[index]) !== undefined,
ErrorCodes.ERR_CONN_EXEC_STMT_INVALID_BIND_VALUES, binds[index]);
}
}
// if an internal option is specified, make sure it's boolean
if (Util.exists(statementOptions.internal))
{
Errors.checkArgumentValid(Util.isBoolean(statementOptions.internal),
ErrorCodes.ERR_CONN_EXEC_STMT_INVALID_INTERNAL);
}
// create a statement context
var statementContext = createStatementContext();
statementContext.sqlText = statementOptions.sqlText;
statementContext.complete = complete;
statementContext.streamResult = statementOptions.streamResult;
statementContext.fetchAsString = statementOptions.fetchAsString;
statementContext.multiResultIds = statementOptions.multiResultIds;
statementContext.multiCurId = statementOptions.multiCurId;
// if a binds array is specified, add it to the statement context
if (Util.exists(statementOptions.binds))
{
statementContext.binds = statementOptions.binds;
}
// if parameters are specified, add them to the statement context
if (Util.exists(statementOptions.parameters))
{
statementContext.parameters = statementOptions.parameters;
}
// if the internal flag is specified, add it to the statement context
if (Util.exists(statementOptions.internal))
{
statementContext.internal = statementOptions.internal;
}
// validate non-user-specified arguments
Errors.assertInternal(Util.isObject(services));
Errors.assertInternal(Util.isObject(connectionConfig));
// if we're not in qa mode, use a random uuid for the statement request id
// or use request id passed by user
if (!connectionConfig.isQaMode())
{
if (statementOptions.requestId)
{
statementContext.requestId = statementOptions.requestId;
statementContext.resubmitRequest = true;
}
else
{
statementContext.requestId = uuidv4();
}
}
else // we're in qa mode
{
// if a request id or sequence id are specified in the statement options,
// use them as is; this is to facilitate testing by making things more
// deterministic
if (Util.isString(statementOptions.requestId))
{
statementContext.requestId = statementOptions.requestId;
}
}
return statementContext;
}
/**
* Creates a new BaseStatement.
*
* @param statementOptions
* @param context
* @param services
* @param connectionConfig
* @constructor
*/
function BaseStatement(
statementOptions, context, services, connectionConfig)
{
// call super
EventEmitter.call(this);
// validate input
Errors.assertInternal(Util.isObject(statementOptions));
Errors.assertInternal(Util.isObject(context));
context.services = services;
context.connectionConfig = connectionConfig;
context.isFetchingResult = true;
// TODO: add the parameters map to the statement context
var statement = this;
/**
* Returns this statement's SQL text.
*
* @returns {String}
*/
this.getSqlText = function ()
{
return context.sqlText;
};
/**
* Returns the current status of this statement.
*
* @returns {String}
*/
this.getStatus = function ()
{
return context.isFetchingResult ? states.FETCHING : states.COMPLETE;
};
/**
* Returns the columns produced by this statement.
*
* @returns {Object[]}
*/
this.getColumns = function ()
{
return context.result ? context.result.getColumns() : undefined;
};
/**
* Given a column identifier, returns the corresponding column. The column
* identifier can be either the column name (String) or the column index
* (Number). If a column is specified and there is more than one column with
* that name, the first column with the specified name will be returned.
*
* @param {String | Number} columnIdentifier
*
* @returns {Object}
*/
this.getColumn = function (columnIdentifier)
{
return context.result ? context.result.getColumn(columnIdentifier) :
undefined;
};
/**
* Returns the number of rows returned by this statement.
*
* @returns {Number}
*/
this.getNumRows = function ()
{
return context.result ? context.result.getReturnedRows() : undefined;
};
/**
* Returns the number of rows updated by this statement.
*
* @returns {Number}
*/
this.getNumUpdatedRows = function ()
{
return context.result ? context.result.getNumUpdatedRows() : undefined;
};
/**
* Returns an object that contains information about the values of the
* current warehouse, current database, etc., when this statement finished
* executing.
*
* @returns {Object}
*/
this.getSessionState = function ()
{
return context.result ? context.result.getSessionState() : undefined;
};
/**
* Returns the request id that was used when the statement was issued.
*
* @returns {String}
*/
this.getRequestId = function ()
{
return context.requestId;
};
/**
* Returns the statement id generated by the server for this statement.
* If the statement is still executing and we don't know the statement id
* yet, this method will return undefined.
*
* @returns {String}
*/
this.getStatementId = function ()
{
return context.statementId;
};
/**
* Cancels this statement if possible.
*
* @param {Function} [callback]
*/
this.cancel = function (callback)
{
sendCancelStatement(context, statement, callback);
};
/**
* Issues a request to get the statement result again.
*
* @param {Function} callback
*/
context.refresh = function (callback)
{
// pick the appropriate function to get the result based on whether we
// have the statement id or request id (we should have at least one)
var sendRequestFn = context.statementId ?
sendRequestPostExec : sendRequestPreExec;
// the current result error might be transient,
// so issue a request to get the result again
sendRequestFn(context, function (err, body)
{
// refresh the result
context.onStatementRequestComp(err, body);
// if a callback was specified, invoke it
if (Util.isFunction(callback))
{
callback(context);
}
});
};
/**
* Called when the statement request is complete.
*
* @param err
* @param body
*/
context.onStatementRequestComp = async function (err, body)
{
// if we already have a result or a result error, we invoked the complete
// callback once, so don't invoke it again
var suppressComplete = context.result || context.resultError;
// clear the previous result error
context.resultError = null;
// if there was no error, call the success function
if (!err)
{
await context.onStatementRequestSucc(body);
}
else
{
// save the error
context.resultError = err;
// if we don't have a statement id and we got a response from GS, extract
// the statement id from the data
if (!context.statementId &&
Errors.isOperationFailedError(err) && err.data)
{
context.statementId = err.data.queryId;
}
}
// we're no longer fetching the result
context.isFetchingResult = false;
if (!suppressComplete)
{
// emit a complete event
context.emit('statement-complete', Errors.externalize(err), statement);
// if a complete function was specified, invoke it
if (Util.exists(context.complete))
{
invokeStatementComplete(statement, context);
}
}
else
{
Logger.getInstance().debug('refreshed result of statement with %s',
context.requestId ?
Util.format('request id = %s', context.requestId) :
Util.format('statement id = %s', context.statementId));
}
};
/**
* Called when the statement request is successful. Subclasses must provide
* their own implementation.
*
* @param {Object} body
*/
context.onStatementRequestSucc = function (body)
{
};
}
Util.inherits(BaseStatement, EventEmitter);
/**
* Invokes the statement complete callback.
*
* @param {Object} statement
* @param {Object} context
*/
function invokeStatementComplete(statement, context)
{
// find out if the result will be streamed;
// if a value is not specified, get it from the connection
var streamResult = context.streamResult;
if (!Util.exists(streamResult))
{
streamResult = context.connectionConfig.getStreamResult();
}
// if the result will be streamed later,
// invoke the complete callback right away
if (streamResult)
{
context.complete(Errors.externalize(context.resultError), statement);
}
else
{
process.nextTick(function ()
{
// aggregate all the rows into an array and pass this
// array to the complete callback as the last argument
var rows = [];
statement.streamRows()
.on('data', function (row)
{
rows.push(row);
})
.on('end', function ()
{
context.complete(null, statement, rows);
})
.on('error', function (err)
{
context.complete(Errors.externalize(err), statement);
});
});
}
}
/**
* Creates a new RowStatementPreExec instance.
*
* @param {Object} statementOptions
* @param {Object} context
* @param {Object} services
* @param {Object} connectionConfig
* @constructor
*/
function RowStatementPreExec(
statementOptions,
context,
services,
connectionConfig)
{
Logger.getInstance().debug('RowStatementPreExec');
// call super
BaseStatement.apply(this, arguments);
// add the result request headers to the context
context.resultRequestHeaders = buildResultRequestHeadersRow();
/**
* Called when the request to get the statement result is successful.
*
* @param {Object} body
*/
context.onStatementRequestSucc =
createOnStatementRequestSuccRow(this, context);
/**
* Fetches the rows in this statement's result and invokes the each()
* callback on each row. If start and end values are specified, the each()
* callback will only be invoked on rows in the specified range.
*
* @param {Object} options
*/
this.fetchRows = createFnFetchRows(this, context);
/**
* Streams the rows in this statement's result. If start and end values are
* specified, only rows in the specified range are streamed.
*
* @param {Object} options
*/
this.streamRows = createFnStreamRows(this, context);
// send a request to execute the statement
sendRequestPreExec(context, context.onStatementRequestComp);
}
Util.inherits(RowStatementPreExec, BaseStatement);
/**
* Creates a function that can be used by row statements to process the response
* when the request is successful.
*
* @param statement
* @param context
* @returns {Function}
*/
function createOnStatementRequestSuccRow(statement, context)
{
return function (body)
{
// if we don't already have a result
if (!context.result)
{
if (body.data.resultIds != undefined && body.data.resultIds.length > 0)
{
//multi statements
this._resultIds = body.data.resultIds.split(',');
context.isMulti = true;
context.multiResultIds = this._resultIds;
context.multiCurId = 0;
context.statementId = this._resultIds[context.multiCurId];
exports.createStatementPostExec(context, context.services, context.connectionConfig);
}
else
{
// build a result from the response
context.result = new Result(
{
response: body,
statement: statement,
services: context.services,
connectionConfig: context.connectionConfig
});
// save the statement id
context.statementId = context.result.getStatementId();
}
}
else
{
// refresh the existing result
context.result.refresh(body);
}
if(context.isMulti == null || context.isMulti == false)
{
// only update the parameters if the statement isn't a post-exec statement
if (context.type !== statementTypes.ROW_POST_EXEC || context.type !== statementTypes.FILE_POST_EXEC)
{
Parameters.update(context.result.getParametersArray());
}
}
};
}
/**
* Creates a new FileStatementPreExec instance.
*
* @param {Object} statementOptions
* @param {Object} context
* @param {Object} services
* @param {Object} connectionConfig
* @constructor
*/
function FileStatementPreExec(
statementOptions, context, services, connectionConfig)
{
// call super
BaseStatement.apply(this, arguments);
// add the result request headers to the context
context.resultRequestHeaders = buildResultRequestHeadersFile();
/**
* Called when the statement request is successful.
*
* @param {Object} body
*/
context.onStatementRequestSucc = async function (body)
{
context.fileMetadata = body;
var fta = new file_transfer_agent(context);
await fta.execute();
// build a result from the response
var result = fta.result();
// init result and meta
body.data.rowset = result.rowset;
body.data.returned = body.data.rowset.length;
body.data.rowtype = result.rowtype;
body.data.parameters = [];
context.result = new Result({
response: body,
statement: this,
services: context.services,
connectionConfig: context.connectionConfig
});
};
/**
* Streams the rows in this statement's result. If start and end values are
* specified, only rows in the specified range are streamed.
*
* @param {Object} options
*/
this.streamRows = createFnStreamRows(this, context);
this.hasNext = hasNextResult(this, context);
this.NextResult = createNextReuslt(this, context);
/**
* Returns the file metadata generated by the statement.
*
* @returns {Object}
*/
this.getFileMetadata = function ()
{
return context.fileMetadata;
};
// send a request to execute the file statement
sendRequestPreExec(context, context.onStatementRequestComp);
}
Util.inherits(FileStatementPreExec, BaseStatement);
/**
* Creates a new StatementPostExec instance.
*
* @param {Object} statementOptions
* @param {Object} context
* @param {Object} services
* @param {Object} connectionConfig
* @constructor
*/
function StatementPostExec(
statementOptions, context, services, connectionConfig)
{
// call super
BaseStatement.apply(this, arguments);
// add the result request headers to the context
context.resultRequestHeaders = buildResultRequestHeadersRow();
/**
* Called when the statement request is successful.
*
* @param {Object} body
*/
context.onStatementRequestSucc =
createOnStatementRequestSuccRow(this, context);
/**
* Fetches the rows in this statement's result and invokes the each()
* callback on each row. If startIndex and endIndex values are specified, the
* each() callback will only be invoked on rows in the requested range. The
* end() callback will be invoked when either all the requested rows have been
* successfully processed, or if an error was encountered while trying to
* fetch the requested rows.
*
* @param {Object} options
*/
this.fetchRows = createFnFetchRows(this, context);
/**
* Streams the rows in this statement's result. If start and end values are
* specified, only rows in the specified range are streamed.
*
* @param {Object} options
*/
this.streamRows = createFnStreamRows(this, context);
this.hasNext = hasNextResult(this, context);
this.NextResult = createNextReuslt(this, context);
// send a request to fetch the result
sendRequestPostExec(context, context.onStatementRequestComp);
}