Skip to content

Commit f992443

Browse files
committed
BUG#34970001: move getAffectedItems() to SqlResult
The "RowResult" API is used to encapsulate the result set and corresponding execution details of a data retrieval statement built using the "Table.select()" API. This type of statements should not yield any kind of side effects, including modifying data in the table, thus, the outcome will not result in any affected items. However, the "RowResult" API includes an extraneous "getAffectedItems()" method, which is useless, given that it will always return 0. This method is only relevant for the case where the statement is created using raw SQL, in which case, the "Result" instance is not aware about the type of statement that it yields from. A "SELECT" will not affect any items, whereas an "UPDATE", "DELETE" or "INSERT" will. This patch removes the "getAffectedItems()" from the "RowResult" API whilst ensuring it is still available for results of executing a raw SQL statement, via the corresponding "SqlResult" API. Change-Id: I65d48a0cbde4a4452f1cc141e65fd80f985cac38
1 parent 4fee43d commit f992443

File tree

6 files changed

+21
-61
lines changed

6 files changed

+21
-61
lines changed

lib/DevAPI/RowResult.js

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2022, Oracle and/or its affiliates.
2+
* Copyright (c) 2018, 2023, Oracle and/or its affiliates.
33
*
44
* This program is free software; you can redistribute it and/or modify
55
* it under the terms of the GNU General Public License, version 2.0, as
@@ -30,7 +30,6 @@
3030

3131
'use strict';
3232

33-
const uint64 = require('../Protocol/Wrappers/ScalarValues/uint64');
3433
const column = require('./Column');
3534
const baseResult = require('./BaseResult');
3635

@@ -59,15 +58,6 @@ const baseResult = require('./BaseResult');
5958
function RowResult ({ index = 0, integerType, metadata = [], results = [], rowsAffected = 0n, warnings } = {}) {
6059
return {
6160
...baseResult({ warnings }),
62-
/**
63-
* Retrieve the number of documents affected by the operation.
64-
* @function
65-
* @name module:RowResult#getAffectedItemsCount
66-
* @returns {number} The number of rows.
67-
*/
68-
getAffectedItemsCount () {
69-
return uint64.create(rowsAffected, { type: integerType }).valueOf();
70-
},
7161

7262
/**
7363
* Consume the current result set from memory (and flush it).

lib/DevAPI/SqlResult.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2022, Oracle and/or its affiliates.
2+
* Copyright (c) 2018, 2023, Oracle and/or its affiliates.
33
*
44
* This program is free software; you can redistribute it and/or modify
55
* it under the terms of the GNU General Public License, version 2.0, as
@@ -59,6 +59,16 @@ const rowResult = require('./RowResult');
5959
function SqlResult ({ generatedInsertId = 0n, integerType, metadata, results, rowsAffected, warnings } = {}) {
6060
return {
6161
...rowResult({ integerType, metadata, results, rowsAffected, warnings }),
62+
/**
63+
* Retrieve the number of documents affected by the operation.
64+
* @function
65+
* @name module:RowResult#getAffectedItemsCount
66+
* @returns {number} The number of rows.
67+
*/
68+
getAffectedItemsCount () {
69+
return uint64.create(rowsAffected, { type: integerType }).valueOf();
70+
},
71+
6272
/**
6373
* Retrieve the first <code>AUTO INCREMENT</code> value generated by the statement.
6474
* @function

test/unit/DevAPI/RowResult.js

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2022, Oracle and/or its affiliates.
2+
* Copyright (c) 2018, 2023, Oracle and/or its affiliates.
33
*
44
* This program is free software; you can redistribute it and/or modify
55
* it under the terms of the GNU General Public License, version 2.0, as
@@ -32,7 +32,6 @@
3232

3333
/* eslint-env node, mocha */
3434

35-
const IntegerType = require('../../../lib/Protocol/Wrappers/ScalarValues/int64').Type;
3635
const expect = require('chai').expect;
3736
const rowResult = require('../../../lib/DevAPI/RowResult');
3837
const td = require('testdouble');
@@ -134,42 +133,6 @@ describe('RowResult', () => {
134133
});
135134
});
136135

