-
Notifications
You must be signed in to change notification settings - Fork 692
Expand file tree
/
Copy pathtest1723a.js
More file actions
161 lines (139 loc) · 4.72 KB
/
test1723a.js
File metadata and controls
161 lines (139 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
if (typeof exports === 'object') {
var assert = require('assert');
var alasql = require('../dist/alasql');
}
describe('Issue #1723 - tagFunction for template strings', function () {
it('Will mark free fields as parameters', function (done) {
assert.deepEqual(tagBraid`SELECT 123 as abc`, ['SELECT 123 as abc']);
assert.deepEqual(tagBraid`SELECT ${123} as abc`, ['SELECT ? as abc', [123]]);
assert.deepEqual(tagBraid`${'SELECT'} ${123} as abc`, ['? ? as abc', ['SELECT', 123]]);
assert.deepEqual(tagBraid`${'SELECT'} ${123} as ${'abc'}`, [
'? ? as ?',
['SELECT', 123, 'abc'],
]);
done();
});
it('Will work second time when data is fetched from the cache', function (done) {
assert.deepEqual(tagBraid`SELECT 123 as abc`, ['SELECT 123 as abc']);
assert.deepEqual(tagBraid`SELECT ${123} as abc`, ['SELECT ? as abc', [123]]);
assert.deepEqual(tagBraid`${'SELECT'} ${123} as abc`, ['? ? as abc', ['SELECT', 123]]);
assert.deepEqual(tagBraid`${'SELECT'} ${123} as ${'abc'}`, [
'? ? as ?',
['SELECT', 123, 'abc'],
]);
done();
});
it('Will inline connected fields', function (done) {
assert.deepEqual(tagBraid`S${'ELECT'} 1${23} as ab${'c'}`, ['SELECT 123 as abc', []]);
assert.deepEqual(tagBraid`SELECT 123 as ${'ab'}${'c'}`, ['SELECT 123 as abc', []]);
done();
});
it('Will treat "()," as free space and become parameter', function (done) {
assert.deepEqual(tagBraid`SELECT AVG(${1},${2},${3}) as abc`, [
'SELECT AVG(?,?,?) as abc',
[1, 2, 3],
]);
done();
});
it('Can force free fields as inline', function (done) {
assert.deepEqual(tagBraid`~${'SELECT'} ~${123} as abc`, ['SELECT 123 as abc', []]);
assert.deepEqual(tagBraid`~${'SELECT'} ~${123} as ${'abc'}`, ['SELECT 123 as ?', ['abc']]);
assert.deepEqual(tagBraid`${'SELECT'} ${123} as ~${'abc'}`, ['? ? as abc', ['SELECT', 123]]);
assert.deepEqual(tagBraid`${'SELECT'} ${123} as ~${'abc'}~`, ['? ? as ~abc~', ['SELECT', 123]]);
assert.deepEqual(tagBraid`SELECT AVG(~${1},~${2},${3}) as abc`, [
'SELECT AVG(1,2,?) as abc',
[3],
]);
done();
});
it('Default to markring as parameter (option B from PR #1512)', function (done) {
let items = `toys`;
let type = 'Montessori';
let item = 'batman';
let orderBy = `ORDER BY x desc, y asc`;
let res = tagBraid`
SELECT author
FROM ${items}
WHERE
AND type = ${type}_v2
AND name = ${item}
~${orderBy}
`;
let expected = `
SELECT author
FROM ?
WHERE
AND type = Montessori_v2
AND name = ?
ORDER BY x desc, y asc
`;
assert.deepEqual(res, [expected, [`toys`, 'batman']]);
done();
});
/*it('Will return a promise', async function (done) {
let res = alasql`SELECT 123`;
assert(typeof res.then === 'function');
assert.equal(await alasql`SELECT 123`.then((x) => 555), 555);
done();
});
it('Will return the data from the query', async function (done) {
assert.equal(await alasql`VAlUE OF SELECT 123`, 123);
assert.deepEqual(await alasql`SELECT 123 as abc`, [{abc: 123}]);
done();
});
it('Will inline string connected to other areas', async function (done) {
assert.deepEqual(await alasql`SELECT 123 as abc`, [{abc: 123}]);
done();
});*/
});
const re = {
preFree: /[\(,\s]~?$/,
postFree: /^[\),\s]/,
};
const cache = new Map();
function tagBraid(template, ...params) {
if (
!Array.isArray(template) ||
!Array.isArray(template.raw) ||
template.length - 1 != params.length
)
throw 'Please use as tagfunction to provide the right arguments';
if (1 == template.length) return [template[0]];
let sql = '';
let paramsIDs = [];
if (cache[template.raw]) {
({sql, paramsIDs} = cache.get(template.raw));
} else {
for (let i = 0; i <= params.length; i++) {
sql += template[i];
if (i === params.length) break;
let inline = true;
// if the field is "free" and not connected to other texts
if (
(re.preFree.test(template[i]) ||
(0 === i && ('' === template[i] || '~' === template[i]))) &&
(re.postFree.test(template[i + 1]) || (params.length - 1 === i && '' === template[i + 1]))
) {
inline = false;
// force inline if prepended with ~
if ('~'.charCodeAt(0) === template[i].charCodeAt(template[i].length - 1)) {
sql = sql.slice(0, -1);
inline = true;
}
}
if (inline) {
if (typeof params[i] !== 'number' && typeof params[i] !== 'string')
console.error(
'You are inlining a value that is not a string or a number so it might not work. Will proceed with the .toString() value but consider making space around the value so it can be provided as a parameter.',
{parameter: params[i], template: template.raw}
);
sql += params[i].toString();
} else {
sql += '?';
paramsIDs.push(i);
}
}
cache.set(template.raw, {sql, paramsIDs});
}
return [sql, [...paramsIDs.map((x) => params[x])]];
}