UNPKG

11 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
5Object.defineProperty(exports, "__esModule", {
6 value: true
7});
8exports["default"] = exports.queryType = void 0;
9
10var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
11
12var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
13
14var _integer = require("./integer");
15
16/**
17 * Copyright (c) 2002-2019 "Neo4j,"
18 * Neo4j Sweden AB [http://neo4j.com]
19 *
20 * This file is part of Neo4j.
21 *
22 * Licensed under the Apache License, Version 2.0 (the "License");
23 * you may not use this file except in compliance with the License.
24 * You may obtain a copy of the License at
25 *
26 * http://www.apache.org/licenses/LICENSE-2.0
27 *
28 * Unless required by applicable law or agreed to in writing, software
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
33 */
34
35/**
36 * A ResultSummary instance contains structured metadata for a {@link Result}.
37 * @access public
38 */
39var ResultSummary =
40/*#__PURE__*/
41function () {
42 /**
43 * @constructor
44 * @param {string} query - The query this summary is for
45 * @param {Object} parameters - Parameters for the query
46 * @param {Object} metadata - Query metadata
47 */
48 function ResultSummary(query, parameters, metadata) {
49 (0, _classCallCheck2["default"])(this, ResultSummary);
50
51 /**
52 * The query and parameters this summary is for.
53 * @type {{text: string, parameters: Object}}
54 * @public
55 */
56 this.query = {
57 text: query,
58 parameters: parameters
59 /**
60 * The type of query executed. Can be "r" for read-only query, "rw" for read-write query,
61 * "w" for write-only query and "s" for schema-write query.
62 * String constants are available in {@link queryType} object.
63 * @type {string}
64 * @public
65 */
66
67 };
68 this.queryType = metadata.type;
69 /**
70 * Counters for operations the query triggered.
71 * @type {QueryStatistics}
72 * @public
73 */
74
75 this.counters = new QueryStatistics(metadata.stats || {}); // for backwards compatibility, remove in future version
76
77 this.updateStatistics = this.counters;
78 /**
79 * This describes how the database will execute the query.
80 * Query plan for the executed query if available, otherwise undefined.
81 * Will only be populated for queries that start with "EXPLAIN".
82 * @type {Plan}
83 */
84
85 this.plan = metadata.plan || metadata.profile ? new Plan(metadata.plan || metadata.profile) : false;
86 /**
87 * This describes how the database did execute your query. This will contain detailed information about what
88 * each step of the plan did. Profiled query plan for the executed query if available, otherwise undefined.
89 * Will only be populated for queries that start with "PROFILE".
90 * @type {ProfiledPlan}
91 * @public
92 */
93
94 this.profile = metadata.profile ? new ProfiledPlan(metadata.profile) : false;
95 /**
96 * An array of notifications that might arise when executing the query. Notifications can be warnings about
97 * problematic queries or other valuable information that can be presented in a client. Unlike failures
98 * or errors, notifications do not affect the execution of a query.
99 * @type {Array<Notification>}
100 * @public
101 */
102
103 this.notifications = this._buildNotifications(metadata.notifications);
104 /**
105 * The basic information of the server where the result is obtained from.
106 * @type {ServerInfo}
107 * @public
108 */
109
110 this.server = new ServerInfo(metadata.server);
111 /**
112 * The time it took the server to consume the result.
113 * @type {number}
114 * @public
115 */
116
117 this.resultConsumedAfter = metadata.result_consumed_after;
118 /**
119 * The time it took the server to make the result available for consumption in milliseconds.
120 * @type {number}
121 * @public
122 */
123
124 this.resultAvailableAfter = metadata.result_available_after;
125 /**
126 * The database name where this summary is obtained from.
127 * @type {{name: string}}
128 * @public
129 */
130
131 this.database = {
132 name: metadata.db || null
133 };
134 }
135
136 (0, _createClass2["default"])(ResultSummary, [{
137 key: "_buildNotifications",
138 value: function _buildNotifications(notifications) {
139 if (!notifications) {
140 return [];
141 }
142
143 return notifications.map(function (n) {
144 return new Notification(n);
145 });
146 }
147 /**
148 * Check if the result summary has a plan
149 * @return {boolean}
150 */
151
152 }, {
153 key: "hasPlan",
154 value: function hasPlan() {
155 return this.plan instanceof Plan;
156 }
157 /**
158 * Check if the result summary has a profile
159 * @return {boolean}
160 */
161
162 }, {
163 key: "hasProfile",
164 value: function hasProfile() {
165 return this.profile instanceof ProfiledPlan;
166 }
167 }]);
168 return ResultSummary;
169}();
170/**
171 * Class for execution plan received by prepending Cypher with EXPLAIN.
172 * @access public
173 */
174
175
176var Plan =
177/**
178 * Create a Plan instance
179 * @constructor
180 * @param {Object} plan - Object with plan data
181 */
182function Plan(plan) {
183 (0, _classCallCheck2["default"])(this, Plan);
184 this.operatorType = plan.operatorType;
185 this.identifiers = plan.identifiers;
186 this.arguments = plan.args;
187 this.children = plan.children ? plan.children.map(function (child) {
188 return new Plan(child);
189 }) : [];
190};
191/**
192 * Class for execution plan received by prepending Cypher with PROFILE.
193 * @access public
194 */
195
196
197var ProfiledPlan =
198/*#__PURE__*/
199function () {
200 /**
201 * Create a ProfiledPlan instance
202 * @constructor
203 * @param {Object} profile - Object with profile data
204 */
205 function ProfiledPlan(profile) {
206 (0, _classCallCheck2["default"])(this, ProfiledPlan);
207 this.operatorType = profile.operatorType;
208 this.identifiers = profile.identifiers;
209 this.arguments = profile.args;
210 this.dbHits = valueOrDefault('dbHits', profile);
211 this.rows = valueOrDefault('rows', profile);
212 this.pageCacheMisses = valueOrDefault('pageCacheMisses', profile);
213 this.pageCacheHits = valueOrDefault('pageCacheHits', profile);
214 this.pageCacheHitRatio = valueOrDefault('pageCacheHitRatio', profile);
215 this.time = valueOrDefault('time', profile);
216 this.children = profile.children ? profile.children.map(function (child) {
217 return new ProfiledPlan(child);
218 }) : [];
219 }
220
221 (0, _createClass2["default"])(ProfiledPlan, [{
222 key: "hasPageCacheStats",
223 value: function hasPageCacheStats() {
224 return this.pageCacheMisses > 0 || this.pageCacheHits > 0 || this.pageCacheHitRatio > 0;
225 }
226 }]);
227 return ProfiledPlan;
228}();
229/**
230 * Get statistical information for a {@link Result}.
231 * @access public
232 */
233
234
235var QueryStatistics =
236/*#__PURE__*/
237function () {
238 /**
239 * Structurize the statistics
240 * @constructor
241 * @param {Object} statistics - Result statistics
242 */
243 function QueryStatistics(statistics) {
244 var _this = this;
245
246 (0, _classCallCheck2["default"])(this, QueryStatistics);
247 this._stats = {
248 nodesCreated: 0,
249 nodesDeleted: 0,
250 relationshipsCreated: 0,
251 relationshipsDeleted: 0,
252 propertiesSet: 0,
253 labelsAdded: 0,
254 labelsRemoved: 0,
255 indexesAdded: 0,
256 indexesRemoved: 0,
257 constraintsAdded: 0,
258 constraintsRemoved: 0
259 };
260 this._systemUpdates = 0;
261 Object.keys(statistics).forEach(function (index) {
262 // To camelCase
263 var camelCaseIndex = index.replace(/(-\w)/g, function (m) {
264 return m[1].toUpperCase();
265 });
266
267 if (camelCaseIndex in _this._stats) {
268 _this._stats[camelCaseIndex] = intValue(statistics[index]);
269 } else if (camelCaseIndex === 'systemUpdates') {
270 _this._systemUpdates = intValue(statistics[index]);
271 }
272 });
273 this._stats = Object.freeze(this._stats);
274 }
275 /**
276 * Did the database get updated?
277 * @return {boolean}
278 */
279
280
281 (0, _createClass2["default"])(QueryStatistics, [{
282 key: "containsUpdates",
283 value: function containsUpdates() {
284 var _this2 = this;
285
286 return Object.keys(this._stats).reduce(function (last, current) {
287 return last + _this2._stats[current];
288 }, 0) > 0;
289 }
290 /**
291 * Returns the query statistics updates in a dictionary.
292 * @returns {*}
293 */
294
295 }, {
296 key: "updates",
297 value: function updates() {
298 return this._stats;
299 }
300 /**
301 * Return true if the system database get updated, otherwise false
302 * @returns {boolean} - If the system database get updated or not.
303 */
304
305 }, {
306 key: "containsSystemUpdates",
307 value: function containsSystemUpdates() {
308 return this._systemUpdates > 0;
309 }
310 /**
311 * @returns {number} - Number of system updates
312 */
313
314 }, {
315 key: "systemUpdates",
316 value: function systemUpdates() {
317 return this._systemUpdates;
318 }
319 }]);
320 return QueryStatistics;
321}();
322/**
323 * Class for Cypher notifications
324 * @access public
325 */
326
327
328var Notification =
329/*#__PURE__*/
330function () {
331 /**
332 * Create a Notification instance
333 * @constructor
334 * @param {Object} notification - Object with notification data
335 */
336 function Notification(notification) {
337 (0, _classCallCheck2["default"])(this, Notification);
338 this.code = notification.code;
339 this.title = notification.title;
340 this.description = notification.description;
341 this.severity = notification.severity;
342 this.position = Notification._constructPosition(notification.position);
343 }
344
345 (0, _createClass2["default"])(Notification, null, [{
346 key: "_constructPosition",
347 value: function _constructPosition(pos) {
348 if (!pos) {
349 return {};
350 }
351
352 return {
353 offset: intValue(pos.offset),
354 line: intValue(pos.line),
355 column: intValue(pos.column)
356 };
357 }
358 }]);
359 return Notification;
360}();
361/**
362 * Class for exposing server info from a result.
363 * @access public
364 */
365
366
367var ServerInfo =
368/**
369 * Create a ServerInfo instance
370 * @constructor
371 * @param {Object} serverMeta - Object with serverMeta data
372 */
373function ServerInfo(serverMeta) {
374 (0, _classCallCheck2["default"])(this, ServerInfo);
375
376 if (serverMeta) {
377 this.address = serverMeta.address;
378 this.version = serverMeta.version;
379 }
380};
381
382function intValue(value) {
383 return (0, _integer.isInt)(value) ? value.toInt() : value;
384}
385
386function valueOrDefault(key, values) {
387 var defaultValue = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
388
389 if (key in values) {
390 var value = values[key];
391 return (0, _integer.isInt)(value) ? value.toInt() : value;
392 } else {
393 return defaultValue;
394 }
395}
396
397var queryType = {
398 READ_ONLY: 'r',
399 READ_WRITE: 'rw',
400 WRITE_ONLY: 'w',
401 SCHEMA_WRITE: 's'
402};
403exports.queryType = queryType;
404var _default = ResultSummary;
405exports["default"] = _default;
\No newline at end of file