-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathSQL.js
190 lines (158 loc) · 4.82 KB
/
SQL.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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
'use strict'
const inspect = Symbol.for('nodejs.util.inspect.custom')
const wrapped = Symbol('wrapped')
const quoteIdentifier = require('./quoteIdentifier')
class SqlStatement {
constructor (strings, values) {
if (values.some(value => value === undefined)) {
throw new Error(
'SQL`...` strings cannot take `undefined` as values as this can generate invalid sql.'
)
}
this.strings = strings
this._values = values
}
glue (pieces, separator) {
const result = { strings: [], values: [] }
let carryover
for (let i = 0; i < pieces.length; i++) {
const strings = Array.from(pieces[i].strings)
if (i > 0) {
strings[0] = carryover + separator + strings[0]
}
carryover = strings.splice(-1)[0]
result.strings.push.apply(result.strings, strings)
result.values.push.apply(result.values, pieces[i]._values)
}
result.strings.push(carryover)
result.strings[result.strings.length - 1] += ' '
return new SqlStatement(result.strings, result.values)
}
/**
* A function that accepts an array of objects and a mapper function
* It returns a clean SQL format using the object properties defined in the mapper function
*/
map (array, mapFunc = i => i) {
if ((mapFunc instanceof Function) && array?.length > 0) {
return this.glue(
array.map(mapFunc).map((item) => SQL`${item}`),
','
)
}
return null
}
_generateString (type, namedValueOffset = 0) {
let text = this.strings[0]
let valueOffset = 0
const values = [...this._values]
for (let i = 1; i < this.strings.length; i++) {
const valueIndex = i - 1 + valueOffset
const valueContainer = values[valueIndex]
if (valueContainer && valueContainer[wrapped]) {
text += `${valueContainer.transform(type)}${this.strings[i]}`
values.splice(valueIndex, 1)
valueOffset--
} else if (valueContainer instanceof SqlStatement) {
text += `${valueContainer._generateString(
type,
valueIndex + namedValueOffset
)}${this.strings[i]}`
valueOffset += valueContainer.values.length - 1
values.splice(valueIndex, 1, ...valueContainer.values)
} else {
let delimiter = '?'
if (type === 'pg') {
delimiter = '$' + (i + valueOffset + namedValueOffset)
}
text += delimiter + this.strings[i]
}
}
return text.replace(/\s+$/gm, ' ').replace(/^\s+|\s+$/gm, '')
}
get debug () {
let text = this.strings[0]
for (let i = 1; i < this.strings.length; i++) {
let data = this._values[i - 1]
let quote = "'"
if (data && data[wrapped]) {
data = data.transform()
quote = ''
} else if (data instanceof SqlStatement) {
data = data.debug
quote = ''
}
typeof data === 'string' ? (text += quote + data + quote) : (text += data)
text += this.strings[i]
}
return text.replace(/\s+$/gm, ' ').replace(/^\s+|\s+$/gm, '')
}
[inspect] () {
return `SQL << ${this.debug} >>`
}
get text () {
return this._generateString('pg')
}
get sql () {
return this._generateString('mysql')
}
get values () {
return this._values
.filter(v => !v || !v[wrapped])
.reduce((acc, v) => {
if (v instanceof SqlStatement) {
acc.push(...v.values)
} else {
acc.push(v)
}
return acc
}, [])
}
/**
* @deprecated Please append within template literals, e.g. SQL`SELECT * ${sql}`
*/
append (statement, options) {
if (!statement) {
return this
}
if (!(statement instanceof SqlStatement)) {
throw new Error(
'"append" accepts only template string prefixed with SQL (SQL`...`)'
)
}
if (options && options.unsafe === true) {
const text = statement.strings.reduce((acc, string, i) => {
acc = `${acc}${string}${statement.values[i] ? statement.values[i] : ''}`
return acc
}, '')
const strings = this.strings.slice(0)
strings[this.strings.length - 1] += text
this.strings = strings
return this
}
const last = this.strings[this.strings.length - 1]
const [first, ...rest] = statement.strings
this.strings = [...this.strings.slice(0, -1), last + first, ...rest]
this._values.push.apply(this._values, statement._values)
return this
}
}
function SQL (strings, ...values) {
return new SqlStatement(strings, values)
}
SQL.glue = SqlStatement.prototype.glue
SQL.map = SqlStatement.prototype.map
module.exports = SQL
module.exports.SQL = SQL
module.exports.default = SQL
module.exports.unsafe = value => ({
transform () {
return value
},
[wrapped]: true
})
module.exports.quoteIdent = value => ({
transform (type) {
return quoteIdentifier(value, type)
},
[wrapped]: true
})