MCPcopy
hub / github.com/sequelize/sequelize / escape

Function escape

lib/sql-string.js:21–93  ·  view source on GitHub ↗
(val, timeZone, dialect, format)

Source from the content-addressed store, hash-verified

19exports.arrayToList = arrayToList;
20
21function escape(val, timeZone, dialect, format) {
22 let prependN = false;
23 if (val === undefined || val === null) {
24 return 'NULL';
25 }
26 switch (typeof val) {
27 case 'boolean':
28 // SQLite doesn't have true/false support. MySQL aliases true/false to 1/0
29 // for us. Postgres actually has a boolean type with true/false literals,
30 // but sequelize doesn't use it yet.
31 if (['sqlite', 'mssql'].includes(dialect)) {
32 return +!!val;
33 }
34 return (!!val).toString();
35 case 'number':
36 return val.toString();
37 case 'string':
38 // In mssql, prepend N to all quoted vals which are originally a string (for
39 // unicode compatibility)
40 prependN = dialect === 'mssql';
41 break;
42 }
43
44 if (val instanceof Date) {
45 val = dataTypes[dialect].DATE.prototype.stringify(val, { timezone: timeZone });
46 }
47
48 if (Buffer.isBuffer(val)) {
49 if (dataTypes[dialect].BLOB) {
50 return dataTypes[dialect].BLOB.prototype.stringify(val);
51 }
52
53 return dataTypes.BLOB.prototype.stringify(val);
54 }
55
56 if (Array.isArray(val)) {
57 const partialEscape = escVal => escape(escVal, timeZone, dialect, format);
58 if (dialect === 'postgres' && !format) {
59 return dataTypes.ARRAY.prototype.stringify(val, { escape: partialEscape });
60 }
61 return arrayToList(val, timeZone, dialect, format);
62 }
63
64 if (!val.replace) {
65 throw new Error(`Invalid value ${logger.inspect(val)}`);
66 }
67
68 if (['postgres', 'sqlite', 'mssql', 'snowflake', 'db2'].includes(dialect)) {
69 // http://www.postgresql.org/docs/8.2/static/sql-syntax-lexical.html#SQL-SYNTAX-STRINGS
70 // http://stackoverflow.com/q/603572/130598
71 val = val.replace(/'/g, "''");
72
73 if (dialect === 'postgres') {
74 // null character is not allowed in Postgres
75 val = val.replace(/\0/g, '\\0');
76 }
77 } else {
78

Callers 4

arrayToListFunction · 0.85
partialEscapeFunction · 0.85
formatFunction · 0.85
formatNamedParametersFunction · 0.85

Calls 4

arrayToListFunction · 0.85
inspectMethod · 0.80
toStringMethod · 0.65
stringifyMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…