|
| 1 | +import BigNumber from "bignumber.js"; |
| 2 | +import { checkArgumentValid } from "../common/util"; |
| 3 | +import { INVALID_PARAMETERS } from "../common/errors"; |
| 4 | + |
| 5 | +const CHARS_GLOBAL_REGEXP = /[\0\b\t\n\r\x1a"'\\]/g; // eslint-disable-line no-control-regex |
| 6 | + |
| 7 | +const COMMENTS_REGEXP = |
| 8 | + /("(""|[^"])*")|('(''|[^'])*')|(--[^\n\r]*)|(\/\*[\w\W]*?(?=\*\/)\*\/)/gm; |
| 9 | + |
| 10 | +const CHARS_ESCAPE_MAP: Record<string, string> = { |
| 11 | + "\0": "\\0", |
| 12 | + "\b": "\\b", |
| 13 | + "\t": "\\t", |
| 14 | + "\n": "\\n", |
| 15 | + "\r": "\\r", |
| 16 | + "\x1a": "\\Z", |
| 17 | + '"': '\\"', |
| 18 | + "'": "\\'", |
| 19 | + "\\": "\\\\" |
| 20 | +}; |
| 21 | + |
| 22 | +const removeComments = (query: string) => { |
| 23 | + query = query.replace(COMMENTS_REGEXP, match => { |
| 24 | + if ( |
| 25 | + (match[0] === '"' && match[match.length - 1] === '"') || |
| 26 | + (match[0] === "'" && match[match.length - 1] === "'") |
| 27 | + ) |
| 28 | + return match; |
| 29 | + |
| 30 | + return ""; |
| 31 | + }); |
| 32 | + return query; |
| 33 | +}; |
| 34 | + |
| 35 | +const zeroPad = (param: number, length: number) => { |
| 36 | + let paded = param.toString(); |
| 37 | + while (paded.length < length) { |
| 38 | + paded = "0" + paded; |
| 39 | + } |
| 40 | + |
| 41 | + return paded; |
| 42 | +}; |
| 43 | + |
| 44 | +export class QueryFormatter { |
| 45 | + private format(query: string, params: unknown[]) { |
| 46 | + const regex = /(''|""|``|\\\\|\\'|\\"|'|"|`|\?)/g; |
| 47 | + |
| 48 | + const STATE = { |
| 49 | + WHITESPACE: 0, |
| 50 | + SINGLE_QUOTE: 1, |
| 51 | + DOUBLE_QUOTE: 2, |
| 52 | + BACKTICK: 3 |
| 53 | + }; |
| 54 | + |
| 55 | + const stateSwitches: Record<string, number> = { |
| 56 | + "'": STATE.SINGLE_QUOTE, |
| 57 | + '"': STATE.DOUBLE_QUOTE, |
| 58 | + "`": STATE.BACKTICK |
| 59 | + }; |
| 60 | + |
| 61 | + let state = STATE.WHITESPACE; |
| 62 | + |
| 63 | + query = query.replace(regex, str => { |
| 64 | + if (str in stateSwitches) { |
| 65 | + if (state === STATE.WHITESPACE) { |
| 66 | + state = stateSwitches[str]; |
| 67 | + } else if (state === stateSwitches[str]) { |
| 68 | + state = STATE.WHITESPACE; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + if (str !== "?") { |
| 73 | + return str; |
| 74 | + } |
| 75 | + |
| 76 | + if (state !== STATE.WHITESPACE) return str; |
| 77 | + |
| 78 | + if (params.length == 0) { |
| 79 | + throw new Error("Too few parameters given"); |
| 80 | + } |
| 81 | + |
| 82 | + return this.escape(params.shift()); |
| 83 | + }); |
| 84 | + |
| 85 | + if (params.length) { |
| 86 | + throw new Error("Too many parameters given"); |
| 87 | + } |
| 88 | + |
| 89 | + return query; |
| 90 | + } |
| 91 | + |
| 92 | + private escape(param: unknown) { |
| 93 | + if (param === undefined || param === null) { |
| 94 | + return "NULL"; |
| 95 | + } |
| 96 | + |
| 97 | + switch (typeof param) { |
| 98 | + case "boolean": { |
| 99 | + return param ? "true" : "false"; |
| 100 | + } |
| 101 | + case "number": { |
| 102 | + return param.toString(); |
| 103 | + } |
| 104 | + case "object": { |
| 105 | + return this.escapeObject(param); |
| 106 | + } |
| 107 | + |
| 108 | + case "string": { |
| 109 | + return this.escapeString(param); |
| 110 | + } |
| 111 | + default: { |
| 112 | + return "" + param; |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + private escapeString(param: string) { |
| 118 | + let chunkIndex = (CHARS_GLOBAL_REGEXP.lastIndex = 0); |
| 119 | + let escapedValue = ""; |
| 120 | + let match; |
| 121 | + |
| 122 | + while ((match = CHARS_GLOBAL_REGEXP.exec(param))) { |
| 123 | + const key = match[0]; |
| 124 | + escapedValue += |
| 125 | + param.slice(chunkIndex, match.index) + CHARS_ESCAPE_MAP[key]; |
| 126 | + chunkIndex = CHARS_GLOBAL_REGEXP.lastIndex; |
| 127 | + } |
| 128 | + |
| 129 | + if (chunkIndex === 0) { |
| 130 | + // Nothing was escaped |
| 131 | + return "'" + param + "'"; |
| 132 | + } |
| 133 | + |
| 134 | + if (chunkIndex < param.length) { |
| 135 | + return "'" + escapedValue + param.slice(chunkIndex) + "'"; |
| 136 | + } |
| 137 | + |
| 138 | + return "'" + escapedValue + "'"; |
| 139 | + } |
| 140 | + |
| 141 | + private escapeBuffer(param: Buffer) { |
| 142 | + return "X" + this.escapeString(param.toString("hex")); |
| 143 | + } |
| 144 | + |
| 145 | + private escapeArray(param: unknown[]) { |
| 146 | + let sql = "["; |
| 147 | + |
| 148 | + for (let i = 0; i < param.length; i++) { |
| 149 | + const value = param[i]; |
| 150 | + const prefix = i === 0 ? "" : ", "; |
| 151 | + |
| 152 | + if (Array.isArray(value)) { |
| 153 | + sql += prefix + this.escapeArray(value); |
| 154 | + } else { |
| 155 | + sql += prefix + this.escape(value); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + sql += "]"; |
| 160 | + |
| 161 | + return sql; |
| 162 | + } |
| 163 | + |
| 164 | + private escapeDate(param: Date) { |
| 165 | + const dt = new Date(param); |
| 166 | + |
| 167 | + if (isNaN(dt.getTime())) { |
| 168 | + return "NULL"; |
| 169 | + } |
| 170 | + |
| 171 | + const year = dt.getFullYear(); |
| 172 | + const month = dt.getMonth() + 1; |
| 173 | + const day = dt.getDate(); |
| 174 | + const hour = dt.getHours(); |
| 175 | + const minute = dt.getMinutes(); |
| 176 | + const second = dt.getSeconds(); |
| 177 | + |
| 178 | + // YYYY-MM-DD HH:mm:ss.mmm |
| 179 | + const str = |
| 180 | + zeroPad(year, 4) + |
| 181 | + "-" + |
| 182 | + zeroPad(month, 2) + |
| 183 | + "-" + |
| 184 | + zeroPad(day, 2) + |
| 185 | + " " + |
| 186 | + zeroPad(hour, 2) + |
| 187 | + ":" + |
| 188 | + zeroPad(minute, 2) + |
| 189 | + ":" + |
| 190 | + zeroPad(second, 2); |
| 191 | + //+ "." + zeroPad(millisecond, 3); |
| 192 | + |
| 193 | + return this.escapeString(str); |
| 194 | + } |
| 195 | + |
| 196 | + private escapeObject(param: unknown) { |
| 197 | + if (BigNumber.isBigNumber(param)) { |
| 198 | + return param.toString(); |
| 199 | + } else if (Object.prototype.toString.call(param) === "[object Date]") { |
| 200 | + return this.escapeDate(param as Date); |
| 201 | + } else if (Array.isArray(param)) { |
| 202 | + return this.escapeArray(param); |
| 203 | + } else if (Buffer.isBuffer(param)) { |
| 204 | + return this.escapeBuffer(param); |
| 205 | + } else { |
| 206 | + return this.escapeString("" + param); |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + formatQuery(query: string, parameters?: unknown[]): string { |
| 211 | + query = removeComments(query); |
| 212 | + if (parameters) { |
| 213 | + checkArgumentValid(Array.isArray(parameters), INVALID_PARAMETERS); |
| 214 | + query = this.format(query, parameters); |
| 215 | + } |
| 216 | + return query; |
| 217 | + } |
| 218 | +} |
0 commit comments