137-
context('getAffectedItemsCount()', () => {
138-
context('when the number of affected items by the statement is below Number.MAX_SAFE_INTEGER', () => {
139-
it('returns the value as a JavaScript number by default', () => {
140-
expect(rowResult({ rowsAffected: 3n }).getAffectedItemsCount()).to.equal(3);
141-
});
142-
143-
it('can return the value as a JavaScript string', () => {
144-
expect(rowResult({ rowsAffected: 3n, integerType: IntegerType.STRING }).getAffectedItemsCount()).to.equal('3');
145-
});
146-
147-
it('can return the value as a JavaScript BigInt', () => {
148-
expect(rowResult({ rowsAffected: 3n, integerType: IntegerType.BIGINT }).getAffectedItemsCount()).to.equal(3n);
149-
});
150-
151-
it('ignores a specific return type for unsafe integers', () => {
152-
expect(rowResult({ rowsAffected: 3n, integerType: IntegerType.UNSAFE_BIGINT }).getAffectedItemsCount()).to.equal(3);
153-
expect(rowResult({ rowsAffected: 3n, integerType: IntegerType.UNSAFE_STRING }).getAffectedItemsCount()).to.equal(3);
154-
});
155-
});
156-
157-
context('when the number of affected items by the statement is above Number.MAX_SAFE_INTEGER', () => {
158-
it('returns the value as a JavaScript string by default', () => {
159-
expect(rowResult({ rowsAffected: 18446744073709551615n }).getAffectedItemsCount()).to.equal('18446744073709551615');
160-
});
161-
162-
it('returns the value as a JavaScript string if explicitly specified', () => {
163-
expect(rowResult({ rowsAffected: 18446744073709551615n, integerType: IntegerType.UNSAFE_STRING }).getAffectedItemsCount()).to.equal('18446744073709551615');
164-
});
165-
166-
it('can return the value as a JavaScript BigInt', () => {
167-
expect(rowResult({ rowsAffected: 18446744073709551615n, integerType: IntegerType.BIGINT }).getAffectedItemsCount()).to.equal(18446744073709551615n);
168-
expect(rowResult({ rowsAffected: 18446744073709551615n, integerType: IntegerType.UNSAFE_BIGINT }).getAffectedItemsCount()).to.equal(18446744073709551615n);
169-
});
170-
});
171-
});
172-
173136
context('getColumns()', () => {
174137
let getAlias;
175138

types/lib/DevAPI/RowResult.d.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022, Oracle and/or its affiliates.
2+
* Copyright (c) 2022, 2023, Oracle and/or its affiliates.
33
*
44
* This program is free software; you can redistribute it and/or modify
55
* it under the terms of the GNU General Public License, version 2.0, as
@@ -50,11 +50,6 @@ interface RowResult extends BaseResult {
5050
* column that is part of the projection or aggregation.
5151
*/
5252
fetchOne: () => Scalar[]
53-
/**
54-
* Retrieves the number of items affected by the statement.
55-
* @returns The total number of items affected by the statement.
56-
*/
57-
getAffectedItemsCount: () => number
5853
/**
5954
* Retrieves the first `AUTO INCREMENT` value generated by the statement.
6055
* @returns The first `AUTO INCREMENT` value.

types/lib/DevAPI/SqlResult.d.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022, Oracle and/or its affiliates.
2+
* Copyright (c) 2022, 2023, Oracle and/or its affiliates.
33
*
44
* This program is free software; you can redistribute it and/or modify
55
* it under the terms of the GNU General Public License, version 2.0, as
@@ -34,6 +34,11 @@ import RowResult from './RowResult';
3434
* Result set type generated by executing SQL statements with `Session.sql()`.
3535
*/
3636
interface SqlResult extends RowResult {
37+
/**
38+
* Retrieves the number of items affected by the statement.
39+
* @returns The total number of items affected by the statement.
40+
*/
41+
getAffectedItemsCount: () => number
3742
/**
3843
* Retrieves the first `AUTO INCREMENT` value generated by the statement.
3944
* @returns The first `AUTO INCREMENT` value.

types/test/DevAPI/RowResult.test-d.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022, Oracle and/or its affiliates.
2+
* Copyright (c) 2022, 2023, Oracle and/or its affiliates.
33
*
44
* This program is free software; you can redistribute it and/or modify
55
* it under the terms of the GNU General Public License, version 2.0, as
@@ -50,9 +50,6 @@ async function test (): Promise<void> {
5050
// fetchOne()
5151
expectType<Scalar[]>(result.fetchOne());
5252

53-
// getAffectedItemsCount()
54-
expectType<number>(result.getAffectedItemsCount());
55-
5653
// getAutoIncrementValue()
5754
expectType<number>(result.getAutoIncrementValue());
5855

0 commit comments

Comments
 (0)