node-firebird-driver 2.3.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/dist/lib/impl/attachment.d.ts +14 -3
  2. package/dist/lib/impl/attachment.js +112 -138
  3. package/dist/lib/impl/attachment.js.map +1 -1
  4. package/dist/lib/impl/blob.d.ts +3 -1
  5. package/dist/lib/impl/blob.js +11 -25
  6. package/dist/lib/impl/blob.js.map +1 -1
  7. package/dist/lib/impl/client.d.ts +1 -0
  8. package/dist/lib/impl/client.js +24 -36
  9. package/dist/lib/impl/client.js.map +1 -1
  10. package/dist/lib/impl/events.d.ts +1 -0
  11. package/dist/lib/impl/events.js +9 -18
  12. package/dist/lib/impl/events.js.map +1 -1
  13. package/dist/lib/impl/fb-util.d.ts +34 -5
  14. package/dist/lib/impl/fb-util.js +82 -42
  15. package/dist/lib/impl/fb-util.js.map +1 -1
  16. package/dist/lib/impl/index.js +5 -1
  17. package/dist/lib/impl/index.js.map +1 -1
  18. package/dist/lib/impl/resultset.d.ts +1 -0
  19. package/dist/lib/impl/resultset.js +33 -46
  20. package/dist/lib/impl/resultset.js.map +1 -1
  21. package/dist/lib/impl/statement.d.ts +6 -0
  22. package/dist/lib/impl/statement.js +47 -57
  23. package/dist/lib/impl/statement.js.map +1 -1
  24. package/dist/lib/impl/transaction.d.ts +1 -0
  25. package/dist/lib/impl/transaction.js +20 -34
  26. package/dist/lib/impl/transaction.js.map +1 -1
  27. package/dist/lib/index.d.ts +57 -3
  28. package/dist/lib/index.js +4 -0
  29. package/dist/lib/index.js.map +1 -1
  30. package/dist/test/tests.js +385 -301
  31. package/dist/test/tests.js.map +1 -1
  32. package/package.json +3 -3
  33. package/src/lib/impl/attachment.ts +32 -9
  34. package/src/lib/impl/blob.ts +6 -1
  35. package/src/lib/impl/client.ts +5 -1
  36. package/src/lib/impl/events.ts +5 -1
  37. package/src/lib/impl/fb-util.ts +55 -0
  38. package/src/lib/impl/resultset.ts +5 -1
  39. package/src/lib/impl/statement.ts +21 -4
  40. package/src/lib/impl/transaction.ts +5 -1
  41. package/src/lib/index.ts +77 -3
  42. package/src/test/tests.ts +123 -3
@@ -1,13 +1,4 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  exports.runCommonTests = void 0;
13
4
  const lib_1 = require("../lib");
@@ -51,15 +42,15 @@ function runCommonTests(client) {
51
42
  testConfig.host == '127.0.0.1';
52
43
  }
53
44
  function getTempFile(name) {
54
- var _a;
55
45
  const database = `${testConfig.tmpDir}/${name}`;
56
- return ((_a = testConfig.host) !== null && _a !== void 0 ? _a : '') +
46
+ return (testConfig.host ?? '') +
57
47
  (testConfig.host && testConfig.port ? `/${testConfig.port}` : '') +
58
48
  (testConfig.host ? ':' : '') +
59
49
  database;
60
50
  }
61
51
  jest.setTimeout(10000);
