-
Notifications
You must be signed in to change notification settings - Fork 5
/
src.js
63 lines (56 loc) · 1.38 KB
/
src.js
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
class SqlLiteral {
constructor(parts, values) {
this._parts = parts;
this._values = values;
}
_resolve() {
if (this._resolved === undefined) {
const context = { fragments: new Map(), values: [] };
const text = this._resolveFor(context);
this._resolved = { text, values: context.values };
}
return this._resolved;
}
_resolveFor(context) {
let fragment = context.fragments.get(this);
if (fragment === undefined) {
fragment = this._parts.reduce((prev, curr, i) => {
const child = this._values[i - 1];
let mid;
if (child instanceof SqlLiteral) {
mid = child._resolveFor(context);
} else {
context.values.push(child);
mid = '$' + context.values.length;
}
return prev + mid + curr;
});
context.fragments.set(this, fragment);
}
return fragment;
}
get text() {
return this._resolve().text;
}
get values() {
return this._resolve().values;
}
}
export default
function SQLTag(parts, ...values) {
return new SqlLiteral(parts, values);
}
export
function sqlLiteral(value) {
return new SqlLiteral([value]);
}
export
function join(array, separator) {
separator = separator || ",";
let parts = [""];
for (let i = 0; i < array.length-1; i++) {
parts.push(separator);
}
parts.push("");
return new SqlLiteral(parts, array);
}