Skip to content

Calling parser with backtick #1512

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 10 commits into
base: v5
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/10start.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@

"use strict";

const isBacktickQuery = (arg) => Array.isArray(arg.raw);

const formatQueryParams = (params) => (queryStr, index) => {
const param = params[index + 1];
return queryStr + (typeof param === 'undefined' ? '' : param);
};

const normalizeBacktickQuery = (args) => {
const stringFormatted = args[0]
.map(formatQueryParams(args))
.join('')
// Remove breakline in case of characters in same line | optional
.replace(/[\r\n]/g, '')
.replace(/\s+/g, ' ') // Remove extras
.trim(); // Remove extras
return stringFormatted;
};

/**
@fileoverview AlaSQL JavaScript SQL library
@see http://github.com/agershun/alasql
Expand Down Expand Up @@ -57,7 +75,9 @@
alasql().From(data).Where(function(x){return x.a == 10}).exec();
*/

var alasql = function(sql, params, cb, scope) {
var alasql = function(...args) {
var [sqlQuery, params, cb, scope] = args;
var sql = isBacktickQuery(sqlQuery) ? normalizeBacktickQuery(args) : sqlQuery;

params = params||[];

Expand Down
4 changes: 2 additions & 2 deletions src/17alasql.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ alasql.parser.parseError = function (str, hash) {
// My own parser here
}
*/
alasql.parse = function (sql) {
return alasqlparser.parse(alasql.utils.uncomment(sql));
alasql.parse = function (command) {
return alasqlparser.parse(alasql.utils.uncomment(command));
};

/**
Expand Down
60 changes: 60 additions & 0 deletions test/_test847.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
if (typeof exports === 'object') {
var alasql = require('..');
}

const table = {
name: 'midnightcalls',
columns: [
{name: 'track_name', type: 'string'},
{name: 'author', type: 'string'},
{name: 'views', type: 'int'},
],
};

describe('Test 847 - Testing backtick call function', function () {
it('1. Create table', function () {
alasql`DROP TABLE IF EXISTS test`;
alasql`CREATE TABLE test (a int, b int)`;
});

it('2. Insert values ', function () {
alasql`INSERT INTO test VALUES (1,1)`;
alasql`INSERT INTO test VALUES (1,7)`;
alasql`INSERT INTO test VALUES (2,2)`;
alasql`INSERT INTO test VALUES (3,3)`;
});

it('3. Create a new table', function () {
alasql`DROP TABLE IF EXISTS ${table.name}`;

alasql(`
CREATE TABLE ${table.name} (${table.columns
.map((item) => ` ${item.name} ${item.type.toUpperCase()}`)
.join(', ')
.toString()})
`);
});

it('4. Insert values', function () {
const values = [
['qhAfaWdLbIE', 'Baby bi', 'Yunk Vino', 72],
['YA-db3f8Ak4', 'Sonar', 'Yunk Vino', 809],
];
const valuesToInsert = values
.map(
(item, i) =>
`('${item[0]}', '${item[1]}', '${item[2]}', ${item[3]})${
i + 1 === values.length ? '' : ', '
}`
)
.join('');

console.log(valuesToInsert);

alasql(`
INSERT INTO ${table.name}
VALUES
${valuesToInsert}
`);
});
});
2 changes: 1 addition & 1 deletion test/test238.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"100": 100}]
[{"100":100}]