|
| 1 | +/** |
| 2 | + * Copyright (c) 2002-2018 "Neo4j," |
| 3 | + * Neo4j Sweden AB [http://neo4j.com] |
| 4 | + * |
| 5 | + * This file is part of Neo4j. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | +import BoltProtocolV2 from './bolt-protocol-v2'; |
| 20 | +import RequestMessage from './request-message'; |
| 21 | + |
| 22 | +export default class BoltProtocol extends BoltProtocolV2 { |
| 23 | + |
| 24 | + constructor(connection, chunker, disableLosslessIntegers) { |
| 25 | + super(connection, chunker, disableLosslessIntegers); |
| 26 | + } |
| 27 | + |
| 28 | + transformMetadata(metadata) { |
| 29 | + if (metadata.t_first) { |
| 30 | + // Bolt V3 uses shorter key 't_first' to represent 'result_available_after' |
| 31 | + // adjust the key to be the same as in Bolt V1 so that ResultSummary can retrieve the value |
| 32 | + metadata.result_available_after = metadata.t_first; |
| 33 | + delete metadata.t_first; |
| 34 | + } |
| 35 | + if (metadata.t_last) { |
| 36 | + // Bolt V3 uses shorter key 't_last' to represent 'result_consumed_after' |
| 37 | + // adjust the key to be the same as in Bolt V1 so that ResultSummary can retrieve the value |
| 38 | + metadata.result_consumed_after = metadata.t_last; |
| 39 | + delete metadata.t_last; |
| 40 | + } |
| 41 | + return metadata; |
| 42 | + } |
| 43 | + |
| 44 | + initialize(userAgent, authToken, observer) { |
| 45 | + prepareToHandleSingleResponse(observer); |
| 46 | + const message = RequestMessage.hello(userAgent, authToken); |
| 47 | + this._connection.write(message, observer, true); |
| 48 | + } |
| 49 | + |
| 50 | + beginTransaction(bookmark, observer) { |
| 51 | + prepareToHandleSingleResponse(observer); |
| 52 | + const message = RequestMessage.begin(bookmark); |
| 53 | + this._connection.write(message, observer, true); |
| 54 | + } |
| 55 | + |
| 56 | + commitTransaction(observer) { |
| 57 | + prepareToHandleSingleResponse(observer); |
| 58 | + const message = RequestMessage.commit(); |
| 59 | + this._connection.write(message, observer, true); |
| 60 | + } |
| 61 | + |
| 62 | + rollbackTransaction(observer) { |
| 63 | + prepareToHandleSingleResponse(observer); |
| 64 | + const message = RequestMessage.rollback(); |
| 65 | + this._connection.write(message, observer, true); |
| 66 | + } |
| 67 | + |
| 68 | + run(statement, parameters, observer) { |
| 69 | + const metadata = {}; |
| 70 | + const runMessage = RequestMessage.runWithMetadata(statement, parameters, metadata); |
| 71 | + const pullAllMessage = RequestMessage.pullAll(); |
| 72 | + |
| 73 | + this._connection.write(runMessage, observer, false); |
| 74 | + this._connection.write(pullAllMessage, observer, true); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +function prepareToHandleSingleResponse(observer) { |
| 79 | + if (observer && typeof observer.prepareToHandleSingleResponse === 'function') { |
| 80 | + observer.prepareToHandleSingleResponse(); |
| 81 | + } |
| 82 | +} |
0 commit comments