-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathyesql.js
131 lines (124 loc) · 3.82 KB
/
yesql.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
const fs = require('fs')
const path = require('path')
const matchQuoted = /('[^']*(\\.[^'\\]*)*')/
const matchDoubleQuoted = /("[^"]*(\\.[^"\\]*)*")/
const readSqlFiles = (dir, options = {}) => {
return fs.readdirSync(dir).filter(file => {
return file.endsWith('.sql')
}).map(file => {
return {
name: file,
content: fs
.readFileSync(path.resolve(dir, file), 'utf8')
.replace(/\r\n/g, '\n')
}
}).reduce((acc, value) => {
acc[value.name] = value.content
value.content.split('\n\n')
.reduce((sqls, lines) => {
if (lines.trim().startsWith('--')) {
sqls.push(lines)
} else if (sqls.length) {
sqls[sqls.length - 1] += '\n\n' + lines
}
return sqls
}, [])
.forEach(sql => {
if (sql.trim().startsWith('--')) {
const sqlName = sql.split('\n')[0].trim().substring(2).trim()
if (acc[sqlName]) {
throw new Error('Duplicate SQL query name "' + sqlName + '" found, please rename other one.')
}
acc[sqlName] = options.type ? module.exports[options.type](sql, options) : sql
}
})
return acc
}, {})
}
const pg = (query, options = {}) => {
return (data = {}) => {
const values = []
const text = query
// remove -- comments
.replace(/--.*$/gm, '')
// remove /* */ comments
.replace(/\/\*(\*(?!\/)|[^*])*\*\//g, '')
.split(matchQuoted)
.map(part => {
if (!part || matchQuoted.test(part)) {
return part
} else {
return part
.split(matchDoubleQuoted)
.map(part => {
if (!part || matchDoubleQuoted.test(part)) {
return part
} else {
return part.replace(/(::?)([a-zA-Z0-9_]+)/g, (_, prefix, key) => {
if (prefix !== ':') {
return prefix + key
} else if (key in data) {
values.push(data[key])
return '$' + values.length
} else if (options.useNullForMissing) {
values.push(null)
return '$' + values.length
} else {
return errorMissingValue(key, query, data)
}
})
}
}
).join('')
}
})
.join('')
.trim()
return {
text, values
}
}
}
const mysql = (query, options = {}) => {
return (data = {}) => {
const values = []
const sql = query.split(matchQuoted)
.map(part => {
if (!part || matchQuoted.test(part)) {
return part
} else {
return part
.split(matchDoubleQuoted)
.map(part => {
if (!part || matchDoubleQuoted.test(part)) {
return part
} else {
return part.replace(/(::?)([a-zA-Z0-9_]+)/g, (_, prefix, key) => {
if (key in data) {
values.push(data[key])
return prefix.replace(/:/g, '?')
} else if (options.useNullForMissing) {
values.push(null)
return prefix.replace(/:/g, '?')
} else {
return errorMissingValue(key, query, data)
}
})
}
}
).join('')
}
})
.join('')
.trim()
return {
sql, values
}
}
}
const errorMissingValue = (key, query, data) => {
throw new Error('Missing value for statement.\n' + key + ' not provided for statement:\n' + query + '\nthis was provided:\n' + JSON.stringify(data))
}
module.exports = readSqlFiles
module.exports.pg = pg
module.exports.mysql = mysql