Skip to content

Commit

Permalink
linter stuff.
Browse files Browse the repository at this point in the history
  • Loading branch information
demmings committed Jan 22, 2023
1 parent dc60e78 commit 8da3b49
Show file tree
Hide file tree
Showing 8 changed files with 2,640 additions and 2,534 deletions.
4,934 changes: 2,487 additions & 2,447 deletions coverage/tests.lcov

Large diffs are not rendered by default.

117 changes: 75 additions & 42 deletions dist/gssql.js
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,9 @@ class Sql {
}
}

/**
* Store and retrieve bind data for use in WHERE portion of SELECT statement.
*/
class BindData {
constructor() {
this.clear();
Expand All @@ -961,11 +964,11 @@ class BindData {

/**
* Add bind data
* @param {any} data
* @returns {String}
* @param {any} data - bind data
* @returns {String} - bind variable name for reference in SQL. e.g. first data point would return '?1'.
*/
add(data) {
const key = "?" + this.next.toString();
const key = `?${this.next.toString()}`;
this.bindMap.set(key, data);
this.bindQueue.push(data);

Expand All @@ -987,7 +990,7 @@ class BindData {
/**
* Pull out a bind data entry.
* @param {String} name - Get by name or get NEXT if empty.
* @returns
* @returns {any}
*/
get(name = "") {
return name === '' ? this.bindQueue.shift() : this.bindMap.get(name);
Expand Down Expand Up @@ -1299,6 +1302,7 @@ class Table { // skipcq: JS-0128
/**
* Append table data from 'concatTable' to the end of this tables existing data.
* @param {Table} concatTable - Append 'concatTable' data to end of current table data.
* @returns {void}
*/
concat(concatTable) {
const fieldsThisTable = this.schema.getAllFieldNames();
Expand Down Expand Up @@ -1600,6 +1604,7 @@ class SelectTables {
/**
* Process any JOIN condition.
* @param {Object} ast - Abstract Syntax Tree
* @returns {void}
*/
join(ast) {
if (typeof ast.JOIN !== 'undefined')
Expand Down Expand Up @@ -2048,7 +2053,7 @@ class SelectTables {

// Fudge the HAVING to look like a SELECT.
const astSelect = {};
astSelect.FROM = [{ table: this.primaryTable, as : '' }];
astSelect.FROM = [{ table: this.primaryTable, as: '' }];
astSelect.SELECT = [{ name: "*" }];
astSelect.WHERE = astHaving;

Expand Down Expand Up @@ -2182,54 +2187,82 @@ class SelectTables {
let calculatedField = "";
/** @type {CorrelatedSubQuery} */
let subQuery = null;

// Maybe a SELECT within...

if (typeof fieldCondition.SELECT !== 'undefined') {
if (SelectTables.isCorrelatedSubQuery(fieldCondition)) {
subQuery = new CorrelatedSubQuery(this.tableInfo, this.tableFields, this.bindVariables, fieldCondition);
}
else {
const subQueryTableInfo = SelectTables.getSubQueryTableSet(fieldCondition, this.tableInfo);
const inData = new Sql()
.setTables(subQueryTableInfo)
.setBindValues(this.bindVariables)
.execute(fieldCondition);

constantData = inData.join(",");
}
// Maybe a SELECT within...
[subQuery, constantData] = this.resolveSubQuery(fieldCondition);
}
else if (SelectTables.isStringConstant(fieldCondition))
// String constant
constantData = SelectTables.extractStringConstant(fieldCondition);
else if (fieldCondition.startsWith('?')) {
// Bind variable data.
constantData = this.bindVariables.get(fieldCondition);
if (typeof constantData === 'undefined') {
if (fieldCondition === '?') {
throw new Error("Bind variable naming is ?1, ?2... where ?1 is first bind data point in list.")
}
else {
throw new Error(`Bind variable ${fieldCondition} was not found`);
}
}
constantData = this.resolveBindData(fieldCondition);
}
else if (!isNaN(fieldCondition)) {
// Literal number.
constantData = fieldCondition;
}
else if (this.tableFields.hasField(fieldCondition)) {
// Table field.
columnNumber = this.tableFields.getFieldColumn(fieldCondition);
fieldConditionTableInfo = this.tableFields.getTableInfo(fieldCondition);
}
else {
if (isNaN(fieldCondition)) {
if (this.tableFields.hasField(fieldCondition)) {
columnNumber = this.tableFields.getFieldColumn(fieldCondition)
fieldConditionTableInfo = this.tableFields.getTableInfo(fieldCondition)
}
else {
// Calculated field?
calculatedField = fieldCondition;
}
}
else
constantData = fieldCondition;
// Calculated field?
calculatedField = fieldCondition;
}

return { fieldConditionTableInfo, columnNumber, constantData, calculatedField, subQuery };
}

/**
* Handle subquery. If correlated subquery, return object to handle, otherwise resolve and return constant data.
* @param {Object} fieldCondition - left or right portion of condition
* @returns {any[]}
*/
resolveSubQuery(fieldCondition) {
/** @type {CorrelatedSubQuery} */
let subQuery = null;
/** @type {String} */
let constantData = null;

if (SelectTables.isCorrelatedSubQuery(fieldCondition)) {
subQuery = new CorrelatedSubQuery(this.tableInfo, this.tableFields, this.bindVariables, fieldCondition);
}
else {
const subQueryTableInfo = SelectTables.getSubQueryTableSet(fieldCondition, this.tableInfo);
const inData = new Sql()
.setTables(subQueryTableInfo)
.setBindValues(this.bindVariables)
.execute(fieldCondition);

constantData = inData.join(",");
}

return [subQuery, constantData];
}

/**
* Get constant bind data
* @param {Object} fieldCondition - left or right portion of condition
* @returns {any}
*/
resolveBindData(fieldCondition) {
// Bind variable data.
const constantData = this.bindVariables.get(fieldCondition);
if (typeof constantData === 'undefined') {
if (fieldCondition === '?') {
throw new Error("Bind variable naming is ?1, ?2... where ?1 is first bind data point in list.")
}
else {
throw new Error(`Bind variable ${fieldCondition} was not found`);
}
}

return constantData;
}

static isCorrelatedSubQuery(ast) {
const tableSet = new Map();
Sql.extractAstTables(ast, tableSet);
Expand Down Expand Up @@ -4714,7 +4747,7 @@ class CondLexer {
let tokenValue = this.currentChar;
this.readNextChar();

while (/[0-9]/.test(this.currentChar)) {
while (/\d/.test(this.currentChar)) {
tokenValue += this.currentChar;
this.readNextChar();
}
Expand Down Expand Up @@ -4973,7 +5006,7 @@ class SelectKeywordAnalysis {
const subQuery = SelectKeywordAnalysis.parseForCorrelatedSubQuery(item);
return { name, terms, as, subQuery };
}
return { name: name, as: as };
return { name, as };
});

return selectResult;
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@demmings/gssql",
"version": "1.1.45",
"version": "1.1.46",
"description": "Google Sheets QUERY function replacement using real SQL select syntax.",
"main": "testGsSql.js",
"files": ["./src", "src", "img", "dist"],
Expand Down
4 changes: 2 additions & 2 deletions src/SimpleParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ class CondLexer {
let tokenValue = this.currentChar;
this.readNextChar();

while (/[0-9]/.test(this.currentChar)) {
while (/\d/.test(this.currentChar)) {
tokenValue += this.currentChar;
this.readNextChar();
}
Expand Down Expand Up @@ -889,7 +889,7 @@ class SelectKeywordAnalysis {
const subQuery = SelectKeywordAnalysis.parseForCorrelatedSubQuery(item);
return { name, terms, as, subQuery };
}
return { name: name, as: as };
return { name, as };
});

return selectResult;
Expand Down
11 changes: 7 additions & 4 deletions src/Sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,9 @@ class Sql {
}
}

/**
* Store and retrieve bind data for use in WHERE portion of SELECT statement.
*/
class BindData {
constructor() {
this.clear();
Expand All @@ -961,11 +964,11 @@ class BindData {

/**
* Add bind data
* @param {any} data
* @returns {String}
* @param {any} data - bind data
* @returns {String} - bind variable name for reference in SQL. e.g. first data point would return '?1'.
*/
add(data) {
const key = "?" + this.next.toString();
const key = `?${this.next.toString()}`;
this.bindMap.set(key, data);
this.bindQueue.push(data);

Expand All @@ -987,7 +990,7 @@ class BindData {
/**
* Pull out a bind data entry.
* @param {String} name - Get by name or get NEXT if empty.
* @returns
* @returns {any}
*/
get(name = "") {
return name === '' ? this.bindQueue.shift() : this.bindMap.get(name);
Expand Down
1 change: 1 addition & 0 deletions src/Table.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ class Table { // skipcq: JS-0128
/**
* Append table data from 'concatTable' to the end of this tables existing data.
* @param {Table} concatTable - Append 'concatTable' data to end of current table data.
* @returns {void}
*/
concat(concatTable) {
const fieldsThisTable = this.schema.getAllFieldNames();
Expand Down
Loading

0 comments on commit 8da3b49

Please sign in to comment.