UNPKG

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