1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
|
8 |
|
9 | var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
|
10 |
|
11 | var _createClass2 = require('babel-runtime/helpers/createClass');
|
12 |
|
13 | var _createClass3 = _interopRequireDefault(_createClass2);
|
14 |
|
15 | var _streamObserver = require('./internal/stream-observer');
|
16 |
|
17 | var _streamObserver2 = _interopRequireDefault(_streamObserver);
|
18 |
|
19 | var _result = require('./result');
|
20 |
|
21 | var _result2 = _interopRequireDefault(_result);
|
22 |
|
23 | var _transaction = require('./transaction');
|
24 |
|
25 | var _transaction2 = _interopRequireDefault(_transaction);
|
26 |
|
27 | var _error = require('./error');
|
28 |
|
29 | var _util = require('./internal/util');
|
30 |
|
31 | var _connectionHolder = require('./internal/connection-holder');
|
32 |
|
33 | var _connectionHolder2 = _interopRequireDefault(_connectionHolder);
|
34 |
|
35 | var _driver = require('./driver');
|
36 |
|
37 | var _driver2 = _interopRequireDefault(_driver);
|
38 |
|
39 | var _transactionExecutor = require('./internal/transaction-executor');
|
40 |
|
41 | var _transactionExecutor2 = _interopRequireDefault(_transactionExecutor);
|
42 |
|
43 | var _bookmark = require('./internal/bookmark');
|
44 |
|
45 | var _bookmark2 = _interopRequireDefault(_bookmark);
|
46 |
|
47 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | var Session = function () {
|
56 |
|
57 | |
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | function Session(mode, connectionProvider, bookmark, config) {
|
65 | (0, _classCallCheck3.default)(this, Session);
|
66 |
|
67 | this._mode = mode;
|
68 | this._readConnectionHolder = new _connectionHolder2.default(_driver.READ, connectionProvider);
|
69 | this._writeConnectionHolder = new _connectionHolder2.default(_driver.WRITE, connectionProvider);
|
70 | this._open = true;
|
71 | this._hasTx = false;
|
72 | this._lastBookmark = bookmark;
|
73 | this._transactionExecutor = _createTransactionExecutor(config);
|
74 | }
|
75 |
|
76 | |
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 | (0, _createClass3.default)(Session, [{
|
87 | key: 'run',
|
88 | value: function run(statement) {
|
89 | var parameters = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
90 |
|
91 | var _validateStatementAnd = (0, _util.validateStatementAndParameters)(statement, parameters),
|
92 | query = _validateStatementAnd.query,
|
93 | params = _validateStatementAnd.params;
|
94 |
|
95 | return this._run(query, params, function (connection, streamObserver) {
|
96 | return connection.run(query, params, streamObserver);
|
97 | });
|
98 | }
|
99 | }, {
|
100 | key: '_run',
|
101 | value: function _run(statement, parameters, statementRunner) {
|
102 | var streamObserver = new _streamObserver2.default(this._onRunFailure());
|
103 | var connectionHolder = this._connectionHolderWithMode(this._mode);
|
104 | if (!this._hasTx) {
|
105 | connectionHolder.initializeConnection();
|
106 | connectionHolder.getConnection(streamObserver).then(function (connection) {
|
107 | statementRunner(connection, streamObserver);
|
108 | connection.pullAll(streamObserver);
|
109 | connection.sync();
|
110 | }).catch(function (error) {
|
111 | return streamObserver.onError(error);
|
112 | });
|
113 | } else {
|
114 | streamObserver.onError((0, _error.newError)('Statements cannot be run directly on a ' + 'session with an open transaction; either run from within the ' + 'transaction or use a different session.'));
|
115 | }
|
116 | return new _result2.default(streamObserver, statement, parameters, function () {
|
117 | return streamObserver.serverMetadata();
|
118 | }, connectionHolder);
|
119 | }
|
120 |
|
121 | |
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 | }, {
|
134 | key: 'beginTransaction',
|
135 | value: function beginTransaction(bookmarkOrBookmarks) {
|
136 | this._updateBookmark(new _bookmark2.default(bookmarkOrBookmarks));
|
137 | return this._beginTransaction(this._mode);
|
138 | }
|
139 | }, {
|
140 | key: '_beginTransaction',
|
141 | value: function _beginTransaction(accessMode) {
|
142 | var _this = this;
|
143 |
|
144 | if (this._hasTx) {
|
145 | throw (0, _error.newError)('You cannot begin a transaction on a session with an open transaction; ' + 'either run from within the transaction or use a different session.');
|
146 | }
|
147 |
|
148 | var mode = _driver2.default._validateSessionMode(accessMode);
|
149 | var connectionHolder = this._connectionHolderWithMode(mode);
|
150 | connectionHolder.initializeConnection();
|
151 | this._hasTx = true;
|
152 |
|
153 | return new _transaction2.default(connectionHolder, function () {
|
154 | _this._hasTx = false;
|
155 | }, this._onRunFailure(), this._lastBookmark, this._updateBookmark.bind(this));
|
156 | }
|
157 |
|
158 | |
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 | }, {
|
165 | key: 'lastBookmark',
|
166 | value: function lastBookmark() {
|
167 | return this._lastBookmark.maxBookmarkAsString();
|
168 | }
|
169 |
|
170 | |
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 | }, {
|
185 | key: 'readTransaction',
|
186 | value: function readTransaction(transactionWork) {
|
187 | return this._runTransaction(_driver.READ, transactionWork);
|
188 | }
|
189 |
|
190 | |
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 | }, {
|
205 | key: 'writeTransaction',
|
206 | value: function writeTransaction(transactionWork) {
|
207 | return this._runTransaction(_driver.WRITE, transactionWork);
|
208 | }
|
209 | }, {
|
210 | key: '_runTransaction',
|
211 | value: function _runTransaction(accessMode, transactionWork) {
|
212 | var _this2 = this;
|
213 |
|
214 | return this._transactionExecutor.execute(function () {
|
215 | return _this2._beginTransaction(accessMode);
|
216 | }, transactionWork);
|
217 | }
|
218 |
|
219 | |
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 | }, {
|
226 | key: '_updateBookmark',
|
227 | value: function _updateBookmark(newBookmark) {
|
228 | if (newBookmark && !newBookmark.isEmpty()) {
|
229 | this._lastBookmark = newBookmark;
|
230 | }
|
231 | }
|
232 |
|
233 | |
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 | }, {
|
240 | key: 'close',
|
241 | value: function close() {
|
242 | var _this3 = this;
|
243 |
|
244 | var callback = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () {
|
245 | return null;
|
246 | };
|
247 |
|
248 | if (this._open) {
|
249 | this._open = false;
|
250 | this._transactionExecutor.close();
|
251 | this._readConnectionHolder.close().then(function () {
|
252 | _this3._writeConnectionHolder.close().then(function () {
|
253 | callback();
|
254 | });
|
255 | });
|
256 | } else {
|
257 | callback();
|
258 | }
|
259 | }
|
260 |
|
261 |
|
262 |
|
263 | }, {
|
264 | key: '_onRunFailure',
|
265 | value: function _onRunFailure() {
|
266 | return function (err) {
|
267 | return err;
|
268 | };
|
269 | }
|
270 | }, {
|
271 | key: '_connectionHolderWithMode',
|
272 | value: function _connectionHolderWithMode(mode) {
|
273 | if (mode === _driver.READ) {
|
274 | return this._readConnectionHolder;
|
275 | } else if (mode === _driver.WRITE) {
|
276 | return this._writeConnectionHolder;
|
277 | } else {
|
278 | throw (0, _error.newError)('Unknown access mode: ' + mode);
|
279 | }
|
280 | }
|
281 | }]);
|
282 | return Session;
|
283 | }(); |
284 |
|
285 |
|
286 |
|
287 |
|
288 |
|
289 |
|
290 |
|
291 |
|
292 |
|
293 |
|
294 |
|
295 |
|
296 |
|
297 |
|
298 |
|
299 |
|
300 |
|
301 |
|
302 | function _createTransactionExecutor(config) {
|
303 | var maxRetryTimeMs = config && config.maxTransactionRetryTime ? config.maxTransactionRetryTime : null;
|
304 | return new _transactionExecutor2.default(maxRetryTimeMs);
|
305 | }
|
306 |
|
307 | exports.default = Session; |
\ | No newline at end of file |