Skip to content

Commit a1efa76

Browse files
committed
SELF JOIN lint changes - Sonarcube
Not all suggestions implemented as many caused issues. Others are just not supported in the current implementation of Javascript in Google Apps Script.
1 parent e87940e commit a1efa76

File tree

12 files changed

+5254
-5110
lines changed

12 files changed

+5254
-5110
lines changed

coverage/tests.lcov

Lines changed: 4745 additions & 4697 deletions
Large diffs are not rendered by default.

dist/gssql.js

Lines changed: 253 additions & 205 deletions
Large diffs are not rendered by default.

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@demmings/gssql",
3-
"version": "1.3.46",
3+
"version": "1.3.47",
44
"description": "Google Sheets QUERY function replacement using real SQL select syntax.",
55
"main": "testGsSql.js",
66
"files": ["./src", "src", "img", "dist"],

src/JoinTables.js

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ class JoinTables { // skipcq: JS-0128
7474
/** @property {DerivedTable} - result table after tables are joined */
7575
this.derivedTable = new DerivedTable();
7676

77-
ast.JOIN.forEach(joinTable => this.joinNextTable(joinTable, ast.FROM.table.toUpperCase(), ast.FROM.as));
77+
for (const joinTable of ast.JOIN) {
78+
this.joinNextTable(joinTable, ast.FROM.table.toUpperCase(), ast.FROM.as);
79+
}
7880
}
7981

