Skip to content

Commit 25e8da7

Browse files
committed
qorbani added support for simple start of line comments
I needed to change where the comments were removed. Also added a test and included the LCOV coverage file - which is not generated in github
1 parent 5c8eecb commit 25e8da7

File tree

8 files changed

+4159
-4037
lines changed

8 files changed

+4159
-4037
lines changed

coverage/tests.lcov

Lines changed: 4083 additions & 4028 deletions
Large diffs are not rendered by default.

dist/gssql.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -935,7 +935,7 @@ class TableExtract {
935935
if (typeof ast.SELECT === 'undefined')
936936
return;
937937

938-
ast.SELECT.forEach(term => this.extractAstTables(term.subQuery, tableSet));
938+
ast.SELECT.forEach(term => this.extractAstTables(term.subQuery, tableSet));
939939
}
940940
}
941941

@@ -5619,10 +5619,12 @@ class SqlParse {
56195619

56205620
/**
56215621
* Parse a query
5622-
* @param {String} query
5622+
* @param {String} sqlStatement
56235623
* @returns {Object}
56245624
*/
5625-
static sql2ast(query) {
5625+
static sql2ast(sqlStatement) {
5626+
const query = SqlParse.filterCommentsFromStatement(sqlStatement)
5627+
56265628
// Define which words can act as separator
56275629
const myKeyWords = SqlParse.generateUsedKeywordList(query);
56285630
const [parts_name, parts_name_escaped] = SqlParse.generateSqlSeparatorWords(myKeyWords);
@@ -5668,6 +5670,20 @@ class SqlParse {
56685670
return result;
56695671
}
56705672

5673+
/**
5674+
* Remove comments from SQL statement.
5675+
* @param {String} statement
5676+
* @returns {String}
5677+
*/
5678+
static filterCommentsFromStatement(statement) {
5679+
// Remove comments with lines starting with '--' and join lines together.
5680+
// If comment is within a STRING on a newline, it will fail ...
5681+
// We leave inline comments and multi-line /* */ comments for another day.
5682+
const filteredStatement = statement.split('\n').filter(line => !line.trim().startsWith('--')).join(' ');
5683+
5684+
return filteredStatement;
5685+
}
5686+
56715687
/**
56725688
*
56735689
* @param {String} logic
@@ -7382,6 +7398,12 @@ class ScriptSettings { // skipcq: JS-0128
73827398
/** @type {PropertyData} */
73837399
const myPropertyData = JSON.parse(myData);
73847400

7401+
if (PropertyData.isExpired(myPropertyData))
7402+
{
7403+
this.delete(propertyKey);
7404+
return null;
7405+
}
7406+
73857407
return PropertyData.getData(myPropertyData);
73867408
}
73877409

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.44",
3+
"version": "1.3.45",
44
"description": "Google Sheets QUERY function replacement using real SQL select syntax.",
55
"main": "testGsSql.js",
66
"files": ["./src", "src", "img", "dist"],

src/ScriptSettings.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ class ScriptSettings { // skipcq: JS-0128
3636
/** @type {PropertyData} */
3737
const myPropertyData = JSON.parse(myData);
3838

39+
if (PropertyData.isExpired(myPropertyData))
40+
{
41+
this.delete(propertyKey);
42+
return null;
43+
}
44+
3945
return PropertyData.getData(myPropertyData);
4046
}
4147

src/SimpleParser.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,12 @@ class SqlParse {
3434

3535
/**
3636
* Parse a query
37-
* @param {String} query
37+
* @param {String} sqlStatement
3838
* @returns {Object}
3939
*/
40-
static sql2ast(query) {
40+
static sql2ast(sqlStatement) {
41+
const query = SqlParse.filterCommentsFromStatement(sqlStatement)
42+
4143
// Define which words can act as separator
4244
const myKeyWords = SqlParse.generateUsedKeywordList(query);
4345
const [parts_name, parts_name_escaped] = SqlParse.generateSqlSeparatorWords(myKeyWords);
@@ -83,6 +85,20 @@ class SqlParse {
8385
return result;
8486
}
8587

88+
/**
89+
* Remove comments from SQL statement.
90+
* @param {String} statement
91+
* @returns {String}
92+
*/
93+
static filterCommentsFromStatement(statement) {
94+
// Remove comments with lines starting with '--' and join lines together.
95+
// If comment is within a STRING on a newline, it will fail ...
96+
// We leave inline comments and multi-line /* */ comments for another day.
97+
const filteredStatement = statement.split('\n').filter(line => !line.trim().startsWith('--')).join(' ');
98+
99+
return filteredStatement;
100+
}
101+
86102
/**
87103
*
88104
* @param {String} logic

src/Sql.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -949,7 +949,7 @@ class TableExtract {
949949
if (typeof ast.SELECT === 'undefined')
950950
return;
951951

952-
ast.SELECT.forEach(term => this.extractAstTables(term.subQuery, tableSet));
952+
ast.SELECT.forEach(term => this.extractAstTables(term.subQuery, tableSet));
953953
}
954954
}
955955

src/SqlTest.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4270,6 +4270,28 @@ class SqlTester {
42704270
return this.isEqual("selectSumMinusSum", data, expected);
42714271
}
42724272

4273+
innerSelectWithComments() {
4274+
let stmt = "SELECT *, customers.name FROM bookSales " +
4275+
"\n -- We join books to customers\n" +
4276+
"LEFT JOIN customers ON bookSales.customer_ID = customers.ID " +
4277+
"\n-- We only want the highest booksales\n" +
4278+
"WHERE bookSales.quantity > (select AVG(quantity) from booksales)";
4279+
4280+
let data = new TestSql()
4281+
.addTableData("bookSales", this.bookSalesTable())
4282+
.addTableData("customers", this.customerTable())
4283+
.enableColumnTitle(true)
4284+
.execute(stmt);
4285+
4286+
let expected = [["BOOKSALES.INVOICE", "BOOKSALES.BOOK_ID", "BOOKSALES.CUSTOMER_ID", "BOOKSALES.QUANTITY", "BOOKSALES.PRICE", "BOOKSALES.DATE", "CUSTOMERS.ID", "CUSTOMERS.NAME", "CUSTOMERS.ADDRESS", "CUSTOMERS.CITY", "CUSTOMERS.PHONE", "CUSTOMERS.EMAIL", "customers.name"],
4287+
["I7204", "2", "C4", 100, 65.49, "05/03/2022", "C4", "ForMe Resellers", "40 Four St", "FourtNight City", "2894441234", "fourtimes@hotmail.com", "ForMe Resellers"],
4288+
["I7204", "3", "C4", 150, 24.95, "05/03/2022", "C4", "ForMe Resellers", "40 Four St", "FourtNight City", "2894441234", "fourtimes@hotmail.com", "ForMe Resellers"],
4289+
["I7204", "4", "C4", 50, 19.99, "05/03/2022", "C4", "ForMe Resellers", "40 Four St", "FourtNight City", "2894441234", "fourtimes@hotmail.com", "ForMe Resellers"],
4290+
["I7206", "7", "C2", 100, 17.99, "05/04/2022", "C2", "Dewy Tuesdays", "202 Second St.", "Second City", "4162022222", "twoguys@gmail.com", "Dewy Tuesdays"]];
4291+
4292+
return this.isEqual("innerSelectWithComments", data, expected);
4293+
}
4294+
42734295
// S T A R T O T H E R T E S T S
42744296
removeTrailingEmptyRecords() {
42754297
let authors = this.authorsTable();
@@ -5465,6 +5487,7 @@ function testerSql() {
54655487
result = result && tester.selectAddDateLastDay();
54665488
result = result && tester.selectCalculatedFieldNotInSelectFieldsWitinGroupBY();
54675489
result = result && tester.selectLocate();
5490+
result = result && tester.innerSelectWithComments();
54685491
// Not supported (yet)
54695492
// result = result && tester.selectSumMinusSum();
54705493

0 commit comments

Comments
 (0)