2020-07-30 19:12:29 +02:00
|
|
|
/* eslint-disable no-useless-escape */
|
|
|
|
// eslint-disable-next-line no-control-regex
|
2020-11-13 12:39:40 +01:00
|
|
|
const pattern = /[\0\x08\x09\x1a\n\r"'\\\%]/gm;
|
|
|
|
const regex = new RegExp(pattern);
|
2020-07-30 19:12:29 +02:00
|
|
|
|
2022-05-10 12:57:25 +02:00
|
|
|
function sqlEscaper (string: string) {
|
2020-08-14 11:06:20 +02:00
|
|
|
return string.replace(regex, char => {
|
2020-11-20 09:16:18 +01:00
|
|
|
const m = ['\\0', '\\x08', '\\x09', '\\x1a', '\\n', '\\r', '\'', '\"', '\\', '\\\\', '%'];
|
2021-04-28 12:10:43 +02:00
|
|
|
const r = ['\\\\0', '\\\\b', '\\\\t', '\\\\z', '\\\\n', '\\\\r', '\\\'', '\\\"', '\\\\', '\\\\\\\\', '\%'];
|
2020-08-14 11:06:20 +02:00
|
|
|
return r[m.indexOf(char)] || char;
|
2020-07-30 19:12:29 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export { sqlEscaper };
|