8082
/**
@@ -105,7 +107,7 @@ class JoinTables { // skipcq: JS-0128
105107
const rightTableName = conditions.table;
106108
const joinType = conditions.type;
107109

108-
if (typeof conditions.cond.logic === 'undefined') {
110+
if (conditions.cond.logic === undefined) {
109111
recIds = this.resolveCondition("OR", [conditions], joinType, rightTableName, leftTableName);
110112
}
111113
else {
@@ -169,7 +171,7 @@ class JoinTables { // skipcq: JS-0128
169171
const result = [];
170172

171173
for (let i = 0; i < recIds[0].length; i++) {
172-
const temp = recIds.map(rec => typeof rec[i] === 'undefined' ? [] : rec[i]);
174+
const temp = recIds.map(rec => rec[i] === undefined ? [] : rec[i]);
173175
const row = temp.reduce((accumulator, currentRecords) => accumulator.filter(c => currentRecords.includes(c)), temp[0]);
174176

175177
if (row.length > 0) {
@@ -191,9 +193,11 @@ class JoinTables { // skipcq: JS-0128
191193
for (let i = 0; i < recIds[0].length; i++) {
192194
let temp = [];
193195

194-
recIds.forEach(rec => { temp = temp.concat(rec[i]) });
196+
for (const rec of recIds) {
197+
temp = temp.concat(rec[i])
198+
};
195199

196-
if (typeof temp[0] !== 'undefined') {
200+
if (temp[0] !== undefined) {
197201
result[i] = Array.from(new Set(temp));
198202
}
199203
}
@@ -206,7 +210,7 @@ class JoinTables { // skipcq: JS-0128
206210
* @returns {Boolean}
207211
*/
208212
isDerivedTable() {
209-
if (typeof this.derivedTable === 'undefined') {
213+
if (this.derivedTable === undefined) {
210214
return false;
211215
}
212216

@@ -441,9 +445,9 @@ class JoinTablesRecordIds {
441445
/** @type {TableField} */
442446
let rightFieldInfo = null;
443447

444-
const left = typeof astJoin.cond === 'undefined' ? astJoin.left : astJoin.cond.left;
445-
const right = typeof astJoin.cond === 'undefined' ? astJoin.right : astJoin.cond.right;
446-
const operator = typeof astJoin.cond === 'undefined' ? astJoin.operator : astJoin.cond.operator;
448+
const left = astJoin.cond === undefined ? astJoin.left : astJoin.cond.left;
449+
const right = astJoin.cond === undefined ? astJoin.right : astJoin.cond.right;
450+
const operator = astJoin.cond === undefined ? astJoin.operator : astJoin.cond.operator;
447451

448452
leftFieldInfo = this.getTableInfoFromCalculatedField(left);
449453
rightFieldInfo = this.getTableInfoFromCalculatedField(right);
@@ -461,7 +465,7 @@ class JoinTablesRecordIds {
461465
}
462466

463467
// joinTable.table is the RIGHT table, so switch if equal to condition left.
464-
if (typeof leftFieldInfo !== 'undefined' && this.rightTableName === leftFieldInfo.originalTable && !isSelfJoin) {
468+
if (leftFieldInfo !== undefined && this.rightTableName === leftFieldInfo.originalTable && !isSelfJoin) {
465469
return {
466470
leftSideInfo: rightSideInfo,
467471
rightSideInfo: leftSideInfo,
@@ -480,7 +484,7 @@ class JoinTablesRecordIds {
480484
getTableInfoFromCalculatedField(calcField) {
481485
let foundTableField = this.tableFields.getFieldInfo(calcField);
482486

483-
if (typeof foundTableField === 'undefined' && calcField !== '') {
487+
if (foundTableField === undefined && calcField !== '') {
484488
// Calculated expression.
485489
foundTableField = this.getReferencedTableInfo(calcField);
486490
}
@@ -511,7 +515,7 @@ class JoinTablesRecordIds {
511515
// We search the calcField for valid columns - except within quotes.
512516
const quotedConstantsRegEx = /["'](.*?)["']/g;
513517
const opRegEx = /[+\-/*()]/g;
514-
const results = calcField.replace(quotedConstantsRegEx, "");
518+
const results = calcField.replaceAll(quotedConstantsRegEx, "");
515519
let parts = results.split(opRegEx);
516520
parts = parts.map(a => a.trim()).filter(a => a !== '');
517521

@@ -531,7 +535,7 @@ class JoinTablesRecordIds {
531535
*/
532536
searchColumnsForTable(calcField, columns) {
533537
const fieldInfoList = columns.map(col => this.tableFields.getFieldInfo(col));
534-
const validFieldInfo = fieldInfoList.filter(fld => typeof fld != 'undefined');
538+
const validFieldInfo = fieldInfoList.filter(fld => fld != undefined);
535539

536540
if (validFieldInfo.length > 0) {
537541
const foundTableField = { ...validFieldInfo[0] };
@@ -601,17 +605,11 @@ class JoinTablesRecordIds {
601605
let rightRecordIDs = [];
602606

603607
if (this.joinFields.operator === '=') {
604-
// "=" - special case.
605-
// Most common case AND far fewer comparisons - especially if right table is large.
608+
// "=" - special case. Most common case AND far fewer comparisons - especially if right table is large.
606609
rightRecordIDs = keyFieldMap.has(keyMasterJoinField) ? keyFieldMap.get(keyMasterJoinField) : [];
607610
}
608611
else {
609-
// @ts-ignore
610-
for (const [key, data] of keyFieldMap) {
611-
if (conditionFunction(keyMasterJoinField, key)) {
612-
rightRecordIDs.unshift(...data);
613-
}
614-
}
612+
rightRecordIDs = this.getJoinRecordIdsForNonEqualCondition(keyMasterJoinField, keyFieldMap, conditionFunction);
615613
}
616614

617615
// For the current LEFT TABLE record, record the linking RIGHT TABLE records.
@@ -668,4 +666,24 @@ class JoinTablesRecordIds {
668666

669667
return keyFieldMap;
670668
}
669+
670+
/**
671+
*
672+
* @param {String} keyMasterJoinField
673+
* @param {Map<String, Number[]>} keyFieldMap
674+
* @param {Function} conditionFunction
675+
* @returns {Number[]}
676+
*/
677+
getJoinRecordIdsForNonEqualCondition(keyMasterJoinField, keyFieldMap, conditionFunction) {
678+
const recordIDs = [];
679+
680+
// @ts-ignore
681+
for (const [key, data] of keyFieldMap) {
682+
if (conditionFunction(keyMasterJoinField, key)) {
683+
recordIDs.unshift(...data);
684+
}
685+
}
686+
687+
return recordIDs;
688+
}
671689
}

src/ScriptSettings.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ class ScriptSettings { // skipcq: JS-0128
7272
*/
7373
putAll(propertyDataObject, daysToHold = 1) {
7474
const keys = Object.keys(propertyDataObject);
75-
keys.forEach(key => this.put(key, propertyDataObject[key], daysToHold));
75+
// keys.forEach(key => this.put(key, propertyDataObject[key], daysToHold));
76+
for (const key of keys) {
77+
this.put(key, propertyDataObject[key], daysToHold);
78+
}
7679
}
7780

7881
/**
@@ -116,7 +119,7 @@ class ScriptSettings { // skipcq: JS-0128
116119
for (const key of cacheKeys) {
117120
const myData = allProperties[key];
118121

119-
if (typeof myData === 'undefined') {
122+
if (myData === undefined) {
120123
values.push(null);
121124
}
122125
else {

src/Select2Object.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,16 @@ class Select2Object { // skipcq: JS-0128
5757

5858
// Add the table name and range.
5959
for (const tab of this.tables) {
60-
parms.push(tab.tableName);
61-
parms.push(tab.data);
60+
parms.push(tab.tableName, tab.data);
6261
}
6362

6463
// Add column output indicator.
6564
parms.push(true); // We want column names returned.
6665

6766
// Add bind data.
68-
this.bindVariables.forEach(bind => parms.push(bind));
67+
for (const bind of this.bindVariables) {
68+
parms.push(bind);
69+
}
6970

7071
const tableDataArray = GasSql.execute(statement, parms);
7172

@@ -211,7 +212,9 @@ class Select2Object { // skipcq: JS-0128
211212
const newRecord = {};
212213
Object.assign(newRecord, emptyTableRecord);
213214

214-
columnNames.forEach((col, j) => {newRecord[col] = tableDataArray[i][j]});
215+
for (const [index, col] of columnNames.entries()) {
216+
newRecord[col] = tableDataArray[i][index];
217+
}
215218

216219
tableData.push(newRecord);
217220
}
@@ -227,7 +230,9 @@ class Select2Object { // skipcq: JS-0128
227230
static createEmptyRecordObject(columnNames) {
228231
// Create empty table record object.
229232
const dataObject = {};
230-
columnNames.forEach(col => {dataObject[col] = ''});
233+
for (const col of columnNames) {
234+
dataObject[col] = '';
235+
}
231236

232237
dataObject.get = function (columnTitle) {
233238
const prop = Select2Object.convertColumnTitleToPropertyName([columnTitle])[0];

0 commit comments

Comments
 (0)