62
52
  beforeAll(() => {
53
+ expect(client.isValid).toBeTruthy();
63
54
  if (isLocal() && !testConfig.tmpDir) {
64
55
  testConfig.tmpDir = tmp.mkdirSync().path.toString();
65
56
  // Important for MacOS tests with non-embedded server.
@@ -69,50 +60,68 @@ function runCommonTests(client) {
69
60
  password: testConfig.password,
70
61
  username: testConfig.username
71
62
  };
72
- client.defaultCreateDatabaseOptions = Object.assign({ forcedWrite: false }, defaultOptions);
73
- client.defaultConnectOptions = Object.assign({}, defaultOptions);
63
+ client.defaultCreateDatabaseOptions = {
64
+ forcedWrite: false,
65
+ ...defaultOptions
66
+ };
67
+ client.defaultConnectOptions = {
68
+ ...defaultOptions
69
+ };
74
70
  });
75
- afterAll(() => __awaiter(this, void 0, void 0, function* () {
76
- yield client.dispose();
71
+ afterAll(async () => {
72
+ await client.dispose();
73
+ expect(client.isValid).toBeFalsy();
77
74
  if (isLocal())
78
75
  fs.rmdirSync(testConfig.tmpDir);
79
- }));
76
+ });
80
77
  describe('Client', () => {
81
- test('#createDatabase()', () => __awaiter(this, void 0, void 0, function* () {
82
- const attachment = yield client.createDatabase(getTempFile('Client-createDatabase.fdb'));
83
- yield attachment.dropDatabase();
84
- }));
85
- test('#connect()', () => __awaiter(this, void 0, void 0, function* () {
78
+ test('#createDatabase()', async () => {
79
+ const attachment = await client.createDatabase(getTempFile('Client-createDatabase.fdb'));
80
+ await attachment.dropDatabase();
81
+ });
82
+ test('#connect()', async () => {
86
83
  const filename = getTempFile('Client-connect.fdb');
87
- const attachment1 = yield client.createDatabase(filename);
88
- const attachment2 = yield client.connect(filename);
89
- yield attachment2.disconnect();
90
- yield attachment1.dropDatabase();
91
- }));
84
+ const attachment1 = await client.createDatabase(filename);
85
+ const attachment2 = await client.connect(filename);
86
+ expect(attachment1.isValid).toBeTruthy();
87
+ expect(attachment2.isValid).toBeTruthy();
88
+ await attachment2.disconnect();
89
+ await attachment1.dropDatabase();
90
+ expect(attachment1.isValid).toBeFalsy();
91
+ expect(attachment2.isValid).toBeFalsy();
92
+ });
92
93
  });
93
94
  describe('Attachment', () => {
94
- test('#startTransaction()', () => __awaiter(this, void 0, void 0, function* () {
95
- const attachment = yield client.createDatabase(getTempFile('Attachment-startTransaction.fdb'));
95
+ test('#startTransaction()', async () => {
96
+ const attachment = await client.createDatabase(getTempFile('Attachment-startTransaction.fdb'));
96
97
  const isolationQuery = 'select rdb$get_context(\'SYSTEM\', \'ISOLATION_LEVEL\') from rdb$database';
97
- const transaction1 = yield attachment.startTransaction();
98
- expect((yield attachment.executeReturning(transaction1, isolationQuery))[0]).toBe('SNAPSHOT');
99
- yield transaction1.commit();
100
- const transaction2 = yield attachment.startTransaction({ isolation: lib_1.TransactionIsolation.READ_COMMITTED });
101
- expect((yield attachment.executeReturning(transaction2, isolationQuery))[0]).toBe('READ COMMITTED');
102
- yield transaction2.commit();
103
- const transaction3 = yield attachment.startTransaction({ isolation: lib_1.TransactionIsolation.CONSISTENCY });
104
- expect((yield attachment.executeReturning(transaction3, isolationQuery))[0]).toBe('CONSISTENCY');
105
- yield transaction3.commit();
106
- yield attachment.dropDatabase();
107
- }));
108
- test('#prepare()', () => __awaiter(this, void 0, void 0, function* () {
109
- const attachment = yield client.createDatabase(getTempFile('Attachment-prepare.fdb'));
110
- const transaction = yield attachment.startTransaction();
111
- const statement = yield attachment.prepare(transaction, 'create table t1 (n1 integer)');
112
- yield statement.dispose();
98
+ const transaction1 = await attachment.startTransaction();
99
+ expect(transaction1.isValid).toBeTruthy();
100
+ expect((await attachment.executeSingleton(transaction1, isolationQuery))[0]).toBe('SNAPSHOT');
101
+ await transaction1.commit();
102
+ expect(transaction1.isValid).toBeFalsy();
103
+ const transaction2 = await attachment.startTransaction({ isolation: lib_1.TransactionIsolation.READ_COMMITTED });
104
+ expect(transaction2.isValid).toBeTruthy();
105
+ expect((await attachment.executeSingleton(transaction2, isolationQuery))[0]).toBe('READ COMMITTED');
106
+ await transaction2.commit();
107
+ expect(transaction2.isValid).toBeFalsy();
108
+ const transaction3 = await attachment.startTransaction({ isolation: lib_1.TransactionIsolation.CONSISTENCY });
109
+ expect(transaction3.isValid).toBeTruthy();
110
+ expect((await attachment.executeSingleton(transaction3, isolationQuery))[0]).toBe('CONSISTENCY');
111
+ await transaction3.commit();
112
+ expect(transaction3.isValid).toBeFalsy();
113
+ await attachment.dropDatabase();
114
+ });
115
+ test('#prepare()', async () => {
116
+ const attachment = await client.createDatabase(getTempFile('Attachment-prepare.fdb'));
117
+ const transaction = await attachment.startTransaction();
118
+ const statement = await attachment.prepare(transaction, 'create table t1 (n1 integer)');
119
+ expect(statement.isValid).toBeTruthy();
120
+ await statement.dispose();
121
+ expect(statement.isValid).toBeFalsy();
113
122
  let error;
114
123
  try {
115
- yield attachment.prepare(transaction, 'create select t1 (n1 integer)');
124
+ await attachment.prepare(transaction, 'create select t1 (n1 integer)');
116
125
  }
117
126
  catch (e) {
118
127
  error = e;
@@ -122,52 +131,75 @@ function runCommonTests(client) {
122
131
  '-select');
123
132
  }
124
133
  expect(error).toBeTruthy();
125
- yield transaction.commit();
126
- yield attachment.dropDatabase();
127
- }));
134
+ await transaction.commit();
135
+ await attachment.dropDatabase();
136
+ });
128
137
  //// TODO: #executeTransaction
129
- test('#execute()', () => __awaiter(this, void 0, void 0, function* () {
130
- const attachment = yield client.createDatabase(getTempFile('Attachment-execute.fdb'));
131
- const transaction = yield attachment.startTransaction();
132
- yield attachment.execute(transaction, 'create table t1 (n1 integer)');
133
- yield transaction.commitRetaining();
134
- yield attachment.execute(transaction, 'insert into t1 (n1) values (1)');
135
- yield transaction.commit();
136
- yield attachment.dropDatabase();
137
- }));
138
- test('#executeQuery()', () => __awaiter(this, void 0, void 0, function* () {
139
- const attachment = yield client.createDatabase(getTempFile('Attachment-executeQuery.fdb'));
140
- const transaction = yield attachment.startTransaction();
141
- yield attachment.execute(transaction, 'create table t1 (n1 integer)');
142
- yield transaction.commitRetaining();
143
- const resultSet = yield attachment.executeQuery(transaction, 'select n1 from t1');
144
- yield resultSet.close();
145
- yield transaction.commit();
146
- yield attachment.dropDatabase();
147
- }));
148
- test('#executeReturning()', () => __awaiter(this, void 0, void 0, function* () {
149
- const attachment = yield client.createDatabase(getTempFile('Attachment-executeReturning.fdb'));
150
- const transaction = yield attachment.startTransaction();
151
- yield attachment.execute(transaction, 'create table t1 (n1 integer)');
152
- yield transaction.commitRetaining();
153
- const result = yield attachment.executeReturning(transaction, 'insert into t1 values (11) returning n1');
138
+ test('#execute()', async () => {
139
+ const attachment = await client.createDatabase(getTempFile('Attachment-execute.fdb'));
140
+ const transaction = await attachment.startTransaction();
141
+ await attachment.execute(transaction, 'create table t1 (n1 integer)');
142
+ await transaction.commitRetaining();
143
+ await attachment.execute(transaction, 'insert into t1 (n1) values (1)');
144
+ await transaction.commit();
145
+ await attachment.dropDatabase();
146
+ });
147
+ test('#executeQuery()', async () => {
148
+ const attachment = await client.createDatabase(getTempFile('Attachment-executeQuery.fdb'));
149
+ const transaction = await attachment.startTransaction();
150
+ await attachment.execute(transaction, 'create table t1 (n1 integer)');
151
+ await transaction.commitRetaining();
152
+ const resultSet = await attachment.executeQuery(transaction, 'select n1 from t1');
153
+ expect(resultSet.isValid).toBeTruthy();
154
+ await resultSet.close();
155
+ expect(resultSet.isValid).toBeFalsy();
156
+ await transaction.commit();
157
+ await attachment.dropDatabase();
158
+ });
159
+ test('#executeSingleton()', async () => {
160
+ const attachment = await client.createDatabase(getTempFile('Attachment-executeSingleton.fdb'));
161
+ const transaction = await attachment.startTransaction();
162
+ await attachment.execute(transaction, 'create table t1 (n1 integer)');
163
+ await transaction.commitRetaining();
164
+ const result = await attachment.executeSingleton(transaction, 'insert into t1 values (11) returning n1');
165
+ expect(result.length).toBe(1);
166
+ expect(result[0]).toBe(11);
167
+ await transaction.commit();
168
+ await attachment.dropDatabase();
169
+ });
170
+ test('#executeSingletonAsObject()', async () => {
171
+ const attachment = await client.createDatabase(getTempFile('Attachment-executeSingletonAsObject.fdb'));
172
+ const transaction = await attachment.startTransaction();
173
+ await attachment.execute(transaction, 'create table t1 (n1 integer)');
174
+ await transaction.commitRetaining();
175
+ const output = await attachment.executeSingletonAsObject(transaction, 'insert into t1 values (11) returning n1');
176
+ expect(output.N1).toBe(11);
177
+ await transaction.commit();
178
+ await attachment.dropDatabase();
179
+ });
180
+ test('#executeReturning()', async () => {
181
+ const attachment = await client.createDatabase(getTempFile('Attachment-executeReturning.fdb'));
182
+ const transaction = await attachment.startTransaction();
183
+ await attachment.execute(transaction, 'create table t1 (n1 integer)');
184
+ await transaction.commitRetaining();
185
+ const result = await attachment.executeReturning(transaction, 'insert into t1 values (11) returning n1');
154
186
  expect(result.length).toBe(1);
155
187
  expect(result[0]).toBe(11);
156
- yield transaction.commit();
157
- yield attachment.dropDatabase();
158
- }));
159
- test('#executeReturningAsObject()', () => __awaiter(this, void 0, void 0, function* () {
160
- const attachment = yield client.createDatabase(getTempFile('Attachment-executeReturningAsObject.fdb'));
161
- const transaction = yield attachment.startTransaction();
162
- yield attachment.execute(transaction, 'create table t1 (n1 integer)');
163
- yield transaction.commitRetaining();
164
- const output = yield attachment.executeReturningAsObject(transaction, 'insert into t1 values (11) returning n1');
188
+ await transaction.commit();
189
+ await attachment.dropDatabase();
190
+ });
191
+ test('#executeReturningAsObject()', async () => {
192
+ const attachment = await client.createDatabase(getTempFile('Attachment-executeReturningAsObject.fdb'));
193
+ const transaction = await attachment.startTransaction();
194
+ await attachment.execute(transaction, 'create table t1 (n1 integer)');
195
+ await transaction.commitRetaining();
196
+ const output = await attachment.executeReturningAsObject(transaction, 'insert into t1 values (11) returning n1');
165
197
  expect(output.N1).toBe(11);
166
- yield transaction.commit();
167
- yield attachment.dropDatabase();
168
- }));
169
- test('#queueEvents()', () => __awaiter(this, void 0, void 0, function* () {
170
- const attachment = yield client.createDatabase(getTempFile('Attachment-queueEvents.fdb'));
198
+ await transaction.commit();
199
+ await attachment.dropDatabase();
200
+ });
201
+ test('#queueEvents()', async () => {
202
+ const attachment = await client.createDatabase(getTempFile('Attachment-queueEvents.fdb'));
171
203
  const eventNames = [
172
204
  ['EVENT1', 16],
173
205
  ['EVENT2', 8]
@@ -180,10 +212,10 @@ function runCommonTests(client) {
180
212
  count: 0,
181
213
  promise: new Promise(resolve => resolver = resolve)
182
214
  };
183
- return Object.assign(Object.assign({}, obj), { resolver });
215
+ return { ...obj, resolver };
184
216
  });
185
217
  const eventsMap = new Map(eventsObj.map(ev => [ev.name, ev]));
186
- const eventHandler = (counters) => __awaiter(this, void 0, void 0, function* () {
218
+ const eventHandler = async (counters) => {
187
219
  counters.forEach(([name, count]) => {
188
220
  const obj = eventsMap.get(name);
189
221
  const newCount = obj.count + count;
@@ -193,18 +225,19 @@ function runCommonTests(client) {
193
225
  });
194
226
  if (Array.from(eventsMap.values()).every(obj => obj.count >= obj.expected)) {
195
227
  if (events) {
196
- yield events.cancel();
228
+ await events.cancel();
229
+ expect(events.isValid).toBeFalsy();
197
230
  events = null;
198
231
  }
199
232
  }
200
- });
201
- let events = yield attachment.queueEvents(Array.from(eventsMap.keys()), eventHandler);
202
- const transaction = yield attachment.startTransaction();
233
+ };
234
+ let events = await attachment.queueEvents(Array.from(eventsMap.keys()), eventHandler);
235
+ const transaction = await attachment.startTransaction();
203
236
  try {
204
237
  // Iterate more times than the neccessary so that
205
238
  // eventHandler may have a chance to cancel the events.
206
239
  for (let i = 0; i < 20; ++i) {
207
- yield attachment.execute(transaction, `
240
+ await attachment.execute(transaction, `
208
241
  execute block as
209
242
  begin
210
243
  post_event 'EVENT1';
@@ -215,149 +248,167 @@ function runCommonTests(client) {
215
248
  `);
216
249
  // Commit retaining to test internal event rescheduling
217
250
  // after each handler dispatch.
218
- yield transaction.commitRetaining();
251
+ await transaction.commitRetaining();
252
+ expect(transaction.isValid).toBeTruthy();
219
253
  }
220
254
  }
221
255
  finally {
222
- yield transaction.commit();
256
+ await transaction.commit();
257
+ expect(transaction.isValid).toBeFalsy();
223
258
  }
224
- yield Promise.all(eventsObj.map(ev => ev.promise));
259
+ await Promise.all(eventsObj.map(ev => ev.promise));
225
260
  if (events)
226
- yield events.cancel();
261
+ await events.cancel();
227
262
  eventsObj.forEach(ev => expect(ev.count).toBeGreaterThanOrEqual(ev.expected));
228
- yield attachment.dropDatabase();
229
- }));
263
+ await attachment.dropDatabase();
264
+ });
230
265
  });
231
266
  describe('Transaction', () => {
232
- test('#commit()', () => __awaiter(this, void 0, void 0, function* () {
233
- const attachment = yield client.createDatabase(getTempFile('Transaction-commit.fdb'));
234
- const transaction = yield attachment.startTransaction();
235
- yield transaction.commit();
236
- yield attachment.dropDatabase();
237
- }));
238
- test('#commitRetaining()', () => __awaiter(this, void 0, void 0, function* () {
239
- const attachment = yield client.createDatabase(getTempFile('Transaction-commitRetaining.fdb'));
240
- const transaction = yield attachment.startTransaction();
241
- yield transaction.commitRetaining();
242
- yield transaction.commit();
243
- yield attachment.dropDatabase();
244
- }));
245
- test('#rollback()', () => __awaiter(this, void 0, void 0, function* () {
246
- const attachment = yield client.createDatabase(getTempFile('Transaction-rollback.fdb'));
247
- const transaction = yield attachment.startTransaction();
248
- yield transaction.rollback();
249
- yield attachment.dropDatabase();
250
- }));
251
- test('#rollbackRetaining()', () => __awaiter(this, void 0, void 0, function* () {
252
- const attachment = yield client.createDatabase(getTempFile('Transaction-rollbackRetaining.fdb'));
253
- const transaction = yield attachment.startTransaction();
254
- yield transaction.rollbackRetaining();
255
- yield transaction.rollback();
256
- yield attachment.dropDatabase();
257
- }));
258
- test('transaction left opened', () => __awaiter(this, void 0, void 0, function* () {
259
- const attachment = yield client.createDatabase(getTempFile('Transaction-left-opened.fdb'));
260
- yield attachment.startTransaction();
261
- yield attachment.dropDatabase();
262
- }));
267
+ test('#commit()', async () => {
268
+ const attachment = await client.createDatabase(getTempFile('Transaction-commit.fdb'));
269
+ const transaction = await attachment.startTransaction();
270
+ await transaction.commit();
271
+ await attachment.dropDatabase();
272
+ });
273
+ test('#commitRetaining()', async () => {
274
+ const attachment = await client.createDatabase(getTempFile('Transaction-commitRetaining.fdb'));
275
+ const transaction = await attachment.startTransaction();
276
+ await transaction.commitRetaining();
277
+ await transaction.commit();
278
+ await attachment.dropDatabase();
279
+ });
280
+ test('#rollback()', async () => {
281
+ const attachment = await client.createDatabase(getTempFile('Transaction-rollback.fdb'));
282
+ const transaction = await attachment.startTransaction();
283
+ await transaction.rollback();
284
+ await attachment.dropDatabase();
285
+ });
286
+ test('#rollbackRetaining()', async () => {
287
+ const attachment = await client.createDatabase(getTempFile('Transaction-rollbackRetaining.fdb'));
288
+ const transaction = await attachment.startTransaction();
289
+ await transaction.rollbackRetaining();
290
+ await transaction.rollback();
291
+ await attachment.dropDatabase();
292
+ });
293
+ test('transaction left opened', async () => {
294
+ const attachment = await client.createDatabase(getTempFile('Transaction-left-opened.fdb'));
295
+ await attachment.startTransaction();
296
+ await attachment.dropDatabase();
297
+ });
263
298
  });
264
299
  describe('Statement', () => {
265
- test('#execute()', () => __awaiter(this, void 0, void 0, function* () {
266
- const attachment = yield client.createDatabase(getTempFile('Statement-execute.fdb'));
267
- const transaction = yield attachment.startTransaction();
268
- const statement1 = yield attachment.prepare(transaction, 'create table t1 (n1 integer)');
269
- yield statement1.execute(transaction);
270
- yield statement1.dispose();
271
- yield transaction.commitRetaining();
272
- const statement2 = yield attachment.prepare(transaction, 'insert into t1 (n1) values (?)');
273
- yield statement2.execute(transaction, [1]);
274
- yield statement2.execute(transaction, [null]);
275
- yield statement2.execute(transaction, [10]);
276
- yield statement2.execute(transaction, [100]);
277
- yield statement2.dispose();
278
- const rs = yield attachment.executeQuery(transaction, `select sum(n1) || ', ' || count(n1) || ', ' || count(*) ret from t1`);
279
- const ret = yield rs.fetchAsObject();
280
- yield rs.close();
300
+ test('#execute()', async () => {
301
+ const attachment = await client.createDatabase(getTempFile('Statement-execute.fdb'));
302
+ const transaction = await attachment.startTransaction();
303
+ const statement1 = await attachment.prepare(transaction, 'create table t1 (n1 integer)');
304
+ await statement1.execute(transaction);
305
+ await statement1.dispose();
306
+ await transaction.commitRetaining();
307
+ const statement2 = await attachment.prepare(transaction, 'insert into t1 (n1) values (?)');
308
+ await statement2.execute(transaction, [1]);
309
+ await statement2.execute(transaction, [null]);
310
+ await statement2.execute(transaction, [10]);
311
+ await statement2.execute(transaction, [100]);
312
+ expect(statement2.isValid).toBeTruthy();
313
+ await statement2.dispose();
314
+ expect(statement2.isValid).toBeFalsy();
315
+ const rs = await attachment.executeQuery(transaction, `select sum(n1) || ', ' || count(n1) || ', ' || count(*) ret from t1`);
316
+ const ret = await rs.fetchAsObject();
317
+ await rs.close();
281
318
  expect(ret[0].RET).toStrictEqual('111, 3, 4');
282
- yield transaction.commit();
283
- yield attachment.dropDatabase();
284
- }));
285
- test('#executeQuery()', () => __awaiter(this, void 0, void 0, function* () {
286
- const attachment = yield client.createDatabase(getTempFile('Statement-executeQuery.fdb'));
287
- const transaction = yield attachment.startTransaction();
288
- const statement1 = yield attachment.prepare(transaction, 'create table t1 (n1 integer)');
289
- yield statement1.execute(transaction);
290
- yield statement1.dispose();
291
- yield transaction.commitRetaining();
292
- const statement2 = yield attachment.prepare(transaction, 'select n1 from t1');
293
- const resultSet2 = yield statement2.executeQuery(transaction);
294
- yield resultSet2.close();
295
- yield statement2.dispose();
296
- yield transaction.commit();
297
- yield attachment.dropDatabase();
298
- }));
299
- test('#executeReturning()', () => __awaiter(this, void 0, void 0, function* () {
300
- const attachment = yield client.createDatabase(getTempFile('Attachment-executeReturning.fdb'));
301
- const transaction = yield attachment.startTransaction();
302
- yield attachment.execute(transaction, 'create table t1 (n1 integer)');
303
- yield transaction.commitRetaining();
304
- const statement = yield attachment.prepare(transaction, 'insert into t1 values (11) returning n1, n1 * 2');
305
- const result = yield statement.executeReturning(transaction);
319
+ await transaction.commit();
320
+ await attachment.dropDatabase();
321
+ });
322
+ test('#executeQuery()', async () => {
323
+ const attachment = await client.createDatabase(getTempFile('Statement-executeQuery.fdb'));
324
+ const transaction = await attachment.startTransaction();
325
+ const statement1 = await attachment.prepare(transaction, 'create table t1 (n1 integer)');
326
+ await statement1.execute(transaction);
327
+ await statement1.dispose();
328
+ await transaction.commitRetaining();
329
+ const statement2 = await attachment.prepare(transaction, 'select n1 from t1');
330
+ const resultSet2 = await statement2.executeQuery(transaction);
331
+ await resultSet2.close();
332
+ await statement2.dispose();
333
+ await transaction.commit();
334
+ await attachment.dropDatabase();
335
+ });
336
+ test('#executeSingleton()', async () => {
337
+ const attachment = await client.createDatabase(getTempFile('Attachment-executeSingleton.fdb'));
338
+ const transaction = await attachment.startTransaction();
339
+ await attachment.execute(transaction, 'create table t1 (n1 integer)');
340
+ await transaction.commitRetaining();
341
+ const statement = await attachment.prepare(transaction, 'insert into t1 values (11) returning n1, n1 * 2');
342
+ const result = await statement.executeSingleton(transaction);
343
+ expect(result.length).toBe(2);
344
+ expect(result[0]).toBe(11);
345
+ expect(result[1]).toBe(11 * 2);
346
+ await statement.dispose();
347
+ await transaction.commit();
348
+ await attachment.dropDatabase();
349
+ });
350
+ test('#executeReturning()', async () => {
351
+ const attachment = await client.createDatabase(getTempFile('Attachment-executeReturning.fdb'));
352
+ const transaction = await attachment.startTransaction();
353
+ await attachment.execute(transaction, 'create table t1 (n1 integer)');
354
+ await transaction.commitRetaining();
355
+ const statement = await attachment.prepare(transaction, 'insert into t1 values (11) returning n1, n1 * 2');
356
+ const result = await statement.executeReturning(transaction);
306
357
  expect(result.length).toBe(2);
307
358
  expect(result[0]).toBe(11);
308
359
  expect(result[1]).toBe(11 * 2);
309
- yield statement.dispose();
310
- yield transaction.commit();
311
- yield attachment.dropDatabase();
312
- }));
313
- test('#columnLabels()', () => __awaiter(this, void 0, void 0, function* () {
314
- const attachment = yield client.createDatabase(getTempFile('Statement-columnLabels.fdb'));
315
- const transaction = yield attachment.startTransaction();
316
- const statement1 = yield attachment.prepare(transaction, 'create table t1 (n1 integer)');
317
- expect(yield statement1.columnLabels).toStrictEqual([]);
318
- yield statement1.execute(transaction);
319
- yield statement1.dispose();
320
- yield transaction.commitRetaining();
321
- const statement2 = yield attachment.prepare(transaction, 'select n1, n1 x from t1');
322
- expect(yield statement2.columnLabels).toStrictEqual(['N1', 'X']);
323
- yield statement2.dispose();
324
- yield transaction.commit();
325
- yield attachment.dropDatabase();
326
- }));
327
- test('#hasResultSet()', () => __awaiter(this, void 0, void 0, function* () {
328
- const attachment = yield client.createDatabase(getTempFile('Statement-hasResultSet.fdb'));
329
- const transaction = yield attachment.startTransaction();
330
- const statement1 = yield attachment.prepare(transaction, 'create table t1 (n1 integer)');
360
+ await statement.dispose();
361
+ await transaction.commit();
362
+ await attachment.dropDatabase();
363
+ });
364
+ test('#columnLabels()', async () => {
365
+ const attachment = await client.createDatabase(getTempFile('Statement-columnLabels.fdb'));
366
+ const transaction = await attachment.startTransaction();
367
+ const statement1 = await attachment.prepare(transaction, 'create table t1 (n1 integer)');
368
+ expect(await statement1.columnLabels).toStrictEqual([]);
369
+ await statement1.execute(transaction);
370
+ await statement1.dispose();
371
+ await transaction.commitRetaining();
372
+ const statement2 = await attachment.prepare(transaction, 'select n1, n1 x from t1');
373
+ expect(await statement2.columnLabels).toStrictEqual(['N1', 'X']);
374
+ await statement2.dispose();
375
+ await transaction.commit();
376
+ await attachment.dropDatabase();
377
+ });
378
+ test('#hasResultSet()', async () => {
379
+ const attachment = await client.createDatabase(getTempFile('Statement-hasResultSet.fdb'));
380
+ const transaction = await attachment.startTransaction();
381
+ const statement1 = await attachment.prepare(transaction, 'create table t1 (n1 integer)');
331
382
  expect(statement1.hasResultSet).toBe(false);
332
- yield statement1.execute(transaction);
333
- yield statement1.dispose();
334
- yield transaction.commitRetaining();
335
- const statement2 = yield attachment.prepare(transaction, 'insert into t1 values (1)');
383
+ await statement1.execute(transaction);
384
+ await statement1.dispose();
385
+ await transaction.commitRetaining();
386
+ const statement2 = await attachment.prepare(transaction, 'insert into t1 values (1)');
336
387
  expect(statement2.hasResultSet).toBe(false);
337
- yield statement2.dispose();
338
- const statement3 = yield attachment.prepare(transaction, 'insert into t1 values (1) returning *');
388
+ await statement2.dispose();
389
+ const statement3 = await attachment.prepare(transaction, 'insert into t1 values (1) returning *');
339
390
  expect(statement3.hasResultSet).toBe(false);
340
- yield statement3.dispose();
341
- const statement4 = yield attachment.prepare(transaction, 'execute block as begin end');
391
+ await statement3.dispose();
392
+ const statement4 = await attachment.prepare(transaction, 'execute block as begin end');
342
393
  expect(statement4.hasResultSet).toBe(false);
343
- yield statement4.dispose();
344
- const statement5 = yield attachment.prepare(transaction, 'select * from t1');
394
+ await statement4.dispose();
395
+ const statement5 = await attachment.prepare(transaction, 'select * from t1');
345
396
  expect(statement5.hasResultSet).toBe(true);
346
- yield statement5.dispose();
347
- const statement6 = yield attachment.prepare(transaction, 'execute block returns (n integer) as begin suspend; end');
397
+ await statement5.dispose();
398
+ const statement6 = await attachment.prepare(transaction, 'execute block returns (n integer) as begin suspend; end');
348
399
  expect(statement6.hasResultSet).toBe(true);
349
- yield statement6.dispose();
350
- const statement7 = yield attachment.prepare(transaction, 'execute block returns (n integer) as begin end');
400
+ await statement6.dispose();
401
+ const statement7 = await attachment.prepare(transaction, 'execute block returns (n integer) as begin end');
351
402
  expect(statement7.hasResultSet).toBe(true);
352
- yield statement7.dispose();
353
- yield transaction.commit();
354
- yield attachment.dropDatabase();
355
- }));
403
+ await statement7.dispose();
404
+ await transaction.commit();
405
+ await attachment.dropDatabase();
406
+ });
356
407
  });
357
408
  describe('ResultSet', () => {
358
- test('#fetch()', () => __awaiter(this, void 0, void 0, function* () {
359
- const attachment = yield client.createDatabase(getTempFile('ResultSet-fetch.fdb'));
360
- let transaction = yield attachment.startTransaction();
409
+ test('#fetch()', async () => {
410
+ const attachment = await client.createDatabase(getTempFile('ResultSet-fetch.fdb'));
411
+ let transaction = await attachment.startTransaction();
361
412
  const blobBuffer = Buffer.alloc(11, '12345678á9');
362
413
  const fields = [
363
414
  { name: 'x_short', type: 'numeric(2)', valToStr: (v) => v },
@@ -403,20 +454,23 @@ function runCommonTests(client) {
403
454
  { name: 'x_blob1', type: 'blob', valToStr: (v) => `'${v.toString()}'` },
404
455
  { name: 'x_blob2', type: 'blob', valToStr: () => `'${blobBuffer.toString()}'` }
405
456
  ];
406
- const statement1 = yield attachment.prepare(transaction, `create table t1 (${fields.map(f => `${f.name} ${f.type}`).join(', ')})`);
407
- yield statement1.execute(transaction);
408
- yield statement1.dispose();
409
- yield transaction.commitRetaining();
457
+ const statement1 = await attachment.prepare(transaction, `create table t1 (${fields.map(f => `${f.name} ${f.type}`).join(', ')})`);
458
+ await statement1.execute(transaction);
459
+ await statement1.dispose();
460
+ await transaction.commitRetaining();
410
461
  const recordCount = 5;
411
462
  let parameters;
412
463
  { // scope
413
- const statement2a = yield attachment.prepare(transaction, `insert into t1 (${fields.map(f => f.name).join(', ')}) values (${fields.map(() => '?').join(', ')})`);
464
+ const statement2a = await attachment.prepare(transaction, `insert into t1 (${fields.map(f => f.name).join(', ')}) values (${fields.map(() => '?').join(', ')})`);
414
465
  // Test execution in a new transaction, after the one used in prepare was committed.
415
- yield transaction.commit();
416
- transaction = yield attachment.startTransaction();
417
- const blob = yield attachment.createBlob(transaction);
418
- yield blob.write(blobBuffer);
419
- yield blob.close();
466
+ await transaction.commit();
467
+ transaction = await attachment.startTransaction();
468
+ const blob = await attachment.createBlob(transaction);
469
+ expect(blob.isValid).toBeTruthy();
470
+ await blob.write(blobBuffer);
471
+ expect(blob.isValid).toBeTruthy();
472
+ await blob.close();
473
+ expect(blob.isValid).toBeFalsy();
420
474
  parameters = [
421
475
  -1,
422
476
  -2,
@@ -452,18 +506,18 @@ function runCommonTests(client) {
452
506
  blob
453
507
  ];
454
508
  for (let i = 0; i < recordCount; ++i)
455
- yield statement2a.execute(transaction, parameters);
456
- yield statement2a.dispose();
509
+ await statement2a.execute(transaction, parameters);
510
+ await statement2a.dispose();
457
511
  }
458
512
  { // scope
459
- const statement2b = yield attachment.prepare(transaction, `insert into t1 (${fields.map(f => f.name).join(', ')}) ` +
513
+ const statement2b = await attachment.prepare(transaction, `insert into t1 (${fields.map(f => f.name).join(', ')}) ` +
460
514
  `values (${parameters.map((val, index) => fields[index].valToStr(val)).join(', ')})`);
461
515
  for (let i = 0; i < recordCount; ++i)
462
- yield statement2b.execute(transaction);
463
- yield statement2b.dispose();
516
+ await statement2b.execute(transaction);
517
+ await statement2b.dispose();
464
518
  }
465
- yield transaction.commitRetaining();
466
- const statement3 = yield attachment.prepare(transaction, `select x_short,
519
+ await transaction.commitRetaining();
520
+ const statement3 = await attachment.prepare(transaction, `select x_short,
467
521
  x_int,
468
522
  x_int_scale,
469
523
  x_bigint,
@@ -496,8 +550,8 @@ function runCommonTests(client) {
496
550
  x_blob1,
497
551
  x_blob2
498
552
  from t1`);
499
- const resultSet3 = yield statement3.executeQuery(transaction);
500
- const data = yield resultSet3.fetch();
553
+ const resultSet3 = await statement3.executeQuery(transaction);
554
+ const data = await resultSet3.fetch();
501
555
  expect(data.length).toBe(recordCount * 2);
502
556
  for (const columns of data) {
503
557
  let n = 0;
@@ -533,39 +587,40 @@ function runCommonTests(client) {
533
587
  expect(columns[n++]).toBeNull();
534
588
  for (const i = n + 2; n < i; ++n) {
535
589
  const blob = columns[n];
536
- const blobStream = yield attachment.openBlob(transaction, blob);
537
- const buffer = Buffer.alloc(yield blobStream.length);
538
- expect(yield blobStream.read(buffer)).toBe(buffer.length);
539
- expect(yield blobStream.read(buffer)).toBe(-1);
540
- yield blobStream.close();
590
+ expect(blob.isValid).toBeTruthy();
591
+ const blobStream = await attachment.openBlob(transaction, blob);
592
+ const buffer = Buffer.alloc(await blobStream.length);
593
+ expect(await blobStream.read(buffer)).toBe(buffer.length);
594
+ expect(await blobStream.read(buffer)).toBe(-1);
595
+ await blobStream.close();
541
596
  expect(buffer.toString()).toBe('12345678á9');
542
597
  }
543
598
  expect(columns.length).toBe(n);
544
599
  }
545
- expect((yield resultSet3.fetch()).length).toBe(0);
546
- expect((yield resultSet3.fetch()).length).toBe(0);
547
- yield resultSet3.close();
548
- yield statement3.dispose();
549
- yield transaction.commit();
550
- yield attachment.dropDatabase();
551
- }));
552
- test('#fetchAsObject()', () => __awaiter(this, void 0, void 0, function* () {
553
- const attachment = yield client.createDatabase(getTempFile('ResultSet-fetchAsObject.fdb'));
554
- const transaction = yield attachment.startTransaction();
555
- const resultSet = yield attachment.executeQuery(transaction, 'select 1 as a, 2 as b from rdb$database');
556
- const output = yield resultSet.fetchAsObject();
600
+ expect((await resultSet3.fetch()).length).toBe(0);
601
+ expect((await resultSet3.fetch()).length).toBe(0);
602
+ await resultSet3.close();
603
+ await statement3.dispose();
604
+ await transaction.commit();
605
+ await attachment.dropDatabase();
606
+ });
607
+ test('#fetchAsObject()', async () => {
608
+ const attachment = await client.createDatabase(getTempFile('ResultSet-fetchAsObject.fdb'));
609
+ const transaction = await attachment.startTransaction();
610
+ const resultSet = await attachment.executeQuery(transaction, 'select 1 as a, 2 as b from rdb$database');
611
+ const output = await resultSet.fetchAsObject();
557
612
  expect(output[0].A).toBe(1);
558
613
  expect(output[0].B).toBe(2);
559
- yield resultSet.close();
560
- yield transaction.commit();
561
- yield attachment.dropDatabase();
562
- }));
563
- test('#fetch() with fetchSize', () => __awaiter(this, void 0, void 0, function* () {
564
- const attachment = yield client.createDatabase(getTempFile('ResultSet-fetch-with-fetchSize.fdb'));
565
- const transaction = yield attachment.startTransaction();
566
- yield attachment.execute(transaction, 'create table t1 (n1 integer)');
567
- yield transaction.commitRetaining();
568
- yield attachment.execute(transaction, `
614
+ await resultSet.close();
615
+ await transaction.commit();
616
+ await attachment.dropDatabase();
617
+ });
618
+ test('#fetch() with fetchSize', async () => {
619
+ const attachment = await client.createDatabase(getTempFile('ResultSet-fetch-with-fetchSize.fdb'));
620
+ const transaction = await attachment.startTransaction();
621
+ await attachment.execute(transaction, 'create table t1 (n1 integer)');
622
+ await transaction.commitRetaining();
623
+ await attachment.execute(transaction, `
569
624
  execute block
570
625
  as
571
626
  declare n integer = 0;
@@ -577,24 +632,24 @@ function runCommonTests(client) {
577
632
  end
578
633
  end
579
634
  `);
580
- const rs = yield attachment.executeQuery(transaction, 'select n1 from t1 order by n1');
635
+ const rs = await attachment.executeQuery(transaction, 'select n1 from t1 order by n1');
581
636
  rs.defaultFetchOptions = { fetchSize: 5 };
582
- expect((yield rs.fetch()).length).toBe(5);
583
- expect((yield rs.fetch({ fetchSize: 2 })).length).toBe(2);
584
- expect((yield rs.fetch()).length).toBe(5);
585
- expect((yield rs.fetch({ fetchSize: 36 })).length).toBe(36);
586
- expect((yield rs.fetch()).length).toBe(2);
587
- expect((yield rs.fetch()).length).toBe(0);
588
- yield rs.close();
589
- yield transaction.commit();
590
- yield attachment.dropDatabase();
591
- }));
592
- test('#fetch() with fetchSize and exception', () => __awaiter(this, void 0, void 0, function* () {
593
- const attachment = yield client.createDatabase(getTempFile('ResultSet-fetch-with-fetchSize.fdb'));
594
- const transaction = yield attachment.startTransaction();
595
- yield attachment.execute(transaction, 'create exception e1 \'e1\'');
596
- yield transaction.commitRetaining();
597
- const rs = yield attachment.executeQuery(transaction, `
637
+ expect((await rs.fetch()).length).toBe(5);
638
+ expect((await rs.fetch({ fetchSize: 2 })).length).toBe(2);
639
+ expect((await rs.fetch()).length).toBe(5);
640
+ expect((await rs.fetch({ fetchSize: 36 })).length).toBe(36);
641
+ expect((await rs.fetch()).length).toBe(2);
642
+ expect((await rs.fetch()).length).toBe(0);
643
+ await rs.close();
644
+ await transaction.commit();
645
+ await attachment.dropDatabase();
646
+ });
647
+ test('#fetch() with fetchSize and exception', async () => {
648
+ const attachment = await client.createDatabase(getTempFile('ResultSet-fetch-with-fetchSize.fdb'));
649
+ const transaction = await attachment.startTransaction();
650
+ await attachment.execute(transaction, 'create exception e1 \'e1\'');
651
+ await transaction.commitRetaining();
652
+ const rs = await attachment.executeQuery(transaction, `
598
653
  execute block returns (n integer)
599
654
  as
600
655
  begin
@@ -608,38 +663,67 @@ function runCommonTests(client) {
608
663
  end
609
664
  `);
610
665
  rs.defaultFetchOptions = { fetchSize: 5 };
611
- expect((yield rs.fetch()).length).toBe(2);
666
+ expect((await rs.fetch()).length).toBe(2);
612
667
  expect(rs.fetch()).rejects.toBeTruthy();
613
- yield rs.close();
614
- yield transaction.commit();
615
- yield attachment.dropDatabase();
616
- }));
617
- test('#fetch() with large blob', () => __awaiter(this, void 0, void 0, function* () {
618
- const attachment = yield client.createDatabase(getTempFile('ResultSet-fetch-with-large-blob.fdb'));
619
- let transaction = yield attachment.startTransaction();
620
- yield attachment.execute(transaction, `create table t1 (x_blob blob)`);
621
- yield transaction.commit();
622
- transaction = yield attachment.startTransaction();
668
+ await rs.close();
669
+ await transaction.commit();
670
+ await attachment.dropDatabase();
671
+ });
672
+ test('#fetch() with large blob', async () => {
673
+ const attachment = await client.createDatabase(getTempFile('ResultSet-fetch-with-large-blob.fdb'));
674
+ let transaction = await attachment.startTransaction();
675
+ await attachment.execute(transaction, `create table t1 (x_blob blob)`);
676
+ await transaction.commit();
677
+ transaction = await attachment.startTransaction();
623
678
  const buffer = Buffer.from('123'.repeat(60000));
624
- yield attachment.execute(transaction, `insert into t1 (x_blob) values (?)`, [buffer]);
625
- yield transaction.commit();
626
- transaction = yield attachment.startTransaction();
627
- const resultSet = yield attachment.executeQuery(transaction, `select x_blob from t1`);
628
- const result = yield resultSet.fetch();
629
- const readStream = yield attachment.openBlob(transaction, result[0][0]);
630
- const blobLength = yield readStream.length;
679
+ await attachment.execute(transaction, `insert into t1 (x_blob) values (?)`, [buffer]);
680
+ await transaction.commit();
681
+ transaction = await attachment.startTransaction();
682
+ const resultSet = await attachment.executeQuery(transaction, `select x_blob from t1`);
683
+ const result = await resultSet.fetch();
684
+ const readStream = await attachment.openBlob(transaction, result[0][0]);
685
+ const blobLength = await readStream.length;
631
686
  const resultBuffer = Buffer.alloc(blobLength);
632
687
  let size = 0;
633
688
  let n;
634
- while (size < blobLength && (n = yield readStream.read(resultBuffer.slice(size))) > 0)
689
+ while (size < blobLength && (n = await readStream.read(resultBuffer.slice(size))) > 0)
635
690
  size += n;
636
- yield readStream.close();
691
+ await readStream.close();
637
692
  expect(resultBuffer.toString().length).toEqual(buffer.toString().length);
638
693
  expect(resultBuffer.toString()).toEqual(buffer.toString());
639
- yield resultSet.close();
640
- yield transaction.commit();
641
- yield attachment.dropDatabase();
642
- }));
694
+ await resultSet.close();
695
+ await transaction.commit();
696
+ await attachment.dropDatabase();
697
+ });
698
+ });
699
+ describe('BlobStream', () => {
700
+ test('#seek()', async () => {
701
+ const attachment = await client.createDatabase(getTempFile('BlobStream-seek.fdb'));
702
+ const transaction = await attachment.startTransaction();
703
+ await attachment.execute(transaction, 'create table t1 (b blob)');
704
+ await transaction.commitRetaining();
705
+ const blobStream = await attachment.createBlob(transaction, { type: 'STREAM' });
706
+ await blobStream.write(Buffer.alloc(10, '1234567890'));
707
+ await blobStream.close();
708
+ await attachment.execute(transaction, 'insert into t1 (b) values (?)', [blobStream.blob]);
709
+ const blob = (await attachment.executeSingleton(transaction, 'select b from t1'))[0];
710
+ const readBlobStream = await attachment.openBlob(transaction, blob);
711
+ const buffer = Buffer.alloc(3);
712
+ expect(await readBlobStream.seek(2)).toBe(2);
713
+ expect(await readBlobStream.read(buffer)).toBe(3);
714
+ expect(buffer.toString()).toBe('345');
715
+ expect(await readBlobStream.seek(-1, 1 /* BlobSeekWhence.CURRENT */)).toBe(4);
716
+ expect(await readBlobStream.read(buffer)).toBe(3);
717
+ expect(buffer.toString()).toBe('567');
718
+ expect(await readBlobStream.seek(1, 0 /* BlobSeekWhence.START */)).toBe(1);
719
+ expect(await readBlobStream.read(buffer)).toBe(3);
720
+ expect(buffer.toString()).toBe('234');
721
+ expect(await readBlobStream.seek(-2, 2 /* BlobSeekWhence.END */)).toBe(8);
722
+ expect(await readBlobStream.read(buffer)).toBe(2);
723
+ expect(buffer.slice(0, 2).toString()).toBe('90');
724
+ await transaction.commit();
725
+ await attachment.dropDatabase();
726
+ });
643
727
  });
644
728
  });
645
729
  }