|
| 1 | +if (typeof exports === 'object') { |
| 2 | + var assert = require('assert'); |
| 3 | + var alasql = require('..'); |
| 4 | +} |
| 5 | + |
| 6 | +/* |
| 7 | + Test for issue #1740 - Parse() then AST.toString() doesn't restore square brackets on column name |
| 8 | + https://github.com/AlaSQL/alasql/issues/1740 |
| 9 | + |
| 10 | + If a column name requires square brackets (e.g. due to a space or a period in the column name), |
| 11 | + toString() on the AST should preserve the brackets. |
| 12 | +*/ |
| 13 | + |
| 14 | +describe('Test 1740 - AST.toString() preserves square brackets on column names', function () { |
| 15 | + it('A) Column with space in name', function () { |
| 16 | + var sql = 'SELECT [Foo Bar] FROM tbl'; |
| 17 | + var ast = alasql.parse(sql); |
| 18 | + assert.equal(ast.toString(), sql); |
| 19 | + }); |
| 20 | + |
| 21 | + it('B) Column with dot in name', function () { |
| 22 | + var sql = 'SELECT [Foo.Bar] FROM tbl'; |
| 23 | + var ast = alasql.parse(sql); |
| 24 | + assert.equal(ast.toString(), sql); |
| 25 | + }); |
| 26 | + |
| 27 | + it('C) Column with hyphen in name', function () { |
| 28 | + var sql = 'SELECT [Foo-Bar] FROM tbl'; |
| 29 | + var ast = alasql.parse(sql); |
| 30 | + assert.equal(ast.toString(), sql); |
| 31 | + }); |
| 32 | + |
| 33 | + it('D) Numeric column index preserves brackets', function () { |
| 34 | + var sql = 'SELECT [1] FROM tbl'; |
| 35 | + var ast = alasql.parse(sql); |
| 36 | + assert.equal(ast.toString(), sql); |
| 37 | + }); |
| 38 | + |
| 39 | + it('E) Table.column with bracket notation (dot omitted for numeric index)', function () { |
| 40 | + // When using table alias with numeric column index, the dot is omitted in toString() |
| 41 | + // Input: d.[1] -> Output: d[1] |
| 42 | + var sql = 'SELECT d.[1] FROM tbl AS d'; |
| 43 | + var ast = alasql.parse(sql); |
| 44 | + assert.equal(ast.toString(), 'SELECT d[1] FROM tbl AS d'); |
| 45 | + }); |
| 46 | + |
| 47 | + it('F) Normal column names without special characters', function () { |
| 48 | + var sql = 'SELECT normalColumn FROM tbl'; |
| 49 | + var ast = alasql.parse(sql); |
| 50 | + assert.equal(ast.toString(), sql); |
| 51 | + }); |
| 52 | +}); |
0 commit comments