UNPKG

5.28 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.QueryExecutor = void 0;
4const bindingutilities_1 = require("./bindingutilities");
5const querytypes_1 = require("./querytypes");
6const streamablepromises_1 = require("./streamablepromises");
7/**
8 * @internal
9 */
10class QueryExecutor {
11 /**
12 * @internal
13 */
14 constructor(cluster) {
15 this._cluster = cluster;
16 }
17 /**
18 * @internal
19 */
20 static execute(exec) {
21 const emitter = new streamablepromises_1.StreamableRowPromise((rows, meta) => {
22 return new querytypes_1.QueryResult({
23 rows: rows,
24 meta: meta,
25 });
26 });
27 exec((cppErr, resp) => {
28 const err = (0, bindingutilities_1.errorFromCpp)(cppErr);
29 if (err) {
30 emitter.emit('error', err);
31 emitter.emit('end');
32 return;
33 }
34 resp.rows.forEach((row) => {
35 emitter.emit('row', JSON.parse(row));
36 });
37 {
38 const metaData = resp.meta;
39 let warnings;
40 if (metaData.warnings) {
41 warnings = metaData.warnings.map((warningData) => new querytypes_1.QueryWarning({
42 code: warningData.code,
43 message: warningData.message,
44 }));
45 }
46 else {
47 warnings = [];
48 }
49 let metrics;
50 if (metaData.metrics) {
51 const metricsData = metaData.metrics;
52 metrics = new querytypes_1.QueryMetrics({
53 elapsedTime: metricsData.elapsed_time,
54 executionTime: metricsData.execution_time,
55 sortCount: metricsData.sort_count || 0,
56 resultCount: metricsData.result_count || 0,
57 resultSize: metricsData.result_size || 0,
58 mutationCount: metricsData.mutation_count || 0,
59 errorCount: metricsData.error_count || 0,
60 warningCount: metricsData.warning_count || 0,
61 });
62 }
63 else {
64 metrics = undefined;
65 }
66 const meta = new querytypes_1.QueryMetaData({
67 requestId: metaData.request_id,
68 clientContextId: metaData.client_context_id,
69 status: metaData.status,
70 signature: metaData.signature
71 ? JSON.parse(metaData.signature)
72 : undefined,
73 warnings: warnings,
74 metrics: metrics,
75 profile: metaData.profile ? JSON.parse(metaData.profile) : undefined,
76 });
77 emitter.emit('meta', meta);
78 }
79 emitter.emit('end');
80 return;
81 });
82 return emitter;
83 }
84 /**
85 * @internal
86 */
87 query(query, options) {
88 const timeout = options.timeout || this._cluster.queryTimeout;
89 return QueryExecutor.execute((callback) => {
90 this._cluster.conn.query({
91 statement: query,
92 client_context_id: options.clientContextId,
93 adhoc: options.adhoc === false ? false : true,
94 metrics: options.metrics || false,
95 readonly: options.readOnly || false,
96 flex_index: options.flexIndex || false,
97 preserve_expiry: options.preserveExpiry || false,
98 use_replica: options.useReplica,
99 max_parallelism: options.maxParallelism,
100 scan_cap: options.scanCap,
101 scan_wait: options.scanWait,
102 pipeline_batch: options.pipelineBatch,
103 pipeline_cap: options.pipelineCap,
104 scan_consistency: (0, bindingutilities_1.queryScanConsistencyToCpp)(options.scanConsistency),
105 mutation_state: (0, bindingutilities_1.mutationStateToCpp)(options.consistentWith).tokens,
106 timeout: timeout,
107 query_context: options.queryContext,
108 profile: (0, bindingutilities_1.queryProfileToCpp)(options.profile),
109 raw: options.raw
110 ? Object.fromEntries(Object.entries(options.raw)
111 .filter(([, v]) => v !== undefined)
112 .map(([k, v]) => [k, JSON.stringify(v)]))
113 : {},
114 positional_parameters: options.parameters && Array.isArray(options.parameters)
115 ? options.parameters.map((v) => JSON.stringify(v !== null && v !== void 0 ? v : null))
116 : [],
117 named_parameters: options.parameters && !Array.isArray(options.parameters)
118 ? Object.fromEntries(Object.entries(options.parameters)
119 .filter(([, v]) => v !== undefined)
120 .map(([k, v]) => [k, JSON.stringify(v)]))
121 : {},
122 body_str: '',
123 }, callback);
124 });
125 }
126}
127exports.QueryExecutor = QueryExecutor;